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 3 // found in the LICENSE file. 4 5 #include "tools/json_schema_compiler/test/choices.h" 6 7 #include "base/strings/string_piece.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "tools/json_schema_compiler/test/test_util.h" 10 11 namespace { 12 13 using namespace test::api::choices; 14 using json_schema_compiler::test_util::Dictionary; 15 using json_schema_compiler::test_util::List; 16 using json_schema_compiler::test_util::ReadJson; 17 using json_schema_compiler::test_util::Vector; 18 19 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) { 20 { 21 scoped_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create( 22 *List(Value::CreateBooleanValue(true)))); 23 EXPECT_FALSE(params); 24 } 25 { 26 scoped_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create( 27 *List(Value::CreateIntegerValue(6)))); 28 ASSERT_TRUE(params); 29 EXPECT_FALSE(params->nums.as_integers); 30 EXPECT_EQ(6, *params->nums.as_integer); 31 } 32 { 33 scoped_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create( 34 *List(List(Value::CreateIntegerValue(2), 35 Value::CreateIntegerValue(6), 36 Value::CreateIntegerValue(8)).release()))); 37 ASSERT_TRUE(params); 38 ASSERT_TRUE(params->nums.as_integers); 39 EXPECT_EQ(Vector(2, 6, 8), *params->nums.as_integers); 40 } 41 } 42 43 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) { 44 { 45 scoped_ptr<ObjectWithChoices::Params> params( 46 ObjectWithChoices::Params::Create(*List( 47 Dictionary("strings", new base::StringValue("asdf")).release()))); 48 ASSERT_TRUE(params); 49 EXPECT_FALSE(params->string_info.strings.as_strings); 50 EXPECT_EQ("asdf", *params->string_info.strings.as_string); 51 EXPECT_FALSE(params->string_info.integers); 52 } 53 { 54 scoped_ptr<ObjectWithChoices::Params> params( 55 ObjectWithChoices::Params::Create(*List( 56 Dictionary("strings", new base::StringValue("asdf"), 57 "integers", new base::FundamentalValue(6)).release()))); 58 ASSERT_TRUE(params); 59 EXPECT_FALSE(params->string_info.strings.as_strings); 60 EXPECT_EQ("asdf", *params->string_info.strings.as_string); 61 ASSERT_TRUE(params->string_info.integers); 62 EXPECT_FALSE(params->string_info.integers->as_integers); 63 EXPECT_EQ(6, *params->string_info.integers->as_integer); 64 } 65 } 66 67 // TODO(kalman): Clean up the rest of these tests to use the 68 // Vector/List/Dictionary helpers. 69 70 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) { 71 { 72 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 73 object_param->SetWithoutPathExpansion("strings", 74 Value::CreateIntegerValue(5)); 75 scoped_ptr<ListValue> params_value(new ListValue()); 76 params_value->Append(object_param.release()); 77 scoped_ptr<ObjectWithChoices::Params> params( 78 ObjectWithChoices::Params::Create(*params_value)); 79 EXPECT_FALSE(params.get()); 80 } 81 { 82 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 83 object_param->SetWithoutPathExpansion("strings", 84 Value::CreateStringValue("asdf")); 85 object_param->SetWithoutPathExpansion("integers", 86 Value::CreateStringValue("asdf")); 87 scoped_ptr<ListValue> params_value(new ListValue()); 88 params_value->Append(object_param.release()); 89 scoped_ptr<ObjectWithChoices::Params> params( 90 ObjectWithChoices::Params::Create(*params_value)); 91 EXPECT_FALSE(params.get()); 92 } 93 { 94 scoped_ptr<DictionaryValue> object_param(new DictionaryValue()); 95 object_param->SetWithoutPathExpansion("integers", 96 Value::CreateIntegerValue(6)); 97 scoped_ptr<ListValue> params_value(new ListValue()); 98 params_value->Append(object_param.release()); 99 scoped_ptr<ObjectWithChoices::Params> params( 100 ObjectWithChoices::Params::Create(*params_value)); 101 EXPECT_FALSE(params.get()); 102 } 103 } 104 105 TEST(JsonSchemaCompilerChoicesTest, PopulateChoiceType) { 106 std::vector<std::string> strings = Vector(std::string("list"), 107 std::string("of"), 108 std::string("strings")); 109 110 ListValue* strings_value = new ListValue(); 111 for (size_t i = 0; i < strings.size(); ++i) 112 strings_value->Append(Value::CreateStringValue(strings[i])); 113 114 DictionaryValue value; 115 value.SetInteger("integers", 4); 116 value.Set("strings", strings_value); 117 118 ChoiceType out; 119 ASSERT_TRUE(ChoiceType::Populate(value, &out)); 120 ASSERT_TRUE(out.integers.as_integer.get()); 121 EXPECT_FALSE(out.integers.as_integers.get()); 122 EXPECT_EQ(4, *out.integers.as_integer); 123 124 EXPECT_FALSE(out.strings->as_string.get()); 125 ASSERT_TRUE(out.strings->as_strings.get()); 126 EXPECT_EQ(strings, *out.strings->as_strings); 127 } 128 129 TEST(JsonSchemaCompilerChoicesTest, ChoiceTypeToValue) { 130 ListValue* strings_value = new ListValue(); 131 strings_value->Append(Value::CreateStringValue("list")); 132 strings_value->Append(Value::CreateStringValue("of")); 133 strings_value->Append(Value::CreateStringValue("strings")); 134 135 DictionaryValue value; 136 value.SetInteger("integers", 5); 137 value.Set("strings", strings_value); 138 139 ChoiceType out; 140 ASSERT_TRUE(ChoiceType::Populate(value, &out)); 141 142 EXPECT_TRUE(value.Equals(out.ToValue().get())); 143 } 144 145 TEST(JsonSchemaCompilerChoicesTest, ReturnChoices) { 146 { 147 ReturnChoices::Results::Result results; 148 results.as_integers.reset(new std::vector<int>(Vector(1, 2))); 149 150 scoped_ptr<base::Value> results_value = results.ToValue(); 151 ASSERT_TRUE(results_value); 152 153 base::ListValue expected; 154 expected.AppendInteger(1); 155 expected.AppendInteger(2); 156 157 EXPECT_TRUE(expected.Equals(results_value.get())); 158 } 159 { 160 ReturnChoices::Results::Result results; 161 results.as_integer.reset(new int(5)); 162 163 scoped_ptr<base::Value> results_value = results.ToValue(); 164 ASSERT_TRUE(results_value); 165 166 base::FundamentalValue expected(5); 167 168 EXPECT_TRUE(expected.Equals(results_value.get())); 169 } 170 } 171 172 TEST(JsonSchemaCompilerChoicesTest, NestedChoices) { 173 // These test both ToValue and FromValue for every legitimate configuration of 174 // NestedChoices. 175 { 176 // The plain integer choice. 177 scoped_ptr<base::Value> value = ReadJson("42"); 178 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 179 180 ASSERT_TRUE(obj); 181 ASSERT_TRUE(obj->as_integer); 182 EXPECT_FALSE(obj->as_choice1); 183 EXPECT_FALSE(obj->as_choice2); 184 EXPECT_EQ(42, *obj->as_integer); 185 186 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 187 } 188 189 { 190 // The string choice within the first choice. 191 scoped_ptr<base::Value> value = ReadJson("\"foo\""); 192 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 193 194 ASSERT_TRUE(obj); 195 EXPECT_FALSE(obj->as_integer); 196 ASSERT_TRUE(obj->as_choice1); 197 EXPECT_FALSE(obj->as_choice2); 198 ASSERT_TRUE(obj->as_choice1->as_string); 199 EXPECT_FALSE(obj->as_choice1->as_boolean); 200 EXPECT_EQ("foo", *obj->as_choice1->as_string); 201 202 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 203 } 204 205 { 206 // The boolean choice within the first choice. 207 scoped_ptr<base::Value> value = ReadJson("true"); 208 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 209 210 ASSERT_TRUE(obj); 211 EXPECT_FALSE(obj->as_integer); 212 ASSERT_TRUE(obj->as_choice1); 213 EXPECT_FALSE(obj->as_choice2); 214 EXPECT_FALSE(obj->as_choice1->as_string); 215 ASSERT_TRUE(obj->as_choice1->as_boolean); 216 EXPECT_TRUE(*obj->as_choice1->as_boolean); 217 218 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 219 } 220 221 { 222 // The double choice within the second choice. 223 scoped_ptr<base::Value> value = ReadJson("42.0"); 224 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 225 226 ASSERT_TRUE(obj); 227 EXPECT_FALSE(obj->as_integer); 228 EXPECT_FALSE(obj->as_choice1); 229 ASSERT_TRUE(obj->as_choice2); 230 ASSERT_TRUE(obj->as_choice2->as_double); 231 EXPECT_FALSE(obj->as_choice2->as_choice_type); 232 EXPECT_FALSE(obj->as_choice2->as_choice_types); 233 EXPECT_EQ(42.0, *obj->as_choice2->as_double); 234 235 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 236 } 237 238 { 239 // The ChoiceType choice within the second choice. 240 scoped_ptr<base::Value> value = ReadJson( 241 "{\"integers\": [1, 2], \"strings\": \"foo\"}"); 242 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 243 244 ASSERT_TRUE(obj); 245 EXPECT_FALSE(obj->as_integer); 246 EXPECT_FALSE(obj->as_choice1); 247 ASSERT_TRUE(obj->as_choice2); 248 EXPECT_FALSE(obj->as_choice2->as_double); 249 ASSERT_TRUE(obj->as_choice2->as_choice_type); 250 EXPECT_FALSE(obj->as_choice2->as_choice_types); 251 { 252 ChoiceType* choice_type = obj->as_choice2->as_choice_type.get(); 253 ASSERT_TRUE(choice_type->integers.as_integers); 254 EXPECT_FALSE(choice_type->integers.as_integer); 255 EXPECT_EQ(Vector(1, 2), *choice_type->integers.as_integers); 256 ASSERT_TRUE(choice_type->strings); 257 EXPECT_FALSE(choice_type->strings->as_strings); 258 ASSERT_TRUE(choice_type->strings->as_string); 259 EXPECT_EQ("foo", *choice_type->strings->as_string); 260 } 261 262 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 263 } 264 265 { 266 // The array of ChoiceTypes within the second choice. 267 scoped_ptr<base::Value> value = ReadJson( 268 "[" 269 " {\"integers\": [1, 2], \"strings\": \"foo\"}," 270 " {\"integers\": 3, \"strings\": [\"bar\", \"baz\"]}" 271 "]"); 272 scoped_ptr<NestedChoice> obj = NestedChoice::FromValue(*value); 273 274 ASSERT_TRUE(obj); 275 EXPECT_FALSE(obj->as_integer); 276 EXPECT_FALSE(obj->as_choice1); 277 ASSERT_TRUE(obj->as_choice2); 278 EXPECT_FALSE(obj->as_choice2->as_double); 279 EXPECT_FALSE(obj->as_choice2->as_choice_type); 280 ASSERT_TRUE(obj->as_choice2->as_choice_types); 281 { 282 std::vector<linked_ptr<ChoiceType> >* choice_types = 283 obj->as_choice2->as_choice_types.get(); 284 // Bleh too much effort to test everything. 285 ASSERT_EQ(2u, choice_types->size()); 286 } 287 288 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); 289 } 290 } 291 292 } // namespace 293