Skip to content

Commit

Permalink
Write a test for static method in IDL interface. (#117)
Browse files Browse the repository at this point in the history
This patch also changes getCalledConstructorInfo() to static method and
rename it with getLastCallInfo() and then use it use generally.

ISSUE=#99,#100
  • Loading branch information
romandev authored Oct 8, 2017
1 parent 80f0fbf commit 8de1fbb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
8 changes: 4 additions & 4 deletions test/interface_constructor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ test('When creating two objects, should be differnt instances', async () => {

test('Test for constructor overloading', async () => {
let constructor1 = new bacardi.TestInterface();
expect(constructor1.getCalledConstructorInfo()).toBe('Constructor()');
expect(bacardi.TestInterface.getLastCallInfo()).toBe('Constructor()');

let constructor2 = new bacardi.TestInterface(1);
expect(constructor2.getCalledConstructorInfo()).toBe('Constructor(long)');
expect(bacardi.TestInterface.getLastCallInfo()).toBe('Constructor(long)');

let constructor3 = new bacardi.TestInterface(2, 3);
expect(constructor3.getCalledConstructorInfo())
expect(bacardi.TestInterface.getLastCallInfo())
.toBe('Constructor(long, long)');

let constructor4 = new bacardi.TestInterface('hello', 'world');
expect(constructor4.getCalledConstructorInfo())
expect(bacardi.TestInterface.getLastCallInfo())
.toBe('Constructor(string, string)');
});
29 changes: 29 additions & 0 deletions test/interface_static_method.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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('Basic of static method', async () => {
// We can call the static methods without object instantiation.
bacardi.TestInterface.staticMethod1();
expect(bacardi.TestInterface.getLastCallInfo())
.toBe('static void staticMethod1()');
bacardi.TestInterface.staticMethod2(10, 'test');
expect(bacardi.TestInterface.getLastCallInfo())
.toBe('static boolean staticMethod2(long, string)');
});
36 changes: 27 additions & 9 deletions test/test_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,35 @@

#include "test/test_interface.h"

TestInterface::TestInterface() : called_constructor_info_("Constructor()") {}
// static
std::string TestInterface::last_call_info_;

TestInterface::TestInterface(long createTime)
: called_constructor_info_("Constructor(long)") {}
TestInterface::TestInterface() {
last_call_info_ = "Constructor()";
}

TestInterface::TestInterface(long arg1, long arg2)
: called_constructor_info_("Constructor(long, long)") {}
TestInterface::TestInterface(long number) {
last_call_info_ = "Constructor(long)";
}

TestInterface::TestInterface(const std::string& msg1, const std::string& msg2)
: called_constructor_info_("Constructor(string, string)") {}
TestInterface::TestInterface(long number1, long number2) {
last_call_info_ = "Constructor(long, long)";
}

TestInterface::TestInterface(const std::string& string1,
const std::string& string2) {
last_call_info_ = "Constructor(string, string)";
}

const std::string& TestInterface::GetLastCallInfo() {
return last_call_info_;
}

void TestInterface::StaticMethod1() {
last_call_info_ = "static void staticMethod1()";
}

const std::string& TestInterface::GetCalledConstructorInfo() const {
return called_constructor_info_;
bool TestInterface::StaticMethod2(long number, const std::string& string) {
last_call_info_ = "static boolean staticMethod2(long, string)";
return 0;
}
11 changes: 9 additions & 2 deletions test/test_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ class TestInterface {
TestInterface(long number1, long number2);
TestInterface(const std::string& string1, const std::string& string2);

const std::string& GetCalledConstructorInfo() const;
static const std::string& GetLastCallInfo();

static void StaticMethod1();
static bool StaticMethod2(long number, const std::string& string);

private:
const std::string called_constructor_info_;
// 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
// automatically. (I tried __FUNCTION__ and __PRETTY_FUNCTION__ but they are
// dependent on each platform.
static std::string last_call_info_;
};

#endif // TEST_TEST_INTERFACE_H_
5 changes: 4 additions & 1 deletion test/test_interface.idl
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
]
interface TestInterface {
// FIXME(zino): It's better to use attribute instead of method.
string getCalledConstructorInfo();
static string getLastCallInfo();

static void staticMethod1();
static boolean staticMethod2(long number, string string);
};

0 comments on commit 8de1fbb

Please sign in to comment.