1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/values.h"
6 #include "chromecast/base/serializers.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 const char kEmptyJsonString
[] = "{}";
12 const char kProperJsonString
[] =
18 " \"some_String\": \"1337\",\n"
19 " \"some_int\": 42,\n"
20 " \"the_list\": [ \"val1\", \"val2\" ]\n"
22 const char kPoorlyFormedJsonString
[] = "{\"key\":";
23 const char kTestKey
[] = "test_key";
24 const char kTestValue
[] = "test_value";
28 TEST(DeserializeFromJson
, EmptyString
) {
30 scoped_ptr
<base::Value
> value
= DeserializeFromJson(str
);
31 EXPECT_EQ(nullptr, value
.get());
34 TEST(DeserializeFromJson
, EmptyJsonObject
) {
35 std::string str
= kEmptyJsonString
;
36 scoped_ptr
<base::Value
> value
= DeserializeFromJson(str
);
37 EXPECT_NE(nullptr, value
.get());
40 TEST(DeserializeFromJson
, ProperJsonObject
) {
41 std::string str
= kProperJsonString
;
42 scoped_ptr
<base::Value
> value
= DeserializeFromJson(str
);
43 EXPECT_NE(nullptr, value
.get());
46 TEST(DeserializeFromJson
, PoorlyFormedJsonObject
) {
47 std::string str
= kPoorlyFormedJsonString
;
48 scoped_ptr
<base::Value
> value
= DeserializeFromJson(str
);
49 EXPECT_EQ(nullptr, value
.get());
52 TEST(SerializeToJson
, BadValue
) {
53 base::BinaryValue
value(scoped_ptr
<char[]>(new char[12]), 12);
54 scoped_ptr
<std::string
> str
= SerializeToJson(value
);
55 EXPECT_EQ(nullptr, str
.get());
58 TEST(SerializeToJson
, EmptyValue
) {
59 base::DictionaryValue value
;
60 scoped_ptr
<std::string
> str
= SerializeToJson(value
);
61 ASSERT_NE(nullptr, str
.get());
62 EXPECT_EQ(kEmptyJsonString
, *str
);
65 TEST(SerializeToJson
, PopulatedValue
) {
66 base::DictionaryValue orig_value
;
67 orig_value
.SetString(kTestKey
, kTestValue
);
68 scoped_ptr
<std::string
> str
= SerializeToJson(orig_value
);
69 ASSERT_NE(nullptr, str
.get());
71 scoped_ptr
<base::Value
> new_value
= DeserializeFromJson(*str
);
72 ASSERT_NE(nullptr, new_value
.get());
73 EXPECT_TRUE(new_value
->Equals(&orig_value
));
76 } // namespace chromecast