summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLauro Moura <lauromoura@expertisesolutions.com.br>2016-06-03 14:51:59 -0300
committerLauro Moura <lauromoura@expertisesolutions.com.br>2016-06-03 20:29:13 -0300
commit1ed4c3c58c199cfe8e5cd7f5294ae833a207855d (patch)
tree97ee674380ced4e3b2af2d9e03443822d8ff6104
parentb4637e2327847a1ce833952a65a99bcea4e5cad3 (diff)
ejson: Add tests for the json model.devs/felipealmeida/ejson-model
-rw-r--r--src/Makefile_Ejson.am29
-rw-r--r--src/tests/ejson/ejson_suite.c118
-rw-r--r--src/tests/ejson/ejson_suite.h16
-rw-r--r--src/tests/ejson/ejson_test_ejson_model.c756
-rw-r--r--src/tests/ejson/ejson_test_ejson_model.h6
5 files changed, 925 insertions, 0 deletions
diff --git a/src/Makefile_Ejson.am b/src/Makefile_Ejson.am
index 852bb2a628..7f0b339fcb 100644
--- a/src/Makefile_Ejson.am
+++ b/src/Makefile_Ejson.am
@@ -33,3 +33,32 @@ lib_ejson_libejson_la_CPPFLAGS = -I$(top_builddir)/src/lib/efl @EJSON_CFLAGS@ @E
33lib_ejson_libejson_la_LIBADD = @EJSON_LIBS@ @EFL_LIBS@ 33lib_ejson_libejson_la_LIBADD = @EJSON_LIBS@ @EFL_LIBS@
34lib_ejson_libejson_la_DEPENDENCIES = @EJSON_INTERNAL_LIBS@ @EFL_INTERNAL_LIBS@ 34lib_ejson_libejson_la_DEPENDENCIES = @EJSON_INTERNAL_LIBS@ @EFL_INTERNAL_LIBS@
35lib_ejson_libejson_la_LDFLAGS = @EFL_LTLIBRARY_FLAGS@ 35lib_ejson_libejson_la_LDFLAGS = @EFL_LTLIBRARY_FLAGS@
36
37if EFL_ENABLE_TESTS
38
39check_PROGRAMS += tests/ejson/ejson_suite
40TESTS += tests/ejson/ejson_suite
41
42tests_ejson_ejson_suite_SOURCES = \
43tests/ejson/ejson_suite.c \
44tests/ejson/ejson_test_ejson_model.c \
45tests/ejson/ejson_suite.h \
46tests/ejson/ejson_test_ejson_model.h
47
48tests_ejson_ejson_suite_CPPFLAGS = -I$(top_builddir)/src/lib/efl \
49-DTESTS_SRC_DIR=\"$(top_srcdir)/src/tests/ejson\" \
50-DTESTS_BUILD_DIR=\"$(top_builddir)/src/tests/ejson\" \
51@CHECK_CFLAGS@ \
52@ECORE_FILE_CFLAGS@ \
53@EJSON_CFLAGS@ @EFL_CFLAGS@
54
55tests_ejson_ejson_suite_LDADD = \
56@CHECK_LIBS@ \
57@USE_EJSON_LIBS@ \
58@USE_ECORE_FILE_LIBS@ \
59@USE_EFL_LIBS@
60
61tests_ejson_ejson_suite_DEPENDENCIES = \
62@USE_EJSON_INTERNAL_LIBS@
63
64endif
diff --git a/src/tests/ejson/ejson_suite.c b/src/tests/ejson/ejson_suite.c
new file mode 100644
index 0000000000..481d415c1a
--- /dev/null
+++ b/src/tests/ejson/ejson_suite.c
@@ -0,0 +1,118 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ejson_suite.h"
6
7#include <Eina.h>
8
9#include <stdbool.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13int _test_ejson_model_log_dom = -1;
14
15typedef struct _Ejson_Test_Case Ejson_Test_Case;
16
17struct _Ejson_Test_Case
18{
19 const char *test_case;
20 void (*build)(TCase *tc);
21};
22
23static const Ejson_Test_Case etc[] = {
24 { "Ejson_Model", ejson_test_ejson_model },
25 { NULL, NULL }
26};
27
28static void
29_list_tests(void)
30{
31 const Ejson_Test_Case *it = etc;
32 fputs("Available Test Cases:\n", stderr);
33 for (; it->test_case; it++)
34 fprintf(stderr, "\t%s\n", it->test_case);
35}
36
37static bool
38_use_test(int argc, const char **argv, const char *test_case)
39{
40 if (argc < 1)
41 return true;
42
43 for (; argc > 0; argc--, argv++)
44 if (strcmp(test_case, *argv) == 0)
45 return true;
46 return false;
47}
48
49static Suite *
50_ejson_suite_build(int argc, const char **argv)
51{
52 Suite *s = suite_create("Ejson");
53
54 for (int i = 0; etc[i].test_case; ++i)
55 {
56 if (!_use_test(argc, argv, etc[i].test_case)) continue;
57 TCase *tc = tcase_create(etc[i].test_case);
58
59 etc[i].build(tc);
60
61 suite_add_tcase(s, tc);
62 }
63
64 return s;
65}
66
67static void
68_init_logging(void)
69{
70 _test_ejson_model_log_dom = eina_log_domain_register("test_ejson_model", EINA_COLOR_LIGHTBLUE);
71 if (_test_ejson_model_log_dom < 0)
72 ck_abort_msg("Could not register log domain: test_ejson_model");
73
74 eina_log_domain_level_set("test_ejson_model", EINA_LOG_LEVEL_DBG);
75}
76
77static void
78_shutdown_logging(void)
79{
80 eina_log_domain_unregister(_test_ejson_model_log_dom);
81 _test_ejson_model_log_dom = -1;
82}
83
84int
85main(int argc, char **argv)
86{
87 for (int i = 1; i < argc; ++i)
88 {
89 if ((strcmp(argv[i], "-h") == 0) ||
90 (strcmp(argv[i], "--help") == 0))
91 {
92 fprintf(stderr, "Usage:\n\t%s [test_case1 .. [test_caseN]]\n", argv[0]);
93 _list_tests();
94 return 0;
95 }
96 else if ((strcmp(argv[i], "-l") == 0) ||
97 (strcmp(argv[i], "--list") == 0))
98 {
99 _list_tests();
100 return 0;
101 }
102 }
103
104 _init_logging();
105
106 Suite *s = _ejson_suite_build(argc - 1, (const char **)argv + 1);
107 SRunner *sr = srunner_create(s);
108
109 srunner_set_xml(sr, TESTS_BUILD_DIR "/check-results.xml");
110
111 srunner_run_all(sr, CK_ENV);
112 int failed_count = srunner_ntests_failed(sr);
113 srunner_free(sr);
114
115 _shutdown_logging();
116
117 return (failed_count == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
118}
diff --git a/src/tests/ejson/ejson_suite.h b/src/tests/ejson/ejson_suite.h
new file mode 100644
index 0000000000..ac1dbcdf47
--- /dev/null
+++ b/src/tests/ejson/ejson_suite.h
@@ -0,0 +1,16 @@
1#ifndef _EJSON_SUITE_H
2#define _EJSON_SUITE_H
3
4#include <check.h>
5
6extern int _test_ejson_model_log_dom;
7
8#define CRI(...) EINA_LOG_DOM_CRIT(_test_ejson_model_log_dom, __VA_ARGS__)
9#define ERR(...) EINA_LOG_DOM_ERR(_test_ejson_model_log_dom, __VA_ARGS__)
10#define WRN(...) EINA_LOG_DOM_WARN(_test_ejson_model_log_dom, __VA_ARGS__)
11#define INF(...) EINA_LOG_DOM_INFO(_test_ejson_model_log_dom, __VA_ARGS__)
12#define DBG(...) EINA_LOG_DOM_DBG(_test_ejson_model_log_dom, __VA_ARGS__)
13
14void ejson_test_ejson_model(TCase *tc);
15
16#endif
diff --git a/src/tests/ejson/ejson_test_ejson_model.c b/src/tests/ejson/ejson_test_ejson_model.c
new file mode 100644
index 0000000000..b1859d8375
--- /dev/null
+++ b/src/tests/ejson/ejson_test_ejson_model.c
@@ -0,0 +1,756 @@
1#ifdef HAVE_CONFIG_H
2# include <config.h>
3#endif
4
5#include "ejson_test_ejson_model.h"
6#include "ejson_suite.h"
7
8#include <Ecore.h>
9#include <Eina.h>
10#include <Ejson.h>
11
12#include <stdbool.h>
13
14static void
15_setup(void)
16{
17 int ret = ejson_init();
18 ck_assert_int_ge(ret, 1);
19}
20
21static void
22_teardown(void)
23{
24 int ret = ejson_shutdown();
25 ck_assert_int_eq(ret, 0);
26}
27
28
29void _name_set_ok(void *data, void *value)
30{
31 const char *given_name = (const char *)data;
32 Eina_Value *stored_value = (Eina_Value *)value;
33 const char *stored_name = NULL;
34 eina_value_get(stored_value, &stored_name);
35
36 ck_assert(given_name);
37 ck_assert(stored_name);
38 ck_assert_str_eq(given_name, stored_name);
39 ecore_main_loop_quit();
40}
41
42void _prop_set_error(void *data, Eina_Error error EINA_UNUSED)
43{
44 ERR("Error!");
45 const char *v = data;
46 ck_abort_msg("Failed to set value %s on some property", v);
47 ecore_main_loop_quit();
48}
49
50static void
51_set_name_property(Eo *model, const char *v)
52{
53 Eina_Value name;
54 eina_value_setup(&name, EINA_VALUE_TYPE_STRING);
55 eina_value_set(&name, v);
56 Eina_Promise *promise;
57 efl_model_property_set(model, EJSON_MODEL_NAME_PROPERTY, &name, &promise);
58 eina_promise_then(promise, _name_set_ok, _prop_set_error, (void*) v);
59 ecore_main_loop_begin();
60 eina_value_flush(&name);
61}
62
63static void
64_int_value_set_ok(void *data, void *value)
65{
66 int *given_value = (int*) data;
67 Eina_Value *stored_generic_value = (Eina_Value *)value;
68 int stored_value = -1;
69 eina_value_get(stored_generic_value, &stored_value);
70 ck_assert_int_eq(*given_value, stored_value);
71 ecore_main_loop_quit();
72}
73
74static void
75_set_int_value_property(Eo *model, int *v)
76{
77 Eina_Value value;
78 Eina_Promise *promise;
79
80 eina_value_setup(&value, EINA_VALUE_TYPE_INT);
81 eina_value_set(&value, *v);
82
83 efl_model_property_set(model, EJSON_MODEL_VALUE_PROPERTY, &value, &promise);
84 eina_promise_then(promise, _int_value_set_ok, _prop_set_error, (void *) v);
85 ecore_main_loop_begin();
86 eina_value_flush(&value);
87}
88
89static void
90_check_value_type_cannot_have_children(Eo *model)
91{
92 Eo *child = efl_model_child_add(model);
93 ck_assert_ptr_eq(NULL, child);
94}
95
96static void
97_check_int_value_property_set(Eo *model, int64_t v)
98{
99 Eina_Value value;
100 Eina_Value *ret_value = NULL;
101 Eina_Promise *promise = NULL;
102 int64_t x = 0;
103
104 eina_value_setup(&value, EINA_VALUE_TYPE_INT64);
105 eina_value_set(&value, v);
106
107 efl_model_property_set(model, EJSON_MODEL_VALUE_PROPERTY, &value, &promise);
108 ck_assert_ptr_ne(NULL, promise);
109
110 ck_assert_int_eq(eina_promise_error_get(promise), 0);
111
112 ret_value = eina_promise_value_get(promise);
113 eina_value_get(ret_value, &x);
114
115 ck_assert_int_eq(x, v);
116
117 eina_promise_unref(promise);
118 eina_value_flush(&value);
119}
120
121static void
122_check_bool_value_property_set(Eo *model, bool v)
123{
124 Eina_Value value;
125 Eina_Value *ret_value = NULL;
126 Eina_Promise *promise;
127 int x = 0;
128
129 eina_value_setup(&value, EINA_VALUE_TYPE_INT);
130 eina_value_set(&value, (int)v);
131
132 efl_model_property_set(model, EJSON_MODEL_VALUE_PROPERTY, &value, &promise);
133
134 ck_assert_int_eq(eina_promise_error_get(promise), 0);
135
136 ret_value = eina_promise_value_get(promise);
137 eina_value_get(ret_value, &x);
138
139 ck_assert_int_eq(x, v);
140
141 eina_promise_unref(promise);
142 eina_value_flush(&value);
143}
144
145static void
146_check_string_value_property_set(Eo *model, const char *v)
147{
148 Eina_Value value;
149 Eina_Value *ret_value = NULL;
150 Eina_Promise *promise;
151 const char *x = NULL;
152
153 eina_value_setup(&value, EINA_VALUE_TYPE_STRING);
154 eina_value_set(&value, v);
155
156 efl_model_property_set(model, EJSON_MODEL_VALUE_PROPERTY, &value, &promise);
157
158 ck_assert_int_eq(eina_promise_error_get(promise), 0);
159
160 ret_value = eina_promise_value_get(promise);
161 eina_value_get(ret_value, &x);
162
163 ck_assert_str_eq(x, v);
164
165 eina_promise_unref(promise);
166 eina_value_flush(&value);
167}
168
169static void
170_check_efl_model_children_count_eq(Eo *model, unsigned int expected_children_count)
171{
172 Eina_Promise *promise = NULL;
173 unsigned int *actual_children_count = NULL;
174 efl_model_children_count_get(model, &promise);
175
176 ck_assert_int_eq(eina_promise_error_get(promise), 0);
177
178 actual_children_count = eina_promise_value_get(promise);
179
180 ck_assert_int_eq(expected_children_count, *actual_children_count);
181
182 eina_promise_unref(promise);
183}
184
185static void
186_check_efl_model_property_int_eq(Eo *emodel, const char *property, int64_t expected_value)
187{
188 const Eina_Value *value;
189 Eina_Promise *promise;
190
191 efl_model_property_get(emodel, property, &promise);
192 ck_assert_ptr_ne(NULL, promise);
193
194 ck_assert_int_eq(eina_promise_error_get(promise), 0);
195
196 value = eina_promise_value_get(promise);
197
198 ck_assert_ptr_ne(NULL, value);
199
200 const Eina_Value_Type *property_type = eina_value_type_get(value);
201 ck_assert_ptr_eq(EINA_VALUE_TYPE_INT64, property_type);
202
203 int64_t actual_value = 0;
204 eina_value_get(value, &actual_value);
205 ck_assert_int_eq(expected_value, actual_value);
206
207 eina_promise_unref(promise);
208}
209
210static void
211_check_efl_model_property_str_eq(Eo *emodel, const char *property, const char *expected_value)
212{
213 const Eina_Value *value;
214 Eina_Promise *promise;
215
216 efl_model_property_get(emodel, property, &promise);
217 ck_assert_ptr_ne(NULL, promise);
218
219 ck_assert_int_eq(eina_promise_error_get(promise), 0);
220
221 value = eina_promise_value_get(promise);
222
223 ck_assert_ptr_ne(NULL, value);
224
225 const Eina_Value_Type *property_type = eina_value_type_get(value);
226 ck_assert_ptr_eq(EINA_VALUE_TYPE_STRING, property_type);
227
228 const char *actual_value = NULL;
229 eina_value_get(value, &actual_value);
230 ck_assert_str_eq(expected_value, actual_value);
231
232 eina_promise_unref(promise);
233}
234
235static Eo *
236_emodel_nth_child_get(Eo *emodel, unsigned int n)
237{
238 Eina_Promise *promise = NULL;
239 Eina_Accessor *accessor;
240 Eo *child = NULL;
241
242 efl_model_children_slice_get(emodel, n, 1, &promise);
243
244 ck_assert_ptr_ne(NULL, promise);
245 ck_assert_int_eq(eina_promise_error_get(promise), 0);
246
247 accessor = eina_promise_value_get(promise);
248
249 ck_assert_ptr_ne(NULL, accessor);
250
251 Eina_Bool ret = eina_accessor_data_get(accessor, 0, (void**)&child);
252 ck_assert(ret);
253 ck_assert_ptr_ne(NULL, child);
254
255 eina_promise_unref(promise);
256
257 return child;
258}
259
260static void
261_check_ejson_type(Eo *model, Ejson_Model_Type expected_type)
262{
263 Ejson_Model_Type actual_type = ejson_model_type_get(model);
264 ck_assert_int_eq(expected_type, actual_type);
265}
266
267
268static void
269_check_efl_model_properties(Eo *model, const char *expected_properties[])
270{
271 const Eina_Array *properties = NULL;
272 properties = efl_model_properties_get(model);
273 ck_assert_ptr_ne(NULL, properties);
274
275 unsigned int actual_properties_count = eina_array_count(properties);
276
277 // This assumes a fixed order for the properties.
278 unsigned int expected_properties_count = 0;
279 const char *expected_property = NULL;
280 while ((expected_property = *expected_properties++))
281 {
282 const char *actual_property = eina_array_data_get(properties,
283 expected_properties_count);
284 ck_assert_str_eq(expected_property, actual_property);
285 ++expected_properties_count;
286 ck_assert_int_le(expected_properties_count, actual_properties_count);
287 }
288
289 ck_assert_int_eq(expected_properties_count, actual_properties_count);
290}
291
292START_TEST(smoke)
293{
294 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL);
295 ck_assert_ptr_ne(NULL, json);
296 eo_unref(json);
297}
298END_TEST
299
300START_TEST(object_constructor)
301{
302 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
303 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_OBJECT));
304 ck_assert_ptr_ne(NULL, json);
305 ck_assert_int_eq(EJSON_MODEL_TYPE_OBJECT, ejson_model_type_get(json));
306
307 eo_unref(json);
308}
309END_TEST
310
311START_TEST(object_get_null_value)
312{
313 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL);
314 ck_assert_ptr_ne(NULL, json);
315
316 Eina_Promise *promise = NULL;
317 efl_model_property_get(json, EJSON_MODEL_VALUE_PROPERTY, &promise);
318
319 // Check below based on returning a null string eina value if not value was
320 // set.
321 Eina_Value *value = eina_promise_value_get(promise);
322 ck_assert_ptr_eq(EINA_VALUE_TYPE_OPTIONAL, eina_value_type_get(value));
323 Eina_Bool empty = EINA_FALSE;
324 ck_assert(eina_value_optional_empty_is(value, &empty));
325 ck_assert(empty);
326
327 eina_promise_unref(promise);
328 eo_unref(json);
329}
330END_TEST
331
332START_TEST(object_simple_get_set)
333{
334 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
335 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_OBJECT));
336 ck_assert_ptr_ne(NULL, json);
337
338 // Add child to an existing object
339 Eo *child = efl_model_child_add(json);
340 ck_assert_ptr_ne(NULL, child);
341
342 // If you don't specify the type, it must be null
343 Ejson_Model_Type type = ejson_model_type_get(child);
344 ck_assert_int_eq(EJSON_MODEL_TYPE_NULL, type);
345
346 // Changing the model type
347 ejson_model_type_set(child, EJSON_MODEL_TYPE_INT);
348 type = ejson_model_type_get(child);
349 ck_assert_int_eq(EJSON_MODEL_TYPE_INT, type);
350
351 // Setting and getting a name
352 const char *name_to_be_set = "a";
353 _set_name_property(child, name_to_be_set);
354
355 Eina_Promise *promise = NULL;
356 efl_model_property_get(child, EJSON_MODEL_NAME_PROPERTY, &promise);
357
358 Eina_Value *property = eina_promise_value_get(promise);
359 ck_assert_ptr_eq(EINA_VALUE_TYPE_STRING, eina_value_type_get(property));
360
361 const char *value = NULL;
362 eina_value_get(property, &value);
363 ck_assert_str_eq(name_to_be_set, value);
364
365 eina_promise_unref(promise);
366
367 // Setting and getting a value
368 int value_to_be_set = 0xdeadbeef;
369 _set_int_value_property(child, &value_to_be_set);
370
371 efl_model_property_get(child, EJSON_MODEL_VALUE_PROPERTY, &promise);
372
373 property = eina_promise_value_get(promise);
374 ck_assert_ptr_eq(EINA_VALUE_TYPE_INT, eina_value_type_get(property));
375
376 int retrieved_value = -1;
377 eina_value_get(property, &retrieved_value);
378 ck_assert_int_eq(value_to_be_set, retrieved_value);
379
380 eina_promise_unref(promise);
381 eo_unref(json);
382}
383END_TEST
384
385START_TEST(object_set_invalid_property)
386{
387 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
388 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_INT));
389 Eina_Promise *promise;
390 Eina_Value value;
391
392 ck_assert_ptr_ne(NULL, json);
393
394 eina_value_setup(&value, EINA_VALUE_TYPE_INT);
395 eina_value_set(&value, 0xdeadbeef);
396
397 // Invalid property
398 efl_model_property_set(json, "foobar", &value, &promise);
399 ck_assert_int_eq(eina_promise_error_get(promise), EFL_MODEL_ERROR_NOT_FOUND);
400
401 eina_promise_unref(promise);
402 promise = NULL;
403
404 // Invalid 'name' property
405 efl_model_property_set(json, EJSON_MODEL_NAME_PROPERTY, NULL, &promise);
406 ck_assert_int_eq(eina_promise_error_get(promise), EFL_MODEL_ERROR_INCORRECT_VALUE);
407
408 eina_promise_unref(promise);
409 eina_value_flush(&value);
410 eo_unref(json);
411
412}
413END_TEST
414
415START_TEST(array_object_property_set)
416{
417 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
418 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_ARRAY));
419 ck_assert_ptr_ne(NULL, json);
420
421 Eo *child = efl_model_child_add(json);
422 ck_assert_ptr_ne(NULL, child);
423
424 Ejson_Model_Type type = ejson_model_type_get(child);
425 ck_assert_int_eq(EJSON_MODEL_TYPE_NULL, type);
426 ejson_model_type_set(child, EJSON_MODEL_TYPE_INT);
427 type = ejson_model_type_get(child);
428 ck_assert_int_eq(EJSON_MODEL_TYPE_INT, type);
429
430 _check_int_value_property_set(child, 1234);
431
432 eo_unref(json);
433}
434END_TEST
435
436START_TEST(int_property_set)
437{
438 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
439 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_INT));
440 ck_assert_ptr_ne(NULL, json);
441
442 _check_int_value_property_set(json, 1234);
443
444 _check_value_type_cannot_have_children(json);
445
446 eo_unref(json);
447}
448END_TEST
449
450START_TEST(boolean_property_set)
451{
452 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
453 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_BOOLEAN));
454 ck_assert_ptr_ne(NULL, json);
455
456 _check_bool_value_property_set(json, true);
457 _check_bool_value_property_set(json, false);
458
459 _check_value_type_cannot_have_children(json);
460
461 eo_unref(json);
462}
463END_TEST
464
465START_TEST(string_property_set)
466{
467 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
468 ejson_model_type_set(eo_self, EJSON_MODEL_TYPE_STRING));
469 ck_assert_ptr_ne(NULL, json);
470
471 _check_string_value_property_set(json, "Hello world!");
472
473 _check_value_type_cannot_have_children(json);
474
475 eo_unref(json);
476}
477END_TEST
478
479START_TEST(json_object_property_set)
480{
481 const char *json_str = "{ \"a\": 1, \"b\": \"Hello\" }";
482 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
483 ejson_model_string_set(eo_self, json_str));
484 ck_assert_ptr_ne(NULL, json);
485
486 _check_efl_model_children_count_eq(json, 2);
487
488 Eo *child1 = _emodel_nth_child_get(json, 1);
489 Eo *child2 = _emodel_nth_child_get(json, 2);
490
491 _check_ejson_type(child1, EJSON_MODEL_TYPE_INT);
492 _check_ejson_type(child2, EJSON_MODEL_TYPE_STRING);
493
494
495 _check_efl_model_property_int_eq(child1, EJSON_MODEL_VALUE_PROPERTY, 1);
496 _check_efl_model_property_str_eq(child1, EJSON_MODEL_NAME_PROPERTY, "a");
497
498 _check_efl_model_property_str_eq(child2, EJSON_MODEL_VALUE_PROPERTY, "Hello");
499 _check_efl_model_property_str_eq(child2, EJSON_MODEL_NAME_PROPERTY, "b");
500
501 eo_unref(child1);
502 eo_unref(child2);
503
504 eo_unref(json);
505}
506END_TEST
507
508START_TEST(json_array_property_set)
509{
510 const char *json_str = "[ 1, \"Hello\" ]";
511 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
512 ejson_model_string_set(eo_self, json_str));
513 ck_assert_ptr_ne(NULL, json);
514
515 _check_efl_model_children_count_eq(json, 2);
516
517 Eo *child1 = _emodel_nth_child_get(json, 1);
518 Eo *child2 = _emodel_nth_child_get(json, 2);
519
520 _check_ejson_type(child1, EJSON_MODEL_TYPE_INT);
521 _check_ejson_type(child2, EJSON_MODEL_TYPE_STRING);
522
523 _check_efl_model_property_int_eq(child1, EJSON_MODEL_VALUE_PROPERTY, 1);
524
525 _check_efl_model_property_str_eq(child2, EJSON_MODEL_VALUE_PROPERTY, "Hello");
526
527 // Get rid of the refs from nth_child_get
528 eo_unref(child1);
529 eo_unref(child2);
530
531 eo_unref(json);
532}
533END_TEST
534
535static const char *_all_json_types_object_string =
536 "{ \"i\": 1, "
537 "\"s\": \"Hello World!\", "
538 "\"b\": true, "
539 "\"d\": 1.234, "
540 "\"n\": null, "
541 "\"o\": { }, "
542 "\"a\": [ ] "
543 "}";
544
545static Eo *
546_create_model_with_all_json_types()
547{
548 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
549 ejson_model_string_set(eo_self, _all_json_types_object_string));
550
551 _check_efl_model_children_count_eq(json, 7);
552 return json;
553}
554
555START_TEST(properties_get)
556{
557 Eo *json = _create_model_with_all_json_types();
558
559 _check_efl_model_properties(json, (const char*[]){NULL});
560
561 Eo *child_i = _emodel_nth_child_get(json, 1);
562 Eo *child_s = _emodel_nth_child_get(json, 2);
563 Eo *child_b = _emodel_nth_child_get(json, 3);
564 Eo *child_d = _emodel_nth_child_get(json, 4);
565 Eo *child_n = _emodel_nth_child_get(json, 5);
566 Eo *child_o = _emodel_nth_child_get(json, 6);
567 Eo *child_a = _emodel_nth_child_get(json, 7);
568
569 _check_ejson_type(child_i, EJSON_MODEL_TYPE_INT);
570 _check_ejson_type(child_s, EJSON_MODEL_TYPE_STRING);
571 _check_ejson_type(child_b, EJSON_MODEL_TYPE_BOOLEAN);
572 _check_ejson_type(child_d, EJSON_MODEL_TYPE_DOUBLE);
573 _check_ejson_type(child_n, EJSON_MODEL_TYPE_NULL);
574 _check_ejson_type(child_o, EJSON_MODEL_TYPE_OBJECT);
575 _check_ejson_type(child_a, EJSON_MODEL_TYPE_ARRAY);
576
577 _check_efl_model_properties(child_i, (const char*[]){"name", "value", NULL});
578 _check_efl_model_properties(child_s, (const char*[]){"name", "value", NULL});
579 _check_efl_model_properties(child_b, (const char*[]){"name", "value", NULL});
580 _check_efl_model_properties(child_d, (const char*[]){"name", "value", NULL});
581 _check_efl_model_properties(child_n, (const char*[]){"name", NULL});
582 _check_efl_model_properties(child_o, (const char*[]){"name", NULL});
583 _check_efl_model_properties(child_a, (const char*[]){"name", NULL});
584
585 // Get rid of the refs we got from the slice
586 eo_unref(child_i);
587 eo_unref(child_s);
588 eo_unref(child_b);
589 eo_unref(child_d);
590 eo_unref(child_n);
591 eo_unref(child_o);
592 eo_unref(child_a);
593
594 eo_unref(json);
595}
596END_TEST
597
598START_TEST(nested_object_test)
599{
600 /* const char *json_str = "{ \"a\": { } }"; */
601 const char *json_str = "{ }";
602 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
603 ejson_model_string_set(eo_self, json_str));
604
605 ck_assert_ptr_ne(NULL, json);
606
607 eo_unref(json);
608}
609END_TEST
610
611START_TEST(object_child_del)
612{
613 Eo *json = _create_model_with_all_json_types();
614
615 Eo *child_i = _emodel_nth_child_get(json, 1);
616 Eo *child_s = _emodel_nth_child_get(json, 2);
617 Eo *child_b = _emodel_nth_child_get(json, 3);
618 Eo *child_d = _emodel_nth_child_get(json, 4);
619 Eo *child_n = _emodel_nth_child_get(json, 5);
620 Eo *child_o = _emodel_nth_child_get(json, 6);
621 Eo *child_a = _emodel_nth_child_get(json, 7);
622
623 efl_model_child_del(json, child_s);
624 _check_efl_model_children_count_eq(json, 6);
625
626 efl_model_child_del(json, child_i);
627 _check_efl_model_children_count_eq(json, 5);
628
629 efl_model_child_del(json, child_a);
630 _check_efl_model_children_count_eq(json, 4);
631
632 efl_model_child_del(json, child_d);
633 _check_efl_model_children_count_eq(json, 3);
634
635 efl_model_child_del(json, child_b);
636 _check_efl_model_children_count_eq(json, 2);
637
638 efl_model_child_del(json, child_o);
639 _check_efl_model_children_count_eq(json, 1);
640
641 efl_model_child_del(json, child_n);
642 _check_efl_model_children_count_eq(json, 0);
643
644 eo_unref(json);
645}
646END_TEST
647
648START_TEST(object_child_out_of_bounds)
649{
650 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL);
651 Eina_Promise *promise;
652
653 ck_assert_ptr_ne(NULL, json);
654
655 efl_model_children_slice_get(json, 10, 2, &promise);
656
657 ck_assert_ptr_ne(NULL, promise);
658 ck_assert_int_eq(eina_promise_error_get(promise), EFL_MODEL_ERROR_NOT_FOUND);
659
660 eina_promise_unref(promise);
661
662 eo_unref(json);
663}
664END_TEST
665
666START_TEST(wrong_properties_get)
667{
668 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL);
669 Eina_Promise *promise = NULL;
670
671 ck_assert_ptr_ne(NULL, json);
672
673 efl_model_property_get(json, NULL, &promise);
674
675 ck_assert_ptr_ne(NULL, promise);
676 ck_assert_int_eq(eina_promise_error_get(promise), EFL_MODEL_ERROR_INCORRECT_VALUE);
677
678 eina_promise_unref(promise);
679 promise = NULL;
680
681 efl_model_property_get(json, "FOOBAR", &promise);
682
683 ck_assert_ptr_ne(NULL, promise);
684 ck_assert_int_eq(eina_promise_error_get(promise), EFL_MODEL_ERROR_NOT_FOUND);
685
686 eina_promise_unref(promise);
687 eo_unref(json);
688}
689END_TEST
690
691START_TEST(array_child_del)
692{
693 const char *_all_json_types_array_string =
694 "[ 1, \"Hello World!\", true, 1.234, null, { }, [ ] ]";
695
696 Eo *json = eo_add(EJSON_MODEL_CLASS, NULL,
697 ejson_model_string_set(eo_self, _all_json_types_array_string));
698 ck_assert_ptr_ne(NULL, json);
699
700 _check_efl_model_children_count_eq(json, 7);
701
702 Eo *child_i = _emodel_nth_child_get(json, 1);
703 Eo *child_s = _emodel_nth_child_get(json, 2);
704 Eo *child_b = _emodel_nth_child_get(json, 3);
705 Eo *child_d = _emodel_nth_child_get(json, 4);
706 Eo *child_n = _emodel_nth_child_get(json, 5);
707 Eo *child_o = _emodel_nth_child_get(json, 6);
708 Eo *child_a = _emodel_nth_child_get(json, 7);
709
710 efl_model_child_del(json, child_s);
711 _check_efl_model_children_count_eq(json, 6);
712
713 efl_model_child_del(json, child_i);
714 _check_efl_model_children_count_eq(json, 5);
715
716 efl_model_child_del(json, child_a);
717 _check_efl_model_children_count_eq(json, 4);
718
719 efl_model_child_del(json, child_d);
720 _check_efl_model_children_count_eq(json, 3);
721
722 efl_model_child_del(json, child_b);
723 _check_efl_model_children_count_eq(json, 2);
724
725 efl_model_child_del(json, child_o);
726 _check_efl_model_children_count_eq(json, 1);
727
728 efl_model_child_del(json, child_n);
729 _check_efl_model_children_count_eq(json, 0);
730
731 eo_unref(json);
732}
733END_TEST
734
735void
736ejson_test_ejson_model(TCase *tc)
737{
738 tcase_add_checked_fixture(tc, _setup, _teardown);
739 tcase_add_test(tc, smoke);
740 tcase_add_test(tc, object_constructor);
741 tcase_add_test(tc, object_get_null_value);
742 tcase_add_test(tc, object_simple_get_set);
743 tcase_add_test(tc, object_set_invalid_property);
744 tcase_add_test(tc, array_object_property_set);
745 tcase_add_test(tc, int_property_set);
746 tcase_add_test(tc, boolean_property_set);
747 tcase_add_test(tc, string_property_set);
748 tcase_add_test(tc, json_object_property_set);
749 tcase_add_test(tc, json_array_property_set);
750 tcase_add_test(tc, properties_get);
751 tcase_add_test(tc, nested_object_test);
752 tcase_add_test(tc, object_child_del);
753 tcase_add_test(tc, object_child_out_of_bounds);
754 tcase_add_test(tc, wrong_properties_get);
755 tcase_add_test(tc, array_child_del);
756}
diff --git a/src/tests/ejson/ejson_test_ejson_model.h b/src/tests/ejson/ejson_test_ejson_model.h
new file mode 100644
index 0000000000..ea5c939761
--- /dev/null
+++ b/src/tests/ejson/ejson_test_ejson_model.h
@@ -0,0 +1,6 @@
1#ifndef _EJSON_TEST_EJSON_MODEL_H
2#define _EJSON_TEST_EJSON_MODEL_H
3
4#include <Ejson.h>
5
6#endif