aboutsummaryrefslogtreecommitdiff
path: root/spec/lua/utils_spec.lua
blob: b115498ab8bfd44b31100387a2759fa321adc47c (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
describe('web.utils.string_trim', function()
  it('trims whitespace from the start and end of string', function()
    assert.are.equal(web.utils.string_trim('  hello'), 'hello')
    assert.are.equal(web.utils.string_trim('hello  '), 'hello')
    assert.are.equal(web.utils.string_trim('  hello  '), 'hello')
  end)
end)

describe('web.utils.table_merge', function()
  context('when both tables have integer keys', function()
    it('returns merged table', function()
      local result = web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })
      assert.are.same(result, { a = 0, b = 1, c = 2 })
    end)
  end)

  context('when both tables unique keys', function()
    it('returns merged table', function()
      local result = web.utils.table_merge({ a = 0 }, { b = 1 }, { c = 2 })
      assert.are.same(result, { a = 0, b = 1, c = 2 })
    end)
  end)

  context('when both tables have a key in common', function()
    it('returns merged table', function()
      local result = web.utils.table_merge({ foo = 1, a = 0 }, { foo = 2 }, { foo = 3 })
      assert.are.same(result, { foo = 3, a = 0 })
    end)

    it('mutates the first table', function()
      local tbl = { foo = 1, a = 0 }
      web.utils.table_merge(tbl, { foo = 2 })
      assert.are.same(tbl, { foo = 2, a = 0 })
    end)
  end)
end)

describe('web.utils.equals', function()
  context('when values are primitives', function()
    it('checks for equality', function()
      assert.is_true(web.utils.equals(1, 1))
      assert.is_false(web.utils.equals(3, 1))
      assert.is_false(web.utils.equals(nil, 1))
      assert.is_false(web.utils.equals(1, nil))
      assert.is_true(web.utils.equals(nil, nil))
    end)
  end)

  context('when values are tables', function()
    it('checks for equality', function()
      assert.is_false(web.utils.equals({}, 0))
      assert.is_false(web.utils.equals(0, {}))
      assert.is_true(web.utils.equals({}, {}))
      assert.is_true(web.utils.equals({ a = 1 }, { a = 1 }))
      assert.is_false(web.utils.equals({ a = 2 }, { a = 1 }))
      assert.is_false(web.utils.equals({}, { a = 1 }))
      assert.is_false(web.utils.equals({ a = 1 }, {}))
    end)

    context('when tables are deeply nested', function()
      it('checks for equality', function()
        assert.is_true(web.utils.equals({ a = { b = 2 } }, { a = { b = 2 } }))
        assert.is_false(web.utils.equals({ a = { b = 3 } }, { a = { b = 2 } }))
      end)
    end)
  end)
end)

describe('web.utils.table_keys', function()
  it('returns keys of given table', function()
    local result = web.utils.table_keys({ a = 1, b = 2 })
    table.sort(result) -- Sort for deterministic ordering
    assert.are.same(result, { 'a', 'b' })
  end)

  context('with index keys', function()
    it('returns indexes of given table', function()
      assert.are.same(web.utils.table_keys({ 'hello', 'world' }), { 1, 2 })
      assert.are.same(web.utils.table_keys({ 'hello', a = 1, 'world' }), { 1, 2, 'a' })
    end)
  end)
end)

describe('web.inspect', function()
  it('returns string representation of value', function()
    assert.are.equal(web.inspect('hello'), '"hello"')
    assert.are.equal(web.inspect(5), '5')
    assert.are.equal(web.inspect({ a = '200', b = 5 }), '{\n  a = "200",\n  b = 5\n}')
  end)
end)

describe('web.utils.table_contains', function()
  context('when table is empty', function()
    it('returns true', function()
      local result = web.utils.table_contains({}, 2)
      assert.is_false(result)
    end)
  end)

  context('when table is a list', function()
    context('when list contains given value', function()
      it('returns true', function()
        local result = web.utils.table_contains({ 1, 2 }, 2)
        assert.is_true(result)
      end)
    end)

    context('when table does not contain given value', function()
      it('returns false', function()
        local result = web.utils.table_contains({ 1, 2 }, 99)
        assert.is_false(result)
      end)
    end)
  end)

  context('when table is a table with keys', function()
    context('when table contains given value', function()
      it('returns true', function()
        local result = web.utils.table_contains({ a = 1, b = 2 }, 2)
        assert.is_true(result)
      end)
    end)

    context('when table does not contain given value', function()
      it('returns false', function()
        local result = web.utils.table_contains({ a = 1, b = 2 }, 99)
        assert.is_false(result)
      end)
    end)
  end)
end)