Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: tools/json_schema_compiler/test/choices_unittest.cc

Issue 11827026: Overhaul JSON Schema Compiler to support a number of features required to (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "tools/json_schema_compiler/test/choices.h" 5 #include "tools/json_schema_compiler/test/choices.h"
6 6
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 using namespace test::api::choices; 9 using namespace test::api::choices;
10 10
11 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) { 11 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) {
12 { 12 {
13 scoped_ptr<ListValue> params_value(new ListValue()); 13 scoped_ptr<ListValue> params_value(new ListValue());
14 params_value->Append(Value::CreateBooleanValue(true)); 14 params_value->Append(Value::CreateBooleanValue(true));
15 scoped_ptr<TakesIntegers::Params> params( 15 scoped_ptr<TakesIntegers::Params> params(
16 TakesIntegers::Params::Create(*params_value)); 16 TakesIntegers::Params::Create(*params_value));
17 EXPECT_FALSE(params.get()); 17 EXPECT_FALSE(params.get());
18 } 18 }
19 { 19 {
20 scoped_ptr<ListValue> params_value(new ListValue()); 20 scoped_ptr<ListValue> params_value(new ListValue());
21 params_value->Append(Value::CreateIntegerValue(6)); 21 params_value->Append(Value::CreateIntegerValue(6));
22 scoped_ptr<TakesIntegers::Params> params( 22 scoped_ptr<TakesIntegers::Params> params(
23 TakesIntegers::Params::Create(*params_value)); 23 TakesIntegers::Params::Create(*params_value));
24 EXPECT_TRUE(params.get()); 24 EXPECT_TRUE(params.get());
25 EXPECT_EQ(TakesIntegers::Params::NUMS_INTEGER, params->nums_type); 25 EXPECT_FALSE(params->nums.as_array);
26 EXPECT_FALSE(params->nums_array.get()); 26 EXPECT_EQ(6, *params->nums.as_integer);
27 EXPECT_EQ(6, *params->nums_integer);
28 } 27 }
29 { 28 {
30 scoped_ptr<ListValue> params_value(new ListValue()); 29 scoped_ptr<ListValue> params_value(new ListValue());
31 scoped_ptr<ListValue> integers(new ListValue()); 30 scoped_ptr<ListValue> integers(new ListValue());
32 integers->Append(Value::CreateIntegerValue(6)); 31 integers->Append(Value::CreateIntegerValue(6));
33 integers->Append(Value::CreateIntegerValue(8)); 32 integers->Append(Value::CreateIntegerValue(8));
34 params_value->Append(integers.release()); 33 params_value->Append(integers.release());
35 scoped_ptr<TakesIntegers::Params> params( 34 scoped_ptr<TakesIntegers::Params> params(
36 TakesIntegers::Params::Create(*params_value)); 35 TakesIntegers::Params::Create(*params_value));
37 EXPECT_TRUE(params.get()); 36 ASSERT_TRUE(params);
38 EXPECT_EQ(TakesIntegers::Params::NUMS_ARRAY, params->nums_type); 37 ASSERT_TRUE(params->nums.as_array);
39 EXPECT_EQ((size_t) 2, (*params->nums_array).size()); 38 EXPECT_EQ((size_t) 2, params->nums.as_array->size());
40 EXPECT_EQ(6, (*params->nums_array)[0]); 39 EXPECT_EQ(6, params->nums.as_array->at(0));
41 EXPECT_EQ(8, (*params->nums_array)[1]); 40 EXPECT_EQ(8, params->nums.as_array->at(1));
42 } 41 }
43 } 42 }
44 43
45 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) { 44 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) {
46 { 45 {
47 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 46 scoped_ptr<DictionaryValue> object_param(new DictionaryValue());
48 object_param->SetWithoutPathExpansion("strings", 47 object_param->SetString("strings", "asdf");
49 Value::CreateStringValue("asdf"));
50 scoped_ptr<ListValue> params_value(new ListValue()); 48 scoped_ptr<ListValue> params_value(new ListValue());
51 params_value->Append(object_param.release()); 49 params_value->Append(object_param.release());
52 scoped_ptr<ObjectWithChoices::Params> params( 50 scoped_ptr<ObjectWithChoices::Params> params(
53 ObjectWithChoices::Params::Create(*params_value)); 51 ObjectWithChoices::Params::Create(*params_value));
54 EXPECT_TRUE(params.get()); 52 ASSERT_TRUE(params);
55 EXPECT_EQ(ObjectWithChoices::Params::StringInfo::STRINGS_STRING, 53 EXPECT_FALSE(params->string_info.strings.as_array);
56 params->string_info.strings_type); 54 EXPECT_EQ("asdf", *params->string_info.strings.as_string);
57 EXPECT_EQ("asdf", *params->string_info.strings_string); 55 EXPECT_FALSE(params->string_info.integers);
58 } 56 }
59 { 57 {
60 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 58 scoped_ptr<DictionaryValue> object_param(new DictionaryValue());
61 object_param->SetWithoutPathExpansion("strings", 59 object_param->SetString("strings", "asdf");
62 Value::CreateStringValue("asdf")); 60 object_param->SetInteger("integers", 6);
63 object_param->SetWithoutPathExpansion("integers",
64 Value::CreateIntegerValue(6));
65 scoped_ptr<ListValue> params_value(new ListValue()); 61 scoped_ptr<ListValue> params_value(new ListValue());
66 params_value->Append(object_param.release()); 62 params_value->Append(object_param.release());
67 scoped_ptr<ObjectWithChoices::Params> params( 63 scoped_ptr<ObjectWithChoices::Params> params(
68 ObjectWithChoices::Params::Create(*params_value)); 64 ObjectWithChoices::Params::Create(*params_value));
69 EXPECT_TRUE(params.get()); 65 ASSERT_TRUE(params);
70 EXPECT_EQ(ObjectWithChoices::Params::StringInfo::STRINGS_STRING, 66 EXPECT_FALSE(params->string_info.strings.as_array);
71 params->string_info.strings_type); 67 EXPECT_EQ("asdf", *params->string_info.strings.as_string);
72 EXPECT_EQ("asdf", *params->string_info.strings_string); 68 ASSERT_TRUE(params->string_info.integers);
73 EXPECT_EQ(ObjectWithChoices::Params::StringInfo::INTEGERS_INTEGER, 69 EXPECT_FALSE(params->string_info.integers->as_array);
74 params->string_info.integers_type); 70 EXPECT_EQ(6, *params->string_info.integers->as_integer);
75 EXPECT_EQ(6, *params->string_info.integers_integer);
76 } 71 }
77 } 72 }
78 73
79 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) { 74 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) {
80 { 75 {
81 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 76 scoped_ptr<DictionaryValue> object_param(new DictionaryValue());
82 object_param->SetWithoutPathExpansion("strings", 77 object_param->SetWithoutPathExpansion("strings",
83 Value::CreateIntegerValue(5)); 78 Value::CreateIntegerValue(5));
84 scoped_ptr<ListValue> params_value(new ListValue()); 79 scoped_ptr<ListValue> params_value(new ListValue());
85 params_value->Append(object_param.release()); 80 params_value->Append(object_param.release());
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 ListValue* strings_value = new ListValue(); 115 ListValue* strings_value = new ListValue();
121 for (size_t i = 0; i < strings.size(); ++i) 116 for (size_t i = 0; i < strings.size(); ++i)
122 strings_value->Append(Value::CreateStringValue(strings[i])); 117 strings_value->Append(Value::CreateStringValue(strings[i]));
123 118
124 DictionaryValue value; 119 DictionaryValue value;
125 value.SetInteger("integers", 4); 120 value.SetInteger("integers", 4);
126 value.Set("strings", strings_value); 121 value.Set("strings", strings_value);
127 122
128 ChoiceType out; 123 ChoiceType out;
129 ASSERT_TRUE(ChoiceType::Populate(value, &out)); 124 ASSERT_TRUE(ChoiceType::Populate(value, &out));
130 EXPECT_EQ(ChoiceType::INTEGERS_INTEGER, out.integers_type); 125 ASSERT_TRUE(out.integers.as_integer.get());
131 ASSERT_TRUE(out.integers_integer.get()); 126 EXPECT_FALSE(out.integers.as_array.get());
132 EXPECT_FALSE(out.integers_array.get()); 127 EXPECT_EQ(4, *out.integers.as_integer);
133 EXPECT_EQ(4, *out.integers_integer);
134 128
135 EXPECT_EQ(ChoiceType::STRINGS_ARRAY, out.strings_type); 129 EXPECT_FALSE(out.strings->as_string.get());
136 EXPECT_FALSE(out.strings_string.get()); 130 ASSERT_TRUE(out.strings->as_array.get());
137 ASSERT_TRUE(out.strings_array.get()); 131 EXPECT_EQ(strings, *out.strings->as_array);
138 EXPECT_EQ(strings, *out.strings_array);
139 } 132 }
140 133
141 TEST(JsonSchemaCompilerChoicesTest, ChoiceTypeToValue) { 134 TEST(JsonSchemaCompilerChoicesTest, ChoiceTypeToValue) {
142 ListValue* strings_value = new ListValue(); 135 ListValue* strings_value = new ListValue();
143 strings_value->Append(Value::CreateStringValue("list")); 136 strings_value->Append(Value::CreateStringValue("list"));
144 strings_value->Append(Value::CreateStringValue("of")); 137 strings_value->Append(Value::CreateStringValue("of"));
145 strings_value->Append(Value::CreateStringValue("strings")); 138 strings_value->Append(Value::CreateStringValue("strings"));
146 139
147 DictionaryValue value; 140 DictionaryValue value;
148 value.SetInteger("integers", 5); 141 value.SetInteger("integers", 5);
149 value.Set("strings", strings_value); 142 value.Set("strings", strings_value);
150 143
151 ChoiceType out; 144 ChoiceType out;
152 ASSERT_TRUE(ChoiceType::Populate(value, &out)); 145 ASSERT_TRUE(ChoiceType::Populate(value, &out));
153 146
154 EXPECT_TRUE(value.Equals(out.ToValue().get())); 147 EXPECT_TRUE(value.Equals(out.ToValue().get()));
155 } 148 }
156 149
157 TEST(JsonSchemaCompilerChoicesTest, ReturnChoices) { 150 TEST(JsonSchemaCompilerChoicesTest, ReturnChoices) {
158 { 151 {
159 std::vector<int> integers; 152 ReturnChoices::Results::Result results;
160 integers.push_back(1); 153 results.as_array.reset(new std::vector<int>());
161 integers.push_back(2); 154 results.as_array->push_back(1);
162 scoped_ptr<ListValue> array_results = 155 results.as_array->push_back(2);
163 ReturnChoices::Results::Create(integers);
164 156
165 ListValue expected; 157 scoped_ptr<base::Value> results_value = results.ToValue();
166 ListValue* expected_argument = new ListValue(); 158 ASSERT_TRUE(results_value);
167 expected_argument->Append(Value::CreateIntegerValue(1)); 159
168 expected_argument->Append(Value::CreateIntegerValue(2)); 160 base::ListValue expected;
169 expected.Append(expected_argument); 161 expected.AppendInteger(1);
170 EXPECT_TRUE(array_results->Equals(&expected)); 162 expected.AppendInteger(2);
163
164 EXPECT_TRUE(expected.Equals(results_value.get()));
171 } 165 }
172 { 166 {
173 scoped_ptr<ListValue> integer_results = ReturnChoices::Results::Create(5); 167 ReturnChoices::Results::Result results;
174 ListValue expected; 168 results.as_integer.reset(new int(5));
175 expected.Append(Value::CreateIntegerValue(5)); 169
176 EXPECT_TRUE(integer_results->Equals(&expected)); 170 scoped_ptr<base::Value> results_value = results.ToValue();
171 ASSERT_TRUE(results_value);
172
173 base::FundamentalValue expected(5);
174
175 EXPECT_TRUE(expected.Equals(results_value.get()));
177 } 176 }
178 } 177 }
OLDNEW
« no previous file with comments | « tools/json_schema_compiler/test/arrays_unittest.cc ('k') | tools/json_schema_compiler/test/enums.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698