diff options
author | mtytel@chromium.org <mtytel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-16 21:47:24 +0000 |
---|---|---|
committer | mtytel@chromium.org <mtytel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-16 21:47:24 +0000 |
commit | b741e8f6ccf1c6bdc5f4e98bd6751b085b7f7bbc (patch) | |
tree | 23087398a8a4e52beffc2c79d5af40595a9491d6 /tools/json_schema_compiler/test | |
parent | e275ae44b030d87722c700bb4ce94e82709b74fb (diff) | |
download | chromium_src-b741e8f6ccf1c6bdc5f4e98bd6751b085b7f7bbc.zip chromium_src-b741e8f6ccf1c6bdc5f4e98bd6751b085b7f7bbc.tar.gz chromium_src-b741e8f6ccf1c6bdc5f4e98bd6751b085b7f7bbc.tar.bz2 |
JSON Schema Compiler: Add event compilation and allow multiple parameters to callbacks
Compiles events to objects that look similar to this:
namespace OnObjectFired {
struct SomeObject {
~SomeObject();
SomeObject();
enum State {
STATE_FOO,
STATE_BAR,
STATE_BAZ,
};
static scoped_ptr<Value> CreateEnumValue(State state);
State state;
// Returns a new DictionaryValue representing the serialized form of this
// SomeObject object. Passes ownership to caller.
scoped_ptr<DictionaryValue> ToValue() const;
private:
DISALLOW_COPY_AND_ASSIGN(SomeObject);
};
Value* Create(const SomeObject& some_object);
};
BUG=133757, 135237
TEST=
Review URL: https://chromiumcodereview.appspot.com/10701012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146899 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/json_schema_compiler/test')
18 files changed, 568 insertions, 83 deletions
diff --git a/tools/json_schema_compiler/test/additional_properties_unittest.cc b/tools/json_schema_compiler/test/additional_properties_unittest.cc index ef0ab29..4c3d720 100644 --- a/tools/json_schema_compiler/test/additional_properties_unittest.cc +++ b/tools/json_schema_compiler/test/additional_properties_unittest.cc @@ -48,17 +48,15 @@ TEST(JsonSchemaCompilerAdditionalPropertiesTest, TEST(JsonSchemaCompilerAdditionalPropertiesTest, ReturnAdditionalPropertiesResultCreate) { - scoped_ptr<DictionaryValue> result_object_value(new DictionaryValue()); - result_object_value->SetString("key", "value"); - scoped_ptr<ReturnAdditionalProperties::Result::ResultObject> result_object( - new ReturnAdditionalProperties::Result::ResultObject()); - result_object->integer = 5; - result_object->additional_properties.MergeDictionary( - result_object_value.get()); - scoped_ptr<Value> result( - ReturnAdditionalProperties::Result::Create(*result_object)); + DictionaryValue additional; + additional.SetString("key", "value"); + ReturnAdditionalProperties::Results::ResultObject result_object; + result_object.integer = 5; + result_object.additional_properties.MergeDictionary(&additional); + scoped_ptr<ListValue> results = + ReturnAdditionalProperties::Results::Create(result_object); DictionaryValue* result_dict = NULL; - EXPECT_TRUE(result->GetAsDictionary(&result_dict)); + EXPECT_TRUE(results->GetDictionary(0, &result_dict)); Value* int_temp_value_out = NULL; int int_temp = 0; @@ -67,5 +65,5 @@ TEST(JsonSchemaCompilerAdditionalPropertiesTest, EXPECT_TRUE(int_temp_value->GetAsInteger(&int_temp)); EXPECT_EQ(5, int_temp); - EXPECT_TRUE(result_dict->Equals(result_object_value.get())); + EXPECT_TRUE(result_dict->Equals(&additional)); } diff --git a/tools/json_schema_compiler/test/any.json b/tools/json_schema_compiler/test/any.json index 02243bb..55da4a6 100644 --- a/tools/json_schema_compiler/test/any.json +++ b/tools/json_schema_compiler/test/any.json @@ -49,6 +49,19 @@ } ] } + ], + "events": [ + { + "name": "onAnyFired", + "type": "function", + "description": "Fired when anything is ready.", + "parameters": [ + { + "name": "something", + "type": "any" + } + ] + } ] } ] diff --git a/tools/json_schema_compiler/test/arrays_unittest.cc b/tools/json_schema_compiler/test/arrays_unittest.cc index 0709677..c6d1e06 100644 --- a/tools/json_schema_compiler/test/arrays_unittest.cc +++ b/tools/json_schema_compiler/test/arrays_unittest.cc @@ -194,15 +194,14 @@ TEST(JsonSchemaCompilerArrayTest, ReturnIntegerArrayResultCreate) { std::vector<int> integers; integers.push_back(1); integers.push_back(2); - scoped_ptr<Value> result(ReturnIntegerArray::Result::Create(integers)); - ListValue* list = NULL; - EXPECT_TRUE(result->GetAsList(&list)); - int temp; - ASSERT_EQ(2u, list->GetSize()); - EXPECT_TRUE(list->GetInteger(0, &temp)); - EXPECT_EQ(1, temp); - EXPECT_TRUE(list->GetInteger(1, &temp)); - EXPECT_EQ(2, temp); + scoped_ptr<ListValue> results = ReturnIntegerArray::Results::Create(integers); + + ListValue expected; + ListValue* expected_argument = new ListValue(); + expected_argument->Append(Value::CreateIntegerValue(1)); + expected_argument->Append(Value::CreateIntegerValue(2)); + expected.Append(expected_argument); + EXPECT_TRUE(results->Equals(&expected)); } TEST(JsonSchemaCompilerArrayTest, ReturnRefArrayResultCreate) { @@ -211,16 +210,16 @@ TEST(JsonSchemaCompilerArrayTest, ReturnRefArrayResultCreate) { items.push_back(linked_ptr<Item>(new Item())); items[0]->val = 1; items[1]->val = 2; - scoped_ptr<Value> result(ReturnRefArray::Result::Create(items)); - ListValue* list = NULL; - EXPECT_TRUE(result->GetAsList(&list)); - ASSERT_EQ(2u, list->GetSize()); - DictionaryValue* item_value = NULL; - int temp; - EXPECT_TRUE(list->GetDictionary(0, &item_value)); - EXPECT_TRUE(item_value->GetInteger("val", &temp)); - EXPECT_EQ(1, temp); - EXPECT_TRUE(list->GetDictionary(1, &item_value)); - EXPECT_TRUE(item_value->GetInteger("val", &temp)); - EXPECT_EQ(2, temp); + scoped_ptr<ListValue> results = ReturnRefArray::Results::Create(items); + + ListValue expected; + ListValue* expected_argument = new ListValue(); + DictionaryValue* first = new DictionaryValue(); + first->SetInteger("val", 1); + expected_argument->Append(first); + DictionaryValue* second = new DictionaryValue(); + second->SetInteger("val", 2); + expected_argument->Append(second); + expected.Append(expected_argument); + EXPECT_TRUE(results->Equals(&expected)); } diff --git a/tools/json_schema_compiler/test/callbacks.json b/tools/json_schema_compiler/test/callbacks.json new file mode 100644 index 0000000..06455b9 --- /dev/null +++ b/tools/json_schema_compiler/test/callbacks.json @@ -0,0 +1,71 @@ +[ + { + "namespace": "callbacks", + "types": [], + "functions": [ + { + "name": "returnsNothing", + "type": "function", + "description": "Takes nothing. Returns nothing.", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [] + } + ] + }, + { + "name": "returnsObject", + "description": "Returns an object.", + "type": "function", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "someObject", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + } + } + ] + } + ] + }, + { + "name": "returnsMultiple", + "description": "Returns an object.", + "type": "function", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "someInteger", + "type": "integer" + }, + { + "name": "someObject", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + } + } + ] + } + ] + } + ] + } +] + diff --git a/tools/json_schema_compiler/test/callbacks_unittest.cc b/tools/json_schema_compiler/test/callbacks_unittest.cc new file mode 100644 index 0000000..163a6c0 --- /dev/null +++ b/tools/json_schema_compiler/test/callbacks_unittest.cc @@ -0,0 +1,35 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "tools/json_schema_compiler/test/callbacks.h" + +#include "testing/gtest/include/gtest/gtest.h" + +using namespace test::api::callbacks; + +TEST(JsonSchemaCompilerCallbacksTest, ReturnsObjectResultCreate) { + ReturnsObject::Results::SomeObject some_object; + some_object.state = ReturnsObject::Results::SomeObject::STATE_FOO; + scoped_ptr<ListValue> results = ReturnsObject::Results::Create(some_object); + + DictionaryValue* expected_dict = new DictionaryValue(); + expected_dict->SetString("state", "foo"); + ListValue expected; + expected.Append(expected_dict); + EXPECT_TRUE(results->Equals(&expected)); +} + +TEST(JsonSchemaCompilerCallbacksTest, ReturnsMultipleResultCreate) { + ReturnsMultiple::Results::SomeObject some_object; + some_object.state = ReturnsMultiple::Results::SomeObject::STATE_FOO; + scoped_ptr<ListValue> results = + ReturnsMultiple::Results::Create(5, some_object); + + DictionaryValue* expected_dict = new DictionaryValue(); + expected_dict->SetString("state", "foo"); + ListValue expected; + expected.Append(Value::CreateIntegerValue(5)); + expected.Append(expected_dict); + EXPECT_TRUE(results->Equals(&expected)); +} diff --git a/tools/json_schema_compiler/test/choices.json b/tools/json_schema_compiler/test/choices.json index 6a13eb5..e53ac13 100644 --- a/tools/json_schema_compiler/test/choices.json +++ b/tools/json_schema_compiler/test/choices.json @@ -95,6 +95,35 @@ ] } ] + }, + { + "name": "returnMultipleChoices", + "type": "function", + "description": "Gives back two values where each is an integer or a list of integers.", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "firstResult", + "choices": [ + {"type": "array", "items": {"type": "integer", "minimum": 0}}, + {"type": "integer"} + ], + "description": "Some integers." + }, + { + "name": "secondResult", + "choices": [ + {"type": "array", "items": {"type": "integer", "minimum": 0}}, + {"type": "integer"} + ], + "description": "Some integers." + } + ] + } + ] } ] } diff --git a/tools/json_schema_compiler/test/choices_unittest.cc b/tools/json_schema_compiler/test/choices_unittest.cc index dcdfd77..ce5a0ee 100644 --- a/tools/json_schema_compiler/test/choices_unittest.cc +++ b/tools/json_schema_compiler/test/choices_unittest.cc @@ -116,20 +116,20 @@ TEST(JsonSchemaCompilerChoicesTest, ReturnChoices) { std::vector<int> integers; integers.push_back(1); integers.push_back(2); - scoped_ptr<Value> array_result(ReturnChoices::Result::Create(integers)); - ListValue* list = NULL; - EXPECT_TRUE(array_result->GetAsList(&list)); - EXPECT_EQ((size_t) 2, list->GetSize()); - int temp; - EXPECT_TRUE(list->GetInteger(0, &temp)); - EXPECT_EQ(1, temp); - EXPECT_TRUE(list->GetInteger(1, &temp)); - EXPECT_EQ(2, temp); + scoped_ptr<ListValue> array_results = + ReturnChoices::Results::Create(integers); + + ListValue expected; + ListValue* expected_argument = new ListValue(); + expected_argument->Append(Value::CreateIntegerValue(1)); + expected_argument->Append(Value::CreateIntegerValue(2)); + expected.Append(expected_argument); + EXPECT_TRUE(array_results->Equals(&expected)); } { - scoped_ptr<Value> single_result(ReturnChoices::Result::Create(5)); - int temp; - EXPECT_TRUE(single_result->GetAsInteger(&temp)); - EXPECT_EQ(5, temp); + scoped_ptr<ListValue> integer_results = ReturnChoices::Results::Create(5); + ListValue expected; + expected.Append(Value::CreateIntegerValue(5)); + EXPECT_TRUE(integer_results->Equals(&expected)); } } diff --git a/tools/json_schema_compiler/test/crossref_unittest.cc b/tools/json_schema_compiler/test/crossref_unittest.cc index cf7189e..d2747fd 100644 --- a/tools/json_schema_compiler/test/crossref_unittest.cc +++ b/tools/json_schema_compiler/test/crossref_unittest.cc @@ -60,8 +60,11 @@ TEST(JsonSchemaCompilerCrossrefTest, GetTestType) { new test::api::simple_api::TestType()); EXPECT_TRUE( test::api::simple_api::TestType::Populate(*value, test_type.get())); - scoped_ptr<Value> result(GetTestType::Result::Create(*test_type)); - EXPECT_TRUE(value->Equals(result.get())); + + scoped_ptr<ListValue> results = GetTestType::Results::Create(*test_type); + DictionaryValue* result_dict = NULL; + results->GetDictionary(0, &result_dict); + EXPECT_TRUE(value->Equals(result_dict)); } TEST(JsonSchemaCompilerCrossrefTest, TestTypeInObjectParamsCreate) { diff --git a/tools/json_schema_compiler/test/enums.json b/tools/json_schema_compiler/test/enums.json index 51e1fd41..ae6be004 100644 --- a/tools/json_schema_compiler/test/enums.json +++ b/tools/json_schema_compiler/test/enums.json @@ -43,6 +43,47 @@ ] }, { + "name": "returnsEnum", + "type": "function", + "description": "Returns an enum through the callback", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "state", + "type": "string", + "enum": ["foo", "bar", "baz"] + } + ] + } + ] + }, + { + "name": "returnsTwoEnums", + "type": "function", + "description": "Returns two enums through the callback", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "firstState", + "type": "string", + "enum": ["foo", "bar", "baz"] + }, + { + "name": "secondState", + "type": "string", + "enum": ["spam", "ham", "eggs"] + } + ] + } + ] + }, + { "name": "takesOptionalEnum", "type": "function", "description": "Takes an enum as its parameter.", @@ -84,6 +125,37 @@ } ] } + ], + "events": [ + { + "name": "onEnumFired", + "type": "function", + "description": "Fired when an enum is ready.", + "parameters": [ + { + "name": "someEnum", + "type": "string", + "enum": ["foo", "bar", "baz"] + } + ] + }, + { + "name": "onTwoEnumsFired", + "type": "function", + "description": "Fired when two enums are ready.", + "parameters": [ + { + "name": "firstEnum", + "type": "string", + "enum": ["foo", "bar", "baz"] + }, + { + "name": "secondEnum", + "type": "string", + "enum": ["spam", "ham", "eggs"] + } + ] + } ] } ] diff --git a/tools/json_schema_compiler/test/enums_unittest.cc b/tools/json_schema_compiler/test/enums_unittest.cc index 14eece2..6037874 100644 --- a/tools/json_schema_compiler/test/enums_unittest.cc +++ b/tools/json_schema_compiler/test/enums_unittest.cc @@ -25,6 +25,34 @@ TEST(JsonSchemaCompilerEnumsTest, EnumTypePopulate) { } } +TEST(JsonSchemaCompilerEnumsTest, ReturnsEnumCreate) { + { + ReturnsEnum::Results::State state = ReturnsEnum::Results::STATE_FOO; + scoped_ptr<Value> result = ReturnsEnum::Results::CreateEnumValue(state); + scoped_ptr<Value> expected(Value::CreateStringValue("foo")); + EXPECT_TRUE(result->Equals(expected.get())); + } + { + ReturnsEnum::Results::State state = ReturnsEnum::Results::STATE_FOO; + scoped_ptr<ListValue> results = ReturnsEnum::Results::Create(state); + ListValue expected; + expected.Append(Value::CreateStringValue("foo")); + EXPECT_TRUE(results->Equals(&expected)); + } +} + +TEST(JsonSchemaCompilerEnumsTest, ReturnsTwoEnumsCreate) { + { + scoped_ptr<ListValue> results = ReturnsTwoEnums::Results::Create( + ReturnsTwoEnums::Results::FIRST_STATE_FOO, + ReturnsTwoEnums::Results::SECOND_STATE_HAM); + ListValue expected; + expected.Append(Value::CreateStringValue("foo")); + expected.Append(Value::CreateStringValue("ham")); + EXPECT_TRUE(results->Equals(&expected)); + } +} + TEST(JsonSchemaCompilerEnumsTest, OptionalEnumTypePopulate) { { scoped_ptr<OptionalEnumType> enum_type(new OptionalEnumType()); @@ -129,3 +157,31 @@ TEST(JsonSchemaCompilerEnumsTest, TakesMultipleOptionalEnumsParamsCreate) { EXPECT_FALSE(params.get()); } } + +TEST(JsonSchemaCompilerEnumsTest, OnEnumFiredCreate) { + { + OnEnumFired::SomeEnum some_enum = OnEnumFired::SOME_ENUM_FOO; + scoped_ptr<Value> result(OnEnumFired::CreateEnumValue(some_enum)); + scoped_ptr<Value> expected(Value::CreateStringValue("foo")); + EXPECT_TRUE(result->Equals(expected.get())); + } + { + OnEnumFired::SomeEnum some_enum = OnEnumFired::SOME_ENUM_FOO; + scoped_ptr<ListValue> results(OnEnumFired::Create(some_enum)); + ListValue expected; + expected.Append(Value::CreateStringValue("foo")); + EXPECT_TRUE(results->Equals(&expected)); + } +} + +TEST(JsonSchemaCompilerEnumsTest, OnTwoEnumsFiredCreate) { + { + scoped_ptr<Value> results(OnTwoEnumsFired::Create( + OnTwoEnumsFired::FIRST_ENUM_FOO, + OnTwoEnumsFired::SECOND_ENUM_HAM)); + ListValue expected; + expected.Append(Value::CreateStringValue("foo")); + expected.Append(Value::CreateStringValue("ham")); + EXPECT_TRUE(results->Equals(&expected)); + } +} diff --git a/tools/json_schema_compiler/test/forbidden.json b/tools/json_schema_compiler/test/forbidden.json new file mode 100644 index 0000000..c7978aa --- /dev/null +++ b/tools/json_schema_compiler/test/forbidden.json @@ -0,0 +1,39 @@ +[ + { + "namespace": "forbidden", + "types": [], + "functions": [ + { + "name": "forbiddenParameters", + "type": "function", + "description": "Don't do this at home. Accepts multiple choices and values", + "parameters": [ + { + "name": "firstChoice", + "description": "a choice between int and array", + "choices": [ + {"type": "integer", "minimum": 0}, + {"type": "array", "items": {"type": "integer"}} + ] + }, + { + "type": "string", + "name": "firstString" + }, + { + "name": "secondChoice", + "description": "a choice between int and array", + "choices": [ + {"type": "integer", "minimum": 0}, + {"type": "array", "items": {"type": "integer"}} + ] + }, + { + "type": "string", + "name": "secondString" + } + ] + } + ] + } +] diff --git a/tools/json_schema_compiler/test/functions_on_types_unittest.cc b/tools/json_schema_compiler/test/functions_on_types_unittest.cc index 0694cb3..73a3cf2 100644 --- a/tools/json_schema_compiler/test/functions_on_types_unittest.cc +++ b/tools/json_schema_compiler/test/functions_on_types_unittest.cc @@ -48,12 +48,13 @@ TEST(JsonSchemaCompilerFunctionsOnTypesTest, StorageAreaGetParamsCreate) { } TEST(JsonSchemaCompilerFunctionsOnTypesTest, StorageAreaGetResultCreate) { - scoped_ptr<StorageArea::Get::Result::Items> items( - new StorageArea::Get::Result::Items()); - items->additional_properties.SetDouble("asdf", 0.1); - items->additional_properties.SetString("sdfg", "zxcv"); - scoped_ptr<Value> result_value(StorageArea::Get::Result::Create(*items)); - EXPECT_TRUE(result_value->Equals(&items->additional_properties)); + StorageArea::Get::Results::Items items; + items.additional_properties.SetDouble("asdf", 0.1); + items.additional_properties.SetString("sdfg", "zxcv"); + scoped_ptr<ListValue> results = StorageArea::Get::Results::Create(items); + DictionaryValue* item_result = NULL; + results->GetDictionary(0, &item_result); + EXPECT_TRUE(item_result->Equals(&items.additional_properties)); } TEST(JsonSchemaCompilerFunctionsOnTypesTest, ChromeSettingGetParamsCreate) { diff --git a/tools/json_schema_compiler/test/idl_schemas_unittest.cc b/tools/json_schema_compiler/test/idl_schemas_unittest.cc index 4969a9b..8afb59c 100644 --- a/tools/json_schema_compiler/test/idl_schemas_unittest.cc +++ b/tools/json_schema_compiler/test/idl_schemas_unittest.cc @@ -54,15 +54,20 @@ TEST(IdlCompiler, Basics) { // Test functions that take a callback function as a parameter, with varying // callback signatures. - scoped_ptr<Value> f4_result(Function4::Result::Create()); - EXPECT_TRUE(f4_result->IsType(Value::TYPE_NULL)); - - scoped_ptr<Value> f5_result(Function5::Result::Create(13)); - EXPECT_TRUE(f5_result->IsType(Value::TYPE_INTEGER)); - - scoped_ptr<Value> f6_result(Function6::Result::Create(a)); + scoped_ptr<ListValue> f4_results = Function4::Results::Create(); + ListValue expected; + EXPECT_TRUE(f4_results->Equals(&expected)); + + scoped_ptr<ListValue> f5_results(Function5::Results::Create(13)); + Value* f5_result_int = NULL; + ASSERT_TRUE(f5_results->Get(0, &f5_result_int)); + EXPECT_TRUE(f5_result_int->IsType(Value::TYPE_INTEGER)); + + scoped_ptr<ListValue> f6_results(Function6::Results::Create(a)); + Value* f6_result_dict = NULL; + ASSERT_TRUE(f6_results->Get(0, &f6_result_dict)); MyType1 c; - EXPECT_TRUE(MyType1::Populate(*f6_result, &c)); + EXPECT_TRUE(MyType1::Populate(*f6_result_dict, &c)); EXPECT_EQ(a.x, c.x); EXPECT_EQ(a.y, c.y); } diff --git a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp index f48809c..f881b29 100644 --- a/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp +++ b/tools/json_schema_compiler/test/json_schema_compiler_tests.gyp @@ -13,6 +13,7 @@ 'any.json', 'additional_properties.json', 'arrays.json', + 'callbacks.json', 'choices.json', 'crossref.json', 'enums.json', diff --git a/tools/json_schema_compiler/test/objects.json b/tools/json_schema_compiler/test/objects.json index 8fb20b4..00a7f53 100644 --- a/tools/json_schema_compiler/test/objects.json +++ b/tools/json_schema_compiler/test/objects.json @@ -53,6 +53,85 @@ ] } ] + }, + { + "name": "returnsTwoObjects", + "description": "Return two objects.", + "type": "function", + "parameters": [ + { + "name": "callback", + "type": "function", + "parameters": [ + { + "name": "firstInfo", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + } + }, + { + "name": "secondInfo", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["spam", "ham", "eggs"] + } + } + } + ] + } + ] + } + ], + "events": [ + { + "name": "onObjectFired", + "type": "function", + "description": "Fired when an object is ready.", + "parameters": [ + { + "name": "someObject", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + } + } + ] + }, + { + "name": "onTwoObjectsFired", + "type": "function", + "description": "Fired when two objects are ready.", + "parameters": [ + { + "name": "firstObject", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["foo", "bar", "baz"] + } + } + }, + { + "name": "secondObject", + "type": "object", + "properties": { + "state": { + "type": "string", + "enum": ["spam", "ham", "eggs"] + } + } + } + ] } ] } diff --git a/tools/json_schema_compiler/test/objects_unittest.cc b/tools/json_schema_compiler/test/objects_unittest.cc index b0ed54f..95c3f0d 100644 --- a/tools/json_schema_compiler/test/objects_unittest.cc +++ b/tools/json_schema_compiler/test/objects_unittest.cc @@ -46,14 +46,25 @@ TEST(JsonSchemaCompilerObjectsTest, ObjectParamParamsCreate) { } TEST(JsonSchemaCompilerObjectsTest, ReturnsObjectResultCreate) { - scoped_ptr<ReturnsObject::Result::Info> info( - new ReturnsObject::Result::Info()); - info->state = ReturnsObject::Result::Info::STATE_FOO; - scoped_ptr<Value> result_value( - ReturnsObject::Result::Create(*info)); - DictionaryValue* result_dict = NULL; - EXPECT_TRUE(result_value->GetAsDictionary(&result_dict)); - std::string state; - EXPECT_TRUE(result_dict->GetString("state", &state)); - EXPECT_EQ("foo", state); + ReturnsObject::Results::Info info; + info.state = ReturnsObject::Results::Info::STATE_FOO; + scoped_ptr<ListValue> results = ReturnsObject::Results::Create(info); + + DictionaryValue expected; + expected.SetString("state", "foo"); + DictionaryValue* result = NULL; + ASSERT_TRUE(results->GetDictionary(0, &result)); + ASSERT_TRUE(result->Equals(&expected)); +} + +TEST(JsonSchemaCompilerObjectsTest, OnObjectFiredCreate) { + OnObjectFired::SomeObject object; + object.state = OnObjectFired::SomeObject::STATE_BAR; + scoped_ptr<ListValue> results(OnObjectFired::Create(object)); + + DictionaryValue expected; + expected.SetString("state", "bar"); + DictionaryValue* result = NULL; + ASSERT_TRUE(results->GetDictionary(0, &result)); + ASSERT_TRUE(result->Equals(&expected)); } diff --git a/tools/json_schema_compiler/test/simple_api.json b/tools/json_schema_compiler/test/simple_api.json index 068432d..82d77b8 100644 --- a/tools/json_schema_compiler/test/simple_api.json +++ b/tools/json_schema_compiler/test/simple_api.json @@ -122,6 +122,41 @@ } ] } + ], + "events": [ + { + "name": "onIntegerFired", + "type": "function", + "description": "Fired when an integer is ready.", + "parameters": [ + { + "name": "someInteger", + "type": "integer" + } + ] + }, + { + "name": "onStringFired", + "type": "function", + "description": "Fired when a string is ready.", + "parameters": [ + { + "name": "someString", + "type": "string" + } + ] + }, + { + "name": "onTestTypeFired", + "type": "function", + "description": "Fired when a TestType is ready.", + "parameters": [ + { + "name": "someTestType", + "$ref": "TestType" + } + ] + } ] } ] diff --git a/tools/json_schema_compiler/test/simple_api_unittest.cc b/tools/json_schema_compiler/test/simple_api_unittest.cc index accf32e..bce1838 100644 --- a/tools/json_schema_compiler/test/simple_api_unittest.cc +++ b/tools/json_schema_compiler/test/simple_api_unittest.cc @@ -22,10 +22,10 @@ static scoped_ptr<DictionaryValue> CreateTestTypeDictionary() { } // namespace TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerResultCreate) { - scoped_ptr<Value> result(IncrementInteger::Result::Create(5)); - int temp = 0; - EXPECT_TRUE(result->GetAsInteger(&temp)); - EXPECT_EQ(5, temp); + scoped_ptr<ListValue> results = IncrementInteger::Results::Create(5); + ListValue expected; + expected.Append(Value::CreateIntegerValue(5)); + EXPECT_TRUE(results->Equals(&expected)); } TEST(JsonSchemaCompilerSimpleTest, IncrementIntegerParamsCreate) { @@ -108,9 +108,9 @@ TEST(JsonSchemaCompilerSimpleTest, OptionalBeforeRequired) { } TEST(JsonSchemaCompilerSimpleTest, NoParamsResultCreate) { - scoped_ptr<Value> result(OptionalString::Result::Create()); - scoped_ptr<Value> expected(Value::CreateNullValue()); - EXPECT_TRUE(Value::Equals(result.get(), expected.get())); + scoped_ptr<ListValue> results = OptionalString::Results::Create(); + ListValue expected; + EXPECT_TRUE(results->Equals(&expected)); } TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) { @@ -133,10 +133,48 @@ TEST(JsonSchemaCompilerSimpleTest, TestTypePopulate) { } TEST(JsonSchemaCompilerSimpleTest, GetTestType) { - scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); - scoped_ptr<TestType> test_type(new TestType()); - EXPECT_TRUE(TestType::Populate(*value, test_type.get())); - scoped_ptr<Value> result(GetTestType::Result::Create(*test_type)); - EXPECT_TRUE(value->Equals(result.get())); + { + scoped_ptr<DictionaryValue> value = CreateTestTypeDictionary(); + scoped_ptr<TestType> test_type(new TestType()); + EXPECT_TRUE(TestType::Populate(*value, test_type.get())); + scoped_ptr<ListValue> results = GetTestType::Results::Create(*test_type); + + DictionaryValue* result = NULL; + results->GetDictionary(0, &result); + EXPECT_TRUE(result->Equals(value.get())); + } } +TEST(JsonSchemaCompilerSimpleTest, OnIntegerFiredCreate) { + { + scoped_ptr<ListValue> results(OnIntegerFired::Create(5)); + ListValue expected; + expected.Append(Value::CreateIntegerValue(5)); + EXPECT_TRUE(results->Equals(&expected)); + } +} + +TEST(JsonSchemaCompilerSimpleTest, OnStringFiredCreate) { + { + scoped_ptr<ListValue> results(OnStringFired::Create("yo dawg")); + ListValue expected; + expected.Append(Value::CreateStringValue("yo dawg")); + EXPECT_TRUE(results->Equals(&expected)); + } +} + +TEST(JsonSchemaCompilerSimpleTest, OnTestTypeFiredCreate) { + { + TestType some_test_type; + scoped_ptr<DictionaryValue> expected = CreateTestTypeDictionary(); + ASSERT_TRUE(expected->GetDouble("number", &some_test_type.number)); + ASSERT_TRUE(expected->GetString("string", &some_test_type.string)); + ASSERT_TRUE(expected->GetInteger("integer", &some_test_type.integer)); + ASSERT_TRUE(expected->GetBoolean("boolean", &some_test_type.boolean)); + + scoped_ptr<ListValue> results(OnTestTypeFired::Create(some_test_type)); + DictionaryValue* result = NULL; + results->GetDictionary(0, &result); + EXPECT_TRUE(result->Equals(expected.get())); + } +} |