Skip to content

Commit

Permalink
VENOM-302: Mock function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent 8290b5b commit 3aff650
Show file tree
Hide file tree
Showing 13 changed files with 453 additions and 120 deletions.
1 change: 1 addition & 0 deletions data/im.tox.venom.desktop.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Icon=im.tox.venom-symbolic
Categories=InstantMessaging;Network;AudioVideo;GTK;
Terminal=false
MimeType=x-scheme-handler/tox;
DBusActivatable=true
5 changes: 3 additions & 2 deletions src/testing/TestDhtNodeDb.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ public class TestDhtNodeDb : UnitTest {
logger = new MockLogger();
statement = new MockStatement();
builder = new SqliteStatementWrapper.Builder(statement);
mock().when(statement, "builder").then_return_object(builder);
statement_factory = new MockStatementFactory();
mock().when(statement_factory, "createStatement").then_return_object(statement);
node_factory = new MockDhtNodeFactory();

mock().when(statement, "builder").then_return_object(builder);
mock().when(statement_factory, "createStatement").then_return_object(statement);
}

private void test_init() throws GLib.Error {
Expand Down
52 changes: 38 additions & 14 deletions src/testing/TestMock.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,59 @@ using Mock;
namespace TestMock {

private static void test_mock() {
mock().expect_one_call(null, "a");
mock().actual_call(null, "a");
check_expectations_noerror();
var o = new GLib.Object();
mock().actual_call(o, "a");
mock().verify(o, "a");
mock().clear();
}

private static void test_mock_empty() {
check_expectations_noerror();
// check_expectations_noerror();
}

private static void test_mock_fail() {
mock().expect_one_call(null, "a");
var o = new GLib.Object();
try {
mock().check_expectations();
} catch (Error e) {
mock().verify(o, "");
mock().clear();
} catch (Error e) {
return;
}
Test.fail();
}

private static void test_mock_calls() {
mock().expect_calls(null, "b", 1);
mock().actual_call(null, "b");
check_expectations_noerror();
var o = new GLib.Object();
mock().actual_call(o, "b");
mock().verify_count(o, "b", 1);
mock().clear();
}

private static void test_mock_calls_multi() {
mock().expect_calls(null, "c", 2);
mock().actual_call(null, "c");
mock().actual_call(null, "c");
check_expectations_noerror();
var o = new GLib.Object();
mock().actual_call(o, "c");
mock().actual_call(o, "c");
mock().verify_count(o, "c", 2);
mock().clear();
}

private static void test_mock_calls_int_arg() {
var o = new GLib.Object();
mock().actual_call(o, "c", args().int(1).create());
mock().verify(o, "c", args().int(1).create());
mock().clear();
}

private static void test_mock_calls_int_arg_fail() {
var o = new GLib.Object();
try {
mock().actual_call(o, "c", args().int(1).create());
mock().verify(o, "c", args().int(2).create());
} catch {
mock().clear();
return;
}
Test.fail();
}

private static void main(string[] args) {
Expand All @@ -65,6 +87,8 @@ namespace TestMock {
Test.add_func("/test_mock_fail", test_mock_fail);
Test.add_func("/test_mock_calls", test_mock_calls);
Test.add_func("/test_mock_calls_multi", test_mock_calls_multi);
Test.add_func("/test_mock_calls_int_arg", test_mock_calls_int_arg);
Test.add_func("/test_mock_calls_int_arg_fail", test_mock_calls_int_arg_fail);
Test.run();
}
}
16 changes: 9 additions & 7 deletions src/testing/TestToxAdapterFiletransferListener.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public class TestToxAdapterFiletransferListener : UnitTest {
session = new MockToxSession();
transfer = new MockFiletransfer();

mock().when(transfer, "get_friend_number").then_return_int(1);
mock().when(transfer, "get_file_number").then_return_int(2);

listener = new ToxAdapterFiletransferListenerImpl(logger, transfers, notification_listener);
}

Expand All @@ -60,18 +57,22 @@ public class TestToxAdapterFiletransferListener : UnitTest {
Assert.assert_not_null(listener);
listener.attach_to_session(session);

mock().verify(session, "set_file_transfer_listener");
mock().verify(session, "set_file_transfer_listener", args().object(listener).create());
}

private void test_start_transfer() throws Error {
Assert.assert_not_null(listener);

mock().when(transfer, "get_friend_number").then_return_int(1);
mock().when(transfer, "get_file_number").then_return_int(2);

listener.attach_to_session(session);
listener.start_transfer(transfer);

mock().verify(transfer, "get_friend_number");
mock().verify(transfer, "get_file_number");
mock().verify(session, "file_control");
mock().verify(transfer, "set_state");
mock().verify(session, "file_control", args().uint(1).uint(2).int(ToxCore.FileControl.RESUME).create());
mock().verify(transfer, "set_state", args().int(FileTransferState.RUNNING).create());
}

private void test_remove_transfer() throws Error {
Expand All @@ -82,7 +83,8 @@ public class TestToxAdapterFiletransferListener : UnitTest {
}

private void test_remove_exception() throws Error {
mock().when(session, "file_control").then_throw(new ToxError.GENERIC(""));
mock().when(session, "file_control", args().uint(0).uint(0).int(ToxCore.FileControl.CANCEL).create())
.then_throw(new ToxError.GENERIC("this should be caught"));

transfers.append(transfer);
listener.attach_to_session(session);
Expand Down
2 changes: 1 addition & 1 deletion src/testing/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ test_tox_transfer = executable('test_tox_transfer', ['TestToxAdapterFiletransfer

test('test glib', test_glib_testing)
test('test mocking framework', test_mock)
test('test tox core vapi', test_tox_transfer)
test('test tox core vapi', test_tox_core)
test('test tox transfer', test_tox_transfer)
test('test observable list', test_observable_list)
test('test venom config', test_venom_config)
Expand Down

0 comments on commit 3aff650

Please sign in to comment.