From 2ea974bbd53d99600678a5da7b46e0f31ff03c56 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 20:49:59 +0200 Subject: [PATCH] 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))); }); }); }