tests: test ModelIndex
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 22 Aug 2016 23:26:58 +0000 (19:26 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 22 Aug 2016 23:26:58 +0000 (19:26 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

25 files changed:
src/test_model_index.nit [new file with mode: 0644]
tests/nitcg.skip
tests/sav/test_model_index.res [new file with mode: 0644]
tests/sav/test_model_index_args1.res [new file with mode: 0644]
tests/sav/test_model_index_args10.res [new file with mode: 0644]
tests/sav/test_model_index_args11.res [new file with mode: 0644]
tests/sav/test_model_index_args12.res [new file with mode: 0644]
tests/sav/test_model_index_args13.res [new file with mode: 0644]
tests/sav/test_model_index_args14.res [new file with mode: 0644]
tests/sav/test_model_index_args15.res [new file with mode: 0644]
tests/sav/test_model_index_args16.res [new file with mode: 0644]
tests/sav/test_model_index_args17.res [new file with mode: 0644]
tests/sav/test_model_index_args18.res [new file with mode: 0644]
tests/sav/test_model_index_args19.res [new file with mode: 0644]
tests/sav/test_model_index_args2.res [new file with mode: 0644]
tests/sav/test_model_index_args20.res [new file with mode: 0644]
tests/sav/test_model_index_args21.res [new file with mode: 0644]
tests/sav/test_model_index_args3.res [new file with mode: 0644]
tests/sav/test_model_index_args4.res [new file with mode: 0644]
tests/sav/test_model_index_args5.res [new file with mode: 0644]
tests/sav/test_model_index_args6.res [new file with mode: 0644]
tests/sav/test_model_index_args7.res [new file with mode: 0644]
tests/sav/test_model_index_args8.res [new file with mode: 0644]
tests/sav/test_model_index_args9.res [new file with mode: 0644]
tests/test_model_index.args [new file with mode: 0644]

diff --git a/src/test_model_index.nit b/src/test_model_index.nit
new file mode 100644 (file)
index 0000000..38f72c8
--- /dev/null
@@ -0,0 +1,105 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import frontend
+import model_index
+import console
+
+redef class ToolContext
+       var opt_name_prefix = new OptionBool("", "--name-prefix")
+       var opt_full_name_prefix = new OptionBool("", "--full-name-prefix")
+       var opt_name_similarity = new OptionBool("", "--name-similarity")
+       var opt_full_name_similarity = new OptionBool("", "--full-name-similarity")
+       var opt_name = new OptionBool("", "--name")
+       var opt_full_name = new OptionBool("", "--full-name")
+
+       redef init do
+               option_context.add_option(opt_name_prefix, opt_full_name_prefix)
+               option_context.add_option(opt_name_similarity, opt_full_name_similarity)
+               option_context.add_option(opt_name, opt_full_name)
+       end
+end
+
+redef class MEntity
+       fun color: String do
+               if visibility == public_visibility then
+                       return name.green
+               else if visibility == protected_visibility then
+                       return name.yellow
+               else
+                       return name.red
+               end
+       end
+end
+
+# build toolcontext
+var toolcontext = new ToolContext
+toolcontext.process_options(args)
+var args = toolcontext.option_context.rest
+
+if not args.length == 2 then
+       print "usage: test_model_index <nitfile> <search_query>"
+       exit 1
+end
+
+# build model
+var model = new Model
+var mbuilder = new ModelBuilder(model, toolcontext)
+var mmodules = mbuilder.parse_full([args.first])
+
+# process
+if mmodules.is_empty then return
+mbuilder.run_phases
+toolcontext.run_global_phases(mmodules)
+
+# Build index
+var index = new ModelIndex
+for mentity in model.private_view.mentities do
+       if mentity isa MClassDef or mentity isa MPropDef then continue
+       index.index(mentity)
+end
+
+var q = args[1]
+
+print "# {q}\n"
+
+var res
+if toolcontext.opt_name_prefix.value then
+       res = index.find_by_name_prefix(q)
+else if toolcontext.opt_full_name_prefix.value then
+       res = index.find_by_full_name_prefix(q)
+else if toolcontext.opt_name_similarity.value then
+       res = index.find_by_name_similarity(q)
+else if toolcontext.opt_full_name_similarity.value then
+       res = index.find_by_full_name_similarity(q)
+else if toolcontext.opt_name.value then
+       res = index.find_by_name(q)
+else if toolcontext.opt_full_name.value then
+       res = index.find_by_full_name(q)
+else
+       res = index.find(q)
+end
+
+res = res.sort(new ScoreComparator, new MEntityComparator).
+               uniq.
+               limit(10).
+               sort(new VisibilityComparator, new NameComparator)
+
+for e in res do
+       if toolcontext.opt_no_color.value then
+               print " * {e.score}: {e.mentity.name} ({e.mentity.full_name})"
+       else
+               print " * {e.score}: {e.mentity.color} ({e.mentity.full_name})"
+       end
+end
index cc9df0a..12e56b7 100644 (file)
@@ -5,4 +5,5 @@ test_phase
 test_parser
 test_highlight
 test_model_visitor
+test_model_index
 ^nit
diff --git a/tests/sav/test_model_index.res b/tests/sav/test_model_index.res
new file mode 100644 (file)
index 0000000..e50ad70
--- /dev/null
@@ -0,0 +1,2 @@
+Usage: [OPTION]... [ARG]...
+Use --help for help
diff --git a/tests/sav/test_model_index_args1.res b/tests/sav/test_model_index_args1.res
new file mode 100644 (file)
index 0000000..62bdafb
--- /dev/null
@@ -0,0 +1,3 @@
+# Obj
+
+ * 1: Object (test_prog::Object)
diff --git a/tests/sav/test_model_index_args10.res b/tests/sav/test_model_index_args10.res
new file mode 100644 (file)
index 0000000..982c125
--- /dev/null
@@ -0,0 +1,12 @@
+# elfves
+
+ * 4: Elf (test_prog::Elf)
+ * 5: Float (test_prog::Float)
+ * 5: Race (test_prog::Race)
+ * 5: careers (test_prog::careers)
+ * 5: excluded (excluded>)
+ * 5: excluded (excluded::excluded)
+ * 5: excluded (excluded)
+ * 5: game (test_prog>game>)
+ * 5: game (test_prog::game)
+ * 4: races (test_prog::races)
diff --git a/tests/sav/test_model_index_args11.res b/tests/sav/test_model_index_args11.res
new file mode 100644 (file)
index 0000000..510eaba
--- /dev/null
@@ -0,0 +1,12 @@
+# Dwarves
+
+ * 13: List (test_prog::List)
+ * 12: Sys (test_prog::Sys)
+ * 8: excluded (excluded>)
+ * 7: excluded (excluded)
+ * 13: game (test_prog>game>)
+ * 13: races (test_prog::races)
+ * 13: rpg (test_prog>rpg>)
+ * 13: rpg (test_prog::rpg)
+ * 9: test_prog (test_prog>)
+ * 9: test_prog (test_prog)
diff --git a/tests/sav/test_model_index_args12.res b/tests/sav/test_model_index_args12.res
new file mode 100644 (file)
index 0000000..b1ae3db
--- /dev/null
@@ -0,0 +1,12 @@
+# test_prof::Dwarves
+
+ * 5: Career (test_prog::Career)
+ * 4: Dwarf (test_prog::Dwarf)
+ * 6: Game (test_prog::Game)
+ * 6: Race (test_prog::Race)
+ * 5: Starter (test_prog::Starter)
+ * 5: careers (test_prog::careers)
+ * 7: character (test_prog::character)
+ * 6: game (test_prog::game)
+ * 5: races (test_prog::races)
+ * 7: rpg (test_prog::rpg)
diff --git a/tests/sav/test_model_index_args13.res b/tests/sav/test_model_index_args13.res
new file mode 100644 (file)
index 0000000..cb5e115
--- /dev/null
@@ -0,0 +1,12 @@
+# Obj
+
+ * 3: * (test_prog::Float::*)
+ * 3: / (test_prog::Int::/)
+ * 3: == (test_prog::Object::==)
+ * 3: Elf (test_prog::Elf)
+ * 3: Int (test_prog::Int)
+ * 1: Object (test_prog::Object)
+ * 3: Sys (test_prog::Sys)
+ * 3: age (test_prog::Character::age)
+ * 3: rpg (test_prog>rpg>)
+ * 3: rpg (test_prog::rpg)
diff --git a/tests/sav/test_model_index_args14.res b/tests/sav/test_model_index_args14.res
new file mode 100644 (file)
index 0000000..3fa271e
--- /dev/null
@@ -0,0 +1,12 @@
+# C
+
+ * 1: * (test_prog::Float::*)
+ * 1: * (test_prog::Int::*)
+ * 1: + (test_prog::Float::+)
+ * 1: - (test_prog::Int::-)
+ * 1: - (test_prog::Float::-)
+ * 1: / (test_prog::Float::/)
+ * 1: / (test_prog::Int::/)
+ * 1: > (test_prog::Int::>)
+ * 1: > (test_prog::Float::>)
+ * 1: Career (test_prog::Career)
diff --git a/tests/sav/test_model_index_args15.res b/tests/sav/test_model_index_args15.res
new file mode 100644 (file)
index 0000000..65b6c73
--- /dev/null
@@ -0,0 +1,12 @@
+# to
+
+ * 2: * (test_prog::Float::*)
+ * 2: + (test_prog::Float::+)
+ * 2: - (test_prog::Float::-)
+ * 2: / (test_prog::Float::/)
+ * 2: / (test_prog::Int::/)
+ * 2: == (test_prog::Object::==)
+ * 2: > (test_prog::Int::>)
+ * 2: > (test_prog::Float::>)
+ * 1: to_f (test_prog::Int::to_f)
+ * 2: total_strengh (test_prog::Character::total_strengh)
diff --git a/tests/sav/test_model_index_args16.res b/tests/sav/test_model_index_args16.res
new file mode 100644 (file)
index 0000000..998a270
--- /dev/null
@@ -0,0 +1,12 @@
+# Dwa
+
+ * 13: Dwarf (test_prog::Dwarf)
+ * 14: Game (test_prog::Game)
+ * 9: excluded (excluded>)
+ * 8: excluded (excluded)
+ * 14: game (test_prog>game>)
+ * 14: game (test_prog::game)
+ * 14: rpg (test_prog::rpg)
+ * 14: rpg (test_prog>rpg>)
+ * 10: test_prog (test_prog>)
+ * 9: test_prog (test_prog)
diff --git a/tests/sav/test_model_index_args17.res b/tests/sav/test_model_index_args17.res
new file mode 100644 (file)
index 0000000..89e9bc8
--- /dev/null
@@ -0,0 +1,12 @@
+# test_prog::C
+
+ * 1: Career (test_prog::Career)
+ * 2: Character (test_prog::Character)
+ * 3: Combatable (test_prog::Combatable)
+ * 3: Elf (test_prog::Elf)
+ * 3: Int (test_prog::Int)
+ * 3: Sys (test_prog::Sys)
+ * 4: game (test_prog::game)
+ * 3: rpg (test_prog::rpg)
+ * 3: test_prog (test_prog)
+ * 3: test_prog (test_prog>)
diff --git a/tests/sav/test_model_index_args18.res b/tests/sav/test_model_index_args18.res
new file mode 100644 (file)
index 0000000..3fa271e
--- /dev/null
@@ -0,0 +1,12 @@
+# C
+
+ * 1: * (test_prog::Float::*)
+ * 1: * (test_prog::Int::*)
+ * 1: + (test_prog::Float::+)
+ * 1: - (test_prog::Int::-)
+ * 1: - (test_prog::Float::-)
+ * 1: / (test_prog::Float::/)
+ * 1: / (test_prog::Int::/)
+ * 1: > (test_prog::Int::>)
+ * 1: > (test_prog::Float::>)
+ * 1: Career (test_prog::Career)
diff --git a/tests/sav/test_model_index_args19.res b/tests/sav/test_model_index_args19.res
new file mode 100644 (file)
index 0000000..d2cf349
--- /dev/null
@@ -0,0 +1,12 @@
+# to
+
+ * 2: * (test_prog::Int::*)
+ * 2: * (test_prog::Float::*)
+ * 2: + (test_prog::Float::+)
+ * 2: - (test_prog::Float::-)
+ * 2: / (test_prog::Float::/)
+ * 2: / (test_prog::Int::/)
+ * 2: > (test_prog::Float::>)
+ * 2: > (test_prog::Int::>)
+ * 1: to_f (test_prog::Int::to_f)
+ * 2: total_strengh (test_prog::Character::total_strengh)
diff --git a/tests/sav/test_model_index_args2.res b/tests/sav/test_model_index_args2.res
new file mode 100644 (file)
index 0000000..b094747
--- /dev/null
@@ -0,0 +1,5 @@
+# C
+
+ * 1: Career (test_prog::Career)
+ * 2: Character (test_prog::Character)
+ * 3: Combatable (test_prog::Combatable)
diff --git a/tests/sav/test_model_index_args20.res b/tests/sav/test_model_index_args20.res
new file mode 100644 (file)
index 0000000..1e858ea
--- /dev/null
@@ -0,0 +1,12 @@
+# Foo
+
+ * 3: != (test_prog::Object::!=)
+ * 3: + (test_prog::Int::+)
+ * 2: Bool (test_prog::Bool)
+ * 3: Elf (test_prog::Elf)
+ * 3: Float (test_prog::Float)
+ * 3: Int (test_prog::Int)
+ * 3: Sys (test_prog::Sys)
+ * 3: dps (test_prog::Weapon::dps)
+ * 3: rpg (test_prog::rpg)
+ * 3: rpg (test_prog>rpg>)
diff --git a/tests/sav/test_model_index_args21.res b/tests/sav/test_model_index_args21.res
new file mode 100644 (file)
index 0000000..c06776c
--- /dev/null
@@ -0,0 +1,12 @@
+# test_prog::C
+
+ * 1: Career (test_prog::Career)
+ * 2: Character (test_prog::Character)
+ * 3: Combatable (test_prog::Combatable)
+ * 3: Elf (test_prog::Elf)
+ * 3: Int (test_prog::Int)
+ * 3: Sys (test_prog::Sys)
+ * 3: rpg (test_prog::rpg)
+ * 3: test_prog (test_prog::test_prog)
+ * 3: test_prog (test_prog)
+ * 3: test_prog (test_prog>)
diff --git a/tests/sav/test_model_index_args3.res b/tests/sav/test_model_index_args3.res
new file mode 100644 (file)
index 0000000..c3ac095
--- /dev/null
@@ -0,0 +1,7 @@
+# e
+
+ * 2: endurance_bonus (test_prog::Career::endurance_bonus)
+ * 1: excluded (excluded>)
+ * 1: excluded (excluded::excluded)
+ * 1: excluded (excluded)
+ * 3: endurance_bonus= (test_prog::Career::endurance_bonus=)
diff --git a/tests/sav/test_model_index_args4.res b/tests/sav/test_model_index_args4.res
new file mode 100644 (file)
index 0000000..dda5763
--- /dev/null
@@ -0,0 +1,6 @@
+# to
+
+ * 1: to_f (test_prog::Int::to_f)
+ * 3: total_endurance (test_prog::Character::total_endurance)
+ * 4: total_intelligence (test_prog::Character::total_intelligence)
+ * 2: total_strengh (test_prog::Character::total_strengh)
diff --git a/tests/sav/test_model_index_args5.res b/tests/sav/test_model_index_args5.res
new file mode 100644 (file)
index 0000000..05661e5
--- /dev/null
@@ -0,0 +1,2 @@
+# C
+
diff --git a/tests/sav/test_model_index_args6.res b/tests/sav/test_model_index_args6.res
new file mode 100644 (file)
index 0000000..490c343
--- /dev/null
@@ -0,0 +1,12 @@
+# test_prog::
+
+ * 6: Bool (test_prog::Bool)
+ * 4: Elf (test_prog::Elf)
+ * 9: Game (test_prog::Game)
+ * 2: Int (test_prog::Int)
+ * 7: List (test_prog::List)
+ * 8: Race (test_prog::Race)
+ * 3: Sys (test_prog::Sys)
+ * 5: game (test_prog::game)
+ * 10: races (test_prog::races)
+ * 1: rpg (test_prog::rpg)
diff --git a/tests/sav/test_model_index_args7.res b/tests/sav/test_model_index_args7.res
new file mode 100644 (file)
index 0000000..5ee70f9
--- /dev/null
@@ -0,0 +1,12 @@
+# test_prog::C
+
+ * 1: Career (test_prog::Career)
+ * 2: Character (test_prog::Character)
+ * 3: Combatable (test_prog::Combatable)
+ * 4: age (test_prog::Character::age)
+ * 8: name (test_prog::Character::name)
+ * 7: quit (test_prog::Character::quit)
+ * 6: race (test_prog::Character::race)
+ * 5: sex (test_prog::Character::sex)
+ * 9: age= (test_prog::Character::age=)
+ * 10: sex= (test_prog::Character::sex=)
diff --git a/tests/sav/test_model_index_args8.res b/tests/sav/test_model_index_args8.res
new file mode 100644 (file)
index 0000000..fc9d8e2
--- /dev/null
@@ -0,0 +1,12 @@
+# A
+
+ * 1: * (test_prog::Int::*)
+ * 1: * (test_prog::Float::*)
+ * 1: + (test_prog::Int::+)
+ * 1: + (test_prog::Float::+)
+ * 1: - (test_prog::Int::-)
+ * 1: - (test_prog::Float::-)
+ * 1: / (test_prog::Float::/)
+ * 1: / (test_prog::Int::/)
+ * 1: > (test_prog::Float::>)
+ * 1: > (test_prog::Int::>)
diff --git a/tests/sav/test_model_index_args9.res b/tests/sav/test_model_index_args9.res
new file mode 100644 (file)
index 0000000..bb6e9e1
--- /dev/null
@@ -0,0 +1,12 @@
+# Foo
+
+ * 3: * (test_prog::Int::*)
+ * 3: / (test_prog::Int::/)
+ * 2: Bool (test_prog::Bool)
+ * 3: Elf (test_prog::Elf)
+ * 3: Float (test_prog::Float)
+ * 3: Int (test_prog::Int)
+ * 3: Sys (test_prog::Sys)
+ * 3: rpg (test_prog::rpg)
+ * 3: rpg (test_prog>rpg>)
+ * 3: sex (test_prog::Character::sex)
diff --git a/tests/test_model_index.args b/tests/test_model_index.args
new file mode 100644 (file)
index 0000000..ac86448
--- /dev/null
@@ -0,0 +1,21 @@
+test_prog Obj --name-prefix --no-color
+test_prog C --name-prefix --no-color
+test_prog e --name-prefix --no-color
+test_prog to --name-prefix --no-color
+test_prog C --full-name-prefix --no-color
+test_prog test_prog:: --full-name-prefix --no-color
+test_prog test_prog::C --full-name-prefix --no-color
+test_prog A --name-similarity --no-color
+test_prog Foo --name-similarity --no-color
+test_prog elfves --name-similarity --no-color
+test_prog Dwarves --full-name-similarity --no-color
+test_prog test_prof::Dwarves --full-name-similarity --no-color
+test_prog Obj --name --no-color
+test_prog C --name --no-color
+test_prog to --name --no-color
+test_prog Dwa --full-name --no-color
+test_prog test_prog::C --full-name --no-color
+test_prog C --no-color
+test_prog to --no-color
+test_prog Foo --no-color
+test_prog test_prog::C --no-color