[gnome-nibbles/arnaudb/rework-warps] Test code factorization.
- From: Arnaud B. <arnaudb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/arnaudb/rework-warps] Test code factorization.
- Date: Wed, 1 Jul 2020 15:03:50 +0000 (UTC)
commit 1e4960ac132419ad4efdee64e59718c7692c9780
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date: Tue Jun 30 18:31:36 2020 +0200
Test code factorization.
src/nibbles-test.vala | 223 +++++++++++++++++---------------------------------
1 file changed, 76 insertions(+), 147 deletions(-)
---
diff --git a/src/nibbles-test.vala b/src/nibbles-test.vala
index d20fe85..8d1eaf5 100644
--- a/src/nibbles-test.vala
+++ b/src/nibbles-test.vala
@@ -40,56 +40,70 @@ namespace NibblesTest
}
/*\
- * * test games
+ * * utilities
\*/
- private static void test_games ()
+ private struct WormTest
+ {
+ int start_x;
+ int start_y;
+ int final_lives;
+ int final_score;
+ }
+
+ private static void test_board (string [] board,
+ uint8 regular_bonuses,
+ uint8 final_bonuses,
+ WormTest [] worms)
{
- NibblesGame game = new NibblesGame (/* start level */ 1, /* speed */ 0, /* fakes */ false,
level_008_width, level_008_height, /* no random */ true);
+ NibblesGame game = new NibblesGame (/* start level */ 1, /* speed */ 0, /* fakes */ false, board
[0].char_count (), board.length, /* no random */ true);
game.numhumans = 0;
- game.numai = 4;
+ game.numai = worms.length;
game.create_worms ();
- game.load_board (level_008, /* regular bonus = 8 + numworms */ 12);
+ game.load_board (board, regular_bonuses);
ulong [] worms_handlers = new ulong [game.worms.size];
foreach (Worm worm in game.worms)
- // FIXME we should not have to connect to anything 1/3
+ // FIXME we should not have to connect to anything // TODO what’s this 3 in
dematerialize?
worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
- assert_true (game.numworms == 4);
- assert_true (game.worms.size == 4);
+ assert_true (game.numworms == worms.length);
+ assert_true (game.worms.size == worms.length);
uint8 applied_bonus = 0;
- ulong game_handler_1 = game.bonus_applied.connect ((bonus, worm) => { applied_bonus++; Test.message
(@"worm $(worm.id) took bonus at [$(bonus.x), $(bonus.y)]"); });
+ ulong game_handler_1 = game.bonus_applied.connect ((bonus, worm) => { applied_bonus++; Test.message
(@"worm $(worm.id) took bonus at ($(bonus.x), $(bonus.y))"); });
game.add_worms ();
game.start (/* add initial bonus */ true);
- assert_true (game.worms.@get (0).head.x == 4 && game.worms.@get (0).head.y == 14);
- assert_true (game.worms.@get (1).head.x == 18 && game.worms.@get (1).head.y == 31);
- assert_true (game.worms.@get (2).head.x == 9 && game.worms.@get (2).head.y == 39);
- assert_true (game.worms.@get (3).head.x == 51 && game.worms.@get (3).head.y == 45);
+ for (uint8 i = 0; i < worms.length; i++)
+ {
+ Worm worm = game.worms.@get (i);
+ WormTest worm_test = worms [i];
+
+ assert_true (worm.lives == 6);
+ assert_true (worm.score == 0);
+ assert_true (worm.head.x == worm_test.start_x && worm.head.y == worm_test.start_y);
+ }
// run until game is finished
bool completed = false;
ulong game_handler_2 = game.level_completed.connect (() => { completed = true; });
MainContext context = MainContext.@default ();
- while (!completed)
- context.iteration (/* may block */ false);
-
- assert_true (applied_bonus == 15);
+ do context.iteration (/* may block */ false);
+ while (!completed && (game.get_game_status () != GameStatus.GAMEOVER));
- assert_true (game.worms.@get (0).lives == 6);
- assert_true (game.worms.@get (1).lives == 5);
- assert_true (game.worms.@get (2).lives == 6);
- assert_true (game.worms.@get (3).lives == 6);
+ for (uint8 i = 0; i < worms.length; i++)
+ {
+ Worm worm = game.worms.@get (i);
+ WormTest worm_test = worms [i];
- assert_true (game.worms.@get (0).score == 11);
- assert_true (game.worms.@get (1).score == 14);
- assert_true (game.worms.@get (2).score == 119);
- assert_true (game.worms.@get (3).score == 19);
+ assert_true (worm.lives == worm_test.final_lives);
+ assert_true (worm.score == worm_test.final_score);
+ }
+ assert_true (applied_bonus == final_bonuses);
foreach (Worm worm in game.worms)
worm.disconnect (worms_handlers [worm.id]);
@@ -97,8 +111,19 @@ namespace NibblesTest
game.disconnect (game_handler_2);
}
- private const int level_008_width = 92;
- private const int level_008_height = 66;
+ /*\
+ * * test games
+ \*/
+
+ private static void test_games ()
+ {
+ test_board (level_008, /* regular bonus = 8 + numworms */ 12, /* final bonuses */ 15,
+ { WormTest () { start_x = 4, start_y = 14, final_lives = 6, final_score = 11 },
+ WormTest () { start_x = 18, start_y = 31, final_lives = 5, final_score = 14 },
+ WormTest () { start_x = 9, start_y = 39, final_lives = 6, final_score = 119 },
+ WormTest () { start_x = 51, start_y = 45, final_lives = 6, final_score = 19 }});
+ }
+
private const string [] level_008 = {
"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛........┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓",
"┃..........................................................................................┃",
@@ -175,92 +200,44 @@ namespace NibblesTest
private static void test_heads ()
{
Test.message ("test heads 1");
- _test_heads (test_heads_1, /* worm 0 */ 6, 4, /* worm 1 */ 11, 4, /* lives */ 6, 6);
+ test_board (test_heads_1, 1, 1, { WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 11, start_y = 4, final_lives = 6,
final_score = 0 }});
Test.message ("test heads 2");
- _test_heads (test_heads_2, /* worm 0 */ 6, 4, /* worm 1 */ 11, 4, /* lives */ 6, 4);
+ test_board (test_heads_2, 1, 1, { WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 11, start_y = 4, final_lives = 4,
final_score = 0 }});
Test.message ("test heads 3");
- _test_heads (test_heads_3, /* worm 0 */ 6, 4, /* worm 1 */ 10, 4, /* lives */ 6, 6);
+ test_board (test_heads_3, 1, 1, { WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 10, start_y = 4, final_lives = 6,
final_score = 0 }});
Test.message ("test heads 4");
- _test_heads (test_heads_4, /* worm 0 */ 6, 4, /* worm 1 */ 10, 4, /* lives */ 0, 0);
+ test_board (test_heads_4, 1, 0, { WormTest () { start_x = 6, start_y = 4, final_lives = 0,
final_score = 0 },
+ WormTest () { start_x = 10, start_y = 4, final_lives = 0,
final_score = 0 }});
Test.message ("test heads 5");
- _test_heads (test_heads_5, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 6, 6);
+ test_board (test_heads_5, 1, 1, { WormTest () { start_x = 6, start_y = 1, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 0 }});
Test.message ("test heads 6");
- _test_heads (test_heads_6, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 4, 6);
+ test_board (test_heads_6, 1, 1, { WormTest () { start_x = 6, start_y = 1, final_lives = 4,
final_score = 0 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 1 }});
Test.message ("test heads 9");
- _test_heads (test_heads_9, /* worm 0 */ 6, 1, /* worm 1 */ 6, 4, /* lives */ 6, 4);
+ test_board (test_heads_9, 1, 1, { WormTest () { start_x = 6, start_y = 1, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 4,
final_score = 0 }});
Test.message ("test heads 7");
- _test_heads (test_heads_7, /* worm 0 */ 6, 2, /* worm 1 */ 6, 4, /* lives */ 6, 6);
+ test_board (test_heads_7, 1, 1, { WormTest () { start_x = 6, start_y = 2, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 0 }});
Test.message ("test heads 8");
- _test_heads (test_heads_8, /* worm 0 */ 6, 2, /* worm 1 */ 6, 4, /* lives */ 0, 0);
+ test_board (test_heads_8, 1, 0, { WormTest () { start_x = 6, start_y = 2, final_lives = 0,
final_score = 0 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 0,
final_score = 0 }});
Test.message ("test heads 0");
- _test_heads (test_heads_0, /* worm 0 */ 6, 2, /* worm 1 */ 6, 4, /* lives */ 6, 6);
- }
-
- private static void _test_heads (string [] board,
- int worm_0_x,
- int worm_0_y,
- int worm_1_x,
- int worm_1_y,
- int first_worm_lives,
- int second_worm_lives)
- {
- NibblesGame game = new NibblesGame (/* start level */ 0, /* speed */ 0, /* fakes */ false,
test_heads_width, test_heads_height, /* no random */ true);
-
- game.numhumans = 0;
- game.numai = 2;
- game.create_worms ();
-
- game.load_board (board, /* regular bonus */ 1);
-
- ulong [] worms_handlers = new ulong [game.worms.size];
- foreach (Worm worm in game.worms)
- // FIXME we should not have to connect to anything 2/3
- worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
-
- assert_true (game.numworms == 2);
- assert_true (game.worms.size == 2);
-
- ulong game_handler_1 = game.bonus_applied.connect ((bonus, worm) => { Test.message (@"worm
$(worm.id) took bonus at [$(bonus.x), $(bonus.y)]"); });
-
- game.add_worms ();
- game.start (/* add initial bonus */ true);
-
- assert_true (game.worms.@get (0).lives == 6);
- assert_true (game.worms.@get (1).lives == 6);
-
- assert_true (game.worms.@get (0).score == 0);
- assert_true (game.worms.@get (1).score == 0);
-
- assert_true (game.worms.@get (0).head.x == worm_0_x && game.worms.@get (0).head.y == worm_0_y);
- assert_true (game.worms.@get (1).head.x == worm_1_x && game.worms.@get (1).head.y == worm_1_y);
-
- // run until game is finished
- bool completed = false;
- ulong game_handler_2 = game.level_completed.connect (() => { completed = true; });
- MainContext context = MainContext.@default ();
- do context.iteration (/* may block */ false);
- while (!completed && (game.get_game_status () != GameStatus.GAMEOVER));
-
- assert_true (game.worms.@get (0).lives == first_worm_lives);
- assert_true (game.worms.@get (1).lives == second_worm_lives);
-
- // FIXME looks like last bonus is not counted...
- assert_true (game.worms.@get (0).score == 0);
- assert_true (game.worms.@get (1).score == 0);
-
- foreach (Worm worm in game.worms)
- worm.disconnect (worms_handlers [worm.id]);
- game.disconnect (game_handler_1);
- game.disconnect (game_handler_2);
+ test_board (test_heads_0, 1, 1, { WormTest () { start_x = 6, start_y = 2, final_lives = 6,
final_score = 1 },
+ WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 0 }});
}
private const int test_heads_width = 18;
@@ -353,64 +330,16 @@ namespace NibblesTest
private static void test_warps ()
{
Test.message ("test warps 1");
- _test_warps (test_warps_1, /* worm 0 */ 6, 4, /* lives */ 6);
+ test_board (test_warps_1, 1, 1, { WormTest () { start_x = 6, start_y = 4, final_lives = 6,
final_score = 1 }});
Test.message ("test warps 2");
- _test_warps (test_warps_2, /* worm 0 */ 6, 4, /* lives */ 0);
+ test_board (test_warps_2, 1, 0, { WormTest () { start_x = 6, start_y = 4, final_lives = 0,
final_score = 0 }});
Test.message ("test warps 3");
- _test_warps (test_warps_3, /* worm 0 */ 9, 4, /* lives */ 0);
+ test_board (test_warps_3, 1, 0, { WormTest () { start_x = 9, start_y = 4, final_lives = 0,
final_score = 0 }});
Test.message ("test warps 4");
- _test_warps (test_warps_4, /* worm 0 */ 9, 4, /* lives */ 6);
- }
-
- private static void _test_warps (string [] board,
- int worm_0_x,
- int worm_0_y,
- int expected)
- {
- NibblesGame game = new NibblesGame (/* start level */ 0, /* speed */ 0, /* fakes */ false,
test_warps_width, test_warps_height, /* no random */ true);
-
- game.numhumans = 0;
- game.numai = 1;
- game.create_worms ();
-
- game.load_board (board, /* regular bonus */ 1);
-
- ulong [] worms_handlers = new ulong [game.worms.size];
- foreach (Worm worm in game.worms)
- // FIXME we should not have to connect to anything 3/3
- worms_handlers [worm.id] = worm.finish_added.connect (() => { worm.dematerialize (game.board,
3); worm.is_stopped = false; });
-
- assert_true (game.numworms == 1);
- assert_true (game.worms.size == 1);
-
- ulong game_handler_1 = game.bonus_applied.connect ((bonus, worm) => { Test.message (@"worm
$(worm.id) took bonus at [$(bonus.x), $(bonus.y)]"); });
-
- game.add_worms ();
- game.start (/* add initial bonus */ true);
-
- assert_true (game.worms.@get (0).lives == 6);
- assert_true (game.worms.@get (0).score == 0);
- assert_true (game.worms.@get (0).head.x == worm_0_x && game.worms.@get (0).head.y == worm_0_y);
-
- // run until game is finished
- bool completed = false;
- ulong game_handler_2 = game.level_completed.connect (() => { completed = true; });
- MainContext context = MainContext.@default ();
- do context.iteration (/* may block */ false);
- while (!completed && (game.get_game_status () != GameStatus.GAMEOVER));
-
- assert_true (game.worms.@get (0).lives == expected);
-
- // FIXME looks like last bonus is not counted...
- assert_true (game.worms.@get (0).score == 0);
-
- foreach (Worm worm in game.worms)
- worm.disconnect (worms_handlers [worm.id]);
- game.disconnect (game_handler_1);
- game.disconnect (game_handler_2);
+ test_board (test_warps_4, 1, 1, { WormTest () { start_x = 9, start_y = 4, final_lives = 6,
final_score = 1 }});
}
private const int test_warps_width = 11;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]