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 <libaddressinput/util/scoped_ptr.h> |
| 18 |
| 19 #include <string> |
| 20 |
| 21 #include <gtest/gtest.h> |
| 22 |
| 23 namespace { |
| 24 |
| 25 using i18n::addressinput::Json; |
| 26 using i18n::addressinput::scoped_ptr; |
| 27 |
| 28 // Tests for Json object. |
| 29 class JsonTest : public testing::Test { |
| 30 public: |
| 31 JsonTest() : json_(Json::Build()) {} |
| 32 virtual ~JsonTest() {} |
| 33 |
| 34 protected: |
| 35 scoped_ptr<Json> json_; |
| 36 }; |
| 37 |
| 38 TEST_F(JsonTest, EmptyStringIsNotValid) { |
| 39 EXPECT_FALSE(json_->ParseObject(std::string())); |
| 40 } |
| 41 |
| 42 TEST_F(JsonTest, EmptyDictionaryContainsNoKeys) { |
| 43 ASSERT_TRUE(json_->ParseObject("{}")); |
| 44 EXPECT_FALSE(json_->HasStringValueForKey("key")); |
| 45 EXPECT_FALSE(json_->HasStringValueForKey(std::string())); |
| 46 } |
| 47 |
| 48 TEST_F(JsonTest, InvalidJsonIsNotValid) { |
| 49 EXPECT_FALSE(json_->ParseObject("{")); |
| 50 } |
| 51 |
| 52 TEST_F(JsonTest, OneKeyIsValid) { |
| 53 ASSERT_TRUE(json_->ParseObject("{\"key\": \"value\"}")); |
| 54 ASSERT_TRUE(json_->HasStringValueForKey("key")); |
| 55 EXPECT_EQ("value", json_->GetStringValueForKey("key")); |
| 56 } |
| 57 |
| 58 TEST_F(JsonTest, EmptyStringKeyIsNotInObject) { |
| 59 ASSERT_TRUE(json_->ParseObject("{\"key\": \"value\"}")); |
| 60 EXPECT_FALSE(json_->HasStringValueForKey(std::string())); |
| 61 } |
| 62 |
| 63 TEST_F(JsonTest, EmptyKeyIsValid) { |
| 64 ASSERT_TRUE(json_->ParseObject("{\"\": \"value\"}")); |
| 65 ASSERT_TRUE(json_->HasStringValueForKey(std::string())); |
| 66 EXPECT_EQ("value", json_->GetStringValueForKey(std::string())); |
| 67 } |
| 68 |
| 69 TEST_F(JsonTest, EmptyValueIsValid) { |
| 70 ASSERT_TRUE(json_->ParseObject("{\"key\": \"\"}")); |
| 71 ASSERT_TRUE(json_->HasStringValueForKey("key")); |
| 72 EXPECT_TRUE(json_->GetStringValueForKey("key").empty()); |
| 73 } |
| 74 |
| 75 TEST_F(JsonTest, Utf8EncodingIsValid) { |
| 76 ASSERT_TRUE(json_->ParseObject("{\"key\": \"Ü\"}")); |
| 77 ASSERT_TRUE(json_->HasStringValueForKey("key")); |
| 78 EXPECT_EQ("Ü", json_->GetStringValueForKey("key")); |
| 79 } |
| 80 |
| 81 TEST_F(JsonTest, InvalidUtf8IsNotValid) { |
| 82 EXPECT_FALSE(json_->ParseObject("{\"key\": \"\xC3\x28\"}")); |
| 83 } |
| 84 |
| 85 TEST_F(JsonTest, NullInMiddleIsNotValid) { |
| 86 static const char kJson[] = "{\"key\": \"val\0ue\"}"; |
| 87 EXPECT_FALSE(json_->ParseObject(std::string(kJson, sizeof kJson - 1))); |
| 88 } |
| 89 |
| 90 TEST_F(JsonTest, TwoKeysAreValid) { |
| 91 ASSERT_TRUE( |
| 92 json_->ParseObject("{\"key1\": \"value1\", \"key2\": \"value2\"}")); |
| 93 ASSERT_TRUE(json_->HasStringValueForKey("key1")); |
| 94 EXPECT_EQ("value1", json_->GetStringValueForKey("key1")); |
| 95 |
| 96 ASSERT_TRUE(json_->HasStringValueForKey("key2")); |
| 97 EXPECT_EQ("value2", json_->GetStringValueForKey("key2")); |
| 98 } |
| 99 |
| 100 TEST_F(JsonTest, ListIsNotValid) { |
| 101 EXPECT_FALSE(json_->ParseObject("[]")); |
| 102 } |
| 103 |
| 104 TEST_F(JsonTest, StringIsNotValid) { |
| 105 EXPECT_FALSE(json_->ParseObject("\"value\"")); |
| 106 } |
| 107 |
| 108 TEST_F(JsonTest, NumberIsNotValid) { |
| 109 EXPECT_FALSE(json_->ParseObject("3")); |
| 110 } |
| 111 |
| 112 } // namespace |
OLD | NEW |