aboutsummaryrefslogtreecommitdiff
path: root/cpp/test/util
diff options
context:
space:
mode:
authorrouslan@chromium.org <rouslan@chromium.org@38ededc0-08b8-5190-f2ac-b31f878777ad>2013-11-14 21:42:19 +0000
committerrouslan@chromium.org <rouslan@chromium.org@38ededc0-08b8-5190-f2ac-b31f878777ad>2013-11-14 21:42:19 +0000
commitc49156d6c80a08cc55ed968e4ab3a616409e72bc (patch)
treee3ea913226646345fefd9a7c1aaaa104a1b64997 /cpp/test/util
parentcd9fa24dde8d32cc1c207db9e96d48f6c6d9b45f (diff)
downloadsrc-c49156d6c80a08cc55ed968e4ab3a616409e72bc.tar.gz
[cpp] JSON utility
This patch is part of a series of patches that enable showing an address input form. This patch adds a rapidjson dependency and a wrapper. git-svn-id: http://libaddressinput.googlecode.com/svn/trunk@155 38ededc0-08b8-5190-f2ac-b31f878777ad
Diffstat (limited to 'cpp/test/util')
-rw-r--r--cpp/test/util/json_test.cc112
1 files changed, 112 insertions, 0 deletions
diff --git a/cpp/test/util/json_test.cc b/cpp/test/util/json_test.cc
new file mode 100644
index 0000000..b701ef1
--- /dev/null
+++ b/cpp/test/util/json_test.cc
@@ -0,0 +1,112 @@
+// Copyright (C) 2013 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "util/json.h"
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+namespace {
+
+using i18n::addressinput::Json;
+
+TEST(JsonTest, EmptyStringIsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject(std::string()));
+}
+
+TEST(JsonTest, EmptyDictionaryContainsNoKeys) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{}"));
+ EXPECT_FALSE(json.HasStringValueForKey("key"));
+ EXPECT_FALSE(json.HasStringValueForKey(std::string()));
+}
+
+TEST(JsonTest, InvalidJsonIsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject("{"));
+}
+
+TEST(JsonTest, OneKeyIsValid) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}"));
+ ASSERT_TRUE(json.HasStringValueForKey("key"));
+ EXPECT_EQ("value", json.GetStringValueForKey("key"));
+}
+
+TEST(JsonTest, EmptyStringKeyIsNotInObject) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}"));
+ EXPECT_FALSE(json.HasStringValueForKey(std::string()));
+}
+
+TEST(JsonTest, EmptyKeyIsValid) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"\": \"value\"}"));
+ ASSERT_TRUE(json.HasStringValueForKey(std::string()));
+ EXPECT_EQ("value", json.GetStringValueForKey(std::string()));
+}
+
+TEST(JsonTest, EmptyValueIsValid) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"key\": \"\"}"));
+ ASSERT_TRUE(json.HasStringValueForKey("key"));
+ EXPECT_TRUE(json.GetStringValueForKey("key").empty());
+}
+
+TEST(JsonTest, Utf8EncodingIsValid) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"key\": \"Ü\"}"));
+ ASSERT_TRUE(json.HasStringValueForKey("key"));
+ EXPECT_EQ("Ü", json.GetStringValueForKey("key"));
+}
+
+TEST(JsonTest, InvalidUtf8IsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject("{\"key\": \"\xC3\x28\"}"));
+}
+
+TEST(JsonTest, NullInMiddleIsNotValid) {
+ Json json;
+ static const char kJson[] = "{\"key\": \"val\0ue\"}";
+ EXPECT_FALSE(json.ParseObject(std::string(kJson, sizeof kJson - 1)));
+}
+
+TEST(JsonTest, TwoKeysAreValid) {
+ Json json;
+ ASSERT_TRUE(json.ParseObject("{\"key1\": \"value1\", \"key2\": \"value2\"}"));
+ ASSERT_TRUE(json.HasStringValueForKey("key1"));
+ EXPECT_EQ("value1", json.GetStringValueForKey("key1"));
+
+ ASSERT_TRUE(json.HasStringValueForKey("key2"));
+ EXPECT_EQ("value2", json.GetStringValueForKey("key2"));
+}
+
+TEST(JsonTest, ListIsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject("[]"));
+}
+
+TEST(JsonTest, StringIsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject("\"value\""));
+}
+
+TEST(JsonTest, NumberIsNotValid) {
+ Json json;
+ EXPECT_FALSE(json.ParseObject("3"));
+}
+
+} // namespace