OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 Google Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "util/json.h" |
| 16 |
| 17 #include <string> |
| 18 |
| 19 #include <gtest/gtest.h> |
| 20 |
| 21 namespace { |
| 22 |
| 23 using i18n::addressinput::Json; |
| 24 |
| 25 TEST(JsonTest, EmptyStringIsNotValid) { |
| 26 Json json; |
| 27 EXPECT_FALSE(json.ParseObject(std::string())); |
| 28 } |
| 29 |
| 30 TEST(JsonTest, EmptyDictionaryContainsNoKeys) { |
| 31 Json json; |
| 32 ASSERT_TRUE(json.ParseObject("{}")); |
| 33 EXPECT_FALSE(json.HasStringValueForKey("key")); |
| 34 EXPECT_FALSE(json.HasStringValueForKey(std::string())); |
| 35 } |
| 36 |
| 37 TEST(JsonTest, InvalidJsonIsNotValid) { |
| 38 Json json; |
| 39 EXPECT_FALSE(json.ParseObject("{")); |
| 40 } |
| 41 |
| 42 TEST(JsonTest, OneKeyIsValid) { |
| 43 Json json; |
| 44 ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}")); |
| 45 ASSERT_TRUE(json.HasStringValueForKey("key")); |
| 46 EXPECT_EQ("value", json.GetStringValueForKey("key")); |
| 47 } |
| 48 |
| 49 TEST(JsonTest, EmptyStringKeyIsNotInObject) { |
| 50 Json json; |
| 51 ASSERT_TRUE(json.ParseObject("{\"key\": \"value\"}")); |
| 52 EXPECT_FALSE(json.HasStringValueForKey(std::string())); |
| 53 } |
| 54 |
| 55 TEST(JsonTest, EmptyKeyIsValid) { |
| 56 Json json; |
| 57 ASSERT_TRUE(json.ParseObject("{\"\": \"value\"}")); |
| 58 ASSERT_TRUE(json.HasStringValueForKey(std::string())); |
| 59 EXPECT_EQ("value", json.GetStringValueForKey(std::string())); |
| 60 } |
| 61 |
| 62 TEST(JsonTest, EmptyValueIsValid) { |
| 63 Json json; |
| 64 ASSERT_TRUE(json.ParseObject("{\"key\": \"\"}")); |
| 65 ASSERT_TRUE(json.HasStringValueForKey("key")); |
| 66 EXPECT_TRUE(json.GetStringValueForKey("key").empty()); |
| 67 } |
| 68 |
| 69 TEST(JsonTest, Utf8EncodingIsValid) { |
| 70 Json json; |
| 71 ASSERT_TRUE(json.ParseObject("{\"key\": \"Ü\"}")); |
| 72 ASSERT_TRUE(json.HasStringValueForKey("key")); |
| 73 EXPECT_EQ("Ü", json.GetStringValueForKey("key")); |
| 74 } |
| 75 |
| 76 TEST(JsonTest, InvalidUtf8IsNotValid) { |
| 77 Json json; |
| 78 EXPECT_FALSE(json.ParseObject("{\"key\": \"\xC3\x28\"}")); |
| 79 } |
| 80 |
| 81 TEST(JsonTest, NullInMiddleIsNotValid) { |
| 82 Json json; |
| 83 static const char kJson[] = "{\"key\": \"val\0ue\"}"; |
| 84 EXPECT_FALSE(json.ParseObject(std::string(kJson, sizeof kJson - 1))); |
| 85 } |
| 86 |
| 87 TEST(JsonTest, TwoKeysAreValid) { |
| 88 Json json; |
| 89 ASSERT_TRUE(json.ParseObject("{\"key1\": \"value1\", \"key2\": \"value2\"}")); |
| 90 ASSERT_TRUE(json.HasStringValueForKey("key1")); |
| 91 EXPECT_EQ("value1", json.GetStringValueForKey("key1")); |
| 92 |
| 93 ASSERT_TRUE(json.HasStringValueForKey("key2")); |
| 94 EXPECT_EQ("value2", json.GetStringValueForKey("key2")); |
| 95 } |
| 96 |
| 97 TEST(JsonTest, ListIsNotValid) { |
| 98 Json json; |
| 99 EXPECT_FALSE(json.ParseObject("[]")); |
| 100 } |
| 101 |
| 102 TEST(JsonTest, StringIsNotValid) { |
| 103 Json json; |
| 104 EXPECT_FALSE(json.ParseObject("\"value\"")); |
| 105 } |
| 106 |
| 107 TEST(JsonTest, NumberIsNotValid) { |
| 108 Json json; |
| 109 EXPECT_FALSE(json.ParseObject("3")); |
| 110 } |
| 111 |
| 112 } // namespace |
OLD | NEW |