From b5907d9da748be5850f9b67b84b4316d44757124 Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Sun, 8 Oct 2017 20:12:16 +0900 Subject: [PATCH 1/2] add test for enum --- core/idl_types.h | 1 + core/native_type_traits.h | 38 ++++++++++++++++++++++++++++++ test/enum.test.ts | 49 +++++++++++++++++++++++++++++++++++++++ test/test_interface.cc | 4 ++++ test/test_interface.h | 3 +++ test/test_interface.idl | 9 +++++++ 6 files changed, 104 insertions(+) create mode 100644 test/enum.test.ts diff --git a/core/idl_types.h b/core/idl_types.h index 3047c76..e26d129 100644 --- a/core/idl_types.h +++ b/core/idl_types.h @@ -27,5 +27,6 @@ struct IDLShort final : public IDLBaseHelper {}; struct IDLString final : public IDLBaseHelper {}; // FIXME(Hwansung): should be generated automatically in another file. struct IDLOperationType final : public IDLBaseHelper {}; +struct IDLTestEnum final : public IDLBaseHelper {}; #endif // CORE_IDL_TYPES_H_ diff --git a/core/native_type_traits.h b/core/native_type_traits.h index 4a3c325..f7e836e 100644 --- a/core/native_type_traits.h +++ b/core/native_type_traits.h @@ -199,4 +199,42 @@ struct NativeTypeTraits } }; +template <> +struct NativeTypeTraits + : public NativeTypeTraitsBase { + static std::string NativeValue(const Napi::Env& env, + const Napi::Value& js_value) { + if (!js_value.IsString()) { + Napi::TypeError::New(env, "It's an invalid string.") + .ThrowAsJavaScriptException(); + return std::string(); + } + + std::string value = js_value.ToString().Utf8Value(); + if (!IsValidValue(value)) { + Napi::TypeError::New(env, "it not matched with values of enum in idl.") + .ThrowAsJavaScriptException(); + return std::string(); + } + + return js_value.ToString().Utf8Value(); + } + + static bool IsTypeEquals(const Napi::Value& js_value) { + if (js_value.IsString()) { + std::string value = js_value.ToString().Utf8Value(); + return IsValidValue(value); + } + return false; + } + + static bool IsValidValue(std::string value) { + if (value.compare("value1") == 0 || value.compare("value2") == 0 || + value.compare("value3") == 0) { + return true; + } + return false; + } +}; + #endif // CORE_NATIVE_TYPE_TRAITS_H_ diff --git a/test/enum.test.ts b/test/enum.test.ts new file mode 100644 index 0000000..966f0dc --- /dev/null +++ b/test/enum.test.ts @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2017 The Bacardi Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as bindings from 'bindings'; + +const bacardi = bindings('bacardi.node'); + +test('Test for enum', async () => { + let test_interface = new bacardi.TestInterface(); + + test_interface.EnumMethod('value1'); + expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value1)'); + + test_interface.EnumMethod('value2'); + expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value2)'); + + test_interface.EnumMethod('value3'); + expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value3)'); +}); + +test('Passing unexpected enum value should throw error', async () => { + let test_interface = new bacardi.TestInterface(); + + expect(() => { + test_interface.EnumMethod(1); + }).toThrowError(); + + expect(() => { + test_interface.EnumMethod(''); + }).toThrowError(); + + expect(() => { + test_interface.EnumMethod('value'); + }).toThrowError(); + +}); diff --git a/test/test_interface.cc b/test/test_interface.cc index fd7d005..99bb6f6 100644 --- a/test/test_interface.cc +++ b/test/test_interface.cc @@ -64,3 +64,7 @@ double TestInterface::DoubleMethod(double number) { const std::string TestInterface::StringMethod(const std::string& string) { return string; } + +void TestInterface::EnumMethod(const std::string& string) { + last_call_info_ = "EnumMethod(" + string + ")"; +} diff --git a/test/test_interface.h b/test/test_interface.h index c5084e9..e1ed245 100644 --- a/test/test_interface.h +++ b/test/test_interface.h @@ -37,6 +37,9 @@ class TestInterface { double DoubleMethod(double number); const std::string StringMethod(const std::string& string); + // Enum + void EnumMethod(const std::string& string); + private: // FIXME(zino): Currently, we should set this variable in each methods. It's // not elegance way. We should find a way to get function name and signature diff --git a/test/test_interface.idl b/test/test_interface.idl index 379e97f..fba81bf 100644 --- a/test/test_interface.idl +++ b/test/test_interface.idl @@ -32,4 +32,13 @@ interface TestInterface { short shortMethod(short number); double doubleMethod(double number); string stringMethod(string string); + + // enum + void EnumMethod(TestEnum enumValue); }; + +enum TestEnum { + "value1", + "value2", + "value3" +}; \ No newline at end of file From 273d0d06fe3200412aca7067a90930d69c5b84b9 Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Sun, 8 Oct 2017 22:20:55 +0900 Subject: [PATCH 2/2] replace function name --- test/enum.test.ts | 21 ++++++++++++--------- test/test_interface.cc | 4 ++-- test/test_interface.h | 2 +- test/test_interface.idl | 2 +- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/test/enum.test.ts b/test/enum.test.ts index 966f0dc..ae7772f 100644 --- a/test/enum.test.ts +++ b/test/enum.test.ts @@ -21,29 +21,32 @@ const bacardi = bindings('bacardi.node'); test('Test for enum', async () => { let test_interface = new bacardi.TestInterface(); - test_interface.EnumMethod('value1'); - expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value1)'); + test_interface.voidMethodTestEnumArg('value1'); + expect(bacardi.TestInterface.getLastCallInfo()) + .toBe('VoidMethodTestEnumArg(value1)'); - test_interface.EnumMethod('value2'); - expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value2)'); + test_interface.voidMethodTestEnumArg('value2'); + expect(bacardi.TestInterface.getLastCallInfo()) + .toBe('VoidMethodTestEnumArg(value2)'); - test_interface.EnumMethod('value3'); - expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value3)'); + test_interface.voidMethodTestEnumArg('value3'); + expect(bacardi.TestInterface.getLastCallInfo()) + .toBe('VoidMethodTestEnumArg(value3)'); }); test('Passing unexpected enum value should throw error', async () => { let test_interface = new bacardi.TestInterface(); expect(() => { - test_interface.EnumMethod(1); + test_interface.voidMethodTestEnumArg(1); }).toThrowError(); expect(() => { - test_interface.EnumMethod(''); + test_interface.voidMethodTestEnumArg(''); }).toThrowError(); expect(() => { - test_interface.EnumMethod('value'); + test_interface.voidMethodTestEnumArg('value'); }).toThrowError(); }); diff --git a/test/test_interface.cc b/test/test_interface.cc index 99bb6f6..0eece45 100644 --- a/test/test_interface.cc +++ b/test/test_interface.cc @@ -65,6 +65,6 @@ const std::string TestInterface::StringMethod(const std::string& string) { return string; } -void TestInterface::EnumMethod(const std::string& string) { - last_call_info_ = "EnumMethod(" + string + ")"; +void TestInterface::VoidMethodTestEnumArg(const std::string& string) { + last_call_info_ = "VoidMethodTestEnumArg(" + string + ")"; } diff --git a/test/test_interface.h b/test/test_interface.h index e1ed245..d096b94 100644 --- a/test/test_interface.h +++ b/test/test_interface.h @@ -38,7 +38,7 @@ class TestInterface { const std::string StringMethod(const std::string& string); // Enum - void EnumMethod(const std::string& string); + void VoidMethodTestEnumArg(const std::string& string); private: // FIXME(zino): Currently, we should set this variable in each methods. It's diff --git a/test/test_interface.idl b/test/test_interface.idl index fba81bf..5af1b2f 100644 --- a/test/test_interface.idl +++ b/test/test_interface.idl @@ -34,7 +34,7 @@ interface TestInterface { string stringMethod(string string); // enum - void EnumMethod(TestEnum enumValue); + void voidMethodTestEnumArg(TestEnum enumValue); }; enum TestEnum {