File: test-tasty.lua

package info (click to toggle)
haskell-tasty-lua 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104 kB
  • sloc: haskell: 289; makefile: 3
file content (182 lines) | stat: -rw-r--r-- 4,654 bytes parent folder | download | duplicates (2)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local tasty = require 'tasty'

local arbitrary = tasty.arbitrary
local assert = tasty.assert
local forall = tasty.forall
local test = tasty.test_case
local group = tasty.test_group

return {
  group 'assertors'  {
    group 'error_matches' {
      test('succeeds if error matches', function ()
        assert.error_matches(
          function () error 'Futurama' end,
          'Futura'
        )
      end),
      test('fails if function succeeds', function ()
        local success = pcall(assert.error_matches, function () end, '')
        assert.is_falsy(success)
      end)
    },

    group 'error_satisfies' {
      test('succeeds if error satisfies the assertion', function ()
        assert.error_satisfies(
          function () error(true) end,
          assert.is_true
        )
      end),
      test('fails if function succeeds', function ()
        local success = pcall(assert.error_satifies, function () end, assert)
        assert.is_falsy(success)
      end)
    },

    group 'error_equals' {
      test('succeeds if error is equal', function ()
        assert.error_equals(
          function () error(42) end,
          42
        )
      end),
      test('fails if function succeeds', function ()
        local success = pcall(assert.error_equals, function () end, '')
        assert.is_falsy(success)
      end)
    },

    group 'is_truthy' {
      test('zero is truthy', function() assert.is_truthy(0) end),
      test('true is truthy', function() assert.is_truthy(true) end),
      test('empty string is truthy', function() assert.is_truthy '' end),
    },

    group 'is_falsy' {
      test('false is falsy', function() assert.is_falsy(false) end),
      test('nil is falsy', function() assert.is_falsy(nil) end),
    },

    group 'is_true' {
      test('succeeds on `true`', function() assert.is_true(true) end),
      test('fails on 1', function()
        local success = pcall(assert.is_true, 1)
        assert.is_true(not success)
      end),
    },

    group 'is_false' {
      test('succeeds on `false`', function() assert.is_false(false) end),
      test('fails on nil', function()
        local success = pcall(assert.is_false, nil)
        assert.is_false(success)
      end),
    },

    group 'is_nil' {
      test('nil is nil', function () assert.is_nil(nil) end)
    },

    group 'are_equal' {
      test('equal strings', function () assert.are_equal('test', 'test') end)
    },

    group 'are_same' {
      test('numbers', function ()
        assert.are_same(1, 1)
      end),
      test('nil', function ()
        assert.are_same(nil, nil)
      end),
      test('table', function ()
        assert.are_same({2, 3, 5, 7}, {2, 3, 5, 7})
      end),
      test('unequal numbers', function ()
        assert.error_matches(
          function () assert.are_same(0, 1) end,
          "expected same values, got 0 and 1"
        )
      end),
      test('tables', function ()
        assert.error_matches(
          function () assert.are_same({}, {1}) end,
          "expected same values, got"
        )
      end),
    },
  },
  group 'access via subtable' {
    test('assert.is.truthy', function ()
      assert(assert.is.truthy == assert.is_truthy)
    end),
    test('assert.are.equal', function ()
      assert(assert.are.equal == assert.are_equal)
    end),
    test('assert.error.matches', function ()
      assert(assert.error.matches == assert.error_matches)
    end),
  },
  group 'test currying' {
    test 'test name' (
      function ()
        return
      end
    )
  },
  group 'property testing'
  {
    test(
      'booleans',
      forall(
        arbitrary.boolean,
        function (b) return type(b) == 'boolean' end
      )
    ),
    test(
      'numbers',
      forall(
        arbitrary.number,
        function (n) return type(n) == 'number' end
      )
    ),
    test(
      'integers',
      forall(
        arbitrary.integer,
        function (i) return type(i) == 'number' and math.floor(i) == i end
      )
    ),
    test(
      'strings',
      forall(
        arbitrary.string,
        function (s) return type(s) == 'string' end
      )
    ),
    test(
      'custom',
      forall(
        arbitrary.custom,
        function (t)
          return type(t) == 'table' and type(t.int) == 'number'
        end
      )
    ),
    test(
      'list of integers',
      forall(
        arbitrary.integer_list,
        function (nums)
          if type(nums) ~= 'table' then return false end
          for _, i in ipairs(nums) do
            if type(i) ~= 'number' then
              return false
            end
          end
          return true
        end
      )
    )
  }
}