@@ -91,56 +91,55 @@ static void test_regex() {
9191 }
9292}
9393
94- static void test_json_with_dumped_args_no_args () {
95- auto test = [](const std::string & input, bool is_partial, const std::vector<std::vector<std::string>> & args_paths, const std::string & expected) {
96- common_chat_msg_parser builder (input, is_partial, {});
97- auto js = builder.try_consume_json_with_dumped_args (args_paths);
98- assert_equals (true , js.has_value ());
99- assert_equals (is_partial, js->is_partial );
100- assert_equals (expected, args_paths.size () == 1 && args_paths[0 ].empty () ? js->value .get <std::string>() : js->value .dump ());
101- };
94+ const std::vector<std::string> empty_srcs = {
95+ " {" ,
96+ " {\" " ,
97+ " {\" n" ,
98+ " {\" name\" " ,
99+ " {\" name\" :" ,
100+ " {\" name\" :\" " ,
101+ " {\" name\" :\" python" ,
102+ };
102103
104+ static void test (const std::string & input, bool is_partial, const std::vector<std::vector<std::string>> & args_paths, const std::string & expected) {
105+ common_chat_msg_parser builder (input, is_partial, {});
106+ auto js = builder.try_consume_json_with_dumped_args (args_paths);
107+ assert_equals (true , js.has_value ());
108+ assert_equals (is_partial, js->is_partial );
109+ assert_equals (expected, args_paths.size () == 1 && args_paths[0 ].empty () ? js->value .get <std::string>() : js->value .dump ());
110+ }
111+ static void test_with_args (const std::string & input, const std::string & expected, bool parse_as_partial = true , bool is_partial = true ) {
112+ common_chat_msg_parser builder (input, parse_as_partial, {});
113+ auto js = builder.try_consume_json_with_dumped_args ({{" args" }});
114+ assert_equals (true , js.has_value ());
115+ assert_equals (is_partial, js->is_partial );
116+ assert_equals (expected, js->value .dump ());
117+ }
118+
119+ static void test_json_with_dumped_args_no_args () {
103120 // Normal JSON, nothing to heal, nothing to dump
104121 test (" {\" name\" : \" python\" }" , false , {}, " {\" name\" :\" python\" }" );
105122 // Full json is args
106123 test (" {\" name\" : \" python\" }" , false , {{}}, " {\" name\" :\" python\" }" );
107124
108- {
109- std::vector<std::string> empty_srcs = {
110- " {" ,
111- " {\" " ,
112- " {\" n" ,
113- " {\" name\" " ,
114- " {\" name\" :" ,
115- " {\" name\" :\" " ,
116- " {\" name\" :\" python" ,
117- };
118- // If the entire JSON is the arguments, healing it them dumping it produces the same output as the input (just reformatted).
119- test (" {\" name\" : \" python" , true , {{}}, " {\" name\" :\" python" );
120- for (const auto & src : empty_srcs) {
121- test (src, true , {{}}, src);
122- }
123- // If the arguments are further down, don't heal partial content.
124- for (const auto & src : empty_srcs) {
125- test (src, true , {{" arguments" }}, " {}" );
126- }
127- // But heal content that isn't partial.
128- test (" {\" name\" : \" python\" " , true , {{" arguments" }}, " {\" name\" :\" python\" }" );
125+ // If the arguments are further down, don't heal partial content.
126+ for (const auto & src : empty_srcs) {
127+ test (src, true , {{" arguments" }}, " {}" );
129128 }
129+ // But heal content that isn't partial.
130+ test (" {\" name\" : \" python\" " , true , {{" arguments" }}, " {\" name\" :\" python\" }" );
130131}
131132
132133static void test_json_with_dumped_args () {
133- auto test = [](const std::string & input, const std::string & expected, bool parse_as_partial = true , bool is_partial = true ) {
134- common_chat_msg_parser builder (input, parse_as_partial, {});
135- auto js = builder.try_consume_json_with_dumped_args ({{" args" }});
136- assert_equals (true , js.has_value ());
137- assert_equals (is_partial, js->is_partial );
138- assert_equals (expected, js->value .dump ());
139- };
134+ // If the entire JSON is the arguments, healing it them dumping it produces the same output as the input (just reformatted).
135+ test (" {\" name\" : \" python" , true , {{}}, " {\" name\" :\" python" );
136+ for (const auto & src : empty_srcs) {
137+ test (src, true , {{}}, src);
138+ }
140139
141140 // Full JSON w/ args
142141 for (auto parse_as_partial : {true , false }) {
143- test (
142+ test_with_args (
144143 R"( {"name": "python", "args": {"arg1": 1}})" ,
145144 R"( {"name":"python","args":"{\"arg1\":1}"})" ,
146145 parse_as_partial,
@@ -149,77 +148,77 @@ static void test_json_with_dumped_args() {
149148 }
150149
151150 // Partial JSON w/ partial args
152- test (
151+ test_with_args (
153152 R"( {"foo": "bar", "args": {")" ,
154153 R"( {"foo":"bar","args":"{\""})"
155154 );
156155 // Partial args broken in object key
157- test (
156+ test_with_args (
158157 R"( {"foo": "bar", "args": {"ar)" ,
159158 R"( {"foo":"bar","args":"{\"ar"})"
160159 );
161160 // Partial args broken after object key
162- test (
161+ test_with_args (
163162 R"( {"foo": "bar", "args": {"arg1")" ,
164163 R"( {"foo":"bar","args":"{\"arg1\""})"
165164 );
166165 // Partial args broken before object value
167- test (
166+ test_with_args (
168167 R"( {"foo": "bar", "args": {"arg1":)" ,
169168 R"( {"foo":"bar","args":"{\"arg1\":"})"
170169 );
171170 // Partial args broken before object value (space)
172- test (
171+ test_with_args (
173172 R"( {"foo": "bar", "args": {"arg1": )" ,
174173 R"( {"foo":"bar","args":"{\"arg1\":"})"
175174 );
176175 // Partial args broken in object value that may not be complete (int)
177- test (
176+ test_with_args (
178177 R"( {"foo": "bar", "args": {"arg1": 1)" ,
179178 R"( {"foo":"bar","args":"{\"arg1\":"})"
180179 );
181180 // Partial args broken in object value that is complete (int)
182- test (
181+ test_with_args (
183182 R"( {"foo": "bar", "args": {"arg1": 1 )" ,
184183 R"( {"foo":"bar","args":"{\"arg1\":1"})"
185184 );
186185 // Partial args broken in object value that is incomplete (string)
187- test (
186+ test_with_args (
188187 R"( {"foo": "bar", "args": {"arg1": ")" ,
189188 R"( {"foo":"bar","args":"{\"arg1\":\""})"
190189 );
191190 // Partial args broken in object value that is complete (string)
192- test (
191+ test_with_args (
193192 R"( {"foo": "bar", "args": {"arg1": "1")" ,
194193 R"( {"foo":"bar","args":"{\"arg1\":\"1\""})"
195194 );
196195 // Partial args broken on array opening
197- test (
196+ test_with_args (
198197 R"( {"foo": "bar", "args": [)" ,
199198 R"( {"foo":"bar","args":"["})"
200199 );
201200 // Partial args broken on array value that is incomplete (int)
202- test (
201+ test_with_args (
203202 R"( {"foo": "bar", "args": [1)" ,
204203 R"( {"foo":"bar","args":"["})"
205204 );
206205 // Partial args broken on array value that is complete (int)
207- test (
206+ test_with_args (
208207 R"( {"foo": "bar", "args": [1 )" ,
209208 R"( {"foo":"bar","args":"[1"})"
210209 );
211210 // Partial args broken on array value that is complete (string)
212- test (
211+ test_with_args (
213212 R"( {"foo": "bar", "args": ["1")" ,
214213 R"( {"foo":"bar","args":"[\"1\""})"
215214 );
216215 // Partial args broken after array value
217- test (
216+ test_with_args (
218217 R"( {"foo": "bar", "args": [1,)" ,
219218 R"( {"foo":"bar","args":"[1,"})"
220219 );
221220 // Partial args broken on nested array
222- test (
221+ test_with_args (
223222 R"( {"foo": "bar", "args": {"arg1": [)" ,
224223 R"( {"foo":"bar","args":"{\"arg1\":["})"
225224 );
0 commit comments