-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_function_test.cpp
More file actions
137 lines (119 loc) · 4.42 KB
/
text_function_test.cpp
File metadata and controls
137 lines (119 loc) · 4.42 KB
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
#include "text_function.h"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
using namespace txtfn;
TEST_CASE("text function void return") {
int i = 0;
auto f = create_text_function([&] (int b, std::string) { i = b; });
std::string res;
f->call(std::vector<std::string>{"3", "go"}, res);
REQUIRE(res.empty());
REQUIRE(i == 3);
REQUIRE("void" == f->return_type());
REQUIRE("int" == f->arg_types()[0]);
REQUIRE("std::string" == f->arg_types()[1]);
REQUIRE("void \x1b[32mname\x1b[0m [int] [std::string]" == f->help("name"));
}
TEST_CASE("text function non void return") {
auto f = create_text_function([&] (int b, const std::string& str) {
return std::to_string(b) + str;;
});
std::string res;
f->call(std::vector<std::string>{"3", "go"}, res);
REQUIRE(res == "3go");
REQUIRE("std::string" == f->return_type());
REQUIRE("int" == f->arg_types()[0]);
REQUIRE("std::string" == f->arg_types()[1]);
REQUIRE("std::string \x1b[32mname\x1b[0m [int] [std::string]" == f->help("name"));
}
TEST_CASE("text function wrong args") {
auto f = create_text_function([&] (int b, const std::string& str) {
return std::to_string(b) + str;;
});
std::string res;
try {
f->call(std::vector<std::string>{"go", "go"}, res);
FAIL("didnt throw");
} catch(const std::exception& e) {
REQUIRE("Unable to convert arg: 'go' to int" == std::string(e.what()));
}
try {
f->call(std::vector<std::string>{"1", "go", "3"}, res);
FAIL("didnt throw");
} catch(const std::exception& e) {
REQUIRE("Wrong number of args: 3 != 2" == std::string(e.what()));
}
}
TEST_CASE("text function library no dupes") {
TextFunctionLibrary lib;
auto fn = create_text_function([&] () { });
auto fn2 = create_text_function([&] () { });
lib.add(std::move(fn), TextFunctionHelp("fn"));
try {
lib.add(std::move(fn2), TextFunctionHelp("fn"));
FAIL("didnt throw");
} catch (const std::exception& e) {
REQUIRE("fn already registered." == std::string(e.what()));
}
}
TEST_CASE("text function library") {
TextFunctionLibrary lib;
auto multiply = create_text_function([&] (int a, int b) { return a * b; });
lib.add(std::move(multiply),
TextFunctionHelp("multiply")
.arg("multiplicand", "it is multiplied")
.arg("multiplier", "it multiplies")
.description("Multiplies two numbers"));
auto concat = create_text_function([&] (std::string a, std::string b) { return a + b; });
lib.add(std::move(concat),
TextFunctionHelp("concat")
.arg("str1", "a string")
.arg("str2", "another string")
.description("Concatenates two strings"));
SECTION("builtin help") {
std::string res;
lib.call("help", std::vector<std::string>{}, res);
const std::string expected =
"std::string \x1b[32mconcat\x1b[0m [std::string] [std::string] -- Concatenates two strings\n"
"std::string \x1b[32mhelp\x1b[0m -- Returns list of functions\n"
"std::string \x1b[32mhelp\x1b[0m [std::string] -- Returns detailed help for matching function\n"
"int \x1b[32mmultiply\x1b[0m [int] [int] -- Multiplies two numbers\n"
"std::string \x1b[32msearch\x1b[0m [std::string] -- Returns list of functions matching regex\n";
REQUIRE(expected == res);
lib.call("help", std::vector<std::string>{"func"}, res);
REQUIRE("func not found" == res);
}
SECTION("builtin detailed help") {
std::string res;
lib.call("help", std::vector<std::string>{"multiply"}, res);
const std::string expected =
"int \x1b[32mmultiply\x1b[0m [int] [int]\n"
" Description: Multiplies two numbers\n"
" Arguments:\n"
" multiplicand: it is multiplied\n"
" multiplier: it multiplies\n";
REQUIRE(expected == res);
}
SECTION("builtin search") {
std::string res;
lib.call("search", std::vector<std::string>{"two"}, res);
const std::string expected =
"std::string \x1b[32mconcat\x1b[0m [std::string] [std::string]\n"
" Description: Concatenates two strings\n"
" Arguments:\n"
" str1: a string\n"
" str2: another string\n"
"\n"
"int \x1b[32mmultiply\x1b[0m [int] [int]\n"
" Description: Multiplies two numbers\n"
" Arguments:\n"
" multiplicand: it is multiplied\n"
" multiplier: it multiplies\n"
"\n";
REQUIRE(expected == res);
}
SECTION("no function") {
std::string res;
REQUIRE_FALSE(lib.call("badfunc", std::vector<std::string>{}, res));
}
}