OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/error_generation.h" |
| 6 |
| 7 #include "base/json/json_writer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "tools/json_schema_compiler/test/test_util.h" |
| 10 |
| 11 using namespace test::api::error_generation; |
| 12 using base::FundamentalValue; |
| 13 using json_schema_compiler::test_util::Dictionary; |
| 14 using json_schema_compiler::test_util::List; |
| 15 |
| 16 template <typename T> |
| 17 std::string GetPopulateError(const base::Value& value) { |
| 18 std::string error; |
| 19 T test_type; |
| 20 T::Populate(value, &test_type, &error); |
| 21 return error; |
| 22 } |
| 23 |
| 24 // GenerateTypePopulate errors |
| 25 |
| 26 TEST(JsonSchemaCompilerErrorTest, RequiredPropertyPopulate) { |
| 27 { |
| 28 scoped_ptr<DictionaryValue> value = Dictionary( |
| 29 "string", new StringValue("bling")); |
| 30 EXPECT_EQ("", GetPopulateError<TestType>(*value)); |
| 31 } |
| 32 { |
| 33 scoped_ptr<base::BinaryValue> value(new base::BinaryValue()); |
| 34 EXPECT_EQ("expected dictionary, got binary", |
| 35 GetPopulateError<TestType>(*value)); |
| 36 } |
| 37 } |
| 38 |
| 39 TEST(JsonSchemaCompilerErrorTest, UnexpectedTypePopulation) { |
| 40 { |
| 41 scoped_ptr<base::ListValue> value(new base::ListValue()); |
| 42 EXPECT_EQ("", GetPopulateError<ChoiceType::Integers>(*value)); |
| 43 } |
| 44 { |
| 45 scoped_ptr<base::BinaryValue> value(new base::BinaryValue()); |
| 46 EXPECT_EQ("expected integers or integer, got binary", |
| 47 GetPopulateError<ChoiceType::Integers>(*value)); |
| 48 } |
| 49 } |
| 50 |
| 51 // GenerateTypePopulateProperty errors |
| 52 |
| 53 TEST(JsonSchemaCompilerErrorTest, TypeIsRequired) { |
| 54 { |
| 55 scoped_ptr<DictionaryValue> value = Dictionary( |
| 56 "integers", new FundamentalValue(5)); |
| 57 EXPECT_EQ("", GetPopulateError<ChoiceType>(*value)); |
| 58 } |
| 59 { |
| 60 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 61 EXPECT_EQ("'integers' is required", |
| 62 GetPopulateError<ChoiceType>(*value)); |
| 63 } |
| 64 } |
| 65 |
| 66 // GenerateParamsCheck errors |
| 67 |
| 68 TEST(JsonSchemaCompilerErrorTest, TooManyParameters) { |
| 69 { |
| 70 scoped_ptr<base::ListValue> params_value = List( |
| 71 new FundamentalValue(5)); |
| 72 EXPECT_TRUE(TestFunction::Params::Create(*params_value)); |
| 73 } |
| 74 { |
| 75 scoped_ptr<base::ListValue> params_value = List( |
| 76 new FundamentalValue(5), |
| 77 new FundamentalValue(5)); |
| 78 std::string error; |
| 79 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error)); |
| 80 EXPECT_EQ("expected 1 arguments, got 2", error); |
| 81 } |
| 82 } |
| 83 |
| 84 // GenerateFunctionParamsCreate errors |
| 85 |
| 86 TEST(JsonSchemaCompilerErrorTest, ParamIsRequired) { |
| 87 { |
| 88 scoped_ptr<base::ListValue> params_value = List( |
| 89 new FundamentalValue(5)); |
| 90 EXPECT_TRUE(TestFunction::Params::Create(*params_value)); |
| 91 } |
| 92 { |
| 93 scoped_ptr<base::ListValue> params_value = List( |
| 94 Value::CreateNullValue()); |
| 95 std::string error; |
| 96 EXPECT_FALSE(TestFunction::Params::Create(*params_value, &error)); |
| 97 EXPECT_EQ("'num' is required", error); |
| 98 } |
| 99 } |
| 100 |
| 101 // GeneratePopulateVariableFromValue errors |
| 102 |
| 103 TEST(JsonSchemaCompilerErrorTest, WrongPropertyValueType) { |
| 104 { |
| 105 scoped_ptr<DictionaryValue> value = Dictionary( |
| 106 "string", new StringValue("yes")); |
| 107 EXPECT_EQ("", GetPopulateError<TestType>(*value)); |
| 108 } |
| 109 { |
| 110 scoped_ptr<DictionaryValue> value = Dictionary( |
| 111 "string", new FundamentalValue(1.1)); |
| 112 EXPECT_EQ("'string': expected string, got number", |
| 113 GetPopulateError<TestType>(*value)); |
| 114 } |
| 115 } |
| 116 |
| 117 TEST(JsonSchemaCompilerErrorTest, WrongParameterCreationType) { |
| 118 { |
| 119 scoped_ptr<base::ListValue> params_value = List( |
| 120 new StringValue("Yeah!")); |
| 121 EXPECT_TRUE(TestString::Params::Create(*params_value)); |
| 122 } |
| 123 { |
| 124 scoped_ptr<base::ListValue> params_value = List( |
| 125 new FundamentalValue(5)); |
| 126 std::string error; |
| 127 EXPECT_FALSE(TestTypeInObject::Params::Create(*params_value, &error)); |
| 128 EXPECT_EQ("'paramObject': expected dictionary, got integer", error); |
| 129 } |
| 130 } |
| 131 |
| 132 TEST(JsonSchemaCompilerErrorTest, WrongTypeValueType) { |
| 133 { |
| 134 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 135 EXPECT_EQ("", GetPopulateError<ObjectType>(*value)); |
| 136 } |
| 137 { |
| 138 scoped_ptr<DictionaryValue> value = Dictionary( |
| 139 "otherType", new FundamentalValue(1.1)); |
| 140 EXPECT_EQ("'otherType': expected dictionary, got number", |
| 141 GetPopulateError<ObjectType>(*value)); |
| 142 } |
| 143 } |
| 144 |
| 145 TEST(JsonSchemaCompilerErrorTest, UnableToPopulateArray) { |
| 146 { |
| 147 scoped_ptr<base::ListValue> params_value = List( |
| 148 new FundamentalValue(5)); |
| 149 EXPECT_EQ("", GetPopulateError<ChoiceType::Integers>(*params_value)); |
| 150 } |
| 151 { |
| 152 scoped_ptr<base::ListValue> params_value = List( |
| 153 new FundamentalValue(5), |
| 154 new FundamentalValue(false)); |
| 155 EXPECT_EQ("unable to populate array 'integers'", |
| 156 GetPopulateError<ChoiceType::Integers>(*params_value)); |
| 157 } |
| 158 } |
| 159 |
| 160 TEST(JsonSchemaCompilerErrorTest, BinaryTypeExpected) { |
| 161 { |
| 162 scoped_ptr<DictionaryValue> value = Dictionary( |
| 163 "data", new base::BinaryValue()); |
| 164 EXPECT_EQ("", GetPopulateError<BinaryData>(*value)); |
| 165 } |
| 166 { |
| 167 scoped_ptr<DictionaryValue> value = Dictionary( |
| 168 "data", new FundamentalValue(1.1)); |
| 169 EXPECT_EQ("'data': expected binary, got number", |
| 170 GetPopulateError<BinaryData>(*value)); |
| 171 } |
| 172 } |
| 173 |
| 174 TEST(JsonSchemaCompilerErrorTest, ListExpected) { |
| 175 { |
| 176 scoped_ptr<DictionaryValue> value = Dictionary( |
| 177 "TheArray", new base::ListValue()); |
| 178 EXPECT_EQ("", GetPopulateError<ArrayObject>(*value)); |
| 179 } |
| 180 { |
| 181 scoped_ptr<DictionaryValue> value = Dictionary( |
| 182 "TheArray", new FundamentalValue(5)); |
| 183 EXPECT_EQ("'TheArray': expected list, got integer", |
| 184 GetPopulateError<ArrayObject>(*value)); |
| 185 } |
| 186 } |
| 187 |
| 188 // GenerateStringToEnumConversion errors |
| 189 |
| 190 TEST(JsonSchemaCompilerErrorTest, BadEnumValue) { |
| 191 { |
| 192 scoped_ptr<DictionaryValue> value = Dictionary( |
| 193 "enumeration", new StringValue("one")); |
| 194 EXPECT_EQ("", GetPopulateError<HasEnumeration>(*value)); |
| 195 } |
| 196 { |
| 197 scoped_ptr<DictionaryValue> value = Dictionary( |
| 198 "enumeration", new StringValue("bad sauce")); |
| 199 EXPECT_EQ("'enumeration': expected \"one\" or \"two\" or \"three\", " |
| 200 "got \"bad sauce\"", |
| 201 GetPopulateError<HasEnumeration>(*value)); |
| 202 } |
| 203 } |
OLD | NEW |