Skip to content

Commit 5459720

Browse files
hwanseungromandev
authored andcommitted
Write a test for enum type using jest. (#122)
Write a test for enum type using jest. ISSUE=#99,#100
1 parent f617601 commit 5459720

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

core/idl_types.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ struct IDLShort final : public IDLBaseHelper<int16_t> {};
2727
struct IDLString final : public IDLBaseHelper<std::string> {};
2828
// FIXME(Hwansung): should be generated automatically in another file.
2929
struct IDLOperationType final : public IDLBaseHelper<std::string> {};
30+
struct IDLTestEnum final : public IDLBaseHelper<std::string> {};
3031

3132
#endif // CORE_IDL_TYPES_H_

core/native_type_traits.h

+38
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,42 @@ struct NativeTypeTraits<IDLOperationType>
199199
}
200200
};
201201

202+
template <>
203+
struct NativeTypeTraits<IDLTestEnum>
204+
: public NativeTypeTraitsBase<IDLTestEnum> {
205+
static std::string NativeValue(const Napi::Env& env,
206+
const Napi::Value& js_value) {
207+
if (!js_value.IsString()) {
208+
Napi::TypeError::New(env, "It's an invalid string.")
209+
.ThrowAsJavaScriptException();
210+
return std::string();
211+
}
212+
213+
std::string value = js_value.ToString().Utf8Value();
214+
if (!IsValidValue(value)) {
215+
Napi::TypeError::New(env, "it not matched with values of enum in idl.")
216+
.ThrowAsJavaScriptException();
217+
return std::string();
218+
}
219+
220+
return js_value.ToString().Utf8Value();
221+
}
222+
223+
static bool IsTypeEquals(const Napi::Value& js_value) {
224+
if (js_value.IsString()) {
225+
std::string value = js_value.ToString().Utf8Value();
226+
return IsValidValue(value);
227+
}
228+
return false;
229+
}
230+
231+
static bool IsValidValue(std::string value) {
232+
if (value.compare("value1") == 0 || value.compare("value2") == 0 ||
233+
value.compare("value3") == 0) {
234+
return true;
235+
}
236+
return false;
237+
}
238+
};
239+
202240
#endif // CORE_NATIVE_TYPE_TRAITS_H_

test/enum.test.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2017 The Bacardi Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import * as bindings from 'bindings';
18+
19+
const bacardi = bindings('bacardi.node');
20+
21+
test('Test for enum', async () => {
22+
let test_interface = new bacardi.TestInterface();
23+
24+
test_interface.voidMethodTestEnumArg('value1');
25+
expect(bacardi.TestInterface.getLastCallInfo())
26+
.toBe('VoidMethodTestEnumArg(value1)');
27+
28+
test_interface.voidMethodTestEnumArg('value2');
29+
expect(bacardi.TestInterface.getLastCallInfo())
30+
.toBe('VoidMethodTestEnumArg(value2)');
31+
32+
test_interface.voidMethodTestEnumArg('value3');
33+
expect(bacardi.TestInterface.getLastCallInfo())
34+
.toBe('VoidMethodTestEnumArg(value3)');
35+
});
36+
37+
test('Passing unexpected enum value should throw error', async () => {
38+
let test_interface = new bacardi.TestInterface();
39+
40+
expect(() => {
41+
test_interface.voidMethodTestEnumArg(1);
42+
}).toThrowError();
43+
44+
expect(() => {
45+
test_interface.voidMethodTestEnumArg('');
46+
}).toThrowError();
47+
48+
expect(() => {
49+
test_interface.voidMethodTestEnumArg('value');
50+
}).toThrowError();
51+
52+
});

test/test_interface.cc

+4
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,7 @@ double TestInterface::DoubleMethod(double number) {
6464
const std::string TestInterface::StringMethod(const std::string& string) {
6565
return string;
6666
}
67+
68+
void TestInterface::VoidMethodTestEnumArg(const std::string& string) {
69+
last_call_info_ = "VoidMethodTestEnumArg(" + string + ")";
70+
}

test/test_interface.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class TestInterface {
3737
double DoubleMethod(double number);
3838
const std::string StringMethod(const std::string& string);
3939

40+
// Enum
41+
void VoidMethodTestEnumArg(const std::string& string);
42+
4043
private:
4144
// FIXME(zino): Currently, we should set this variable in each methods. It's
4245
// not elegance way. We should find a way to get function name and signature

test/test_interface.idl

+9
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,13 @@ interface TestInterface {
3232
short shortMethod(short number);
3333
double doubleMethod(double number);
3434
string stringMethod(string string);
35+
36+
// enum
37+
void voidMethodTestEnumArg(TestEnum enumValue);
3538
};
39+
40+
enum TestEnum {
41+
"value1",
42+
"value2",
43+
"value3"
44+
};

0 commit comments

Comments
 (0)