Add Tests to Simulation

This commit is contained in:
Unknown 2018-10-19 19:35:09 +02:00
parent e8c1e6ed8b
commit 2169de16fd
2 changed files with 48 additions and 10 deletions

View file

@ -13,15 +13,15 @@ class Simulation {
RuleSet rules = GameOfLife(); RuleSet rules = GameOfLife();
bool _dirty = true; bool _dirty = true;
bool get dirty => _dirty;
bool _renderEdges = true; bool _renderEdges = true;
bool get renderEdges => _renderEdges;
int _amount; int _amount;
int _dispersal; int _dispersal;
int get w => map.width; Point get gridSize => Point<int>(map.width, map.height);
int get h => map.height;
Point get gridSize => Point<int>(w, h);
void set gridSize(Point<int> value) { void set gridSize(Point<int> value) {
if (value.x <= 0 || value.y <= 0) if (value.x <= 0 || value.y <= 0)
throw ArgumentError("grid size must not be smaller than 1"); 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) { Simulation(int w, int h) : this.map = new Grid(w, h) {
reset(); this.map = reset();
print("Grid Created");
} }
void reset() { Simulation.fromGrid(Grid<bool> map) : this.map = map;
map.setAll(0, List.filled(map.length, false));
Grid<bool> reset([Grid<bool> map]) {
map ??= this.map;
_dirty = true; _dirty = true;
map.setAll(0, List.filled(map.length, false));
return map;
} }
void addRandomPattern({int amount, int dispersal}) { void addRandomPattern({int amount, int dispersal}) {
@ -148,6 +151,4 @@ class Simulation {
_renderEdges = on; _renderEdges = on;
_dirty = true; _dirty = true;
} }
bool get renderEdges => _renderEdges;
} }

37
test/simulation_test.dart Normal file
View file

@ -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<int>(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<Grid>(), 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");
});
}