static void test_show_object_simple(void **state) { JsonElement *json = JsonObjectCreate(10); JsonObjectAppendString(json, "first", "one"); JsonObjectAppendString(json, "second", "two"); Writer *writer = StringWriter(); JsonElementPrint(writer, json, 0); char *output = StringWriterClose(writer); assert_string_equal(OBJECT_SIMPLE, output); JsonElementDestroy(json); free(output); }
static void test_object_get_array(void **state) { JsonElement *arr = JsonArrayCreate(10); JsonArrayAppendString(arr, "one"); JsonArrayAppendString(arr, "two"); JsonElement *obj = JsonObjectCreate(10); JsonObjectAppendArray(obj, "array", arr); JsonElement *arr2 = JsonObjectGetAsArray(obj, "array"); assert_string_equal(JsonArrayGetAsString(arr2, 1), "two"); JsonElementDestroy(obj); }
static void test_show_object_numeric(void **state) { JsonElement *json = JsonObjectCreate(10); JsonObjectAppendReal(json, "real", 1234.5678); JsonObjectAppendInteger(json, "int", -1234567890); Writer *writer = StringWriter(); JsonElementPrint(writer, json, 0); char *output = StringWriterClose(writer); assert_string_equal(OBJECT_NUMERIC, output); JsonElementDestroy(json); free(output); }
static void test_show_array_object(void **state) { JsonElement *array = JsonArrayCreate(10); JsonElement *object = JsonObjectCreate(10); JsonObjectAppendString(object, "first", "one"); JsonArrayAppendObject(array, object); Writer *writer = StringWriter(); JsonElementPrint(writer, array, 0); assert_string_equal(ARRAY_OBJECT, StringWriterData(writer)); JsonElementDestroy(array); }
static JsonElement *FunctionsToJson(void) { JsonElement *functions = JsonObjectCreate(500); for (int i = 0; CF_FNCALL_TYPES[i].name; i++) { const FnCallType *fn_syntax = &CF_FNCALL_TYPES[i]; if (fn_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonObjectAppendObject(functions, fn_syntax->name, FnCallTypeToJson(fn_syntax)); } return functions; }
static void test_show_array_object(void) { JsonElement *array = JsonArrayCreate(10); JsonElement *object = JsonObjectCreate(10); JsonObjectAppendString(object, "first", "one"); JsonArrayAppendObject(array, object); Writer *writer = StringWriter(); JsonWrite(writer, array, 0); char *output = StringWriterClose(writer); assert_string_equal(ARRAY_OBJECT, output); JsonDestroy(array); free(output); }
static void test_show_object_array(void **state) { JsonElement *json = JsonObjectCreate(10); JsonElement *array = JsonArrayCreate(10); JsonArrayAppendString(array, "one"); JsonArrayAppendString(array, "two"); JsonObjectAppendArray(json, "first", array); Writer *writer = StringWriter(); JsonElementPrint(writer, json, 0); assert_string_equal(OBJECT_ARRAY, StringWriterData(writer)); JsonElementDestroy(json); }
static void test_iterator_current(void **state) { const char *data = ARRAY_SIMPLE; JsonElement *arr = JsonParse(&data); JsonElement *json = JsonObjectCreate(1); JsonObjectAppendArray(json, "array", arr); JsonIterator it = JsonIteratorInit(json); while (JsonIteratorNextValue(&it) != NULL) { assert_int_equal((int)JsonIteratorCurrentElementType(&it), (int)JSON_ELEMENT_TYPE_CONTAINER); assert_int_equal((int)JsonIteratorCurrentContrainerType(&it), (int)JSON_CONTAINER_TYPE_ARRAY); assert_string_equal(JsonIteratorCurrentKey(&it), "array"); } JsonElementDestroy(json); }
static void test_object_iterator(void) { JsonElement *obj = JsonObjectCreate(10); JsonObjectAppendString(obj, "first", "one"); JsonObjectAppendString(obj, "second", "two"); JsonObjectAppendInteger(obj, "third", 3); JsonObjectAppendBool(obj, "fourth", true); JsonObjectAppendBool(obj, "fifth", false); { JsonIterator it = JsonIteratorInit(obj); assert_true(JsonIteratorHasMore(&it)); assert_string_equal("first", JsonIteratorNextKey(&it)); assert_string_equal("second", JsonIteratorNextKey(&it)); assert_string_equal("third", JsonIteratorNextKey(&it)); assert_string_equal("fourth", JsonIteratorNextKey(&it)); assert_true(JsonIteratorHasMore(&it)); assert_string_equal("fifth", JsonIteratorNextKey(&it)); assert_false(JsonIteratorHasMore(&it)); assert_false(JsonIteratorNextKey(&it)); } { JsonIterator it = JsonIteratorInit(obj); assert_true(JsonIteratorHasMore(&it)); assert_string_equal("one", JsonPrimitiveGetAsString(JsonIteratorNextValue(&it))); assert_string_equal("two", JsonPrimitiveGetAsString(JsonIteratorNextValue(&it))); assert_int_equal(3, JsonPrimitiveGetAsInteger(JsonIteratorNextValue(&it))); assert_true(JsonPrimitiveGetAsBool(JsonIteratorNextValue(&it))); assert_true(JsonIteratorHasMore(&it)); assert_false(JsonPrimitiveGetAsBool(JsonIteratorNextValue(&it))); assert_false(JsonIteratorHasMore(&it)); assert_false(JsonIteratorNextValue(&it)); } JsonDestroy(obj); }
static void test_show_object_array(void) { JsonElement *json = JsonObjectCreate(10); JsonElement *array = JsonArrayCreate(10); JsonArrayAppendString(array, "one"); JsonArrayAppendString(array, "two"); JsonObjectAppendArray(json, "first", array); Writer *writer = StringWriter(); JsonWrite(writer, json, 0); char *output = StringWriterClose(writer); assert_string_equal(OBJECT_ARRAY, output); JsonDestroy(json); free(output); }
JsonElement *RvalToJson(Rval rval) { assert(rval.item); switch (rval.type) { case RVAL_TYPE_SCALAR: return JsonStringCreate(RvalScalarValue(rval)); case RVAL_TYPE_LIST: return RlistToJson(RvalRlistValue(rval)); case RVAL_TYPE_FNCALL: return FnCallToJson(RvalFnCallValue(rval)); case RVAL_TYPE_CONTAINER: return JsonCopy(RvalContainerValue(rval)); case RVAL_TYPE_NOPROMISEE: assert(false); return JsonObjectCreate(1); } assert(false); return NULL; }
static JsonElement *ExportBundleTypeSyntaxAsJson(const char *bundle_type) { JsonElement *json = JsonObjectCreate(10); const PromiseTypeSyntax *st; int i = 0, j = 0; for (i = 0; i < CF3_MODULES; i++) { st = CF_ALL_PROMISE_TYPES[i]; for (j = 0; st[j].bundle_type != NULL; j++) { if (strcmp(bundle_type, st[j].bundle_type) == 0 || strcmp("*", st[j].bundle_type) == 0) { JsonElement *attributes = ExportAttributesSyntaxAsJson(st[j].constraints); JsonObjectAppendObject(json, st[j].promise_type, attributes); } } } return json; }
static JsonElement *ExportBodyClassesAsJson(Constraint *constraints) { JsonElement *json_contexts = JsonArrayCreate(10); JsonElement *json_attributes = JsonArrayCreate(10); char *current_context = "any"; size_t context_offset_start = -1; size_t context_offset_end = -1; Constraint *cp = NULL; for (cp = constraints; cp != NULL; cp = cp->next) { JsonElement *json_attribute = JsonObjectCreate(10); JsonObjectAppendInteger(json_attribute, "offset", cp->offset.start); JsonObjectAppendInteger(json_attribute, "offset-end", cp->offset.end); context_offset_start = cp->offset.context; context_offset_end = cp->offset.end; JsonObjectAppendString(json_attribute, "lval", cp->lval); JsonObjectAppendObject(json_attribute, "rval", ExportAttributeValueAsJson(cp->rval)); JsonArrayAppendObject(json_attributes, json_attribute); if (cp->next == NULL || strcmp(current_context, cp->next->classes) != 0) { JsonArrayAppendObject(json_contexts, CreateContextAsJson(current_context, context_offset_start, context_offset_end, "attributes", json_attributes)); current_context = cp->classes; } } return json_contexts; }
static JsonElement *PromiseTypesToJson(void) { JsonElement *promise_types = JsonObjectCreate(50); const PromiseTypeSyntax *global_promise_type = PromiseTypeSyntaxGet("*", "*"); for (int module_index = 0; module_index < CF3_MODULES; module_index++) { for (int promise_type_index = 0; CF_ALL_PROMISE_TYPES[module_index][promise_type_index].promise_type; promise_type_index++) { const PromiseTypeSyntax *promise_type_syntax = &CF_ALL_PROMISE_TYPES[module_index][promise_type_index]; // skip global and bundle-local common constraints if (strcmp("*", promise_type_syntax->promise_type) == 0) { continue; } if (promise_type_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonElement *promise_type = JsonObjectGet(promise_types, promise_type_syntax->promise_type); if (!promise_type) { promise_type = JsonPromiseTypeNew(promise_type_syntax->status); JsonObjectAppendObject(promise_types, promise_type_syntax->promise_type, promise_type); } assert(promise_type); JsonElement *attributes = JsonObjectGet(promise_type, "attributes"); assert(attributes); for (int i = 0; promise_type_syntax->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &promise_type_syntax->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "promiseType"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } // append bundle common constraints const PromiseTypeSyntax *bundle_promise_type = PromiseTypeSyntaxGet(promise_type_syntax->bundle_type, "*"); if (strcmp("*", bundle_promise_type->bundle_type) != 0) { for (int i = 0; bundle_promise_type->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &bundle_promise_type->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "bundle"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } } // append global common constraints for (int i = 0; global_promise_type->constraints[i].lval; i++) { const ConstraintSyntax *constraint_syntax = &global_promise_type->constraints[i]; JsonElement *json_constraint = ConstraintSyntaxToJson(constraint_syntax); JsonObjectAppendString(json_constraint, "visibility", "global"); JsonObjectAppendObject(attributes, constraint_syntax->lval, json_constraint); } } } return promise_types; }
static JsonElement *BundleTypesToJson(void) { JsonElement *bundle_types = JsonObjectCreate(50); Seq *common_promise_types = SeqNew(50, free); for (int module_index = 0; module_index < CF3_MODULES; module_index++) { for (int promise_type_index = 0; CF_ALL_PROMISE_TYPES[module_index][promise_type_index].promise_type; promise_type_index++) { const PromiseTypeSyntax *promise_type_syntax = &CF_ALL_PROMISE_TYPES[module_index][promise_type_index]; // skip global constraints if (strcmp("*", promise_type_syntax->promise_type) == 0) { continue; } // collect common promise types to be appended at the end if (strcmp("*", promise_type_syntax->bundle_type) == 0) { SeqAppend(common_promise_types, xstrdup(promise_type_syntax->promise_type)); continue; } if (promise_type_syntax->status == SYNTAX_STATUS_REMOVED) { continue; } JsonElement *bundle_type = JsonObjectGet(bundle_types, promise_type_syntax->bundle_type); if (!bundle_type) { bundle_type = JsonBundleTypeNew(); JsonObjectAppendObject(bundle_types, promise_type_syntax->bundle_type, bundle_type); } assert(bundle_type); JsonElement *promise_types = JsonObjectGet(bundle_type, "promiseTypes"); assert(promise_types); JsonArrayAppendString(promise_types, promise_type_syntax->promise_type); } } // Append the common bundle, which has only common promise types, but is not declared in syntax { JsonElement *bundle_type = JsonBundleTypeNew(); JsonObjectAppendObject(bundle_types, "common", bundle_type); } JsonIterator it = JsonIteratorInit(bundle_types); const char *bundle_type = NULL; while ((bundle_type = JsonIteratorNextKey(&it))) { JsonElement *promise_types = JsonObjectGetAsArray(JsonObjectGetAsObject(bundle_types, bundle_type), "promiseTypes"); for (int i = 0; i < SeqLength(common_promise_types); i++) { const char *common_promise_type = SeqAt(common_promise_types, i); JsonArrayAppendString(promise_types, common_promise_type); } } SeqDestroy(common_promise_types); return bundle_types; }