Skip to content

Commit

Permalink
Write a test for enum type using jest. (#122)
Browse files Browse the repository at this point in the history
Write a test for enum type using jest.

ISSUE=#99,#100
  • Loading branch information
hwanseung authored and romandev committed Oct 18, 2017
1 parent d4e72e1 commit 0cc9490
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/idl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ struct IDLShort final : public IDLBaseHelper<int16_t> {};
struct IDLString final : public IDLBaseHelper<std::string> {};
// FIXME(Hwansung): should be generated automatically in another file.
struct IDLOperationType final : public IDLBaseHelper<std::string> {};
struct IDLTestEnum final : public IDLBaseHelper<std::string> {};

#endif // CORE_IDL_TYPES_H_
38 changes: 38 additions & 0 deletions core/native_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,42 @@ struct NativeTypeTraits<IDLOperationType>
}
};

template <>
struct NativeTypeTraits<IDLTestEnum>
: public NativeTypeTraitsBase<IDLTestEnum> {
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_
52 changes: 52 additions & 0 deletions test/enum.test.ts
Original file line number Diff line number Diff line change
@@ -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();

});
4 changes: 4 additions & 0 deletions test/test_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ")";
}
3 changes: 3 additions & 0 deletions test/test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions test/test_interface.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};

0 comments on commit 0cc9490

Please sign in to comment.