summaryrefslogtreecommitdiff
path: root/src/world/cell.test.js
blob: a3749e5f509ee747e6d5f5e5f07a39324ec8910c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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);
});