88 lines
2.6 KiB
Dart
88 lines
2.6 KiB
Dart
import 'dart:math';
|
|
|
|
import 'package:mockito/mockito.dart';
|
|
import 'package:rules_of_living/src/Grid.dart';
|
|
import 'package:rules_of_living/src/Simulation.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
class MockGrid extends Mock implements Grid<bool> {}
|
|
|
|
// TODO: reinstate when decoupling rendering from Simulation @Tags(const ["nobrowser"])
|
|
|
|
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(same(oldMap)));
|
|
});
|
|
});
|
|
group("resetMap", () {
|
|
test("sets the internal map filled with 'false' ", () {
|
|
sut.map.set(1, 1, true);
|
|
sut.clearMap();
|
|
expect(sut.map, allOf(TypeMatcher<Grid>(), isNot(contains(true))));
|
|
});
|
|
test("sets the simulation to need re-rendering", () {
|
|
sut.dirty = false;
|
|
sut.clearMap();
|
|
expect(sut.dirty, true);
|
|
});
|
|
});
|
|
group("save&load", () {
|
|
test(
|
|
"saves a copy of the map which does not change when the actual map changes",
|
|
() {
|
|
sut.saveSnapshot();
|
|
sut.mergeStateChanges({1: true, 2: true});
|
|
var snapshot = Grid.from(sut.map);
|
|
|
|
expect(sut.loadSnapshot(), isNot(equals(snapshot)));
|
|
});
|
|
});
|
|
group("getCellState", () {
|
|
test("throws RangeError if outside map bounds", () {
|
|
expect(() => sut.getCell(9, 10), throwsRangeError);
|
|
});
|
|
test("returns cell value", () {
|
|
sut.map.set(1, 1, true);
|
|
expect(sut.getCell(1, 1), true);
|
|
});
|
|
});
|
|
group("setCellState", () {
|
|
test("throws RangeError if outside map bounds", () {
|
|
expect(() => sut.setCell(9, 10, true), throwsRangeError);
|
|
});
|
|
test("sets cell value", () {
|
|
sut.setCell(1, 1, true);
|
|
expect(sut.getCell(1, 1), true);
|
|
});
|
|
});
|
|
group("toggleCellState", () {
|
|
test("throws RangeError if outside the map bounds", () {
|
|
expect(() => sut.toggleCellState(10, 9), throwsRangeError);
|
|
});
|
|
test("sets the cell to false if currently true", () {
|
|
sut.map.set(1, 1, true);
|
|
sut.toggleCellState(1, 1);
|
|
expect(sut.map.get(1, 1), false);
|
|
});
|
|
test("sets the cell to true if currently false", () {
|
|
sut.map.set(1, 1, false);
|
|
sut.toggleCellState(1, 1);
|
|
expect(sut.map.get(1, 1), true);
|
|
});
|
|
});
|
|
}
|