OLD | NEW |
(Empty) | |
| 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/array.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 using namespace test::api::array; |
| 10 |
| 11 namespace { |
| 12 |
| 13 // TODO(calamity): Change to AppendString etc once kalman's patch goes through |
| 14 static scoped_ptr<DictionaryValue> CreateBasicArrayTypeDictionary() { |
| 15 DictionaryValue* value = new DictionaryValue(); |
| 16 ListValue* strings_value = new ListValue(); |
| 17 strings_value->Append(Value::CreateStringValue("a")); |
| 18 strings_value->Append(Value::CreateStringValue("b")); |
| 19 strings_value->Append(Value::CreateStringValue("c")); |
| 20 strings_value->Append(Value::CreateStringValue("it's easy as")); |
| 21 ListValue* integers_value = new ListValue(); |
| 22 integers_value->Append(Value::CreateIntegerValue(1)); |
| 23 integers_value->Append(Value::CreateIntegerValue(2)); |
| 24 integers_value->Append(Value::CreateIntegerValue(3)); |
| 25 ListValue* booleans_value = new ListValue(); |
| 26 booleans_value->Append(Value::CreateBooleanValue(false)); |
| 27 booleans_value->Append(Value::CreateBooleanValue(true)); |
| 28 ListValue* numbers_value = new ListValue(); |
| 29 numbers_value->Append(Value::CreateDoubleValue(6.1)); |
| 30 value->Set("numbers", numbers_value); |
| 31 value->Set("booleans", booleans_value); |
| 32 value->Set("strings", strings_value); |
| 33 value->Set("integers", integers_value); |
| 34 return scoped_ptr<DictionaryValue>(value); |
| 35 } |
| 36 |
| 37 static Value* CreateItemValue(int val) { |
| 38 DictionaryValue* value(new DictionaryValue()); |
| 39 value->Set("val", Value::CreateIntegerValue(val)); |
| 40 return value; |
| 41 } |
| 42 |
| 43 } // namespace |
| 44 |
| 45 TEST(JsonSchemaCompilerArrayTest, BasicArrayType) { |
| 46 { |
| 47 scoped_ptr<DictionaryValue> value = CreateBasicArrayTypeDictionary(); |
| 48 scoped_ptr<BasicArrayType> basic_array_type(new BasicArrayType()); |
| 49 EXPECT_TRUE(BasicArrayType::Populate(*value, basic_array_type.get())); |
| 50 EXPECT_TRUE(value->Equals(basic_array_type->ToValue().get())); |
| 51 } |
| 52 } |
| 53 |
| 54 TEST(JsonSchemaCompilerArrayTest, RefArrayType) { |
| 55 { |
| 56 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 57 scoped_ptr<ListValue> ref_array(new ListValue()); |
| 58 ref_array->Append(CreateItemValue(1)); |
| 59 ref_array->Append(CreateItemValue(2)); |
| 60 ref_array->Append(CreateItemValue(3)); |
| 61 value->Set("refs", ref_array.release()); |
| 62 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType()); |
| 63 EXPECT_TRUE(RefArrayType::Populate(*value, ref_array_type.get())); |
| 64 EXPECT_EQ(3UL, ref_array_type->refs.size()); |
| 65 EXPECT_EQ(1, ref_array_type->refs[0]->val); |
| 66 EXPECT_EQ(2, ref_array_type->refs[1]->val); |
| 67 EXPECT_EQ(3, ref_array_type->refs[2]->val); |
| 68 } |
| 69 { |
| 70 scoped_ptr<DictionaryValue> value(new DictionaryValue()); |
| 71 scoped_ptr<ListValue> not_ref_array(new ListValue()); |
| 72 not_ref_array->Append(CreateItemValue(1)); |
| 73 not_ref_array->Append(Value::CreateIntegerValue(3)); |
| 74 value->Set("refs", not_ref_array.release()); |
| 75 scoped_ptr<RefArrayType> ref_array_type(new RefArrayType()); |
| 76 EXPECT_FALSE(RefArrayType::Populate(*value, ref_array_type.get())); |
| 77 } |
| 78 } |
| 79 |
| 80 TEST(JsonSchemaCompilerArrayTest, IntegerArrayParamsCreate) { |
| 81 scoped_ptr<ListValue> params_value(new ListValue()); |
| 82 scoped_ptr<ListValue> integer_array(new ListValue()); |
| 83 integer_array->Append(Value::CreateIntegerValue(2)); |
| 84 integer_array->Append(Value::CreateIntegerValue(4)); |
| 85 integer_array->Append(Value::CreateIntegerValue(8)); |
| 86 params_value->Append(integer_array.release()); |
| 87 scoped_ptr<IntegerArray::Params> params( |
| 88 IntegerArray::Params::Create(*params_value)); |
| 89 EXPECT_TRUE(params.get()); |
| 90 EXPECT_EQ(3UL, params->nums.size()); |
| 91 EXPECT_EQ(2, params->nums[0]); |
| 92 EXPECT_EQ(4, params->nums[1]); |
| 93 EXPECT_EQ(8, params->nums[2]); |
| 94 } |
| 95 |
| 96 TEST(JsonSchemaCompilerArrayTest, RefArrayParamsCreate) { |
| 97 scoped_ptr<ListValue> params_value(new ListValue()); |
| 98 scoped_ptr<ListValue> item_array(new ListValue()); |
| 99 item_array->Append(CreateItemValue(1)); |
| 100 item_array->Append(CreateItemValue(2)); |
| 101 params_value->Append(item_array.release()); |
| 102 scoped_ptr<RefArray::Params> params( |
| 103 RefArray::Params::Create(*params_value)); |
| 104 EXPECT_TRUE(params.get()); |
| 105 EXPECT_EQ(2UL, params->refs.size()); |
| 106 EXPECT_EQ(1, params->refs[0]->val); |
| 107 EXPECT_EQ(2, params->refs[1]->val); |
| 108 } |
| 109 |
| 110 TEST(JsonSchemaCompilerArrayTest, ReturnIntegerArrayResultCreate) { |
| 111 std::vector<int> integers; |
| 112 integers.push_back(1); |
| 113 integers.push_back(2); |
| 114 scoped_ptr<Value> result(ReturnIntegerArray::Result::Create(integers)); |
| 115 ListValue* list = NULL; |
| 116 EXPECT_TRUE(result->GetAsList(&list)); |
| 117 int temp; |
| 118 EXPECT_EQ(2UL, list->GetSize()); |
| 119 EXPECT_TRUE(list->GetInteger(0, &temp)); |
| 120 EXPECT_EQ(1, temp); |
| 121 EXPECT_TRUE(list->GetInteger(1, &temp)); |
| 122 EXPECT_EQ(2, temp); |
| 123 } |
| 124 |
| 125 TEST(JsonSchemaCompilerArrayTest, ReturnRefArrayResultCreate) { |
| 126 std::vector<linked_ptr<Item> > items; |
| 127 items.push_back(linked_ptr<Item>(new Item())); |
| 128 items.push_back(linked_ptr<Item>(new Item())); |
| 129 items[0]->val = 1; |
| 130 items[1]->val = 2; |
| 131 scoped_ptr<Value> result(ReturnRefArray::Result::Create(items)); |
| 132 ListValue* list = NULL; |
| 133 EXPECT_TRUE(result->GetAsList(&list)); |
| 134 EXPECT_EQ(2UL, list->GetSize()); |
| 135 DictionaryValue* item_value = NULL; |
| 136 int temp; |
| 137 EXPECT_TRUE(list->GetDictionary(0, &item_value)); |
| 138 EXPECT_TRUE(item_value->GetInteger("val", &temp)); |
| 139 EXPECT_EQ(1, temp); |
| 140 EXPECT_TRUE(list->GetDictionary(1, &item_value)); |
| 141 EXPECT_TRUE(item_value->GetInteger("val", &temp)); |
| 142 EXPECT_EQ(2, temp); |
| 143 } |
OLD | NEW |