1
exercism fetch javascript zipper

zipper.spec.js

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var Zipper = require('./zipper');

// Tests adapted from `problem-specifications/zipper/canonical-data.json` @ v1.0.0

function bt(value, left, right) {
  return {
    value: value,
    left: left,
    right: right
  };
}

function leaf(value) {
  return bt(value, null, null);
}

describe('Zipper', function () {
  var t1 = bt(1, bt(2, null, leaf(3)), leaf(4));
  var t2 = bt(1, bt(5, null, leaf(3)), leaf(4));
  var t3 = bt(1, bt(2, leaf(5), leaf(3)), leaf(4));
  var t4 = bt(1, leaf(2), leaf(4));
  var t5 = bt(1, bt(2, null, leaf(3)), bt(6, leaf(7), leaf(8)));
  var t6 = bt(1, bt(2, null, leaf(5)), leaf(4));
  var zipper;

  beforeEach(function () {
    zipper = Zipper.fromTree(t1);
  });

  it('data is retained', function () {
    expect(zipper.toTree(t1)).toEqual(t1);
  });

  xit('left, right and value', function () {
    expect(zipper.left().right().value()).toEqual(3);
  });

  xit('dead end', function () {
    expect(zipper.left().left()).toBe(null);
  });

  xit('tree from deep focus', function () {
    expect(zipper.left().right().toTree()).toEqual(t1);
  });

  xit('traversing up from top', function () {
    expect(zipper.up()).toEqual(null);
  });

  xit('left, right and up', function () {
    expect(zipper.left().up().right().up().left().right().value()).toEqual(3);
  });

  xit('setValue', function () {
    expect(zipper.left().setValue(5).toTree()).toEqual(t2);
  });

  xit('setValue after traversing up', function () {
    expect(zipper.left().right().up().setValue(5).toTree()).toEqual(t2);
  });

  xit('setLeft with leaf', function () {
    expect(zipper.left().setLeft(leaf(5)).toTree()).toEqual(t3);
  });

  xit('setRight with null', function () {
    expect(zipper.left().setRight(null).toTree()).toEqual(t4);
  });

  xit('setRight with subtree', function () {
    expect(zipper.setRight(bt(6, leaf(7), leaf(8))).toTree()).toEqual(t5);
  });

  xit('setValue on deep focus', function () {
    expect(zipper.left().right().setValue(5).toTree()).toEqual(t6);
  });
});