From 0da2d08b74dccbeb630882e23d0cb017a27198c8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 19:59:00 +0200 Subject: [PATCH 1/5] Add Simulation Saving and Loading Methods --- lib/src/Simulation.dart | 5 +++++ test/simulation_test.dart | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 54c7efa..5243b84 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -10,6 +10,8 @@ enum CellPattern { SpaceShip, Blinker } class Simulation { Grid map; + Grid _snapshot; + RuleSet rules = GameOfLife(); bool _dirty = true; @@ -151,4 +153,7 @@ class Simulation { _renderEdges = on; _dirty = true; } + + void saveSnapshot() => _snapshot = map; + Grid loadSnapshot() => map = _snapshot; } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index b8f5db6..d137f97 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -5,6 +5,8 @@ import 'package:test/test.dart'; import 'package:rules_of_living/src/Simulation.dart'; +class MockGrid extends Mock implements Grid {} + void main() { Simulation sut; setUp(() { @@ -34,4 +36,13 @@ void main() { expect(sut.dirty, true); }, skip: "can not find a way to set dirty to true first yet"); }); + group("save&load", () { + test("saves the current map, can be loaded by loadSnapshot", () { + var snapshot = sut.map; + sut.saveSnapshot(); + sut.map = MockGrid(); + + expect(sut.loadSnapshot(), equals(snapshot)); + }); + }); } From 37bff59f83f7a4e08254ff9ec34e565df4dff7ff Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 20:01:52 +0200 Subject: [PATCH 2/5] Remove clear function: Code Duplication --- lib/components/controls_component.dart | 2 +- lib/service/simulation_service.dart | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 3f31741..1d7ee17 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -39,6 +39,6 @@ class ControlsComponent { } void onClearClicked() { - sim.clear(); + sim.reset(); } } diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 396290b..5f538ec 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -24,10 +24,6 @@ class SimulationService { _sim.addRandomPattern(); } - void clear() { - _sim.reset(); - } - Point get gridSize => _sim.gridSize; void set gridSize(Point size) { _sim.gridSize = size; From 2ea974bbd53d99600678a5da7b46e0f31ff03c56 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 20:49:59 +0200 Subject: [PATCH 3/5] Fix Snapshot Load and Save Functions Were just pointers to the map before. Fix to be actual clones of the map. --- lib/src/Simulation.dart | 8 ++++++-- test/simulation_test.dart | 10 ++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 5243b84..0955c9a 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -154,6 +154,10 @@ class Simulation { _dirty = true; } - void saveSnapshot() => _snapshot = map; - Grid loadSnapshot() => map = _snapshot; + void saveSnapshot() => _snapshot = Grid.from(map); + Grid loadSnapshot() { + map = Grid.from(_snapshot); + _dirty = true; + return map; + } } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index d137f97..7b8bf82 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -37,12 +37,14 @@ void main() { }, skip: "can not find a way to set dirty to true first yet"); }); group("save&load", () { - test("saves the current map, can be loaded by loadSnapshot", () { - var snapshot = sut.map; + test( + "saves a copy of the map which does not change when the actual map changes", + () { sut.saveSnapshot(); - sut.map = MockGrid(); + sut.mergeStateChanges({1: true, 2: true}); + var snapshot = Grid.from(sut.map); - expect(sut.loadSnapshot(), equals(snapshot)); + expect(sut.loadSnapshot(), isNot(equals(snapshot))); }); }); } From 22dabda987fcc3217274831df8c2a6e42a60bd7d Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 20:50:24 +0200 Subject: [PATCH 4/5] Add Save and Load Functions to SimService --- lib/service/simulation_service.dart | 8 ++++++-- test/service/simulation_service_test.dart | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/service/simulation_service_test.dart diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 5f538ec..8e0c14f 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -9,9 +9,10 @@ class SimulationService { static final int DEFAULT_GRID_SIZE = 50; final EngineService _engine; - final Simulation _sim = Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE); + final Simulation _sim; - SimulationService(this._engine) { + SimulationService(this._engine, [Simulation sim]) + : this._sim = sim ?? Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE) { _engine.simulation = _sim; _sim.addRandomPattern(amount: 15, dispersal: 5); } @@ -36,4 +37,7 @@ class SimulationService { void toggleGrid() { _sim.renderEdges = !_sim.renderEdges; } + + void save() => _sim.saveSnapshot(); + void load() => _sim.loadSnapshot(); } diff --git a/test/service/simulation_service_test.dart b/test/service/simulation_service_test.dart new file mode 100644 index 0000000..3816099 --- /dev/null +++ b/test/service/simulation_service_test.dart @@ -0,0 +1,23 @@ +import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/simulation_service.dart'; +import 'package:rules_of_living/src/Simulation.dart'; +import 'package:test/test.dart'; +import 'package:mockito/mockito.dart'; + +class MockSimulation extends Mock implements Simulation {} + +class MockEngineService extends Mock implements EngineService {} + +void main() { + SimulationService sut; + MockSimulation mockSim = MockSimulation(); + setUp(() => sut = SimulationService(MockEngineService(), mockSim)); + test("calling save calls through to Simulation.saveSnapshot", () { + sut.save(); + verify(mockSim.saveSnapshot()); + }); + test("calling load calls through to Simulation.loadSnapshot", () { + sut.load(); + verify(mockSim.loadSnapshot()); + }); +} From 92e147028e3a134fcbd64a3c0fdd396d07f47b43 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 20:51:14 +0200 Subject: [PATCH 5/5] Add Save and Load Buttons to Interface Fully functional and tested. --- lib/components/controls_component.dart | 8 ++++++-- lib/components/controls_component.html | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 1d7ee17..538bc27 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -29,8 +29,12 @@ class ControlsComponent { engine.step(); } - void onResetClicked() { - sim.reset(); + void onSaveClicked() { + sim.save(); + } + + void onLoadClicked() { + sim.load(); } void onRandomClicked() { diff --git a/lib/components/controls_component.html b/lib/components/controls_component.html index 42e79f7..764ca37 100644 --- a/lib/components/controls_component.html +++ b/lib/components/controls_component.html @@ -1,5 +1,6 @@
- + +