From 80f0fbf344eb131e5786badbd57e0ff50062c48d Mon Sep 17 00:00:00 2001 From: Jinho Bang Date: Sun, 8 Oct 2017 10:49:24 +0900 Subject: [PATCH] Add some tests for constructor in IDL interface. (#116) Rewrite examples/ tests using jest. ISSUE=#99,#100 --- core/js_type_traits.h | 5 +++++ test/interface_constructor.test.ts | 34 ++++++++++++++++++++++++++++++ test/test_interface.cc | 15 ++++++++++++- test/test_interface.h | 10 +++++++++ test/test_interface.idl | 8 +++++++ 5 files changed, 71 insertions(+), 1 deletion(-) diff --git a/core/js_type_traits.h b/core/js_type_traits.h index 6992984..f307062 100644 --- a/core/js_type_traits.h +++ b/core/js_type_traits.h @@ -50,4 +50,9 @@ inline Napi::Value JSTypeTraits(Napi::Env env, int64_t value) { return Napi::Number::New(env, value); } +template <> +inline Napi::Value JSTypeTraits(Napi::Env env, const std::string value) { + return Napi::String::New(env, value); +} + #endif // CORE_JS_TYPE_TRAITS_H_ diff --git a/test/interface_constructor.test.ts b/test/interface_constructor.test.ts index 6447e71..96c5889 100644 --- a/test/interface_constructor.test.ts +++ b/test/interface_constructor.test.ts @@ -22,3 +22,37 @@ test('Default constructor', async () => { let test_interface: bacardi.TestInterface = new bacardi.TestInterface(); expect(test_interface instanceof bacardi.TestInterface).toBe(true); }); + +test('Calling undefined constructor should throw error', async () => { + expect(() => { + // There is no TestInterface(string) constructor. + new bacardi.TestInterface('Wrong argument'); + }).toThrowError(); + + expect(() => { + // There is no TestInterface(number, number, number) constructor. + new bacardi.TestInterface(1, 2, 3); + }).toThrowError(); +}); + +test('When creating two objects, should be differnt instances', async () => { + let instance1: bacardi.TestInterface = new bacardi.TestInterface(); + let instance2: bacardi.TestInterface = new bacardi.TestInterface(); + expect(instance1 !== instance2).toBe(true); +}); + +test('Test for constructor overloading', async () => { + let constructor1 = new bacardi.TestInterface(); + expect(constructor1.getCalledConstructorInfo()).toBe('Constructor()'); + + let constructor2 = new bacardi.TestInterface(1); + expect(constructor2.getCalledConstructorInfo()).toBe('Constructor(long)'); + + let constructor3 = new bacardi.TestInterface(2, 3); + expect(constructor3.getCalledConstructorInfo()) + .toBe('Constructor(long, long)'); + + let constructor4 = new bacardi.TestInterface('hello', 'world'); + expect(constructor4.getCalledConstructorInfo()) + .toBe('Constructor(string, string)'); +}); diff --git a/test/test_interface.cc b/test/test_interface.cc index 90e0219..0c1c975 100644 --- a/test/test_interface.cc +++ b/test/test_interface.cc @@ -16,4 +16,17 @@ #include "test/test_interface.h" -TestInterface::TestInterface() {} +TestInterface::TestInterface() : called_constructor_info_("Constructor()") {} + +TestInterface::TestInterface(long createTime) + : called_constructor_info_("Constructor(long)") {} + +TestInterface::TestInterface(long arg1, long arg2) + : called_constructor_info_("Constructor(long, long)") {} + +TestInterface::TestInterface(const std::string& msg1, const std::string& msg2) + : called_constructor_info_("Constructor(string, string)") {} + +const std::string& TestInterface::GetCalledConstructorInfo() const { + return called_constructor_info_; +} diff --git a/test/test_interface.h b/test/test_interface.h index fe3f701..648c1d0 100644 --- a/test/test_interface.h +++ b/test/test_interface.h @@ -17,9 +17,19 @@ #ifndef TEST_TEST_INTERFACE_H_ #define TEST_TEST_INTERFACE_H_ +#include + class TestInterface { public: TestInterface(); + TestInterface(long number); + TestInterface(long number1, long number2); + TestInterface(const std::string& string1, const std::string& string2); + + const std::string& GetCalledConstructorInfo() const; + + private: + const std::string called_constructor_info_; }; #endif // TEST_TEST_INTERFACE_H_ diff --git a/test/test_interface.idl b/test/test_interface.idl index dc1c180..a3b3555 100644 --- a/test/test_interface.idl +++ b/test/test_interface.idl @@ -14,5 +14,13 @@ * limitations under the License. */ +[ + Constructor(), + Constructor(long number), + Constructor(long number1, long number2), + Constructor(string string1, string string2) +] interface TestInterface { + // FIXME(zino): It's better to use attribute instead of method. + string getCalledConstructorInfo(); };