From d06d8c221be5b82d00da821323fb6d1889e58105 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 28 Dec 2017 01:04:01 +0000 Subject: simplify API to avoid (and raise on) user errors This fixes our API to match the documentation in making Msgthr#order! optional. Furthermore, the block previously passed to Msgthr#order! may now be passed to Msgthr#thread! instead. We accomplish this by tracking internal state explicitly, so a Msgthr::StateError exception will be raised when methods are called in an unsupported order. This internal state is reset with Msgthr#clear. For users who truly do not care about ordering, Msgthr#walk_thread may be called immediately after the last call to Msgthr#add. Thanks to Dimid Duchovny for the feedback which led to this: https://80x24.org/msgthr-public/CANKvuDc2mkxLuh+3+WXWfMXzxK2bShNesrD5xLocGOD1RybbwQ@mail.gmail.com/ --- test/test_msgthr.rb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'test/test_msgthr.rb') diff --git a/test/test_msgthr.rb b/test/test_msgthr.rb index 19cec75..b14e135 100644 --- a/test/test_msgthr.rb +++ b/test/test_msgthr.rb @@ -11,8 +11,9 @@ class TestMsgthr < Test::Unit::TestCase thr.add('c', nil, 'c') thr.add('D', nil, 'D') thr.add('d', %w(missing), 'd') - thr.thread! + rset = thr.thread! rootset = thr.order! { |c| c.sort_by!(&:mid) } + assert_same rset, rootset assert_equal %w(D c missing), rootset.map(&:mid) assert_equal 'D', rootset[0].msg assert_equal %w(b), rootset[1].children.map(&:mid) @@ -30,6 +31,67 @@ class TestMsgthr < Test::Unit::TestCase 0. abc 2. [missing: ] 0. d +EOF + assert_equal exp, out + end + + def test_order_in_thread + thr = Msgthr.new + thr.add(1, nil, 'a') + thr.add(2, [1], 'b') + thr.thread! do |ary| + ary.sort_by! do |cont| + cur = cont.topmost + cur ? cur : 0 + end + end + out = '' + thr.walk_thread do |level, container, index| + msg = container.msg + out << "#{level} [#{index}] #{msg}\n" + end + exp = <