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..ae7772f --- /dev/null +++ b/test/enum.test.ts @@ -0,0 +1,52 @@ +/** + * 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.voidMethodTestEnumArg('value1'); + expect(bacardi.TestInterface.getLastCallInfo()) + .toBe('VoidMethodTestEnumArg(value1)'); + + test_interface.voidMethodTestEnumArg('value2'); + expect(bacardi.TestInterface.getLastCallInfo()) + .toBe('VoidMethodTestEnumArg(value2)'); + + 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.voidMethodTestEnumArg(1); + }).toThrowError(); + + expect(() => { + test_interface.voidMethodTestEnumArg(''); + }).toThrowError(); + + expect(() => { + test_interface.voidMethodTestEnumArg('value'); + }).toThrowError(); + +}); diff --git a/test/test_interface.cc b/test/test_interface.cc index fd7d005..0eece45 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::VoidMethodTestEnumArg(const std::string& string) { + last_call_info_ = "VoidMethodTestEnumArg(" + string + ")"; +} diff --git a/test/test_interface.h b/test/test_interface.h index c5084e9..d096b94 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 VoidMethodTestEnumArg(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..5af1b2f 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 voidMethodTestEnumArg(TestEnum enumValue); }; + +enum TestEnum { + "value1", + "value2", + "value3" +}; \ No newline at end of file