require 'test/unit' require 'topical' require 'pp' class TestMatcher < Test::Unit::TestCase def test_path2regexp rx = Topical::TopicTree.path2regexp("/programming/ruby") assert_match rx, "/programming/ruby/" assert_no_match rx, "/goo/programming/ruby" rx = Topical::TopicTree.path2regexp("programming/**/ruby") assert_match rx, "/programming/ruby/" rx = Topical::TopicTree.path2regexp("ruby") assert_match rx, "/programming/ruby/" rx = Topical::TopicTree.path2regexp("foo/*/*/bar") assert_match rx, "/quux/foo/a/b/bar/" rx = Topical::TopicTree.path2regexp("foo/**/**/bar") assert_match rx, "/quux/foo/a/b/bar/" assert_raise(Topical::ParseError) { p Topical::TopicTree.path2regexp("/programming//ruby") } assert_raise(Topical::ParseError) { Topical::TopicTree.path2regexp("/open/paren(thesis") } assert_raise(Topical::ParseError) { Topical::TopicTree.path2regexp("/open/bra{ce") } end def test_simple_topictree tree = Topical::TopicTree.new tree.create "/a" tree.create "/b" tree.add "/a", "foo" tree.add "/a", "bar" tree.add "/b", "quux" assert_equal ["quux"], tree["/b"] assert_equal ["foo", "bar"], tree["/a"] assert_equal [], tree["/not-existant"] assert_equal [], tree["/not/existant"] tree.create "/a/b/c" tree.add "/a/b/c", "bleh" assert_equal ["bleh"], tree["/a/b/c"] tree.create "/foo/bar/baz/quux" tree.add "/foo/bar/baz/quux", "bleh" assert_equal ["bleh"], tree["/foo/bar/baz/quux"] end def test_extended_topictree tree = Topical::TopicTree.new tree.create "/a" tree.add "/a", "foo" assert_equal ["foo"], tree["a"] tree.create "/b/a" tree.create "/b/c" tree.create "/a/c" tree.create "/z/a/c" tree.add "/b/a", "bar" tree.add "/b/c", "foo" tree.add "/a/c", "bar" tree.add "/z/a/c", "baz" assert_equal ["bar", "baz", "foo"], tree["c"].sort assert_equal ["bar", "baz"], tree["a/c"].sort end def test_extended_add tree = Topical::TopicTree.new tree.create "/a" tree.add "/a", "foo" assert_equal ["foo"], tree["a"] assert_equal ["foo"], tree["/a"] tree.create "/a/b" tree.add "/a/b", "foo" assert_equal ["foo"], tree["a/b"] assert_equal ["foo"], tree["/a/b"] tree.create "/sample" tree.create "/sample/foo/bar" tree.create "/foo/bar" tree.create "/sample/foo/bar/quux" tree.create "/sample/foo/bar/quuxle" tree.add "/sample", 1 tree.add "/sample/foo/bar", 2 tree.add "foo/bar", 3 tree.add "/**/quux", 4 tree.add "/**/quuxle", 5 assert_equal [4], tree["/**/quux"] assert_equal [4], tree["/sample/**/quux"] assert_equal [4], tree["/sample/*/bar/quux"] assert_equal [4], tree["/sample/**/bar/quux"] assert_equal [4], tree["/sample/foo/bar/quux"] assert_equal [2, 3, 4, 5], tree["/sample/**/foo/**/bar"].sort end def test_describe tree = Topical::TopicTree.new tree.create "/a", "Topic A", "This is Topic A." assert_equal "Topic A", tree.name("/a") assert_equal "This is Topic A.", tree.description("/a") tree.create "/", "Root" assert_equal "Root", tree.name("/") tree.create "/undoc" assert_nil tree.name("/undoc") assert_nil tree.description("/undoc") tree.describe "/undoc", "Undocumented" assert_equal "Undocumented", tree.name("/undoc") assert_equal nil, tree.description("/undoc") tree.describe "/undoc", "Undocumented", "Not any more." assert_equal "Undocumented", tree.name("/undoc") assert_equal "Not any more.", tree.description("/undoc") tree.create "/undoc/other" assert_nil tree.name("/undoc/other") assert_nil tree.description("/undoc/other") tree.create "/other/one" tree.create "/other/one/subtopic" tree.create "/other/two" tree.create "/other/two/subtopic" tree.describe "/other/**/subtopic", "General Subtopic" assert_equal "General Subtopic", tree.name("/other/one/subtopic") assert_equal "General Subtopic", tree.name("/other/two/subtopic") assert_nil tree.name("/not-exist") assert_nil tree.description("/not-exist") assert_raise(Topical::ParseError) { tree.name("/**/rec") } assert_raise(Topical::ParseError) { tree.name("/*/rec") } assert_raise(Topical::ParseError) { tree.name("rec") } assert_raise(Topical::ParseError) { tree.name("/rec?/") } end def test_each_path tree = Topical::TopicTree.new tree.create "/sample" tree.create "/foo/bar" tree.create "/sample/foo/bar/quux" tree.create "/sample/foo/bar/quuxle" r = [] tree.each_path('/') { |path| r << path } assert_equal ["/", "/foo/", "/foo/bar/", "/sample/", "/sample/foo/", "/sample/foo/bar/", "/sample/foo/bar/quux/", "/sample/foo/bar/quuxle/" ], r.sort r = [] tree.each_path('sample/**/bar/*') { |path| r << path } assert_equal ["/sample/foo/bar/quux/", "/sample/foo/bar/quuxle/"], r.sort r = [] tree.each_path('sample/*/quuxle') { |path| r << path } assert_equal [], r.sort r = [] tree.each_path('sample/**/quuxle') { |path| r << path } assert_equal ["/sample/foo/bar/quuxle/"], r.sort r = [] tree.each_path('/sample/*', false) { |path| r << path } assert_equal ["/sample/foo/"], r.sort end def test_subtopics tree = Topical::TopicTree.new tree.create "/sample" tree.create "/foo/bar" tree.create "/sample/foo/bar/quux" tree.create "/sample/foo/bar/quuxle" assert_equal ["foo", "sample"], tree.subtopics.sort assert_equal ["bar"], tree.subtopics('/sample/foo').sort assert_equal ["quux", "quuxle"], tree.subtopics('/sample/**/bar').sort assert_equal ["quux", "quuxle"], tree.subtopics('/sample/**/bar').sort assert_equal ["foo"], tree.subtopics('/sample') assert_equal [], tree.subtopics('/ehehe').sort end def test_unified_fetch tree = Topical::TopicTree.new tree.create "/one/foo" tree.create "/one/bar" tree.create "/one/quux" tree.create "/two/foo" tree.create "/two/bar" tree.create "/two/quux" tree.add "/one/foo", 1 tree.add "/two/bar", 1 tree.add "/one/bar", 2 tree.add "/two/foo", 2 tree.add "/two/quux", 2 assert_equal [1], tree['/one/foo', 'bar'] assert_equal [2], tree['foo', 'two/quux'] assert_equal [], tree['foo', 'two/quux', 'nx'] end def test_clear tree = Topical::TopicTree.new tree.create "/one/foo" tree.create "/one/bar" tree.add "/one/foo", 1 tree.add "/one/bar", 1 tree.clear! assert_equal [], tree["/one/foo"] assert_equal [], tree["/one"] assert_equal [], tree["/"] end def test_delete tree = Topical::TopicTree.new tree.create "/one/foo" tree.create "/one/bar" tree.add "/one/foo", 1 tree.add "/one/foo", 2 tree.add "/one/bar", "hehe" tree.add "/one/bar", "hoho" tree.delete 1 assert_equal [2], tree["/one/foo"] assert_equal [2, "hehe", "hoho"], tree["/one"] tree.delete "hoho" assert_equal [2, "hehe"], tree["/"] end end