crossref_unittest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/simple_api.h"
6#include "tools/json_schema_compiler/test/crossref.h"
7
8#include "testing/gtest/include/gtest/gtest.h"
9
10using namespace test::api::crossref;
11
12namespace {
13
14static scoped_ptr<base::DictionaryValue> CreateTestTypeDictionary() {
15  base::DictionaryValue* value(new base::DictionaryValue());
16  value->SetWithoutPathExpansion("number", base::Value::CreateDoubleValue(1.1));
17  value->SetWithoutPathExpansion("integer", new base::FundamentalValue(4));
18  value->SetWithoutPathExpansion("string",
19                                 base::Value::CreateStringValue("bling"));
20  value->SetWithoutPathExpansion("boolean", new base::FundamentalValue(true));
21  return scoped_ptr<base::DictionaryValue>(value);
22}
23
24}  // namespace
25
26TEST(JsonSchemaCompilerCrossrefTest, CrossrefTypePopulate) {
27  scoped_ptr<CrossrefType> crossref_type(new CrossrefType());
28  scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue());
29  value->Set("testType", CreateTestTypeDictionary().release());
30  EXPECT_TRUE(CrossrefType::Populate(*value, crossref_type.get()));
31  EXPECT_TRUE(crossref_type->test_type.get());
32  EXPECT_TRUE(CreateTestTypeDictionary()->Equals(
33        crossref_type->test_type->ToValue().get()));
34  EXPECT_TRUE(value->Equals(crossref_type->ToValue().get()));
35}
36
37TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamCreate) {
38  scoped_ptr<base::ListValue> params_value(new base::ListValue());
39  params_value->Append(CreateTestTypeDictionary().release());
40  scoped_ptr<TestTypeOptionalParam::Params> params(
41      TestTypeOptionalParam::Params::Create(*params_value));
42  EXPECT_TRUE(params.get());
43  EXPECT_TRUE(params->test_type.get());
44  EXPECT_TRUE(
45      CreateTestTypeDictionary()->Equals(params->test_type->ToValue().get()));
46}
47
48TEST(JsonSchemaCompilerCrossrefTest, TestTypeOptionalParamFail) {
49  scoped_ptr<base::ListValue> params_value(new base::ListValue());
50  scoped_ptr<base::DictionaryValue> test_type_value =
51      CreateTestTypeDictionary();
52  test_type_value->RemoveWithoutPathExpansion("number", NULL);
53  params_value->Append(test_type_value.release());
54  scoped_ptr<TestTypeOptionalParam::Params> params(
55      TestTypeOptionalParam::Params::Create(*params_value));
56  EXPECT_FALSE(params.get());
57}
58
59TEST(JsonSchemaCompilerCrossrefTest, GetTestType) {
60  scoped_ptr<base::DictionaryValue> value = CreateTestTypeDictionary();
61  scoped_ptr<test::api::simple_api::TestType> test_type(
62      new test::api::simple_api::TestType());
63  EXPECT_TRUE(
64      test::api::simple_api::TestType::Populate(*value, test_type.get()));
65
66  scoped_ptr<base::ListValue> results =
67      GetTestType::Results::Create(*test_type);
68  base::DictionaryValue* result_dict = NULL;
69  results->GetDictionary(0, &result_dict);
70  EXPECT_TRUE(value->Equals(result_dict));
71}
72
73TEST(JsonSchemaCompilerCrossrefTest, TestTypeInObjectParamsCreate) {
74  {
75    scoped_ptr<base::ListValue> params_value(new base::ListValue());
76    scoped_ptr<base::DictionaryValue> param_object_value(
77        new base::DictionaryValue());
78    param_object_value->Set("testType", CreateTestTypeDictionary().release());
79    param_object_value->Set("boolean", new base::FundamentalValue(true));
80    params_value->Append(param_object_value.release());
81    scoped_ptr<TestTypeInObject::Params> params(
82        TestTypeInObject::Params::Create(*params_value));
83    EXPECT_TRUE(params.get());
84    EXPECT_TRUE(params->param_object.test_type.get());
85    EXPECT_TRUE(params->param_object.boolean);
86    EXPECT_TRUE(CreateTestTypeDictionary()->Equals(
87          params->param_object.test_type->ToValue().get()));
88  }
89  {
90    scoped_ptr<base::ListValue> params_value(new base::ListValue());
91    scoped_ptr<base::DictionaryValue> param_object_value(
92        new base::DictionaryValue());
93    param_object_value->Set("boolean", new base::FundamentalValue(true));
94    params_value->Append(param_object_value.release());
95    scoped_ptr<TestTypeInObject::Params> params(
96        TestTypeInObject::Params::Create(*params_value));
97    EXPECT_TRUE(params.get());
98    EXPECT_FALSE(params->param_object.test_type.get());
99    EXPECT_TRUE(params->param_object.boolean);
100  }
101  {
102    scoped_ptr<base::ListValue> params_value(new base::ListValue());
103    scoped_ptr<base::DictionaryValue> param_object_value(
104        new base::DictionaryValue());
105    param_object_value->Set("testType",
106                            base::Value::CreateStringValue("invalid"));
107    param_object_value->Set("boolean", new base::FundamentalValue(true));
108    params_value->Append(param_object_value.release());
109    scoped_ptr<TestTypeInObject::Params> params(
110        TestTypeInObject::Params::Create(*params_value));
111    EXPECT_FALSE(params.get());
112  }
113  {
114    scoped_ptr<base::ListValue> params_value(new base::ListValue());
115    scoped_ptr<base::DictionaryValue> param_object_value(
116        new base::DictionaryValue());
117    param_object_value->Set("testType", CreateTestTypeDictionary().release());
118    params_value->Append(param_object_value.release());
119    scoped_ptr<TestTypeInObject::Params> params(
120        TestTypeInObject::Params::Create(*params_value));
121    EXPECT_FALSE(params.get());
122  }
123}
124