From 07e04b19a10137eb57032a908520163feb9cdbf9 Mon Sep 17 00:00:00 2001 From: Marco Betschart Date: Tue, 25 Jan 2022 10:49:18 +0100 Subject: [PATCH] Experimenting with GLib.TestSuite and TestCase --- meson.build | 3 ++- tests/Tests.vala | 6 +++++- tests/Util.vala | 37 +++++++++++++++++++++++++++++++++++-- vapi/glib-2.0-fixes.vapi | 6 ++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 vapi/glib-2.0-fixes.vapi diff --git a/meson.build b/meson.build index c3001d5..969aa11 100644 --- a/meson.build +++ b/meson.build @@ -22,7 +22,8 @@ dependencies = [ dependency('gtk+-3.0'), dependency('granite', version: '>=0.5'), dependency('libhandy-1', version: '>=0.83.0'), - meson.get_compiler('c').find_library('m', required : false) + meson.get_compiler('c').find_library('m', required : false), + meson.get_compiler('vala').find_library('glib-2.0-fixes', dirs: meson.current_source_dir() / 'vapi') ] subdir('src') diff --git a/tests/Tests.vala b/tests/Tests.vala index 9c04207..983d522 100644 --- a/tests/Tests.vala +++ b/tests/Tests.vala @@ -24,7 +24,11 @@ namespace Tests { int main(string[] args) { Test.init (ref args); - Test.add_func("/Tests/Util/test_truncating_reminder", Tests.Util.test_truncating_remainder); + // Using GLib.TestSuite allows for set_up/tear_down methods: + Test.get_root ().add(new Util.TruncatingRemainder ()); + + // If we don't need the set_up/tear_down methods, we can simply register a function: + Test.add_func("/Util/truncating_remaindssser", Util.test_truncating_remainder); return Test.run(); } diff --git a/tests/Util.vala b/tests/Util.vala index eeb353d..e1330c3 100644 --- a/tests/Util.vala +++ b/tests/Util.vala @@ -21,8 +21,41 @@ namespace Tests.Util { + class TruncatingRemainder : TestCase { + + // test env variables need to be static: + private static string test_value = null; + + public TruncatingRemainder () { + base ( + "TruncatingRemainder", + set_up, // or: null + test, + tear_down // or: null + ); + } + + void set_up () { + test_value = "my-test-value"; + } + + void test () { + assert_true (1.0 == Timer.Util.truncating_remainder (5.0, 4.0)); + assert_true (0.0 == Timer.Util.truncating_remainder (7.0, 7.0)); + + assert_nonnull (test_value); + assert_true ("my-test-value" == test_value); + // https://docs.gtk.org/glib/func.assert_cmpstr.html + // assert_cmpstr (test_value, ==, "my-test-value"); + } + + void tear_down () { + test_value = null; + } + } + void test_truncating_remainder () { - assert (1.0 == Timer.Util.truncating_remainder (5.0, 4.0)); - assert (0.0 == Timer.Util.truncating_remainder (7.0, 7.0)); + assert_true (1.0 == Timer.Util.truncating_remainder (5.0, 4.0)); + assert_true (0.0 == Timer.Util.truncating_remainder (7.0, 7.0)); } } \ No newline at end of file diff --git a/vapi/glib-2.0-fixes.vapi b/vapi/glib-2.0-fixes.vapi new file mode 100644 index 0000000..29fa36f --- /dev/null +++ b/vapi/glib-2.0-fixes.vapi @@ -0,0 +1,6 @@ +namespace GLib { + namespace Test { + [CCode (cheader_filename = "glib-2.0/glib/gtestutils.h", cname = "g_test_get_root")] + public static TestSuite get_root (); + } +} \ No newline at end of file