diff options
author | sanine <sanine.not@pm.me> | 2023-11-06 01:55:26 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2023-11-06 01:55:26 -0600 |
commit | 41d720dbd78d91ac85239c8352cd479ec898f9e0 (patch) | |
tree | 49e4d73ed89fb6ab69c635525a8e4b1ce88b0b81 /src/world/cell.test.js | |
parent | 541b103179cac2dc58962c52b5144b41f75316b0 (diff) |
add cell.js
Diffstat (limited to 'src/world/cell.test.js')
-rw-r--r-- | src/world/cell.test.js | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/world/cell.test.js b/src/world/cell.test.js new file mode 100644 index 0000000..a3749e5 --- /dev/null +++ b/src/world/cell.test.js @@ -0,0 +1,56 @@ +'use strict'; + +import { cells_update, cells_valid, cells_apply } from './cell.js'; + + +test("growth update rule", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + const update_rules = { + plant: () => {}, + empty: (cells, x, y) => { + if (cells[y][x+1].type === 'plant') { + return { world_updates: [{ x, y, from: 'empty', to: 'plant' }] }; + } + }, + }; + + expect(cells_update(cells, update_rules)).toEqual([ + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + cells[0][1] = { type: 'plant' }; + + expect(cells_update(cells, update_rules)).toEqual([ + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + cells[0][0] = { type: 'plant' }; + + expect(cells_update(cells, update_rules)).toEqual([]); +}); + + +test("growth update rule applied", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + expect(cells_apply(cells, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([ + [ { type: 'empty' }, { type: 'plant' }, { type: 'plant' } ], + ]); + + expect(cells_apply(cells, [ + { world_updates: [{ x: 2, y: 0, from: 'plant', to: 'empty' } ]}, + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' } ]}, + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' } ]}, + ])).toEqual([ + [ { type: 'plant' }, { type: 'plant' }, { type: 'empty' } ], + ]); +}); + + +test("check proposals agains cells for validity", () => { + const cells = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + expect(cells_valid(cells, { world_updates: [{ x: -1, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 0, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'blah' }] })).toBe(true); + expect(cells_valid(cells, { world_updates: [{ x: 2, y: 0, from: 'empty', to: 'blah' }] })).toBe(false); + expect(cells_valid(cells, { world_updates: [{ x: 2, y: 1, from: 'empty', to: 'blah' }] })).toBe(false); +}); |