From 2169de16fdd391e43e3670c2c388d7b3b63dc722 Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 19 Oct 2018 19:35:09 +0200 Subject: [PATCH] Add Tests to Simulation --- lib/src/Simulation.dart | 21 +++++++++++---------- test/simulation_test.dart | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 test/simulation_test.dart diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index fd8bbcb..54c7efa 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -13,15 +13,15 @@ class Simulation { RuleSet rules = GameOfLife(); bool _dirty = true; + bool get dirty => _dirty; + bool _renderEdges = true; + bool get renderEdges => _renderEdges; int _amount; int _dispersal; - int get w => map.width; - int get h => map.height; - - Point get gridSize => Point(w, h); + Point get gridSize => Point(map.width, map.height); void set gridSize(Point value) { if (value.x <= 0 || value.y <= 0) throw ArgumentError("grid size must not be smaller than 1"); @@ -29,13 +29,16 @@ class Simulation { } Simulation(int w, int h) : this.map = new Grid(w, h) { - reset(); - print("Grid Created"); + this.map = reset(); } - void reset() { - map.setAll(0, List.filled(map.length, false)); + Simulation.fromGrid(Grid map) : this.map = map; + + Grid reset([Grid map]) { + map ??= this.map; _dirty = true; + map.setAll(0, List.filled(map.length, false)); + return map; } void addRandomPattern({int amount, int dispersal}) { @@ -148,6 +151,4 @@ class Simulation { _renderEdges = on; _dirty = true; } - - bool get renderEdges => _renderEdges; } diff --git a/test/simulation_test.dart b/test/simulation_test.dart new file mode 100644 index 0000000..b8f5db6 --- /dev/null +++ b/test/simulation_test.dart @@ -0,0 +1,37 @@ +import 'dart:math'; +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/src/Grid.dart'; +import 'package:test/test.dart'; + +import 'package:rules_of_living/src/Simulation.dart'; + +void main() { + Simulation sut; + setUp(() { + sut = Simulation(10, 10); + }); + group("gridSize", () { + test( + "returns the width and height of the underlying grid", + () => expect( + sut.gridSize, equals(Point(sut.map.width, sut.map.height)))); + test("sets the underlying grid width and height", () { + sut.gridSize = Point(2, 3); + expect(sut.gridSize, equals(Point(2, 3))); + }); + test("creates a new underlying grid on resizing", () { + var oldMap = sut.map; + sut.gridSize = Point(10, 10); + expect(sut.map, isNot(oldMap)); + }); + }); + group("reset", () { + test("returns a map filled with 'false' ", () { + expect(sut.reset(), allOf(TypeMatcher(), isNot(contains(true)))); + }); + test("sets the simulation to need re-rendering", () { + sut.reset(); + expect(sut.dirty, true); + }, skip: "can not find a way to set dirty to true first yet"); + }); +}