From 5ea24d0d69e43fdca49b90278da728334b048386 Mon Sep 17 00:00:00 2001 From: Jinho Bang Date: Sun, 8 Oct 2017 17:05:09 +0900 Subject: [PATCH] Write a test for Basic Types. (#119) Write a test for Basic Types such as boolean, short, double, and string. Not include long type testing in this patch because it has some problem. We should fix the issue in follow-up patch. ISSUE=#99,#100 --- test/interface_basic_types.test.ts | 63 ++++++++++++++++++++++++++++++ test/test_interface.cc | 16 ++++++++ test/test_interface.h | 6 +++ test/test_interface.idl | 6 +++ 4 files changed, 91 insertions(+) create mode 100644 test/interface_basic_types.test.ts diff --git a/test/interface_basic_types.test.ts b/test/interface_basic_types.test.ts new file mode 100644 index 0000000..93d13f4 --- /dev/null +++ b/test/interface_basic_types.test.ts @@ -0,0 +1,63 @@ +/** + * 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 IDL \'boolean\' type', async () => { + let test_interface = new bacardi.TestInterface(); + + // The boolean type has two values: true and false. + expect(test_interface.booleanMethod(true)).toBe(true); + expect(test_interface.booleanMethod(false)).toBe(false); +}); + +test('Test for IDL \'short\' type', async () => { + let test_interface = new bacardi.TestInterface(); + + // The short type is a signed integer type that has values in the range + // [−32768, 32767]. + expect(test_interface.shortMethod(32767)).toBe(32767); + expect(test_interface.shortMethod(-32768)).toBe(-32768); + expect(test_interface.shortMethod(32768) != 32768).toBe(true); + expect(test_interface.shortMethod(-32769) != -32769).toBe(true); +}); + +// FIXME(zino): We should write a test for long type. +// Please see: https://github.com/lunchclass/bacardi/issues/120 + +test('Test for IDL \'double\' type', async () => { + let test_interface = new bacardi.TestInterface(); + + // The double type is a floating point numeric type that corresponds to the + // set of finite double-precision 64 bit IEEE 754 floating point numbers. + expect(test_interface.doubleMethod(0.123456789012345)) + .toBe(0.123456789012345); + + // FIXME(zino): We should test for comparing single-precision floating point. + + // FIXME(zino): We should check whether the type is restricted or + // unrestricted. +}); + +test('Test for IDL \'string\' type', async () => { + let test_interface = new bacardi.TestInterface(); + + // The string type is not exact matching WebIDL spec but it will convert + // UTF8 string in platform side. + expect(test_interface.stringMethod('Hello World!')).toBe('Hello World!'); +}); diff --git a/test/test_interface.cc b/test/test_interface.cc index 231839a..fd7d005 100644 --- a/test/test_interface.cc +++ b/test/test_interface.cc @@ -48,3 +48,19 @@ bool TestInterface::StaticMethod2(long number, const std::string& string) { last_call_info_ = "static boolean staticMethod2(long, string)"; return 0; } + +bool TestInterface::BooleanMethod(bool boolean) { + return boolean; +} + +short TestInterface::ShortMethod(short number) { + return number; +} + +double TestInterface::DoubleMethod(double number) { + return number; +} + +const std::string TestInterface::StringMethod(const std::string& string) { + return string; +} diff --git a/test/test_interface.h b/test/test_interface.h index 28d74df..c5084e9 100644 --- a/test/test_interface.h +++ b/test/test_interface.h @@ -31,6 +31,12 @@ class TestInterface { static void StaticMethod1(); static bool StaticMethod2(long number, const std::string& string); + // Basic types + bool BooleanMethod(bool boolean); + short ShortMethod(short number); + double DoubleMethod(double number); + const std::string StringMethod(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 diff --git a/test/test_interface.idl b/test/test_interface.idl index 88709f3..379e97f 100644 --- a/test/test_interface.idl +++ b/test/test_interface.idl @@ -26,4 +26,10 @@ interface TestInterface { static void staticMethod1(); static boolean staticMethod2(long number, string string); + + // Basic types + boolean booleanMethod(boolean boolean); + short shortMethod(short number); + double doubleMethod(double number); + string stringMethod(string string); };