blob: 67c06d5a0f1d8cd41ce4a6dfe2eb78ee608682a7 [file] [log] [blame]
[email protected]712eca0f2012-02-21 01:13:071// 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
[email protected]712eca0f2012-02-21 01:13:075#include "tools/json_schema_compiler/test/crossref.h"
6
dchenga500b692016-04-08 19:55:427#include <memory>
dcheng5d090492016-06-09 17:53:348#include <utility>
dchenga500b692016-04-08 19:55:429
jdoerriecb205a52017-06-08 16:16:4410#include "base/memory/ptr_util.h"
11#include "base/values.h"
[email protected]712eca0f2012-02-21 01:13:0712#include "testing/gtest/include/gtest/gtest.h"
dchenga500b692016-04-08 19:55:4213#include "tools/json_schema_compiler/test/simple_api.h"
[email protected]712eca0f2012-02-21 01:13:0714
kalman16a91332015-04-24 00:33:2915using namespace test::api;
[email protected]712eca0f2012-02-21 01:13:0716
17namespace {
18
dchenga500b692016-04-08 19:55:4219std::unique_ptr<base::DictionaryValue> CreateTestTypeValue() {
jdoerriecb205a52017-06-08 16:16:4420 auto value = base::MakeUnique<base::DictionaryValue>();
21 value->SetDouble("number", 1.1);
22 value->SetInteger("integer", 4);
23 value->SetString("string", "bling");
24 value->SetBoolean("boolean", true);
dchenga500b692016-04-08 19:55:4225 return value;
[email protected]712eca0f2012-02-21 01:13:0726}
27
28} // namespace
29
kalman16a91332015-04-24 00:33:2930TEST(JsonSchemaCompilerCrossrefTest, CrossrefTypePopulateAndToValue) {
31 base::DictionaryValue crossref_orig;
jdoerriecb205a52017-06-08 16:16:4432 crossref_orig.Set("testType", CreateTestTypeValue());
33 crossref_orig.SetString("testEnumRequired", "one");
34 crossref_orig.SetString("testEnumOptional", "two");
kalman16a91332015-04-24 00:33:2935
36 // Test Populate of the value --> compiled type.
37 crossref::CrossrefType crossref_type;
38 ASSERT_TRUE(crossref::CrossrefType::Populate(crossref_orig, &crossref_type));
39 EXPECT_EQ(1.1, crossref_type.test_type.number);
40 EXPECT_EQ(4, crossref_type.test_type.integer);
41 EXPECT_EQ("bling", crossref_type.test_type.string);
42 EXPECT_EQ(true, crossref_type.test_type.boolean);
43 EXPECT_EQ(simple_api::TEST_ENUM_ONE, crossref_type.test_enum_required);
44 EXPECT_EQ(simple_api::TEST_ENUM_TWO, crossref_type.test_enum_optional);
45 EXPECT_EQ(simple_api::TEST_ENUM_NONE, crossref_type.test_enum_optional_extra);
46
47 // Test ToValue of the compiled type --> value.
dchenga500b692016-04-08 19:55:4248 std::unique_ptr<base::DictionaryValue> crossref_value =
49 crossref_type.ToValue();
kalman16a91332015-04-24 00:33:2950 ASSERT_TRUE(crossref_value);
51 EXPECT_TRUE(crossref_orig.Equals(crossref_value.get()));
[email protected]25cbf6012012-02-28 05:51:4452}
53
[email protected]712eca0f2012-02-21 01:13:0754TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamCreate) {
dchenga500b692016-04-08 19:55:4255 std::unique_ptr<base::ListValue> params_value(new base::ListValue());
dcheng5d090492016-06-09 17:53:3456 params_value->Append(CreateTestTypeValue());
dchenga500b692016-04-08 19:55:4257 std::unique_ptr<crossref::TestTypeOptionalParam::Params> params(
kalman16a91332015-04-24 00:33:2958 crossref::TestTypeOptionalParam::Params::Create(*params_value));
[email protected]712eca0f2012-02-21 01:13:0759 EXPECT_TRUE(params.get());
60 EXPECT_TRUE(params->test_type.get());
61 EXPECT_TRUE(
kalman16a91332015-04-24 00:33:2962 CreateTestTypeValue()->Equals(params->test_type->ToValue().get()));
[email protected]712eca0f2012-02-21 01:13:0763}
64
65TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamFail) {
dchenga500b692016-04-08 19:55:4266 std::unique_ptr<base::ListValue> params_value(new base::ListValue());
67 std::unique_ptr<base::DictionaryValue> test_type_value =
68 CreateTestTypeValue();
[email protected]712eca0f2012-02-21 01:13:0769 test_type_value->RemoveWithoutPathExpansion("number", NULL);
dcheng5d090492016-06-09 17:53:3470 params_value->Append(std::move(test_type_value));
dchenga500b692016-04-08 19:55:4271 std::unique_ptr<crossref::TestTypeOptionalParam::Params> params(
kalman16a91332015-04-24 00:33:2972 crossref::TestTypeOptionalParam::Params::Create(*params_value));
[email protected]712eca0f2012-02-21 01:13:0773 EXPECT_FALSE(params.get());
74}
75
76TEST(JsonSchemaCompilerCrossrefTest, GetTestType) {
dchenga500b692016-04-08 19:55:4277 std::unique_ptr<base::DictionaryValue> value = CreateTestTypeValue();
78 std::unique_ptr<simple_api::TestType> test_type(new simple_api::TestType());
kalman16a91332015-04-24 00:33:2979 EXPECT_TRUE(simple_api::TestType::Populate(*value, test_type.get()));
[email protected]b741e8f2012-07-16 21:47:2480
dchenga500b692016-04-08 19:55:4281 std::unique_ptr<base::ListValue> results =
kalman16a91332015-04-24 00:33:2982 crossref::GetTestType::Results::Create(*test_type);
[email protected]a644f1e22013-12-24 15:31:2183 base::DictionaryValue* result_dict = NULL;
[email protected]b741e8f2012-07-16 21:47:2484 results->GetDictionary(0, &result_dict);
85 EXPECT_TRUE(value->Equals(result_dict));
[email protected]712eca0f2012-02-21 01:13:0786}
[email protected]25cbf6012012-02-28 05:51:4487
88TEST(JsonSchemaCompilerCrossrefTest, TestTypeInObjectParamsCreate) {
89 {
jdoerriecb205a52017-06-08 16:16:4490 auto params_value = base::MakeUnique<base::ListValue>();
91 auto param_object_value = base::MakeUnique<base::DictionaryValue>();
92 param_object_value->Set("testType", CreateTestTypeValue());
93 param_object_value->SetBoolean("boolean", true);
dcheng5d090492016-06-09 17:53:3494 params_value->Append(std::move(param_object_value));
dchenga500b692016-04-08 19:55:4295 std::unique_ptr<crossref::TestTypeInObject::Params> params(
kalman16a91332015-04-24 00:33:2996 crossref::TestTypeInObject::Params::Create(*params_value));
[email protected]25cbf6012012-02-28 05:51:4497 EXPECT_TRUE(params.get());
98 EXPECT_TRUE(params->param_object.test_type.get());
99 EXPECT_TRUE(params->param_object.boolean);
kalman16a91332015-04-24 00:33:29100 EXPECT_TRUE(CreateTestTypeValue()->Equals(
101 params->param_object.test_type->ToValue().get()));
[email protected]25cbf6012012-02-28 05:51:44102 }
103 {
jdoerriecb205a52017-06-08 16:16:44104 auto params_value = base::MakeUnique<base::ListValue>();
105 auto param_object_value = base::MakeUnique<base::DictionaryValue>();
106 param_object_value->SetBoolean("boolean", true);
dcheng5d090492016-06-09 17:53:34107 params_value->Append(std::move(param_object_value));
dchenga500b692016-04-08 19:55:42108 std::unique_ptr<crossref::TestTypeInObject::Params> params(
kalman16a91332015-04-24 00:33:29109 crossref::TestTypeInObject::Params::Create(*params_value));
[email protected]25cbf6012012-02-28 05:51:44110 EXPECT_TRUE(params.get());
111 EXPECT_FALSE(params->param_object.test_type.get());
112 EXPECT_TRUE(params->param_object.boolean);
113 }
114 {
jdoerriecb205a52017-06-08 16:16:44115 auto params_value = base::MakeUnique<base::ListValue>();
116 auto param_object_value = base::MakeUnique<base::DictionaryValue>();
117 param_object_value->SetString("testType", "invalid");
118 param_object_value->SetBoolean("boolean", true);
dcheng5d090492016-06-09 17:53:34119 params_value->Append(std::move(param_object_value));
dchenga500b692016-04-08 19:55:42120 std::unique_ptr<crossref::TestTypeInObject::Params> params(
kalman16a91332015-04-24 00:33:29121 crossref::TestTypeInObject::Params::Create(*params_value));
[email protected]25cbf6012012-02-28 05:51:44122 EXPECT_FALSE(params.get());
123 }
124 {
jdoerriecb205a52017-06-08 16:16:44125 auto params_value = base::MakeUnique<base::ListValue>();
126 auto param_object_value = base::MakeUnique<base::DictionaryValue>();
127 param_object_value->Set("testType", CreateTestTypeValue());
dcheng5d090492016-06-09 17:53:34128 params_value->Append(std::move(param_object_value));
dchenga500b692016-04-08 19:55:42129 std::unique_ptr<crossref::TestTypeInObject::Params> params(
kalman16a91332015-04-24 00:33:29130 crossref::TestTypeInObject::Params::Create(*params_value));
[email protected]25cbf6012012-02-28 05:51:44131 EXPECT_FALSE(params.get());
132 }
133}