Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write a test for enum type using jest. #122

Merged
merged 2 commits into from
Oct 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_
49 changes: 49 additions & 0 deletions test/enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* 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.EnumMethod('value1');
expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value1)');

test_interface.EnumMethod('value2');
expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value2)');

test_interface.EnumMethod('value3');
expect(bacardi.TestInterface.getLastCallInfo()).toBe('EnumMethod(value3)');
});

test('Passing unexpected enum value should throw error', async () => {
let test_interface = new bacardi.TestInterface();

expect(() => {
test_interface.EnumMethod(1);
}).toThrowError();

expect(() => {
test_interface.EnumMethod('');
}).toThrowError();

expect(() => {
test_interface.EnumMethod('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::EnumMethod(const std::string& string) {
last_call_info_ = "EnumMethod(" + string + ")";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string should be IDL definition like other methods.
"void voidMethodTestEnumArg(TestEnum)";

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed it.

}
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 EnumMethod(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 EnumMethod(TestEnum enumValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should be enumMethod(...) in IDL.
Also, I think better name is voidMethodTestEnumArg(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i fixed it.

};

enum TestEnum {
"value1",
"value2",
"value3"
};