src/test_model_index: expect `-q` option for query
authorAlexandre Terrasa <alexandre@moz-code.org>
Thu, 14 Jun 2018 16:20:17 +0000 (12:20 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Wed, 4 Jul 2018 23:50:29 +0000 (19:50 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/test_model_index.nit
tests/test_model_index.args

index e19b442..34b4dd2 100644 (file)
@@ -17,6 +17,7 @@ import model_index
 import console
 
 redef class ToolContext
+       var opt_query = new OptionString("String to search", "-q", "--query")
        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")
@@ -25,6 +26,7 @@ redef class ToolContext
        var opt_full_name = new OptionBool("", "--full-name")
 
        redef init do
+               option_context.add_option(opt_query)
                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)
@@ -48,15 +50,16 @@ 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>"
+if args.is_empty then
+       print "usage: test_model_index nitfiles..."
        exit 1
+       return
 end
 
 # build model
 var model = new Model
 var mbuilder = new ModelBuilder(model, toolcontext)
-var mmodules = mbuilder.parse_full([args.first])
+var mmodules = mbuilder.parse_full(args)
 
 # process
 if mmodules.is_empty then return
@@ -74,25 +77,31 @@ for mentity in model.collect_mentities(filters) do
        index.index(mentity)
 end
 
-var q = args[1]
+var query = toolcontext.opt_query.value
+if query == null then
+       # TODO add interactive mode
+       print "usage: test_model_index nitfiles... -q query"
+       exit 1
+       return
+end
 
-print "# {q}\n"
+print "# {query}\n"
 
 var res
 if toolcontext.opt_name_prefix.value then
-       res = index.find_by_name_prefix(q)
+       res = index.find_by_name_prefix(query)
 else if toolcontext.opt_full_name_prefix.value then
-       res = index.find_by_full_name_prefix(q)
+       res = index.find_by_full_name_prefix(query)
 else if toolcontext.opt_name_similarity.value then
-       res = index.find_by_name_similarity(q)
+       res = index.find_by_name_similarity(query)
 else if toolcontext.opt_full_name_similarity.value then
-       res = index.find_by_full_name_similarity(q)
+       res = index.find_by_full_name_similarity(query)
 else if toolcontext.opt_name.value then
-       res = index.find_by_name(q)
+       res = index.find_by_name(query)
 else if toolcontext.opt_full_name.value then
-       res = index.find_by_full_name(q)
+       res = index.find_by_full_name(query)
 else
-       res = index.find(q)
+       res = index.find(query)
 end
 
 res = res.sort(new ScoreComparator, new MEntityComparator).
index ac86448..62f7bde 100644 (file)
@@ -1,21 +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
+test_prog -q Obj --name-prefix --no-color
+test_prog -q C --name-prefix --no-color
+test_prog -q e --name-prefix --no-color
+test_prog -q to --name-prefix --no-color
+test_prog -q C --full-name-prefix --no-color
+test_prog -q test_prog:: --full-name-prefix --no-color
+test_prog -q test_prog::C --full-name-prefix --no-color
+test_prog -q A --name-similarity --no-color
+test_prog -q Foo --name-similarity --no-color
+test_prog -q elfves --name-similarity --no-color
+test_prog -q Dwarves --full-name-similarity --no-color
+test_prog -q test_prof::Dwarves --full-name-similarity --no-color
+test_prog -q Obj --name --no-color
+test_prog -q C --name --no-color
+test_prog -q to --name --no-color
+test_prog -q Dwa --full-name --no-color
+test_prog -q test_prog::C --full-name --no-color
+test_prog -q C --no-color
+test_prog -q to --no-color
+test_prog -q Foo --no-color
+test_prog -q test_prog::C --no-color