Skip to content

Commit 80f0fbf

Browse files
authored
Add some tests for constructor in IDL interface. (#116)
Rewrite examples/ tests using jest. ISSUE=#99,#100
1 parent 13dfe16 commit 80f0fbf

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

core/js_type_traits.h

+5
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,9 @@ inline Napi::Value JSTypeTraits(Napi::Env env, int64_t value) {
5050
return Napi::Number::New(env, value);
5151
}
5252

53+
template <>
54+
inline Napi::Value JSTypeTraits(Napi::Env env, const std::string value) {
55+
return Napi::String::New(env, value);
56+
}
57+
5358
#endif // CORE_JS_TYPE_TRAITS_H_

test/interface_constructor.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@ test('Default constructor', async () => {
2222
let test_interface: bacardi.TestInterface = new bacardi.TestInterface();
2323
expect(test_interface instanceof bacardi.TestInterface).toBe(true);
2424
});
25+
26+
test('Calling undefined constructor should throw error', async () => {
27+
expect(() => {
28+
// There is no TestInterface(string) constructor.
29+
new bacardi.TestInterface('Wrong argument');
30+
}).toThrowError();
31+
32+
expect(() => {
33+
// There is no TestInterface(number, number, number) constructor.
34+
new bacardi.TestInterface(1, 2, 3);
35+
}).toThrowError();
36+
});
37+
38+
test('When creating two objects, should be differnt instances', async () => {
39+
let instance1: bacardi.TestInterface = new bacardi.TestInterface();
40+
let instance2: bacardi.TestInterface = new bacardi.TestInterface();
41+
expect(instance1 !== instance2).toBe(true);
42+
});
43+
44+
test('Test for constructor overloading', async () => {
45+
let constructor1 = new bacardi.TestInterface();
46+
expect(constructor1.getCalledConstructorInfo()).toBe('Constructor()');
47+
48+
let constructor2 = new bacardi.TestInterface(1);
49+
expect(constructor2.getCalledConstructorInfo()).toBe('Constructor(long)');
50+
51+
let constructor3 = new bacardi.TestInterface(2, 3);
52+
expect(constructor3.getCalledConstructorInfo())
53+
.toBe('Constructor(long, long)');
54+
55+
let constructor4 = new bacardi.TestInterface('hello', 'world');
56+
expect(constructor4.getCalledConstructorInfo())
57+
.toBe('Constructor(string, string)');
58+
});

test/test_interface.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@
1616

1717
#include "test/test_interface.h"
1818

19-
TestInterface::TestInterface() {}
19+
TestInterface::TestInterface() : called_constructor_info_("Constructor()") {}
20+
21+
TestInterface::TestInterface(long createTime)
22+
: called_constructor_info_("Constructor(long)") {}
23+
24+
TestInterface::TestInterface(long arg1, long arg2)
25+
: called_constructor_info_("Constructor(long, long)") {}
26+
27+
TestInterface::TestInterface(const std::string& msg1, const std::string& msg2)
28+
: called_constructor_info_("Constructor(string, string)") {}
29+
30+
const std::string& TestInterface::GetCalledConstructorInfo() const {
31+
return called_constructor_info_;
32+
}

test/test_interface.h

+10
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@
1717
#ifndef TEST_TEST_INTERFACE_H_
1818
#define TEST_TEST_INTERFACE_H_
1919

20+
#include <string>
21+
2022
class TestInterface {
2123
public:
2224
TestInterface();
25+
TestInterface(long number);
26+
TestInterface(long number1, long number2);
27+
TestInterface(const std::string& string1, const std::string& string2);
28+
29+
const std::string& GetCalledConstructorInfo() const;
30+
31+
private:
32+
const std::string called_constructor_info_;
2333
};
2434

2535
#endif // TEST_TEST_INTERFACE_H_

test/test_interface.idl

+8
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
[
18+
Constructor(),
19+
Constructor(long number),
20+
Constructor(long number1, long number2),
21+
Constructor(string string1, string string2)
22+
]
1723
interface TestInterface {
24+
// FIXME(zino): It's better to use attribute instead of method.
25+
string getCalledConstructorInfo();
1826
};

0 commit comments

Comments
 (0)