dakrone-fastri 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,182 @@
1
+ require 'test/unit'
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+ $:.unshift "lib"
4
+ require 'fastri/full_text_index'
5
+
6
+ class TestFullTextIndex < Test::Unit::TestCase
7
+ require 'stringio'
8
+ include FastRI
9
+
10
+ magic = FullTextIndexer::MAGIC
11
+ data = <<EOF
12
+ #{magic}this is a test
13
+ \r\000\000\000foo.txt\000\004\b{\000
14
+ zzzz
15
+ \r\000\000\000bar.txt\000\004\b{\000
16
+ EOF
17
+ DATA = (data.split(/\n/) << "").join("\0")
18
+ SUFFIXES = %w[a\ test is\ a test this zzzz].map{|w| [DATA.index(w)].pack("V")}.join("")
19
+
20
+ data = <<EOF
21
+ #{magic}this is a test
22
+ \r\000\000\000foo.txt\000\004\b{\000
23
+ zzzz this
24
+ \r\000\000\000bar.txt\000\004\b{\000
25
+ EOF
26
+ DATA2 = (data.split(/\n/) << "").join("\0")
27
+ SUFFIXES2 = ["a test", "is a", "test", "this\0", "this", "zzzz"].map{|x| [DATA2.index(x)].pack("V")}.join("")
28
+
29
+ data = <<EOF
30
+ #{magic}this is a test
31
+ SIZ1foo.txt\000#{Marshal.dump({:foo => :bar, :bar => 1})}
32
+ zzzz this
33
+ SIZ2bar.txt\000#{Marshal.dump({:foo => :baz, :bar => 42})}
34
+ EOF
35
+ lines = data.split(/\n/)
36
+ len1 = lines[1].size - 4 + 1
37
+ lines[1].sub!(/SIZ1/, [len1].pack("V"))
38
+ len2 = lines[3].size - 4 + 1
39
+ lines[3].sub!(/SIZ2/, [len2].pack("V"))
40
+ DATA3 = (lines << "").join("\0")
41
+ SUFFIXES3 = ["a test", "is a", "test", "this\0", "this", "zzzz"].map{|x| [DATA3.index(x)].pack("V")}.join("")
42
+
43
+ def setup
44
+ @index = FullTextIndex.new_from_ios(StringIO.new(DATA), StringIO.new(SUFFIXES))
45
+ @index2 = FullTextIndex.new_from_ios(StringIO.new(DATA2), StringIO.new(SUFFIXES2))
46
+ @index3 = FullTextIndex.new_from_ios(StringIO.new(DATA3), StringIO.new(SUFFIXES3))
47
+ end
48
+
49
+ def test_new_from_ios
50
+ a = nil
51
+ assert_nothing_raised { a = FullTextIndex.new_from_ios(StringIO.new(DATA), StringIO.new(SUFFIXES)) }
52
+ assert_equal(FullTextIndex::DEFAULT_OPTIONS[:max_query_size], a.max_query_size)
53
+ end
54
+
55
+ def test_lookup_basic
56
+ %w[this is a test].each do |term|
57
+ result = @index.lookup(term)
58
+ assert_kind_of(FullTextIndex::Result, result)
59
+ assert_equal(term, result.query)
60
+ assert_equal("foo.txt", result.path)
61
+ end
62
+ assert_equal(0, @index.lookup("a").index)
63
+ assert_equal(2, @index.lookup("t").index)
64
+ assert_equal(3, @index.lookup("th").index)
65
+
66
+ assert_equal(4, @index.lookup("z").index)
67
+ assert_equal("bar.txt", @index.lookup("z").path)
68
+ end
69
+
70
+ def test_lookup_metadata
71
+ assert_equal({}, @index.lookup("test").metadata)
72
+ assert_equal({}, @index.lookup("zzzz").metadata)
73
+ assert_equal({:foo => :bar, :bar => 1}, @index3.lookup("test").metadata)
74
+ assert_equal({:foo => :baz, :bar => 42}, @index3.lookup("zzz").metadata)
75
+ end
76
+
77
+ def test_Result_text
78
+ assert_equal("t", @index.lookup("this").text(1))
79
+ assert_equal("this", @index.lookup("this").text(4))
80
+ assert_equal("this is a ", @index.lookup("this").text(10))
81
+ assert_equal("this is a test ", @index.lookup("th").text(100))
82
+
83
+ assert_equal("test ", @index.lookup("t").text(10))
84
+ assert_equal("test ", @index.lookup("t").text(20))
85
+
86
+ assert_equal("z", @index.lookup("z").text(1))
87
+ assert_equal("zzzz", @index.lookup("z").text(10))
88
+ end
89
+
90
+ def test_Result_context
91
+ assert_equal(" a ", @index.lookup("a").context(1))
92
+ assert_equal("s a t", @index.lookup("a").context(2))
93
+ assert_equal("is a te", @index.lookup("a").context(3))
94
+ assert_equal("s is a test", @index.lookup("a").context(5))
95
+ assert_equal("this is a test ", @index.lookup("a").context(10))
96
+ end
97
+
98
+ def test_Result_context_non_initial_entry
99
+ assert_equal("zz", @index.lookup("z").context(1))
100
+ assert_equal("zzz", @index.lookup("z").context(2))
101
+ assert_equal("zzzz", @index.lookup("z").context(3))
102
+ assert_equal("zzzz", @index.lookup("z").context(4))
103
+ assert_equal("zzzz", @index.lookup("z").context(10))
104
+ end
105
+
106
+ def test_lookup_nonexistent
107
+ assert_nil(@index.lookup("bogus"))
108
+ end
109
+
110
+ def test_next_match_basic
111
+ first = @index2.lookup("t")
112
+ assert_equal("foo.txt", first.path)
113
+ assert_equal(2, first.index)
114
+ assert_equal("test ", first.text(10))
115
+
116
+ second = @index2.next_match(first)
117
+ assert_equal("bar.txt", second.path)
118
+ assert_equal(3, second.index)
119
+ assert_equal("this", second.text(10))
120
+
121
+ third = @index2.next_match(second)
122
+ assert_kind_of(FullTextIndex::Result, third)
123
+ assert_equal(4, third.index)
124
+ assert_equal("this is a ", third.text(10))
125
+
126
+ assert_nil(@index2.next_match(third))
127
+ end
128
+
129
+ def test_next_match_restricted
130
+ first = @index2.lookup("t")
131
+ assert_equal("foo.txt", first.path)
132
+ assert_equal(2, first.index)
133
+ assert_equal("test ", first.text(10))
134
+
135
+ second = @index2.next_match(first, "this is")
136
+ assert_equal("foo.txt", second.path)
137
+ assert_equal(4, second.index)
138
+ assert_equal("this is a ", second.text(10))
139
+
140
+ assert_nil(@index2.next_match(first, "foo"))
141
+ end
142
+
143
+ def test_next_match_regexp
144
+ first = @index2.lookup("t")
145
+ assert_equal("foo.txt", first.path)
146
+ assert_equal(2, first.index)
147
+ assert_equal("test ", first.text(10))
148
+
149
+ second = @index2.next_match(first, /.*test/)
150
+ assert_equal("foo.txt", second.path)
151
+ assert_equal(4, second.index)
152
+ assert_equal("this is a test ", second.text(20))
153
+ end
154
+
155
+
156
+ def test_next_matches
157
+ first = @index2.lookup("t")
158
+ all = [first] + @index2.next_matches(first)
159
+ assert_equal([2, 3, 4], all.map{|x| x.index})
160
+ assert_equal(["foo.txt", "bar.txt", "foo.txt"], all.map{|x| x.path})
161
+ one, two, three = *all
162
+ assert_equal(["test ", "this", "this is a test "], all.map{|x| x.text(20)})
163
+ end
164
+
165
+ def test_next_matches_restricted
166
+ first = @index2.lookup("t")
167
+ assert_equal([], @index2.next_matches(first, "this is not"))
168
+ all = @index2.next_matches(first, "this is")
169
+ assert_equal(["foo.txt"], all.map{|x| x.path})
170
+ assert_equal([4], all.map{|x| x.index})
171
+ assert_equal(["this is a test "], all.map{|x| x.text(20)})
172
+ end
173
+
174
+ def test_next_matches_regexp
175
+ first = @index2.lookup("t")
176
+ all = @index2.next_matches(first, /.*test/)
177
+ assert_equal(["foo.txt"], all.map{|x| x.path})
178
+ assert_equal([4], all.map{|x| x.index})
179
+ assert_equal(["this is a test "], all.map{|x| x.text(20)})
180
+ end
181
+
182
+ end
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+ $:.unshift "lib"
4
+ require 'fastri/full_text_indexer'
5
+
6
+ class TestFullTextIndexer < Test::Unit::TestCase
7
+ require 'stringio'
8
+ include FastRI
9
+ def setup
10
+ @indexer = FullTextIndexer.new(20)
11
+ end
12
+
13
+ DATA1 = "this is a test " * 1000
14
+ DATA2 = "this is another test " * 1000
15
+ def test_add_document
16
+ @indexer.add_document("foo.txt", DATA1)
17
+ assert_equal(["foo.txt"], @indexer.documents)
18
+ assert_equal(DATA1, @indexer.data("foo.txt"))
19
+ @indexer.add_document("foo.txt", DATA2)
20
+ assert_equal(["foo.txt"], @indexer.documents)
21
+ assert_equal(DATA2, @indexer.data("foo.txt"))
22
+ @indexer.add_document("bar.txt", DATA2)
23
+ assert_equal(["foo.txt", "bar.txt"], @indexer.documents)
24
+ assert_equal(DATA2, @indexer.data("bar.txt"))
25
+ end
26
+
27
+ def test_preprocess
28
+ data = "this is a \0foo bar\0 bla"
29
+ assert_equal("this is a foo bar bla", @indexer.preprocess(data))
30
+ end
31
+
32
+ def test_find_suffixes_simple
33
+ data = <<EOF
34
+ this is a simple test with these words: Aaaa01 0.1 _asdA1
35
+ EOF
36
+ assert_equal([0, 5, 8, 10, 17, 22, 27, 33, 40, 47, 49, 51],
37
+ @indexer.find_suffixes_simple(data, /[A-Za-z0-9_]+/, /[^A-Za-z0-9_]+/,0))
38
+ assert_equal([0, 5, 8, 10, 17, 22, 27, 33, 40, 52],
39
+ @indexer.find_suffixes_simple(data, /[A-Za-z]+/, /[^A-Za-z]+/, 0))
40
+ assert_equal([0, 5, 8, 10, 17, 22, 27, 33, 40, 52].map{|x| x+10},
41
+ @indexer.find_suffixes_simple(data, /[A-Za-z]+/, /[^A-Za-z]+/, 10))
42
+ assert_equal([0, 1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20,
43
+ 22, 23, 24, 25, 27, 28, 29, 30, 31, 33, 34, 35, 36, 37,
44
+
45
+ 40, 41, 42, 43, 52, 53, 54, 55],
46
+ @indexer.find_suffixes_simple(data, /[A-Za-z]/, /[^A-Za-z]+/, 0))
47
+ assert_equal([0, 5], @indexer.find_suffixes_simple("abcd\ndefg", /\S+/, /\s+/, 0))
48
+ assert_equal([1, 6], @indexer.find_suffixes_simple("abcd\ndefg", /\S+/, /\s+/, 1))
49
+ end
50
+
51
+ def test_build_index_trivial
52
+ @indexer.add_document("foo.txt", DATA1)
53
+ fulltext = StringIO.new("")
54
+ suffixarray = StringIO.new("")
55
+ @indexer.build_index(fulltext, suffixarray)
56
+ assert_equal(["\000\027\000\000\000foo.txt\000\004\b{\006:\tsizei\002\230:\000"],
57
+ fulltext.string[-200..-1].scan(/\0.*$/))
58
+ assert_equal(4000 * 4, suffixarray.string.size)
59
+ end
60
+
61
+ def build_index_test_helper(data, suffixes)
62
+ @indexer.add_document("foo.txt", data)
63
+ offset = FullTextIndexer::MAGIC.size
64
+ suffixes = suffixes.map{|x| x + offset}
65
+ sorted = suffixes.sort_by{|i| data[i - offset]}
66
+ f_io = StringIO.new("")
67
+ sa_io = StringIO.new("")
68
+ @indexer.build_index(f_io, sa_io)
69
+ assert_equal(sorted, sa_io.string.scan(/..../m).map{|x| x.unpack("V")[0]})
70
+ end
71
+
72
+ def test_build_index_harder
73
+ data = <<EOF
74
+ a bcd efghi jklmn opqrst
75
+ EOF
76
+ suffixes = [0, 2, 6, 12, 18]
77
+ build_index_test_helper(data, suffixes)
78
+ data = <<EOF
79
+ e xcd afghi zklmn bpqrst
80
+ EOF
81
+ suffixes = [0, 2, 6, 12, 18]
82
+ build_index_test_helper(data, suffixes)
83
+ end
84
+ end
@@ -0,0 +1,60 @@
1
+ require 'test/unit'
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+ require 'fastri/ri_service'
4
+ require 'fastri/ri_index'
5
+
6
+ class TestFunctionalRiService < Test::Unit::TestCase
7
+ # only created once, since it takes a long time
8
+ @@ri = FastRI::RiService.new(FastRI::RiIndex.new_from_paths(::RDoc::RI::Paths.path(true, true, true, true)))
9
+ def setup
10
+ @ri = @@ri
11
+ end
12
+
13
+ def assert_include(arr, actual)
14
+ arr.each{|w| assert(actual.include?(w), "#{w} should be included in #{actual}") }
15
+ end
16
+
17
+ def test_completion_list
18
+ completion_each = %w[each_pair each_byte each_key each_value]
19
+ assert_include(completion_each, @ri.completion_list("each_"))
20
+ assert_kind_of(Array, @ri.completion_list("each_"))
21
+
22
+ assert_include(%w[Array Fixnum Hash String], @ri.completion_list(""))
23
+ assert_kind_of(Array, @ri.completion_list(""))
24
+
25
+ assert_include(%w[Array ArgumentError], @ri.completion_list("Ar"))
26
+ end
27
+
28
+ def test_info
29
+ assert_include(%w[collect collect! compact compact! concat],
30
+ @ri.info("Array"))
31
+ assert_include(%w[each each_key each_pair each_value empty],
32
+ @ri.info("Hash"))
33
+ assert_kind_of(String, @ri.info("Hash"))
34
+ end
35
+
36
+ def test_args
37
+ assert_match(/map\s+\{\|\s*\w+\s*\|\s+block\s+\}/,
38
+ @ri.args("map"))
39
+ assert_kind_of(String,@ri.args("map"))
40
+ end
41
+
42
+ def test_class_list
43
+ assert_include(%w[StringIO Range Array Hash String Struct],
44
+ @ri.class_list("each"))
45
+ assert_kind_of(Array, @ri.class_list("each"))
46
+ assert_include(%w[Hash Struct],
47
+ @ri.class_list("each_pair"))
48
+ assert_equal(nil, @ri.class_list("__super_bogus_method___"))
49
+ end
50
+
51
+ def test_class_list_with_flag
52
+ assert_include(%w[StringIO# Range# Array# Hash# String# Struct#],
53
+ @ri.class_list_with_flag("each"))
54
+ assert_include(%w[Hash# Struct#],
55
+ @ri.class_list_with_flag("each_pair"))
56
+ assert_equal(nil, @ri.class_list_with_flag("__super_bogus_method___"))
57
+ end
58
+ end
59
+
60
+
@@ -0,0 +1,43 @@
1
+ # Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
2
+ #
3
+
4
+ require 'test/unit'
5
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
+ $:.unshift "lib"
7
+ require 'fastri/full_text_indexer'
8
+ require 'fastri/full_text_index'
9
+
10
+ class TestIntegrationFullTextIndex < Test::Unit::TestCase
11
+ include FastRI
12
+ def setup
13
+ @indexer = FullTextIndexer.new(20)
14
+ end
15
+
16
+ require 'stringio'
17
+ def get_index
18
+ fulltextIO = StringIO.new("")
19
+ suffixIO = StringIO.new("")
20
+ @indexer.build_index(fulltextIO, suffixIO)
21
+ FullTextIndex.new_from_ios(fulltextIO, suffixIO)
22
+ end
23
+
24
+ def test_basic_matching
25
+ @indexer.add_document("first.txt", "this is the first document: foo")
26
+ @indexer.add_document("second.txt", "this is the second document: bar")
27
+ index = get_index
28
+ assert_equal("first.txt", index.lookup("foo").path)
29
+ assert_equal("second.txt", index.lookup("bar").path)
30
+ end
31
+
32
+ def test_metadata
33
+ @indexer.add_document("first.txt", "this is the first document: foo",
34
+ :type => "text/plain", :foo => 'baz')
35
+ @indexer.add_document("second.doc", "this is the second document: bar",
36
+ :type => "application/msword", :bar => "foo")
37
+ index = get_index
38
+ assert_equal("first.txt", index.lookup("foo").path)
39
+ assert_equal("second.doc", index.lookup("bar").path)
40
+ assert_equal({:foo=>"baz", :type=>"text/plain", :size => 31}, index.lookup("foo").metadata)
41
+ assert_equal({:bar=>"foo", :type=>"application/msword", :size => 32}, index.lookup("bar").metadata)
42
+ end
43
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright (C) 2006 Mauricio Fernandez <mfp@acm.org>
2
+ #
3
+
4
+ require 'test/unit'
5
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
6
+ $:.unshift "lib"
7
+ require 'fastri/name_descriptor'
8
+
9
+ class TestNameDescriptor < Test::Unit::TestCase
10
+ include FastRI
11
+ def helper(text)
12
+ desc = NameDescriptor.new(text)
13
+ [desc.class_names, desc.method_name, desc.is_class_method]
14
+ end
15
+
16
+ def test_unqualified_methods
17
+ assert_equal([[], "foo", nil], helper("foo"))
18
+ assert_equal([[], "foo", false], helper("#foo"))
19
+ assert_equal([[], "foo", nil], helper(".foo"))
20
+ assert_equal([[], "foo", true], helper("::foo"))
21
+ end
22
+
23
+ def test_qualified_methods
24
+ assert_equal([["Foo"], "bar", nil], helper("foo.bar"))
25
+ assert_equal([["Foo"], "bar", true], helper("foo::bar"))
26
+ assert_equal([["Foo"], "bar", false], helper("foo#bar"))
27
+ assert_equal([["Foo", "Bar"], "baz", true], helper("foo::bar::baz"))
28
+ end
29
+
30
+ def test_namespaces
31
+ assert_equal([["Foo"], nil, nil], helper("Foo"))
32
+ assert_equal([["Foo", "Bar"], nil, nil], helper("foo::Bar"))
33
+ assert_equal([["Foo", "Bar", "Baz"], nil, nil], helper("Foo::Bar::Baz"))
34
+ end
35
+ end
@@ -0,0 +1,389 @@
1
+ require 'test/unit'
2
+ $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
3
+ $:.unshift "lib"
4
+ require 'fastri/ri_index'
5
+
6
+ class TestRiIndex < Test::Unit::TestCase
7
+ INDEX_DATA =<<EOF
8
+ #{FastRI::RiIndex::MAGIC}
9
+ Sources:
10
+ system /usr/share/ri/system/
11
+ somegem-0.1.0 /long/path/somegem-0.1.0
12
+ stuff-1.1.0 /long/path/stuff-1.1.0
13
+ ================================================================================
14
+ Namespaces:
15
+ ABC 0 1
16
+ ABC::DEF 0 1
17
+ ABC::DEF::Foo 1
18
+ ABC::Zzz 0
19
+ CDE 1 2
20
+ FGH 2
21
+ FGH::Adfdsf 2
22
+ ================================================================================
23
+ Methods:
24
+ ABC::DEF.bar 0
25
+ ABC::DEF::Foo#baz 1
26
+ ABC::DEF::Foo#foo 1
27
+ ABC::Zzz.foo 0 1
28
+ ABC::Zzz#foo 0
29
+ CDE.foo 1 2
30
+ FGH::Adfdsf#foo 2
31
+ ================================================================================
32
+ EOF
33
+ INDEX_DATA2 =<<EOF
34
+ #{FastRI::RiIndex::MAGIC}
35
+ Sources:
36
+ system /usr/share/ri/system/
37
+ somegem-0.1.0 /long/path/somegem-0.1.0
38
+ stuff-1.1.0 /long/path/stuff-1.1.0
39
+ ================================================================================
40
+ Namespaces:
41
+ ABC 0 1
42
+ ABCDEF 1 2
43
+ ================================================================================
44
+ Methods:
45
+ ABC.bar 0
46
+ ABC#baz 1
47
+ ABCDEF#foo 1
48
+ ABCDEF.foo 1 2
49
+ ================================================================================
50
+ EOF
51
+
52
+ require 'stringio'
53
+ def setup
54
+ @index = FastRI::RiIndex.new_from_IO(StringIO.new(INDEX_DATA))
55
+ @index2 = FastRI::RiIndex.new_from_IO(StringIO.new(INDEX_DATA2))
56
+ end
57
+
58
+ def test_dump
59
+ s = StringIO.new("")
60
+ @index.dump(s)
61
+ assert_equal(INDEX_DATA, s.string)
62
+ end
63
+
64
+ def test_toplevel_namespace
65
+ ret = @index.top_level_namespace
66
+ assert_kind_of(Array, ret)
67
+ assert_kind_of(FastRI::RiIndex::TopLevelEntry, ret[0])
68
+ end
69
+
70
+ def test_full_class_names
71
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "ABC::Zzz", "CDE", "FGH", "FGH::Adfdsf"], @index.full_class_names)
72
+ assert_equal(["ABC", "ABC::DEF", "ABC::Zzz"], @index.full_class_names(0))
73
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "CDE"], @index.full_class_names(1))
74
+ assert_equal(["CDE", "FGH", "FGH::Adfdsf"], @index.full_class_names(2))
75
+ assert_equal(["CDE", "FGH", "FGH::Adfdsf"], @index.full_class_names("stuff-1.1.0"))
76
+ assert_equal([], @index.full_class_names("nonexistent-1.1.0"))
77
+ end
78
+
79
+ def test_full_method_names
80
+ assert_equal(["ABC::DEF.bar", "ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo",
81
+ "ABC::Zzz.foo", "ABC::Zzz#foo", "CDE.foo", "FGH::Adfdsf#foo"],
82
+ @index.full_method_names)
83
+ assert_equal(["ABC::DEF.bar", "ABC::Zzz.foo", "ABC::Zzz#foo"],
84
+ @index.full_method_names(0))
85
+ assert_equal(["ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo", "ABC::Zzz.foo", "CDE.foo"],
86
+ @index.full_method_names(1))
87
+ assert_equal(["CDE.foo", "FGH::Adfdsf#foo"], @index.full_method_names(2))
88
+ assert_equal(["CDE.foo", "FGH::Adfdsf#foo"], @index.full_method_names("stuff-1.1.0"))
89
+ assert_equal([], @index.full_method_names("nonexistent-1.1.0"))
90
+ end
91
+
92
+ def test_all_names
93
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "ABC::Zzz", "CDE", "FGH",
94
+ "FGH::Adfdsf", "ABC::DEF.bar", "ABC::DEF::Foo#baz",
95
+ "ABC::DEF::Foo#foo", "ABC::Zzz.foo", "ABC::Zzz#foo",
96
+ "CDE.foo", "FGH::Adfdsf#foo"], @index.all_names)
97
+ assert_equal(["ABC", "ABC::DEF", "ABC::Zzz", "ABC::DEF.bar",
98
+ "ABC::Zzz.foo", "ABC::Zzz#foo"], @index.all_names(0))
99
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "CDE",
100
+ "ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo", "ABC::Zzz.foo",
101
+ "CDE.foo"], @index.all_names(1))
102
+ assert_equal(["CDE", "FGH", "FGH::Adfdsf", "CDE.foo", "FGH::Adfdsf#foo"],
103
+ @index.all_names(2))
104
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "CDE",
105
+ "ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo", "ABC::Zzz.foo",
106
+ "CDE.foo"], @index.all_names("somegem-0.1.0"))
107
+ assert_equal([], @index.all_names("notinstalled-1.0"))
108
+ end
109
+
110
+ def test_get_class_entry
111
+ assert_nil(@index.get_class_entry("NONEXISTENT_FOO"))
112
+ assert_equal("ABC", @index.get_class_entry("ABC", nil).full_name)
113
+ assert_nil(@index.get_class_entry("ABC", nil).source_index)
114
+ assert_nil(@index.get_class_entry("ABC::DEF::Foo", 0))
115
+ assert_equal(1, @index.get_class_entry("ABC::DEF::Foo", 1).source_index)
116
+ assert_equal(2, @index.get_class_entry("ABC::DEF::Foo", 1).index)
117
+ end
118
+
119
+ def test_get_method_entry
120
+ assert_equal("ABC::DEF.bar", @index.get_method_entry("ABC::DEF.bar", nil).full_name)
121
+ assert_equal(0, @index.get_method_entry("ABC::DEF.bar", 0).source_index)
122
+ assert_nil(@index.get_method_entry("FGH::Adfdsf#foo", 1))
123
+ assert_equal(6, @index.get_method_entry("FGH::Adfdsf#foo", 2).index)
124
+ assert_equal(2, @index.get_method_entry("FGH::Adfdsf#foo", 2).source_index)
125
+ assert_equal("FGH::Adfdsf#foo", @index.get_method_entry("FGH::Adfdsf#foo", 2).full_name)
126
+ end
127
+
128
+ def test_namespaces_under
129
+ assert_kind_of(Array, @index.namespaces_under("ABC", true, nil))
130
+ results = @index.namespaces_under("ABC", true, nil)
131
+ assert_equal(3, results.size)
132
+ assert_equal(["ABC::DEF", "ABC::DEF::Foo", "ABC::Zzz"], results.map{|x| x.full_name})
133
+ results = @index.namespaces_under("ABC", false, nil)
134
+ assert_equal(2, results.size)
135
+ assert_equal(["ABC::DEF", "ABC::Zzz"], results.map{|x| x.full_name})
136
+ end
137
+
138
+ def test_namespaces_under_scoped
139
+ results = @index.namespaces_under("ABC", false, 1)
140
+ assert_kind_of(Array, results)
141
+ assert_equal(["ABC::DEF"], results.map{|x| x.full_name})
142
+ results = @index.namespaces_under("ABC", true, 1)
143
+ assert_equal(2, results.size)
144
+ assert_equal(["ABC::DEF", "ABC::DEF::Foo"], results.map{|x| x.full_name})
145
+ results = @index.namespaces_under("ABC", true, "somegem-0.1.0")
146
+ assert_equal(2, results.size)
147
+ assert_equal(["ABC::DEF", "ABC::DEF::Foo"], results.map{|x| x.full_name})
148
+ results = @index.namespaces_under("ABC", true, 0)
149
+ assert_equal(2, results.size)
150
+ assert_equal(["ABC::DEF", "ABC::Zzz"], results.map{|x| x.full_name})
151
+ end
152
+
153
+ def test_namespaces_under_toplevel
154
+ toplevel = @index.top_level_namespace[0]
155
+ assert_equal(["ABC", "CDE", "FGH"],
156
+ @index.namespaces_under(toplevel, false, nil).map{|x| x.full_name})
157
+ assert_equal(["ABC", "ABC::DEF", "ABC::DEF::Foo", "ABC::Zzz",
158
+ "CDE", "FGH", "FGH::Adfdsf"],
159
+ @index.namespaces_under(toplevel, true, nil).map{|x| x.full_name})
160
+ assert_equal(["CDE", "FGH", "FGH::Adfdsf"],
161
+ @index.namespaces_under(toplevel, true, "stuff-1.1.0").map{|x| x.full_name})
162
+ end
163
+
164
+ def test_source_paths_for_string
165
+ assert_equal([], @index.source_paths_for(""))
166
+ assert_equal([], @index.source_paths_for(nil))
167
+
168
+ assert_equal(["/usr/share/ri/system/", "/long/path/somegem-0.1.0"], @index.source_paths_for("ABC::DEF"))
169
+ assert_equal(["/long/path/somegem-0.1.0", "/long/path/stuff-1.1.0"], @index.source_paths_for("CDE.foo"))
170
+ end
171
+
172
+ def test_source_paths_for_entry
173
+ assert_equal(["/usr/share/ri/system/", "/long/path/somegem-0.1.0"],
174
+ @index.source_paths_for(@index.get_class_entry("ABC::DEF")))
175
+ assert_equal(["/long/path/somegem-0.1.0", "/long/path/stuff-1.1.0"],
176
+ @index.source_paths_for(@index.get_method_entry("CDE.foo")))
177
+ end
178
+
179
+ def test_methods_under_same_prefix
180
+ results = @index2.methods_under("ABC", true, nil)
181
+ results.map{|x| x.full_name}
182
+ assert_equal(["ABC.bar", "ABC#baz"], results.map{|x| x.full_name})
183
+ end
184
+
185
+ def test_methods_under_scoped
186
+ results = @index.methods_under("ABC", true, 1)
187
+ assert_equal(["ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo", "ABC::Zzz.foo"], results.map{|x| x.full_name})
188
+ results = @index.methods_under("CDE", false, "stuff-1.1.0")
189
+ assert_equal(["CDE.foo"], results.map{|x| x.full_name})
190
+ results = @index.methods_under("ABC", true, nil)
191
+ assert_equal(["ABC::DEF.bar", "ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo",
192
+ "ABC::Zzz.foo", "ABC::Zzz#foo"], results.map{|x| x.full_name})
193
+ assert_equal(["ABC::DEF.bar", "ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo",
194
+ "ABC::Zzz.foo", "ABC::Zzz#foo", "CDE.foo", "FGH::Adfdsf#foo"],
195
+ @index.methods_under("", true, nil).map{|x| x.full_name})
196
+ assert_equal([], @index.methods_under("ABC", false, nil).map{|x| x.full_name})
197
+ assert_equal(["CDE.foo"],
198
+ @index.methods_under("CDE", false, nil).map{|x| x.full_name})
199
+ assert_equal(["FGH::Adfdsf#foo"],
200
+ @index.methods_under("FGH", true, nil).map{|x| x.full_name})
201
+ assert_equal([], @index.methods_under("FGH", true, 0).map{|x| x.full_name})
202
+ assert_equal(["FGH::Adfdsf#foo"],
203
+ @index.methods_under("FGH", true, 2).map{|x| x.full_name})
204
+ assert_equal([], @index.methods_under("FGH", false, 2).map{|x| x.full_name})
205
+ assert_equal(["FGH::Adfdsf#foo"],
206
+ @index.methods_under("FGH::Adfdsf", false, 2).map{|x| x.full_name})
207
+ assert_equal(["FGH::Adfdsf#foo"],
208
+ @index.methods_under("FGH::Adfdsf", true, 2).map{|x| x.full_name})
209
+ assert_equal([], @index.methods_under("FGH::Adfdsf", false, 0).map{|x| x.full_name})
210
+ end
211
+
212
+ def test_lookup_namespace_in
213
+ toplevel = @index.top_level_namespace
214
+ res = @index.lookup_namespace_in("ABC", toplevel)
215
+ assert_equal(["ABC"], res.map{|x| x.full_name})
216
+ toplevel2 = @index.top_level_namespace(2)
217
+ assert_equal([], @index.lookup_namespace_in("ABC", toplevel2))
218
+ assert_equal(["FGH"], @index.lookup_namespace_in("FGH", toplevel2).map{|x| x.full_name})
219
+ end
220
+
221
+ def test_find_class_by_name
222
+ class_entry = nil
223
+ class << @index; self end.module_eval do
224
+ define_method(:get_class){|x| class_entry = x}
225
+ end
226
+ @index.find_class_by_name("ABC")
227
+ assert_kind_of(FastRI::RiIndex::ClassEntry, class_entry)
228
+ assert_equal("ABC", class_entry.full_name)
229
+ assert_nil(class_entry.source_index)
230
+ assert_equal(0, class_entry.index)
231
+ class_entry = nil
232
+ @index.find_class_by_name("ABC::DEF::Foo")
233
+ assert_kind_of(FastRI::RiIndex::ClassEntry, class_entry)
234
+ assert_equal("ABC::DEF::Foo", class_entry.full_name)
235
+ assert_nil(class_entry.source_index)
236
+ assert_equal(2, class_entry.index)
237
+ class_entry = nil
238
+ @index.find_class_by_name("ABC::DEF::Foo", 1)
239
+ assert_kind_of(FastRI::RiIndex::ClassEntry, class_entry)
240
+ assert_equal("ABC::DEF::Foo", class_entry.full_name)
241
+ assert_equal(1, class_entry.source_index)
242
+ assert_equal(2, class_entry.index)
243
+ class_entry = nil
244
+ @index.find_class_by_name("ABC::DEF::Foo", 0)
245
+ assert_nil(class_entry)
246
+ @index.find_class_by_name("AB", nil)
247
+ assert_nil(class_entry)
248
+ end
249
+
250
+ def test_find_method_by_name
251
+ method_entry = nil
252
+ class << @index; self end.module_eval do
253
+ define_method(:get_method){|x| method_entry = x}
254
+ end
255
+ @index.find_method_by_name("ABC")
256
+ assert_nil(method_entry)
257
+ @index.find_method_by_name("ABC::DEF.bar")
258
+ assert_equal("ABC::DEF.bar", method_entry.full_name)
259
+ method_entry = nil
260
+ @index.find_method_by_name("ABC::DEF::Foo#baz")
261
+ assert_equal("ABC::DEF::Foo#baz", method_entry.full_name)
262
+ assert_nil(method_entry.source_index)
263
+ assert_equal(1, method_entry.index)
264
+ method_entry = nil
265
+ @index.find_method_by_name("ABC::DEF::Foo#baz", 1)
266
+ assert_equal("ABC::DEF::Foo#baz", method_entry.full_name)
267
+ assert_equal(1, method_entry.source_index)
268
+ assert_equal(1, method_entry.index)
269
+ method_entry = nil
270
+ @index.find_method_by_name("CDE.foo", 2)
271
+ assert_equal("CDE.foo", method_entry.full_name)
272
+ assert_equal(5, method_entry.index)
273
+ assert_equal(2, method_entry.source_index)
274
+ method_entry = nil
275
+ @index.find_method_by_name("ABC::DEF::Foo#ba", 1)
276
+ assert_nil(method_entry)
277
+ @index.find_method_by_name("ABC::DEF.bar", 1)
278
+ assert_nil(method_entry)
279
+ end
280
+
281
+ def test_find_methods
282
+ toplevel = @index.top_level_namespace
283
+ assert_equal(["ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo",
284
+ "ABC::Zzz#foo", "FGH::Adfdsf#foo"],
285
+ @index.find_methods("", false, toplevel).map{|x| x.full_name})
286
+ assert_equal(["ABC::DEF.bar", "ABC::Zzz.foo", "CDE.foo"],
287
+ @index.find_methods("", true, toplevel).map{|x| x.full_name})
288
+ assert_equal([], @index.find_methods("ABC", true, toplevel).map{|x| x.full_name})
289
+ assert_equal(["ABC::DEF::Foo#foo", "ABC::Zzz#foo", "FGH::Adfdsf#foo"],
290
+ @index.find_methods("foo", false, toplevel).map{|x| x.full_name})
291
+ assert_equal(["ABC::Zzz.foo", "CDE.foo"],
292
+ @index.find_methods("foo", true, toplevel).map{|x| x.full_name})
293
+ toplevel = @index.top_level_namespace(1)
294
+ assert_equal(["ABC::DEF::Foo#foo"], @index.find_methods("foo", false, toplevel).map{|x| x.full_name})
295
+ toplevel = @index.top_level_namespace("stuff-1.1.0")
296
+ assert_equal(["CDE.foo"], @index.find_methods("foo", true, toplevel).map{|x| x.full_name})
297
+ end
298
+
299
+ def test_num_namespaces
300
+ assert_equal(7, @index.num_namespaces)
301
+ end
302
+
303
+ def test_num_methods
304
+ assert_equal(7, @index.num_methods)
305
+ end
306
+
307
+ #{{{ ClassEntry and MethodEntry
308
+ def test_ClassEntry_contained_modules_matching
309
+ toplevel = @index.top_level_namespace[0]
310
+ assert_equal(["ABC"], toplevel.contained_modules_matching("ABC").map{|x| x.full_name})
311
+
312
+ class_entry = @index.get_class_entry("ABC")
313
+ assert_equal([], class_entry.contained_modules_matching("ABC").map{|x| x.full_name})
314
+ assert_equal(["ABC::DEF", "ABC::Zzz"], class_entry.contained_modules_matching("").map{|x| x.full_name})
315
+ end
316
+
317
+ def test_ClassEntry_type
318
+ class_entry = @index.get_class_entry("ABC")
319
+ assert_equal(:namespace, class_entry.type)
320
+ end
321
+
322
+ def test_ClassEntry_path_names
323
+ class_entry = @index.get_class_entry("ABC")
324
+ assert_equal(["/usr/share/ri/system/ABC", "/long/path/somegem-0.1.0/ABC"], class_entry.path_names)
325
+
326
+ class_entry = @index.get_class_entry("ABC", 0)
327
+ assert_equal(["/usr/share/ri/system/ABC"], class_entry.path_names)
328
+ class_entry = @index.get_class_entry("ABC", 1)
329
+ assert_equal(["/long/path/somegem-0.1.0/ABC"], class_entry.path_names)
330
+ end
331
+
332
+ def test_ClassEntry_classes_and_modules
333
+ class_entry = @index.get_class_entry("ABC")
334
+ assert_equal(["ABC::DEF", "ABC::Zzz"],
335
+ class_entry.classes_and_modules.map{|x| x.full_name})
336
+ class_entry = @index.get_class_entry("ABC::DEF")
337
+ assert_equal(["ABC::DEF::Foo"], class_entry.classes_and_modules.map{|x| x.full_name})
338
+ end
339
+
340
+ def test_ClassEntry_contained_class_named
341
+ class_entry = @index.get_class_entry("ABC")
342
+ class_entry = class_entry.contained_class_named("DEF")
343
+ assert_equal("ABC::DEF", class_entry.full_name)
344
+ assert_equal(1, class_entry.index)
345
+ class_entry = class_entry.contained_class_named("Foo")
346
+ assert_equal(2, class_entry.index)
347
+ assert_nil(class_entry.contained_class_named("Bar"))
348
+ assert_equal(3, @index.get_class_entry("ABC").contained_class_named("Zzz").index)
349
+ end
350
+
351
+ def test_ClassEntry_methods_matching
352
+ class_entry = @index.get_class_entry("ABC::Zzz")
353
+ assert_equal([], class_entry.methods_matching("nonexistent", false))
354
+ assert_equal([3], class_entry.methods_matching("foo", true).map{|x| x.index})
355
+ assert_equal([4], class_entry.methods_matching("foo", false).map{|x| x.index})
356
+ end
357
+
358
+ def test_ClassEntry_recursively_find_methods_matching
359
+ class_entry = @index.get_class_entry("ABC")
360
+ assert_equal(["ABC::DEF.bar", "ABC::Zzz.foo"],
361
+ class_entry.recursively_find_methods_matching(//, true).map{|x| x.full_name})
362
+ assert_equal(["ABC::DEF::Foo#baz", "ABC::DEF::Foo#foo", "ABC::Zzz#foo"],
363
+ class_entry.recursively_find_methods_matching(//, false).map{|x| x.full_name})
364
+ assert_equal([], class_entry.recursively_find_methods_matching(/nonono/, false).map{|x| x.full_name})
365
+ end
366
+
367
+ def test_ClassEntry_all_method_names
368
+ class_entry = @index.get_class_entry("ABC")
369
+ assert_equal([], class_entry.all_method_names)
370
+ class_entry = @index.get_class_entry("ABC::Zzz")
371
+ assert_equal(["ABC::Zzz.foo", "ABC::Zzz#foo"], class_entry.all_method_names)
372
+ end
373
+
374
+ def test_MethodEntry_path_name
375
+ method_entry = @index.get_method_entry("CDE.foo")
376
+ assert_equal("/long/path/somegem-0.1.0/CDE/foo-c.yaml", method_entry.path_name)
377
+
378
+ method_entry = @index.get_method_entry("CDE.foo", 1)
379
+ assert_equal("/long/path/somegem-0.1.0/CDE/foo-c.yaml", method_entry.path_name)
380
+ method_entry = @index.get_method_entry("CDE.foo", 2)
381
+ assert_equal("/long/path/stuff-1.1.0/CDE/foo-c.yaml", method_entry.path_name)
382
+ end
383
+
384
+ def test_MethodEntry_type
385
+ method_entry = @index.get_method_entry("CDE.foo")
386
+ assert_equal(:method, method_entry.type)
387
+ end
388
+ end
389
+