Skip to content

Commit

Permalink
VENOM-302: Avoid recompilation for testing
Browse files Browse the repository at this point in the history
* Move mocks out of util
* Add mox mock library
  • Loading branch information
naxuroqa committed Apr 8, 2018
1 parent 4b7f656 commit 57f0661
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 167 deletions.
2 changes: 1 addition & 1 deletion src/core/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with Venom. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Venom {
class Application : Gtk.Application {
public class Application : Gtk.Application {
private const GLib.ActionEntry app_entries[] = {
{ "preferences", on_preferences, null, null, null },
{ "about", on_about, null, null, null },
Expand Down
20 changes: 13 additions & 7 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ venom_icons_resources = gnome.compile_resources( 'venom_icons_resources', 'icons
source_dir : 'icons',
c_name : 'c_venom_icons_res')

venom_source = [files(
venom_source = files(
'core/Application.vala',
'core/Contact.vala',
'core/FileTransfer.vala',
Expand Down Expand Up @@ -94,14 +94,20 @@ venom_source = [files(
'viewmodel/FriendInfoViewModel.vala',
'viewmodel/MessageViewModel.vala',
'viewmodel/UserInfoViewModel.vala'
),
venom_ui_resources,
venom_icons_resources
]
)

venom_deps = [gtk_dep, gio_dep, gmodule_dep, gee_dep, sqlite_dep, json_dep, tox_dep, toxav_dep, config_dep, soup_dep]

venom_dep = [gtk_dep, gio_dep, gmodule_dep, gee_dep, sqlite_dep, json_dep, tox_dep, toxav_dep, config_dep, soup_dep]
venom_lib = static_library('venom', [venom_source, venom_ui_resources, venom_icons_resources],
dependencies : venom_deps
)

venom_dep = declare_dependency(
dependencies : venom_deps,
link_with : venom_lib
)

venom_binary = executable('venom', ['Main.vala', venom_source],
venom_binary = executable('venom', ['Main.vala', venom_ui_resources, venom_icons_resources],
dependencies : venom_dep,
install : true
)
50 changes: 30 additions & 20 deletions src/testing/TestAbout.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* TestAbout.vala
*
* Copyright (C) 2017 Venom authors and contributors
* Copyright (C) 2017-2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand All @@ -20,28 +20,41 @@
*/

using Venom;
using Mock;
using Testing;

namespace TestAbout {
public class TestAbout : UnitTest {
private ILogger logger;

private static void testGtk() {
public TestAbout() {
add_func("/test_gtk", test_gtk);
add_func("/test_about", test_about);
add_func("/test_about_text", test_about_text);
}

public override void set_up() throws Error {
logger = new MockLogger();
}

private void test_gtk() throws Error {
var widget = new Gtk.Button();
assert(widget is Gtk.Button);
Assert.assert_true(widget is Gtk.Button);
}

private static void testAbout() {
var widget = new Venom.AboutDialog(new Mock.MockLogger());
assert(widget is Gtk.AboutDialog);
private void test_about() throws Error {
var widget = new Venom.AboutDialog(logger);
Assert.assert_true(widget is Gtk.AboutDialog);
}

private static void testAboutText() {
var widget = new Venom.AboutDialog(new Mock.MockLogger());
assert(widget.authors != null);
assert(widget.artists != null);
assert(widget.comments == Config.SHORT_DESCRIPTION);
assert(widget.translator_credits != null);
assert(widget.copyright == Config.COPYRIGHT_NOTICE);
assert(widget.license_type == Gtk.License.GPL_3_0);
assert(widget.logo == null);
private void test_about_text() throws Error {
var widget = new Venom.AboutDialog(logger);
Assert.assert_not_null(widget.authors);
Assert.assert_not_null(widget.artists);
Assert.assert_equals<string>(widget.comments, Config.SHORT_DESCRIPTION);
Assert.assert_not_null(widget.translator_credits);
Assert.assert_equals<string>(widget.copyright, Config.COPYRIGHT_NOTICE);
Assert.assert_equals<uint>(widget.license_type, Gtk.License.GPL_3_0);
Assert.assert_null(widget.logo);
}

private static int main(string[] args) {
Expand All @@ -53,11 +66,8 @@ namespace TestAbout {

Gtk.init(ref args);

Test.add_func("/test_gtk", testGtk);
Test.add_func("/test_about", testAbout);
Test.add_func("/test_about_text", testAboutText);

Idle.add(() => {
var test = new TestAbout();
Test.run();
Gtk.main_quit();
return false;
Expand Down
29 changes: 17 additions & 12 deletions src/testing/TestContact.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,36 @@
*/

using Venom;
using Mock;
using Testing;

namespace TestContact {
public class TestContact : UnitTest {
public TestContact() {
add_func("/test_contact", test_contact);
add_func("/test_contact_name", test_contact_name);
}

private static void testContact() {
private static void test_contact() throws Error {
var public_key = new uint8[ToxCore.public_key_size()];
var contact = new Contact(1, "id");
assert(contact.tox_friend_number == 1);
Assert.assert_true(contact.tox_friend_number == 1);

assert(contact.get_id() == "id");
assert(contact.get_name_string() == "id");
assert(contact.get_status_string() == "");
assert(contact.get_status() == UserStatus.OFFLINE);
Assert.assert_true(contact.get_id() == "id");
Assert.assert_true(contact.get_name_string() == "id");
Assert.assert_true(contact.get_status_string() == "");
Assert.assert_true(contact.get_status() == UserStatus.OFFLINE);
}

private static void testContactName() {
private static void test_contact_name() throws Error {
var contact = new Contact(0, "");
contact.name = "name";
assert(contact.name == "name");
assert(contact.get_name_string() == "name");
Assert.assert_true(contact.name == "name");
Assert.assert_true(contact.get_name_string() == "name");
}

private static int main(string[] args) {
Test.init(ref args);
Test.add_func("/test_contact", testContact);
Test.add_func("/test_contact_name", testContactName);
var test = new TestContact();
Test.run();
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions src/testing/TestDhtNodeDb.vala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class TestDhtNodeDb : UnitTest {
Assert.assert_not_null(node_database);

var nodes = node_database.getDhtNodes(node_factory);
Assert.assert_uint_equals(0, nodes.length());
Assert.assert_equals<uint>(0, nodes.length());
}

private void test_real_dht_node_db_insert_select() throws GLib.Error {
Expand All @@ -91,7 +91,7 @@ public class TestDhtNodeDb : UnitTest {

node_database.insertDhtNode("a", "b", 0, false, "c", "d");
var nodes = node_database.getDhtNodes(node_factory);
Assert.assert_uint_equals(1, nodes.length());
Assert.assert_equals<uint>(1, nodes.length());
}

private void test_real_dht_node_db_insert_select_duplicate() throws GLib.Error {
Expand All @@ -102,7 +102,7 @@ public class TestDhtNodeDb : UnitTest {
node_database.insertDhtNode("a", "b", 0, false, "c", "d");
node_database.insertDhtNode("a", "e", 0, false, "f", "g");
var nodes = node_database.getDhtNodes(node_factory);
Assert.assert_uint_equals(1, nodes.length());
Assert.assert_equals<uint>(1, nodes.length());
}

private void test_real_dht_node_db_delete() throws GLib.Error {
Expand All @@ -121,7 +121,7 @@ public class TestDhtNodeDb : UnitTest {
node_database.insertDhtNode("a", "b", 0, false, "c", "d");
node_database.deleteDhtNode("a");
var nodes = node_database.getDhtNodes(node_factory);
Assert.assert_uint_equals(0, nodes.length());
Assert.assert_equals<uint>(0, nodes.length());
}

private IDatabaseStatementFactory create_memory_stmt_factory() {
Expand Down
4 changes: 2 additions & 2 deletions src/testing/TestGlibTesting.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

namespace TestGlibTesting {

private static void testGlibTesting() {
private static void test_glib_testing() {
stdout.printf("Unit test frameworks appears to be ok.\n");
}

private static void main(string[] args) {
Test.init(ref args);
Test.add_func("/test_glib_testing", testGlibTesting);
Test.add_func("/test_glib_testing", test_glib_testing);
Test.run();
}
}
13 changes: 9 additions & 4 deletions src/testing/TestMessageDb.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* TestMessageDb.vala
*
* Copyright (C) 2017 Venom authors and contributors
* Copyright (C) 2017-2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand Down Expand Up @@ -40,13 +40,18 @@ public class TestMessageDb : UnitTest {
public override void set_up() throws Error {
statement = new MockStatement();
builder = new SqliteStatementWrapper.Builder(statement);
mock().when(statement, "builder").then_return_object(builder);
when(statement, "builder")
.then_return_object(builder);

statementFactory = new MockStatementFactory();
mock().when(statementFactory, "createStatement").then_return_object(statement);
when(statementFactory, "createStatement")
.then_return_object(statement);

message = new MockLoggedMessage();
messageFactory = new MockLoggedMessageFactory();
mock().when(messageFactory, "createLoggedMessage").then_return_object(message);
when(messageFactory, "createLoggedMessage")
.then_return_object(message);

logger = new MockLogger();
}

Expand Down
3 changes: 2 additions & 1 deletion src/testing/TestPlugin.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ namespace TestPlugin {
stdout.printf("Loading plugins failed: %s\n", e.message);
Test.fail();
}
check_expectations_noerror();
//check_expectations_noerror();
}

private static int main(string[] args) {
return 77;
Test.init(ref args);
Expand Down
70 changes: 30 additions & 40 deletions src/testing/TestSqliteDb.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* TestSqliteDb.vala
*
* Copyright (C) 2017 Venom authors and contributors
* Copyright (C) 2017-2018 Venom authors and contributors
*
* This file is part of Venom.
*
Expand All @@ -20,39 +20,39 @@
*/

using Venom;
using Mock;
using Testing;

namespace TestSqliteDb {
public class TestSqliteDb : UnitTest {
public TestSqliteDb() {
add_func("/test_sqlite_factory", test_sqlite_factory);
add_func("/test_sqlite_database_wrapper", test_sqlite_database_wrapper);
add_func("/test_sqlite_statement_wrapper", test_sqlite_statement_wrapper);
add_func("/test_sqlite_fail_real_database", test_sqlite_fail_real_database);
add_func("/test_sqlite_fail_real_statement", test_sqlite_fail_real_statement);
add_func("/test_sqlite_real_statement", test_sqlite_real_statement);
}

private static void testSqliteFactory() {
private void test_sqlite_factory() throws Error {
var factory = new SqliteWrapperFactory();
assert_nonnull(factory);
Assert.assert_not_null(factory);
}

private static void testSqliteDatabaseWrapper() {
private void test_sqlite_database_wrapper() throws Error {
var factory = new SqliteWrapperFactory();
try {
var database = factory.createDatabase(":memory:");
assert_nonnull(database);
} catch (Error e) {
stderr.printf(e.message);
Test.fail();
}
var database = factory.createDatabase(":memory:");
Assert.assert_not_null(database);
}

private static void testSqliteStatementWrapper() {
private void test_sqlite_statement_wrapper() throws Error {
var factory = new SqliteWrapperFactory();
try {
var database = factory.createDatabase(":memory:");
var stmtFactory = factory.createStatementFactory(database);
var statement = stmtFactory.createStatement("");
assert_nonnull(statement);
} catch (Error e) {
stderr.printf(e.message);
Test.fail();
}
var database = factory.createDatabase(":memory:");
var stmtFactory = factory.createStatementFactory(database);
var statement = stmtFactory.createStatement("");
Assert.assert_not_null(statement);
}

private static void testSqliteFailRealDatabase() {
private void test_sqlite_fail_real_database() throws Error {
var factory = new SqliteWrapperFactory();
try {
factory.createDatabase("file://invalid_path");
Expand All @@ -62,7 +62,7 @@ namespace TestSqliteDb {
Test.fail();
}

private static void testSqliteFailRealStatement() {
private void test_sqlite_fail_real_statement() throws Error {
var factory = new SqliteWrapperFactory();
try {
var database = factory.createDatabase(":memory:");
Expand All @@ -75,27 +75,17 @@ namespace TestSqliteDb {
Test.fail();
}

private static void testSqliteRealStatement() {
private void test_sqlite_real_statement() throws Error {
var factory = new SqliteWrapperFactory();
try {
var database = factory.createDatabase(":memory:");
var stmtFactory = factory.createStatementFactory(database);
var statement = stmtFactory.createStatement("ANALYZE sqlite_master");
statement.step();
} catch (Error e) {
stdout.printf(e.message + "\n");
Test.fail();
}
var database = factory.createDatabase(":memory:");
var stmtFactory = factory.createStatementFactory(database);
var statement = stmtFactory.createStatement("ANALYZE sqlite_master");
statement.step();
}

private static void main(string[] args) {
Test.init(ref args);
Test.add_func("/test_sqlite_factory", testSqliteFactory);
Test.add_func("/test_sqlite_database_wrapper", testSqliteDatabaseWrapper);
Test.add_func("/test_sqlite_statement_wrapper", testSqliteStatementWrapper);
Test.add_func("/test_sqlite_fail_real_database", testSqliteFailRealDatabase);
Test.add_func("/test_sqlite_fail_real_statement", testSqliteFailRealStatement);
Test.add_func("/test_sqlite_real_statement", testSqliteRealStatement);
var test = new TestSqliteDb();
Test.run();
}
}

0 comments on commit 57f0661

Please sign in to comment.