| OLD | NEW |
| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "tools/json_schema_compiler/test/test_util.h" | 13 #include "tools/json_schema_compiler/test/test_util.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 using namespace test::api::choices; | 17 using namespace test::api::choices; |
| 18 using json_schema_compiler::test_util::Dictionary; | 18 using json_schema_compiler::test_util::Dictionary; |
| 19 using json_schema_compiler::test_util::List; | 19 using json_schema_compiler::test_util::List; |
| 20 using json_schema_compiler::test_util::ReadJson; | 20 using json_schema_compiler::test_util::ReadJson; |
| 21 using json_schema_compiler::test_util::Vector; | 21 using json_schema_compiler::test_util::Vector; |
| 22 | 22 |
| 23 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) { | 23 TEST(JsonSchemaCompilerChoicesTest, TakesIntegersParamsCreate) { |
| 24 { | 24 { |
| 25 std::unique_ptr<TakesIntegers::Params> params( | 25 std::unique_ptr<TakesIntegers::Params> params( |
| 26 TakesIntegers::Params::Create(*List(new base::FundamentalValue(true)))); | 26 TakesIntegers::Params::Create(*List(new base::Value(true)))); |
| 27 EXPECT_FALSE(params); | 27 EXPECT_FALSE(params); |
| 28 } | 28 } |
| 29 { | 29 { |
| 30 std::unique_ptr<TakesIntegers::Params> params( | 30 std::unique_ptr<TakesIntegers::Params> params( |
| 31 TakesIntegers::Params::Create(*List(new base::FundamentalValue(6)))); | 31 TakesIntegers::Params::Create(*List(new base::Value(6)))); |
| 32 ASSERT_TRUE(params); | 32 ASSERT_TRUE(params); |
| 33 EXPECT_FALSE(params->nums.as_integers); | 33 EXPECT_FALSE(params->nums.as_integers); |
| 34 EXPECT_EQ(6, *params->nums.as_integer); | 34 EXPECT_EQ(6, *params->nums.as_integer); |
| 35 } | 35 } |
| 36 { | 36 { |
| 37 std::unique_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create( | 37 std::unique_ptr<TakesIntegers::Params> params(TakesIntegers::Params::Create( |
| 38 *List(List(new base::FundamentalValue(2), new base::FundamentalValue(6), | 38 *List(List(new base::Value(2), new base::Value(6), |
| 39 new base::FundamentalValue(8)) | 39 new base::Value(8)) |
| 40 .release()))); | 40 .release()))); |
| 41 ASSERT_TRUE(params); | 41 ASSERT_TRUE(params); |
| 42 ASSERT_TRUE(params->nums.as_integers); | 42 ASSERT_TRUE(params->nums.as_integers); |
| 43 EXPECT_EQ(Vector(2, 6, 8), *params->nums.as_integers); | 43 EXPECT_EQ(Vector(2, 6, 8), *params->nums.as_integers); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) { | 47 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreate) { |
| 48 { | 48 { |
| 49 std::unique_ptr<ObjectWithChoices::Params> params( | 49 std::unique_ptr<ObjectWithChoices::Params> params( |
| 50 ObjectWithChoices::Params::Create(*List( | 50 ObjectWithChoices::Params::Create(*List( |
| 51 Dictionary("strings", new base::StringValue("asdf")).release()))); | 51 Dictionary("strings", new base::StringValue("asdf")).release()))); |
| 52 ASSERT_TRUE(params); | 52 ASSERT_TRUE(params); |
| 53 EXPECT_FALSE(params->string_info.strings.as_strings); | 53 EXPECT_FALSE(params->string_info.strings.as_strings); |
| 54 EXPECT_EQ("asdf", *params->string_info.strings.as_string); | 54 EXPECT_EQ("asdf", *params->string_info.strings.as_string); |
| 55 EXPECT_FALSE(params->string_info.integers); | 55 EXPECT_FALSE(params->string_info.integers); |
| 56 } | 56 } |
| 57 { | 57 { |
| 58 std::unique_ptr<ObjectWithChoices::Params> params( | 58 std::unique_ptr<ObjectWithChoices::Params> params( |
| 59 ObjectWithChoices::Params::Create( | 59 ObjectWithChoices::Params::Create( |
| 60 *List(Dictionary("strings", new base::StringValue("asdf"), | 60 *List(Dictionary("strings", new base::StringValue("asdf"), |
| 61 "integers", new base::FundamentalValue(6)) | 61 "integers", new base::Value(6)) |
| 62 .release()))); | 62 .release()))); |
| 63 ASSERT_TRUE(params); | 63 ASSERT_TRUE(params); |
| 64 EXPECT_FALSE(params->string_info.strings.as_strings); | 64 EXPECT_FALSE(params->string_info.strings.as_strings); |
| 65 EXPECT_EQ("asdf", *params->string_info.strings.as_string); | 65 EXPECT_EQ("asdf", *params->string_info.strings.as_string); |
| 66 ASSERT_TRUE(params->string_info.integers); | 66 ASSERT_TRUE(params->string_info.integers); |
| 67 EXPECT_FALSE(params->string_info.integers->as_integers); | 67 EXPECT_FALSE(params->string_info.integers->as_integers); |
| 68 EXPECT_EQ(6, *params->string_info.integers->as_integer); | 68 EXPECT_EQ(6, *params->string_info.integers->as_integer); |
| 69 } | 69 } |
| 70 } | 70 } |
| 71 | 71 |
| 72 // TODO(kalman): Clean up the rest of these tests to use the | 72 // TODO(kalman): Clean up the rest of these tests to use the |
| 73 // Vector/List/Dictionary helpers. | 73 // Vector/List/Dictionary helpers. |
| 74 | 74 |
| 75 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) { | 75 TEST(JsonSchemaCompilerChoicesTest, ObjectWithChoicesParamsCreateFail) { |
| 76 { | 76 { |
| 77 std::unique_ptr<base::DictionaryValue> object_param( | 77 std::unique_ptr<base::DictionaryValue> object_param( |
| 78 new base::DictionaryValue()); | 78 new base::DictionaryValue()); |
| 79 object_param->SetWithoutPathExpansion("strings", | 79 object_param->SetWithoutPathExpansion("strings", |
| 80 new base::FundamentalValue(5)); | 80 new base::Value(5)); |
| 81 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); | 81 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); |
| 82 params_value->Append(std::move(object_param)); | 82 params_value->Append(std::move(object_param)); |
| 83 std::unique_ptr<ObjectWithChoices::Params> params( | 83 std::unique_ptr<ObjectWithChoices::Params> params( |
| 84 ObjectWithChoices::Params::Create(*params_value)); | 84 ObjectWithChoices::Params::Create(*params_value)); |
| 85 EXPECT_FALSE(params.get()); | 85 EXPECT_FALSE(params.get()); |
| 86 } | 86 } |
| 87 { | 87 { |
| 88 std::unique_ptr<base::DictionaryValue> object_param( | 88 std::unique_ptr<base::DictionaryValue> object_param( |
| 89 new base::DictionaryValue()); | 89 new base::DictionaryValue()); |
| 90 object_param->SetWithoutPathExpansion("strings", | 90 object_param->SetWithoutPathExpansion("strings", |
| 91 new base::StringValue("asdf")); | 91 new base::StringValue("asdf")); |
| 92 object_param->SetWithoutPathExpansion("integers", | 92 object_param->SetWithoutPathExpansion("integers", |
| 93 new base::StringValue("asdf")); | 93 new base::StringValue("asdf")); |
| 94 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); | 94 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); |
| 95 params_value->Append(std::move(object_param)); | 95 params_value->Append(std::move(object_param)); |
| 96 std::unique_ptr<ObjectWithChoices::Params> params( | 96 std::unique_ptr<ObjectWithChoices::Params> params( |
| 97 ObjectWithChoices::Params::Create(*params_value)); | 97 ObjectWithChoices::Params::Create(*params_value)); |
| 98 EXPECT_FALSE(params.get()); | 98 EXPECT_FALSE(params.get()); |
| 99 } | 99 } |
| 100 { | 100 { |
| 101 std::unique_ptr<base::DictionaryValue> object_param( | 101 std::unique_ptr<base::DictionaryValue> object_param( |
| 102 new base::DictionaryValue()); | 102 new base::DictionaryValue()); |
| 103 object_param->SetWithoutPathExpansion("integers", | 103 object_param->SetWithoutPathExpansion("integers", |
| 104 new base::FundamentalValue(6)); | 104 new base::Value(6)); |
| 105 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); | 105 std::unique_ptr<base::ListValue> params_value(new base::ListValue()); |
| 106 params_value->Append(std::move(object_param)); | 106 params_value->Append(std::move(object_param)); |
| 107 std::unique_ptr<ObjectWithChoices::Params> params( | 107 std::unique_ptr<ObjectWithChoices::Params> params( |
| 108 ObjectWithChoices::Params::Create(*params_value)); | 108 ObjectWithChoices::Params::Create(*params_value)); |
| 109 EXPECT_FALSE(params.get()); | 109 EXPECT_FALSE(params.get()); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 TEST(JsonSchemaCompilerChoicesTest, PopulateChoiceType) { | 113 TEST(JsonSchemaCompilerChoicesTest, PopulateChoiceType) { |
| 114 std::vector<std::string> strings = Vector(std::string("list"), | 114 std::vector<std::string> strings = Vector(std::string("list"), |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 164 |
| 165 EXPECT_TRUE(expected.Equals(results_value.get())); | 165 EXPECT_TRUE(expected.Equals(results_value.get())); |
| 166 } | 166 } |
| 167 { | 167 { |
| 168 ReturnChoices::Results::Result results; | 168 ReturnChoices::Results::Result results; |
| 169 results.as_integer.reset(new int(5)); | 169 results.as_integer.reset(new int(5)); |
| 170 | 170 |
| 171 std::unique_ptr<base::Value> results_value = results.ToValue(); | 171 std::unique_ptr<base::Value> results_value = results.ToValue(); |
| 172 ASSERT_TRUE(results_value); | 172 ASSERT_TRUE(results_value); |
| 173 | 173 |
| 174 base::FundamentalValue expected(5); | 174 base::Value expected(5); |
| 175 | 175 |
| 176 EXPECT_TRUE(expected.Equals(results_value.get())); | 176 EXPECT_TRUE(expected.Equals(results_value.get())); |
| 177 } | 177 } |
| 178 } | 178 } |
| 179 | 179 |
| 180 TEST(JsonSchemaCompilerChoicesTest, NestedChoices) { | 180 TEST(JsonSchemaCompilerChoicesTest, NestedChoices) { |
| 181 // These test both ToValue and FromValue for every legitimate configuration of | 181 // These test both ToValue and FromValue for every legitimate configuration of |
| 182 // NestedChoices. | 182 // NestedChoices. |
| 183 { | 183 { |
| 184 // The plain integer choice. | 184 // The plain integer choice. |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 obj->as_choice2->as_choice_types.get(); | 291 obj->as_choice2->as_choice_types.get(); |
| 292 // Bleh too much effort to test everything. | 292 // Bleh too much effort to test everything. |
| 293 ASSERT_EQ(2u, choice_types->size()); | 293 ASSERT_EQ(2u, choice_types->size()); |
| 294 } | 294 } |
| 295 | 295 |
| 296 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); | 296 EXPECT_TRUE(base::Value::Equals(value.get(), obj->ToValue().get())); |
| 297 } | 297 } |
| 298 } | 298 } |
| 299 | 299 |
| 300 } // namespace | 300 } // namespace |
| OLD | NEW |