summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-06-29 12:02:13 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-29 12:07:42 +0200
commitb1d113e32ab770e3b1aaa7811b6a024b4217d5bd (patch)
treef69fa71fbb763f49d8017a9b6bb96380cc31fe5b
parentc9826bb42988626daa6a0dbf2803afab8bcbb2e3 (diff)
downloadserenity-b1d113e32ab770e3b1aaa7811b6a024b4217d5bd.zip
AK: Make a tiny JSON unit test based on a saved VisualBuilder form.
-rw-r--r--AK/Tests/.gitignore1
-rw-r--r--AK/Tests/Makefile5
-rw-r--r--AK/Tests/TestJSON.cpp47
-rw-r--r--Base/home/anon/test.frm1
4 files changed, 53 insertions, 1 deletions
diff --git a/AK/Tests/.gitignore b/AK/Tests/.gitignore
index c7ff67cd46..a8d8df0d88 100644
--- a/AK/Tests/.gitignore
+++ b/AK/Tests/.gitignore
@@ -2,5 +2,6 @@ TestString
TestQueue
TestVector
TestHashMap
+TestJSON
*.d
*.o
diff --git a/AK/Tests/Makefile b/AK/Tests/Makefile
index a703bb0ea6..ecfc22750e 100644
--- a/AK/Tests/Makefile
+++ b/AK/Tests/Makefile
@@ -1,4 +1,4 @@
-PROGRAMS = TestString TestQueue TestVector TestHashMap
+PROGRAMS = TestString TestQueue TestVector TestHashMap TestJSON
all: $(PROGRAMS)
@@ -16,5 +16,8 @@ TestVector: TestVector.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp
TestHashMap: TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestHashMap.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
+TestJSON: TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
+ $(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestJSON.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp ../JsonObject.cpp ../JsonValue.cpp ../JsonArray.cpp ../JsonParser.cpp
+
clean:
rm -f $(PROGRAMS)
diff --git a/AK/Tests/TestJSON.cpp b/AK/Tests/TestJSON.cpp
new file mode 100644
index 0000000000..72fb007de5
--- /dev/null
+++ b/AK/Tests/TestJSON.cpp
@@ -0,0 +1,47 @@
+#include "TestHelpers.h"
+#include <AK/AKString.h>
+#include <AK/HashMap.h>
+#include <AK/JsonArray.h>
+#include <AK/JsonObject.h>
+#include <AK/JsonValue.h>
+#include <AK/StringBuilder.h>
+
+typedef HashMap<int, int> IntIntMap;
+
+int main()
+{
+ FILE* fp = fopen("../../Base/home/anon/test.frm", "r");
+ ASSERT(fp);
+
+ StringBuilder builder;
+ for (;;) {
+ char buffer[1024];
+ if (!fgets(buffer, sizeof(buffer), fp))
+ break;
+ builder.append(buffer);
+ }
+
+ fclose(fp);
+
+ JsonValue form_json = JsonValue::from_string(builder.to_string());
+
+ EXPECT(form_json.is_object());
+
+ auto name = form_json.as_object().get("name").to_string();
+
+ EXPECT_EQ(name, "Form1");
+
+ auto widgets = form_json.as_object().get("widgets").as_array();
+
+ widgets.for_each([&](const JsonValue& widget_value) {
+ auto& widget_object = widget_value.as_object();
+ auto widget_class = widget_object.get("class").as_string();
+ widget_object.for_each_member([&](auto& property_name, const JsonValue& property_value) {
+ (void)property_name;
+ (void)property_value;
+ //dbgprintf("Set property %s.%s to '%s'\n", widget_class.characters(), property_name.characters(), property_value.serialized().characters());
+ });
+ });
+
+ return 0;
+}
diff --git a/Base/home/anon/test.frm b/Base/home/anon/test.frm
new file mode 100644
index 0000000000..481eead9e6
--- /dev/null
+++ b/Base/home/anon/test.frm
@@ -0,0 +1 @@
+{"name":"Form1","widgets":[{"enabled":true,"forecolor":"#000000ff","ruler_visible":false,"autofill":false,"x":155,"tooltip":null,"height":121,"width":126,"y":10,"class":"GTextEditor","text":"Hi","backcolor":"#c0c0c0ff","visible":true},{"tooltip":null,"backcolor":"#c0c0c0ff","forecolor":"#000000ff","text":"Okydoky","class":"GButton","autofill":false,"enabled":true,"visible":true,"x":10,"height":21,"y":70,"width":81},{"tooltip":null,"backcolor":"#c0c0c0ff","forecolor":"#000000ff","class":"GGroupBox","autofill":true,"enabled":true,"visible":true,"title":"groupie","x":10,"height":71,"y":100,"width":141},{"tooltip":null,"forecolor":"#000000ff","y":10,"max":100,"autofill":false,"x":10,"min":0,"class":"GProgressBar","backcolor":"#c0c0c0ff","height":21,"enabled":true,"visible":true,"width":136,"value":50},{"tooltip":null,"backcolor":"#c0c0c0ff","forecolor":"#000000ff","text":"Looks like success!","class":"GLabel","autofill":true,"enabled":true,"visible":true,"x":10,"height":26,"y":35,"width":136},{"enabled":true,"forecolor":"#000000ff","checked":false,"autofill":false,"x":160,"tooltip":null,"height":26,"width":91,"y":140,"class":"GCheckBox","text":"checkbox_1","backcolor":"#c0c0c0ff","visible":true},{"enabled":true,"forecolor":"#000000ff","checked":false,"autofill":false,"x":160,"tooltip":null,"height":26,"width":61,"y":160,"class":"GRadioButton","text":"radio_1","backcolor":"#c0c0c0ff","visible":true},{"tooltip":null,"forecolor":"#000000ff","y":125,"max":100,"autofill":false,"x":25,"min":0,"class":"GSlider","backcolor":"#c0c0c0ff","height":26,"enabled":true,"visible":true,"width":116,"value":"60"}]} \ No newline at end of file