Serenity Operating System
at hosted 111 lines 3.6 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/TestSuite.h> 28 29#include <AK/HashMap.h> 30#include <AK/JsonArray.h> 31#include <AK/JsonObject.h> 32#include <AK/JsonValue.h> 33#include <AK/String.h> 34#include <AK/StringBuilder.h> 35 36TEST_CASE(load_form) 37{ 38 FILE* fp = fopen("test.frm", "r"); 39 ASSERT(fp); 40 41 StringBuilder builder; 42 for (;;) { 43 char buffer[1024]; 44 if (!fgets(buffer, sizeof(buffer), fp)) 45 break; 46 builder.append(buffer); 47 } 48 49 fclose(fp); 50 51 JsonValue form_json = JsonValue::from_string(builder.to_string()); 52 53 EXPECT(form_json.is_object()); 54 55 auto name = form_json.as_object().get("name").to_string(); 56 57 EXPECT_EQ(name, "Form1"); 58 59 auto widgets = form_json.as_object().get("widgets").as_array(); 60 61 widgets.for_each([&](const JsonValue& widget_value) { 62 auto& widget_object = widget_value.as_object(); 63 auto widget_class = widget_object.get("class").as_string(); 64 widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) { 65 (void)property_name; 66 (void)property_value; 67 //dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.serialized().characters()); 68 }); 69 }); 70} 71 72BENCHMARK_CASE(load_4chan_catalog) 73{ 74 FILE* fp = fopen("4chan_catalog.json", "r"); 75 ASSERT(fp); 76 77 StringBuilder builder; 78 for (;;) { 79 char buffer[1024]; 80 if (!fgets(buffer, sizeof(buffer), fp)) 81 break; 82 builder.append(buffer); 83 } 84 85 fclose(fp); 86 87 auto json_string = builder.to_string(); 88 89 for (int i = 0; i < 10; ++i) { 90 JsonValue form_json = JsonValue::from_string(json_string); 91 EXPECT(form_json.is_array()); 92 } 93} 94 95TEST_CASE(json_empty_string) 96{ 97 auto json = JsonValue::from_string("\"\""); 98 EXPECT_EQ(json.type(), JsonValue::Type::String); 99 EXPECT_EQ(json.as_string().is_null(), false); 100 EXPECT_EQ(json.as_string().is_empty(), true); 101} 102 103TEST_CASE(json_utf8_character) 104{ 105 auto json = JsonValue::from_string("\"\xc3\x84\""); 106 EXPECT_EQ(json.type(), JsonValue::Type::String); 107 EXPECT_EQ(json.as_string().is_null(), false); 108 EXPECT_EQ(json.as_string().length(), size_t { 2 }); 109} 110 111TEST_MAIN(JSON)