Class: RubyIndexer::PrefixTreeTest

Inherits:
Minitest::Test
  • Object
show all
Defined in:
lib/ruby_indexer/test/prefix_tree_test.rb

Instance Method Summary collapse

Instance Method Details

#test_delete_does_not_impact_other_keys_with_the_same_valueObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 109

def test_delete_does_not_impact_other_keys_with_the_same_value
  tree = PrefixTree[String].new
  tree.insert("key1", "value")
  tree.insert("key2", "value")
  assert_equal(["value", "value"], tree.search("key"))

  tree.delete("key2")
  assert_empty(tree.search("key2"))
  assert_equal(["value"], tree.search("key1"))
end

#test_deleted_node_is_removed_from_the_treeObject



120
121
122
123
124
125
126
127
128
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 120

def test_deleted_node_is_removed_from_the_tree
  tree = PrefixTree[String].new
  tree.insert("foo/bar", "foo/bar")
  assert_equal(["foo/bar"], tree.search("foo"))

  tree.delete("foo/bar")
  root = tree.instance_variable_get(:@root)
  assert_empty(root.children)
end

#test_deleting_non_terminal_nodesObject



130
131
132
133
134
135
136
137
138
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 130

def test_deleting_non_terminal_nodes
  tree = PrefixTree[String].new
  tree.insert("abc", "value1")
  tree.insert("abcdef", "value2")

  tree.delete("abcdef")
  assert_empty(tree.search("abcdef"))
  assert_equal(["value1"], tree.search("abc"))
end

#test_deletionObject



99
100
101
102
103
104
105
106
107
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 99

def test_deletion
  tree = PrefixTree[String].new
  ["foo/bar", "foo/baz"].each { |item| tree.insert(item, item) }
  assert_equal(["foo/bar", "foo/baz"], tree.search("foo"))

  tree.delete("foo/bar")
  assert_empty(tree.search("foo/bar"))
  assert_equal(["foo/baz"], tree.search("foo"))
end

#test_emptyObject



8
9
10
11
12
13
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 8

def test_empty
  tree = PrefixTree.new

  assert_empty(tree.search(""))
  assert_empty(tree.search("foo"))
end

#test_multiple_itemsObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 24

def test_multiple_items
  tree = PrefixTree[String].new
  ["foo", "bar", "baz"].each { |item| tree.insert(item, item) }

  assert_equal(["foo", "bar", "baz"], tree.search(""))
  assert_equal(["bar", "baz"], tree.search("b"))
  assert_equal(["foo"], tree.search("fo"))
  assert_equal(["bar", "baz"], tree.search("ba"))
  assert_equal(["baz"], tree.search("baz"))
  assert_empty(tree.search("qux"))
end

#test_multiple_prefixesObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 36

def test_multiple_prefixes
  tree = PrefixTree[String].new
  ["fo", "foo"].each { |item| tree.insert(item, item) }

  assert_equal(["fo", "foo"], tree.search(""))
  assert_equal(["fo", "foo"], tree.search("f"))
  assert_equal(["fo", "foo"], tree.search("fo"))
  assert_equal(["foo"], tree.search("foo"))
  assert_empty(tree.search("fooo"))
end

#test_multiple_prefixes_with_shuffled_orderObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 47

def test_multiple_prefixes_with_shuffled_order
  tree = PrefixTree[String].new
  [
    "foo/bar/base",
    "foo/bar/on",
    "foo/bar/support/selection",
    "foo/bar/support/runner",
    "foo/internal",
    "foo/bar/document",
    "foo/bar/code",
    "foo/bar/support/rails",
    "foo/bar/diagnostics",
    "foo/bar/document2",
    "foo/bar/support/runner2",
    "foo/bar/support/diagnostic",
    "foo/document",
    "foo/bar/formatting",
    "foo/bar/support/highlight",
    "foo/bar/semantic",
    "foo/bar/support/prefix",
    "foo/bar/folding",
    "foo/bar/selection",
    "foo/bar/support/syntax",
    "foo/bar/document3",
    "foo/bar/hover",
    "foo/bar/support/semantic",
    "foo/bar/support/source",
    "foo/bar/inlay",
    "foo/requests",
    "foo/bar/support/formatting",
    "foo/bar/path",
    "foo/executor",
  ].each { |item| tree.insert(item, item) }

  assert_equal(
    [
      "foo/bar/support/selection",
      "foo/bar/support/semantic",
      "foo/bar/support/syntax",
      "foo/bar/support/source",
      "foo/bar/support/runner",
      "foo/bar/support/runner2",
      "foo/bar/support/rails",
      "foo/bar/support/diagnostic",
      "foo/bar/support/highlight",
      "foo/bar/support/prefix",
      "foo/bar/support/formatting",
    ],
    tree.search("foo/bar/support"),
  )
end

#test_overriding_valuesObject



140
141
142
143
144
145
146
147
148
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 140

def test_overriding_values
  tree = PrefixTree[Integer].new

  tree.insert("foo/bar", 123)
  assert_equal([123], tree.search("foo/bar"))

  tree.insert("foo/bar", 456)
  assert_equal([456], tree.search("foo/bar"))
end

#test_single_itemObject



15
16
17
18
19
20
21
22
# File 'lib/ruby_indexer/test/prefix_tree_test.rb', line 15

def test_single_item
  tree = PrefixTree.new
  tree.insert("foo", "foo")

  assert_equal(["foo"], tree.search(""))
  assert_equal(["foo"], tree.search("foo"))
  assert_empty(tree.search("bar"))
end