[gnome-break-timer] Added some basic tests for StatefulTimer.
- From: Dylan McCall <dylanmccall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-break-timer] Added some basic tests for StatefulTimer.
- Date: Sat, 21 Sep 2013 04:25:58 +0000 (UTC)
commit 201c41139d295516fd59cb320a7b73f53f664362
Author: Dylan McCall <dylanmccall ubuntu com>
Date: Fri Sep 20 21:15:57 2013 -0700
Added some basic tests for StatefulTimer.
tests/Makefile.am | 3 +-
tests/helper-util/helper_util_runner.vala | 1 +
tests/helper-util/test_Countdown.vala | 412 ++++++++++++++--------------
tests/helper-util/test_StatefulTimer.vala | 102 +++++++
4 files changed, 311 insertions(+), 207 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index bedc385..f1ac710 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -71,7 +71,8 @@ TEST_PROGS += test-helper-util
test_helper_util_SOURCES = \
tests.vala \
helper-util/helper_util_runner.vala \
- helper-util/test_Countdown.vala
+ helper-util/test_Countdown.vala \
+ helper-util/test_StatefulTimer.vala
test_helper_util_LDADD = $(TEST_LIBS)
diff --git a/tests/helper-util/helper_util_runner.vala b/tests/helper-util/helper_util_runner.vala
index e3928c0..a72dce2 100644
--- a/tests/helper-util/helper_util_runner.vala
+++ b/tests/helper-util/helper_util_runner.vala
@@ -18,5 +18,6 @@
public static int main (string[] args) {
var runner = new TestRunner (ref args);
runner.add (new test_Countdown ());
+ runner.add (new test_StatefulTimer ());
return runner.run ();
}
diff --git a/tests/helper-util/test_Countdown.vala b/tests/helper-util/test_Countdown.vala
index 82860a7..1419abb 100644
--- a/tests/helper-util/test_Countdown.vala
+++ b/tests/helper-util/test_Countdown.vala
@@ -33,291 +33,291 @@ public class test_Countdown : TestSuiteWithActivityMonitor {
new test_timers ().add_to (this);
new test_serialization ().add_to (this);
}
-}
-class test_construct : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_construct : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- assert (countdown.get_time_remaining () == 30);
+ assert (countdown.get_time_remaining () == 30);
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 30);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 30);
+ }
}
-}
-class test_reset : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_reset : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start_from (-10);
- countdown.set_penalty (20);
- assert (countdown.get_time_remaining () == 40);
- assert (countdown.is_counting () == true);
+ countdown.start_from (-10);
+ countdown.set_penalty (20);
+ assert (countdown.get_time_remaining () == 40);
+ assert (countdown.is_counting () == true);
- countdown.reset ();
- assert (countdown.get_time_remaining () == 30);
- assert (countdown.is_counting () == false);
+ countdown.reset ();
+ assert (countdown.get_time_remaining () == 30);
+ assert (countdown.is_counting () == false);
+ }
}
-}
-class test_start : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_start : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- assert (countdown.is_counting () == false);
+ assert (countdown.is_counting () == false);
- countdown.start ();
- assert (countdown.get_time_remaining () == 30);
- assert (countdown.is_counting () == true);
+ countdown.start ();
+ assert (countdown.get_time_remaining () == 30);
+ assert (countdown.is_counting () == true);
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.start ();
- assert (countdown.get_time_remaining () == 30);
+ countdown.start ();
+ assert (countdown.get_time_remaining () == 30);
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- context.time_step (false, 35, 35);
- assert (countdown.get_time_remaining () == 0);
+ context.time_step (false, 35, 35);
+ assert (countdown.get_time_remaining () == 0);
+ }
}
-}
-class test_start_from : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_start_from : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start_from (-10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.start_from (-10);
+ assert (countdown.get_time_remaining () == 20);
- context.time_step (false, 5, 5);
- assert (countdown.get_time_remaining () == 15);
+ context.time_step (false, 5, 5);
+ assert (countdown.get_time_remaining () == 15);
- countdown.start_from (-10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.start_from (-10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.start_from (-50);
- assert (countdown.get_time_remaining () == 0);
+ countdown.start_from (-50);
+ assert (countdown.get_time_remaining () == 0);
- // Test that start_from doesn't allow time_remaining to exceed duration
- countdown.start_from (10);
- assert (countdown.get_time_remaining () == 30);
+ // Test that start_from doesn't allow time_remaining to exceed duration
+ countdown.start_from (10);
+ assert (countdown.get_time_remaining () == 30);
+ }
}
-}
-class test_pause : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_pause : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.start ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.pause ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.pause ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
+ }
}
-}
-class test_continue : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_continue : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.continue ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.continue ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.pause ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.pause ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.continue ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 10);
+ countdown.continue ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 10);
+ }
}
-}
-class test_continue_from : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_continue_from : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.continue_from (-10);
- assert (countdown.get_time_remaining () == 20);
- assert (countdown.is_counting () == true);
+ countdown.continue_from (-10);
+ assert (countdown.get_time_remaining () == 20);
+ assert (countdown.is_counting () == true);
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 10);
- assert (countdown.is_counting () == true);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 10);
+ assert (countdown.is_counting () == true);
- // Test that continue_from doesn't do anything if countdown is already running
- countdown.continue_from (-10);
- assert (countdown.get_time_remaining () == 10);
- assert (countdown.is_counting () == true);
+ // Test that continue_from doesn't do anything if countdown is already running
+ countdown.continue_from (-10);
+ assert (countdown.get_time_remaining () == 10);
+ assert (countdown.is_counting () == true);
+ }
}
-}
-class test_cancel_pause : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_cancel_pause : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start ();
+ countdown.start ();
- countdown.pause ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 30);
- assert (countdown.is_counting () == false);
+ countdown.pause ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 30);
+ assert (countdown.is_counting () == false);
- countdown.cancel_pause ();
- assert (countdown.get_time_remaining () == 20);
- assert (countdown.is_counting () == true);
+ countdown.cancel_pause ();
+ assert (countdown.get_time_remaining () == 20);
+ assert (countdown.is_counting () == true);
+ }
}
-}
-class test_advance_time : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_advance_time : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.advance_time (10);
- assert (countdown.get_time_remaining () == 20);
+ countdown.advance_time (10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.continue ();
- assert (countdown.get_time_remaining () == 20);
+ countdown.continue ();
+ assert (countdown.get_time_remaining () == 20);
- countdown.advance_time (10);
- assert (countdown.get_time_remaining () == 10);
+ countdown.advance_time (10);
+ assert (countdown.get_time_remaining () == 10);
+ }
}
-}
-class test_set_penalty : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_set_penalty : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.set_penalty (20);
- assert (countdown.get_penalty () == 20);
- assert (countdown.get_time_remaining () == 50);
+ countdown.set_penalty (20);
+ assert (countdown.get_penalty () == 20);
+ assert (countdown.get_time_remaining () == 50);
- countdown.continue ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 40);
+ countdown.continue ();
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 40);
- countdown.set_penalty (5);
- assert (countdown.get_penalty () == 5);
- assert (countdown.get_time_remaining () == 25);
+ countdown.set_penalty (5);
+ assert (countdown.get_penalty () == 5);
+ assert (countdown.get_time_remaining () == 25);
- countdown.start ();
- assert (countdown.get_penalty () == 0);
- assert (countdown.get_time_remaining () == 30);
+ countdown.start ();
+ assert (countdown.get_penalty () == 0);
+ assert (countdown.get_time_remaining () == 30);
+ }
}
-}
-class test_set_base_duration : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_set_base_duration : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start ();
+ countdown.start ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_remaining () == 20);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_remaining () == 20);
- countdown.set_base_duration (10);
- assert (countdown.get_time_remaining () == 0);
+ countdown.set_base_duration (10);
+ assert (countdown.get_time_remaining () == 0);
- countdown.set_base_duration (15);
- assert (countdown.get_time_remaining () == 5);
+ countdown.set_base_duration (15);
+ assert (countdown.get_time_remaining () == 5);
- countdown.start ();
- assert (countdown.get_time_remaining () == 15);
+ countdown.start ();
+ assert (countdown.get_time_remaining () == 15);
+ }
}
-}
-class test_get_duration : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_get_duration : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- assert (countdown.get_duration () == 30);
+ assert (countdown.get_duration () == 30);
- countdown.set_penalty (5);
- assert (countdown.get_duration () == 35);
+ countdown.set_penalty (5);
+ assert (countdown.get_duration () == 35);
- countdown.set_base_duration (40);
- assert (countdown.get_duration () == 45);
+ countdown.set_base_duration (40);
+ assert (countdown.get_duration () == 45);
- countdown.reset ();
- assert (countdown.get_duration () == 40);
+ countdown.reset ();
+ assert (countdown.get_duration () == 40);
+ }
}
-}
-class test_get_time_elapsed : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_get_time_elapsed : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- assert (countdown.get_time_elapsed () == 0);
- assert (countdown.get_time_remaining () == 30);
- assert (countdown.is_finished () == false);
+ assert (countdown.get_time_elapsed () == 0);
+ assert (countdown.get_time_remaining () == 30);
+ assert (countdown.is_finished () == false);
- countdown.start ();
+ countdown.start ();
- context.time_step (false, 10, 10);
- assert (countdown.get_time_elapsed () == 10);
- assert (countdown.get_time_remaining () == 20);
- assert (countdown.is_finished () == false);
+ context.time_step (false, 10, 10);
+ assert (countdown.get_time_elapsed () == 10);
+ assert (countdown.get_time_remaining () == 20);
+ assert (countdown.is_finished () == false);
- context.time_step (false, 50, 50);
- assert (countdown.get_time_elapsed () == 60);
- assert (countdown.get_time_remaining () == 0);
- assert (countdown.is_finished () == true);
+ context.time_step (false, 50, 50);
+ assert (countdown.get_time_elapsed () == 60);
+ assert (countdown.get_time_remaining () == 0);
+ assert (countdown.is_finished () == true);
- countdown.reset ();
+ countdown.reset ();
- assert (countdown.get_time_elapsed () == 0);
- assert (countdown.get_time_remaining () == 30);
- assert (countdown.is_finished () == false);
+ assert (countdown.get_time_elapsed () == 0);
+ assert (countdown.get_time_remaining () == 30);
+ assert (countdown.is_finished () == false);
+ }
}
-}
-class test_timers : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
+ class test_timers : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
- countdown.start ();
+ countdown.start ();
- // Test that timer advances with change in wall time
- context.time_step (false, 10, 0);
- assert (countdown.get_time_remaining () == 20);
+ // Test that timer advances with change in wall time
+ context.time_step (false, 10, 0);
+ assert (countdown.get_time_remaining () == 20);
- // Test that it ignores change in monotonic time
- context.time_step (false, 0, 10);
- assert (countdown.get_time_remaining () == 20);
+ // Test that it ignores change in monotonic time
+ context.time_step (false, 0, 10);
+ assert (countdown.get_time_remaining () == 20);
+ }
}
-}
-
-class test_serialization : Object, SimpleTestCase<test_Countdown> {
- public void run (test_Countdown context) {
- var countdown = new Countdown (30);
-
- var data_0 = countdown.serialize ();
-
- countdown.start ();
- context.time_step (false, 10, 10);
- var data_1 = countdown.serialize ();
-
- countdown.pause ();
- var data_2 = countdown.serialize ();
- context.time_step (false, 10, 10);
-
- this.assert_deserialize (30, data_0, false, 0, 30);
- this.assert_deserialize (30, data_1, true, 10, 20);
- this.assert_deserialize (30, data_2, false, 10, 20);
- }
-
- private void assert_deserialize (int known_duration, string data, bool is_counting, int time_elapsed,
int time_remaining) {
- var countdown = new Countdown (known_duration);
- countdown.deserialize (data);
- assert (countdown.is_counting () == is_counting);
- assert (countdown.get_time_elapsed () == time_elapsed);
- assert (countdown.get_time_remaining () == time_remaining);
+ class test_serialization : Object, SimpleTestCase<test_Countdown> {
+ public void run (test_Countdown context) {
+ var countdown = new Countdown (30);
+
+ var data_0 = countdown.serialize ();
+
+ countdown.start ();
+ context.time_step (false, 10, 10);
+ var data_1 = countdown.serialize ();
+
+ countdown.pause ();
+ var data_2 = countdown.serialize ();
+ context.time_step (false, 10, 10);
+
+ this.assert_deserialize (30, data_0, false, 0, 30);
+ this.assert_deserialize (30, data_1, true, 10, 20);
+ this.assert_deserialize (30, data_2, false, 10, 20);
+ }
+
+ private void assert_deserialize (int known_duration, string data, bool is_counting, int
time_elapsed, int time_remaining) {
+ var countdown = new Countdown (known_duration);
+ countdown.deserialize (data);
+
+ assert (countdown.is_counting () == is_counting);
+ assert (countdown.get_time_elapsed () == time_elapsed);
+ assert (countdown.get_time_remaining () == time_remaining);
+ }
}
}
\ No newline at end of file
diff --git a/tests/helper-util/test_StatefulTimer.vala b/tests/helper-util/test_StatefulTimer.vala
new file mode 100644
index 0000000..b51a11a
--- /dev/null
+++ b/tests/helper-util/test_StatefulTimer.vala
@@ -0,0 +1,102 @@
+/*
+ * This file is part of GNOME Break Timer.
+ *
+ * GNOME Break Timer is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * GNOME Break Timer is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with GNOME Break Timer. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+public class test_StatefulTimer : TestSuiteWithActivityMonitor {
+ /* We won't be able to test the timer mechanics in much detail because,
+ * internally, it uses GTimer and the (real) system clock. Still, we can
+ * test that StatefulTimer keeps track of state, as well as serialization.
+ */
+
+ public test_StatefulTimer () {
+ new test_construct ().add_to (this);
+ new test_stop_start ().add_to (this);
+ new test_continue ().add_to (this);
+ new test_start_lap ().add_to (this);
+ new test_serialize_hardcoded ().add_to (this);
+ new test_deserialize_hardcoded ().add_to (this);
+ }
+
+ class test_construct : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ assert (timer.state == StatefulTimer.State.COUNTING);
+ }
+ }
+
+ class test_stop_start : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ timer.stop ();
+ assert (timer.state == StatefulTimer.State.STOPPED);
+ assert (timer.is_stopped () == true);
+ assert (timer.is_counting () == false);
+
+ timer.start ();
+ assert (timer.state == StatefulTimer.State.COUNTING);
+ assert (timer.is_stopped () == false);
+ assert (timer.is_counting () == true);
+ }
+ }
+
+ class test_continue : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ timer.stop ();
+
+ timer.continue ();
+ assert (timer.state == StatefulTimer.State.COUNTING);
+ }
+ }
+
+ class test_start_lap : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ timer.stop ();
+
+ timer.start_lap ();
+ assert (timer.state == StatefulTimer.State.COUNTING);
+ }
+ }
+
+ class test_serialize_hardcoded : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ var data = timer.serialize ();
+ string[] data_parts = data.split (",");
+
+ assert (data_parts[0] == "1");
+ }
+ }
+
+ class test_deserialize_hardcoded : Object, SimpleTestCase<test_StatefulTimer> {
+ public void run (test_StatefulTimer context) {
+ var timer = new StatefulTimer ();
+
+ var data = "0,20.0,15.0";
+ timer.deserialize (data);
+
+ assert (timer.state == StatefulTimer.State.STOPPED);
+ assert (timer.elapsed () == 20.0);
+ assert (timer.lap_time () == 5.0);
+ }
+ }
+}
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]