1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
** Copyright (C) 2020 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public License
** as published by the Free Software Foundation; either version 2.1
** of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free
** Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*/
#include <vector>
#include <glib.h>
#include <iostream>
#include <sstream>
#include "mu-command-parser.hh"
#include "mu-utils.hh"
using namespace Mu;
static void
test_param_getters()
{
const auto sexp{Sexp::make_parse(R"((foo :bar 123 :cuux "456" :boo nil :bah true))")};
if (g_test_verbose())
std::cout << sexp << "\n";
g_assert_cmpint(Command::get_int_or(sexp.list(), ":bar"), ==, 123);
assert_equal(Command::get_string_or(sexp.list(), ":bra", "bla"), "bla");
assert_equal(Command::get_string_or(sexp.list(), ":cuux"), "456");
g_assert_true(Command::get_bool_or(sexp.list(), ":boo") == false);
g_assert_true(Command::get_bool_or(sexp.list(), ":bah") == true);
}
static bool
call(const Command::CommandMap& cmap, const std::string& str)
try {
const auto sexp{Sexp::make_parse(str)};
invoke(cmap, sexp);
return true;
} catch (const Error& err) {
g_warning("%s", err.what());
return false;
}
static void
test_command()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
{":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
"My command,",
{}});
g_assert_true(call(cmap, "(my-command :param1 \"hello\")"));
g_assert_true(call(cmap, "(my-command :param1 \"hello\" :param2 123)"));
g_assert_false(call(cmap, "(my-command :param1 \"hello\" :param2 123 :param3 xxx)"));
}
static void
test_command2()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace("bla",
CommandInfo{ArgMap{
{":foo", ArgInfo{Sexp::Type::Number, false, "foo"}},
{":bar", ArgInfo{Sexp::Type::String, false, "bar"}},
},
"yeah",
[&](const auto& params) {}});
g_assert_true(call(cmap, "(bla :foo nil)"));
g_assert_false(call(cmap, "(bla :foo nil :bla nil)"));
}
static void
test_command_fail()
{
using namespace Command;
allow_warnings();
CommandMap cmap;
cmap.emplace(
"my-command",
CommandInfo{ArgMap{{":param1", ArgInfo{Sexp::Type::String, true, "some string"}},
{":param2", ArgInfo{Sexp::Type::Number, false, "some integer"}}},
"My command,",
{}});
g_assert_false(call(cmap, "(my-command)"));
g_assert_false(call(cmap, "(my-command2)"));
g_assert_false(call(cmap, "(my-command :param1 123 :param2 123)"));
g_assert_false(call(cmap, "(my-command :param1 \"hello\" :param2 \"123\")"));
}
static void
black_hole()
{
}
int
main(int argc, char* argv[])
try {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/utils/command-parser/param-getters", test_param_getters);
g_test_add_func("/utils/command-parser/command", test_command);
g_test_add_func("/utils/command-parser/command2", test_command2);
g_test_add_func("/utils/command-parser/command-fail", test_command_fail);
g_log_set_handler(
NULL,
(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
(GLogFunc)black_hole,
NULL);
return g_test_run();
} catch (const std::runtime_error& re) {
std::cerr << re.what() << "\n";
return 1;
}
|