Skip to content

Commit

Permalink
Add some tests for constructor in IDL interface.
Browse files Browse the repository at this point in the history
Rewrite examples/ tests using jest.

ISSUE=#99,#100
  • Loading branch information
romandev committed Oct 7, 2017
1 parent 13dfe16 commit dbf2223
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/js_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
34 changes: 34 additions & 0 deletions test/interface_constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
});
15 changes: 14 additions & 1 deletion test/test_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
10 changes: 10 additions & 0 deletions test/test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@
#ifndef TEST_TEST_INTERFACE_H_
#define TEST_TEST_INTERFACE_H_

#include <string>

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_
8 changes: 8 additions & 0 deletions test/test_interface.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

0 comments on commit dbf2223

Please sign in to comment.