Line data Source code
1 : #include <fas/testing.hpp>
2 : #include <wfc/json.hpp>
3 : #include <matchmaker/matchmaker.hpp>
4 : #include <utility>
5 : #include <regex>
6 : #include <boost/regex.hpp>
7 :
8 : using namespace wfc::json::literals;
9 : using namespace wfc::jsonrpc;
10 : using json_error = wfc::json::json_error;
11 : using strerr = wfc::json::strerror;
12 :
13 : namespace {
14 :
15 : struct _matchmaker_;
16 1 : static const std::string configs[]=
17 : {
18 : "'hello'"_json, // 0
19 : "'hell'"_json, // 1
20 : "'hello'"_json, // 2
21 : "'hell'"_json, // 3
22 : "'.*hello.*'"_json, // 4
23 : "'.*hell.*'"_json, // 5
24 : "['hello', 'hell', 'hellx']"_json, // 6 По сути это проверка целого слова, т.к. нужно учитывать кавычки
25 : "{'foo':'bar'}"_json, // 7
26 : "{'foo':'bar', 'baz':{'.*':'.(\\\\w+)(\\\\.|_)?(\\\\w*)@(\\\\w+)(\\\\.(\\\\w+))+.'}}"_json, // 8
27 : "{'foo':'bar', 'baz':{'email':null}}"_json, // 9
28 : "{'foo':'bar', 'baz':{'a':['b','c','d']}}"_json, // 10
29 : "{'foo':'bar', 'baz':{'a':[['b','c','d']]}}"_json, // 11
30 : "{'foo':'bar', 'baz':{'a':[['b',[['c']],'d']]}}"_json, // 12
31 : "{'foo':'bar', 'baz':{'a':'.{13,17}'}}"_json, // 13
32 : "{'baz':{'email':'.(\\\\w+)(\\\\.|_)?(\\\\w*)@(\\\\w+)(\\\\.(\\\\w+))+.', 'email':'.{17,19}'}, 'foo':'bar'}"_json, // 14
33 : "{'baz':{'email':'.(\\\\w+)(\\\\.|_)?(\\\\w*)@(\\\\w+)(\\\\.(\\\\w+))+.', 'email':'.{17,19}', 'user':'migashko'}, 'foo':'bar'}"_json // 15
34 : //"{'baz':{'email':'.{0,190}'}, 'foo':'bar'}"_json // 14
35 1 : };
36 :
37 3 : UNIT(create, "")
38 : {
39 : using namespace fas::testing;
40 1 : GET_REF(_matchmaker_) = std::make_shared<matchmaker>();
41 1 : }
42 :
43 :
44 : template<bool Val, int mode, typename T>
45 56 : void match(T& t, int nconfig, const std::string& json, const std::string& fl)
46 : {
47 : using namespace fas::testing;
48 56 : json_error err;
49 56 : auto mm = GET_REF(_matchmaker_);
50 56 : t << message(configs[nconfig]) << " ws " << json;
51 56 : t << flush;
52 56 : bool res = mm->reconfigure( mode, configs[nconfig], err );
53 112 : t << is_false<assert>(err) << wfc::json::strerror::message_trace(err, json.begin(), json.end() )
54 56 : << FAS_FL << std::endl << " from:" << fl;
55 56 : t << is_true<assert>(res) << " reconfigure for "<< configs[nconfig] << FAS_FL;
56 56 : t << stop;
57 56 : bool match_result = mm->match( json, err );
58 112 : t << is_false<assert>(err) << wfc::json::strerror::message_trace(err, json.begin(), json.end() )
59 56 : << FAS_FL<< std::endl << " from:" << fl;
60 56 : t << stop;
61 56 : t << equal<expect, bool>(match_result, Val) << fl;
62 56 : }
63 :
64 112 : struct get_fl
65 : {
66 : std::stringstream ss;
67 : template<typename V>
68 224 : get_fl& operator << (const V& val) {ss << val; return *this; }
69 56 : operator std::string () const { return ss.str();}
70 : };
71 :
72 : #define FAS_FLS std::string(get_fl() << FAS_FL)
73 :
74 3 : UNIT(match0, "")
75 : {
76 : using namespace fas::testing;
77 1 : t << nothing ;
78 :
79 1 : match<true, match_mode::FullMatch>(t, 0, "'hello'"_json, FAS_FLS );
80 1 : match<false, match_mode::FullMatch>(t, 0, "'hell'"_json, FAS_FLS );
81 1 : match<false, match_mode::FullMatch>(t, 1, "'hello'"_json, FAS_FLS );
82 1 : match<true, match_mode::FullMatch>(t, 1, "'hell'"_json, FAS_FLS );
83 :
84 1 : match<true, match_mode::PrefixMatchValue>(t, 0, "'hello'"_json, FAS_FLS );
85 1 : match<false, match_mode::PrefixMatchValue>(t, 0, "'hell'"_json, FAS_FLS );
86 1 : match<true, match_mode::PrefixMatchValue>(t, 1, "'hello'"_json, FAS_FLS );
87 1 : match<true, match_mode::PrefixMatchValue>(t, 1, "'hell'"_json, FAS_FLS );
88 :
89 1 : match<true, match_mode::RegexMatchValue>(t, 0, "'hello'"_json, FAS_FLS );
90 1 : match<false, match_mode::RegexMatchValue>(t, 0, "'hell'"_json, FAS_FLS );
91 1 : match<false, match_mode::RegexMatchValue>(t, 1, "'hello'"_json, FAS_FLS );
92 1 : match<true, match_mode::RegexMatchValue>(t, 1, "'hell'"_json, FAS_FLS );
93 :
94 : /*
95 : match<true, match_mode::RegexMatchValue>(t, 2, "'hello'"_json, FAS_FLS );
96 : match<false, match_mode::RegexMatchValue>(t, 2, "'hell'"_json, FAS_FLS );
97 : match<false, match_mode::RegexMatchValue>(t, 3, "'hello'"_json, FAS_FLS );
98 : match<true, match_mode::RegexMatchValue>(t, 3, "'hell'"_json, FAS_FLS );
99 : */
100 :
101 1 : match<true, match_mode::RegexMatchValue>(t, 4, "'hello'"_json, FAS_FLS );
102 1 : match<false, match_mode::RegexMatchValue>(t, 4, "'hell'"_json, FAS_FLS );
103 1 : match<true, match_mode::RegexMatchValue>(t, 5, "'hello'"_json, FAS_FLS );
104 1 : match<true, match_mode::RegexMatchValue>(t, 5, "'hell'"_json, FAS_FLS );
105 1 : match<true, match_mode::RegexMatchValue>(t, 5, "'hellx'"_json, FAS_FLS );
106 :
107 1 : match<true, match_mode::RegexMatchValue>(t, 6, "'hello'"_json, FAS_FLS );
108 1 : match<true, match_mode::RegexMatchValue>(t, 6, "'hell'"_json, FAS_FLS );
109 1 : match<true, match_mode::RegexMatchValue>(t, 6, "'hellx'"_json, FAS_FLS );
110 :
111 1 : match<true, match_mode::FullMatch>(t, 7, "{'foo':'bar'}"_json, FAS_FLS );
112 1 : match<true, match_mode::FullMatch>(t, 7, "{'foo1':'bar', 'foo':'bar', 'foo2':'bar1'}"_json, FAS_FLS );
113 1 : match<false, match_mode::FullMatch>(t, 7, "{'foo1':'bar', 'foo':'bar1'}"_json, FAS_FLS );
114 :
115 1 : match<true, match_mode::PrefixMatch>(t, 7, "{'foo':'bar'}"_json, FAS_FLS );
116 1 : match<true, match_mode::PrefixMatch>(t, 7, "{'foo1':'bar', 'foo':'bar', 'foo2':'bar1'}"_json, FAS_FLS );
117 1 : match<false, match_mode::PrefixMatch>(t, 7, "{'fo1':'bar', 'foo':'ba1'}"_json, FAS_FLS );
118 :
119 1 : match<true, match_mode::RegexMatch>(t, 8, "{'foo':'bar', 'baz':{'email':'migashko@gmail.com'}}"_json, FAS_FLS );
120 :
121 1 : match<false, match_mode::RegexMatch>(t, 9, "{'foo':'bar', 'baz':{'email1':null}}"_json, FAS_FLS );
122 1 : match<true, match_mode::RegexMatch>(t, 9, "{'foo':'bar', 'baz':{'email':null}}"_json, FAS_FLS );
123 1 : match<true, match_mode::RegexMatch>(t, 9, "{'foo':'bar', 'baz':{'email':'anystring'}}"_json, FAS_FLS );
124 :
125 1 : match<true, match_mode::FullMatch>(t, 10, "{'foo':'bar', 'baz':{'a':'b'}}"_json, FAS_FLS );
126 1 : match<true, match_mode::FullMatch>(t, 10, "{'foo':'bar', 'baz':{'a':'c'}}"_json, FAS_FLS );
127 1 : match<true, match_mode::FullMatch>(t, 10, "{'foo':'bar', 'baz':{'a':'d'}}"_json, FAS_FLS );
128 1 : match<false, match_mode::FullMatch>(t, 10, "{'foo':'bar', 'baz':{'a':'e'}}"_json, FAS_FLS );
129 :
130 1 : match<true, match_mode::FullMatch>(t, 11, "{'foo':'bar', 'baz':{'a':['b','d','c']}}"_json, FAS_FLS );
131 1 : match<true, match_mode::FullMatch>(t, 11, "{'foo':'bar', 'baz':{'a':['b','d','c', 1]}}"_json, FAS_FLS );
132 1 : match<false, match_mode::FullMatch>(t, 11, "{'foo':'bar', 'baz':{'a':['b','d']}}"_json, FAS_FLS );
133 :
134 1 : match<true, match_mode::FullMatch>(t, 12, "{'foo':'bar', 'baz':{'a':['b','d',['c']]}}"_json, FAS_FLS );
135 1 : match<true, match_mode::FullMatch>(t, 12, "{'foo':'bar', 'baz':{'a':[['c', 'e'],'b','d']}}"_json, FAS_FLS );
136 1 : match<false, match_mode::FullMatch>(t, 12, "{'foo':'bar', 'baz':{'a':[['c', 'e'],'b',]}}"_json, FAS_FLS );
137 1 : match<false, match_mode::FullMatch>(t, 12, "{'foo':'bar', 'baz':{'a':[['e'],'b','d']}}"_json, FAS_FLS );
138 :
139 1 : constexpr int mode1 = match_mode::RegexMatchValue | match_mode::FullMatchName;
140 1 : match<true, mode1>(t, 13, "{'foo':'bar', 'baz':{'a':['b','d','c']}}"_json, FAS_FLS );
141 1 : match<true, mode1>(t, 13, "{'foo':'bar', 'baz':{'a':['b','d','c','d']}}"_json, FAS_FLS );
142 1 : match<false, mode1>(t, 13, "{'foo':'bar', 'baz':{'a':['b','d']}}"_json, FAS_FLS );
143 1 : match<false, mode1>(t, 13, "{'foo':'bar', 'baz':{'a':['b','d','c','d','e']}}"_json, FAS_FLS );
144 :
145 1 : match<true, mode1>(t, 14, "{'foo':'bar','baz':{'email':'migashko@gmail.com'}}"_json, FAS_FLS );
146 1 : match<false,mode1>(t, 14, "{'foo1':'bar', 'baz':{'email':'migashko@gmail.com'}}"_json, FAS_FLS );
147 1 : match<false,mode1>(t, 14, "{'foo':'bar1', 'baz':{'email':'migashko@gmail.com'}}"_json, FAS_FLS );
148 1 : match<false,mode1>(t, 14, "{'foo':'bar', 'baz':{'email':'mig@gmail.com'}}"_json, FAS_FLS );
149 1 : match<false,mode1>(t, 14, "{'foo':'bar', 'baz':{'email':'migashko1111@gmail.com'}}"_json, FAS_FLS );
150 1 : match<false, mode1>(t, 14, "{'foo':'bar', 'baz':{'emailX':'migashko@gmail.com'}}"_json, FAS_FLS );
151 :
152 1 : match<false, mode1>(t, 15, "{'foo':'bar', 'baz':{'emailX':'migashko@gmail.com', 'user':'migashko'}}"_json, FAS_FLS );
153 1 : match<true, mode1>(t, 15, "{'foo':'bar', 'baz':{'email':'migashko@gmail.com', 'user':'migashko'}}"_json, FAS_FLS );
154 1 : match<false, mode1>(t, 15, "{'foo':'bar', 'baz':{'email':'migashko@gmail.com', 'user':'migashko1'}}"_json, FAS_FLS );
155 1 : match<true, mode1>(t, 15, "{'foo':'bar', 'baz':{'email':'migashko1@gmail.com', 'user':'migashko'}}"_json, FAS_FLS );
156 1 : match<false, mode1>(t, 15, "{'foo':'bar', 'baz':{'email':'migashko1111@gmail.com', 'user':'migashko'}}"_json, FAS_FLS );
157 1 : }
158 :
159 : } // namespace
160 :
161 1 : BEGIN_SUITE(matchmaker_suite, "")
162 : ADD_VALUE(_matchmaker_, std::shared_ptr<matchmaker> )
163 : ADD_UNIT( create )
164 : ADD_UNIT( match0 )
165 : ADD_UNIT( match0 )
166 7 : END_SUITE(matchmaker_suite)
|