robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
5.4 KiB173 linesraw
1local t = require('test.testutil')
2local describe, it, before_each = t.describe, t.it, t.before_each
3local pcall_err = t.pcall_err
4local n = require('test.functional.testnvim')()
5
6local api, call = n.api, n.call
7local clear, eq = n.clear, t.eq
8local source, command = n.source, n.command
9local eval = n.eval
10local command = n.command
11
12local function expected_errors(errors)
13  eq(errors, api.nvim_get_vvar('errors'))
14end
15
16local function expected_empty()
17  eq({}, api.nvim_get_vvar('errors'))
18end
19
20describe('assert function:', function()
21  before_each(function()
22    clear()
23  end)
24
25  -- assert_equal({expected}, {actual}, [, {msg}])
26  describe('assert_equal', function()
27    it('should not change v:errors when expected is equal to actual', function()
28      source([[
29        fu Func()
30        endfu
31        let F1 = function('Func')
32        let F2 = function('Func')
33        call assert_equal(F1, F2)
34      ]])
35      expected_empty()
36    end)
37
38    it('should not change v:errors when expected is equal to actual', function()
39      eq(0, call('assert_equal', '', ''))
40      eq(0, call('assert_equal', 'string', 'string'))
41      expected_empty()
42    end)
43
44    it('should change v:errors when expected is not equal to actual', function()
45      eq(1, call('assert_equal', 0, { 0 }))
46      expected_errors({ 'Expected 0 but got [0]' })
47    end)
48
49    it('should change v:errors when expected is not equal to actual', function()
50      eq(1, call('assert_equal', 0, '0'))
51      expected_errors({ "Expected 0 but got '0'" })
52    end)
53
54    it('should change v:errors when expected is not equal to actual', function()
55      -- Lua does not tell integer from float.
56      command('call assert_equal(1, 1.0)')
57      expected_errors({ 'Expected 1 but got 1.0' })
58    end)
59
60    it('should change v:errors when expected is not equal to actual', function()
61      call('assert_equal', 'true', 'false')
62      expected_errors({ "Expected 'true' but got 'false'" })
63    end)
64
65    it('should change v:errors when expected is not equal to actual', function()
66      source([[
67      function CheckAssert()
68        let s:v = {}
69        let s:x = {"a": s:v}
70        let s:v["b"] = s:x
71        let s:w = {"c": s:x, "d": ''}
72        call assert_equal(s:w, '')
73      endfunction
74      ]])
75      t.matches(
76        'Vim%(call%):E724: unable to correctly dump variable with self%-referencing container',
77        pcall_err(command, 'call CheckAssert()')
78      )
79    end)
80  end)
81
82  -- assert_false({actual}, [, {msg}])
83  describe('assert_false', function()
84    it('should not change v:errors when actual is false', function()
85      eq(0, call('assert_false', 0))
86      eq(0, call('assert_false', false))
87      expected_empty()
88    end)
89
90    it('should change v:errors when actual is not false', function()
91      eq(1, call('assert_false', 1))
92      expected_errors({ 'Expected False but got 1' })
93    end)
94
95    it('should change v:errors when actual is not false', function()
96      call('assert_false', {})
97      expected_errors({ 'Expected False but got []' })
98    end)
99  end)
100
101  -- assert_true({actual}, [, {msg}])
102  describe('assert_true', function()
103    it('should not change v:errors when actual is true', function()
104      eq(0, call('assert_true', 1))
105      eq(0, call('assert_true', -1)) -- In Vim script, non-zero Numbers are TRUE.
106      eq(0, call('assert_true', true))
107      expected_empty()
108    end)
109
110    it('should change v:errors when actual is not true', function()
111      eq(1, call('assert_true', 1.5))
112      expected_errors({ 'Expected True but got 1.5' })
113    end)
114  end)
115
116  describe('v:errors', function()
117    it('should be initialized at startup', function()
118      expected_empty()
119    end)
120
121    it('should have function names and relative line numbers', function()
122      source([[
123        fu Func_one()
124          call assert_equal([0], {'0' : 0})
125          call assert_false('False')
126          call assert_true("True")
127        endfu
128        fu Func_two()
129          " for shifting a line number
130          call assert_true('line two')
131        endfu
132      ]])
133      call('Func_one')
134      call('Func_two')
135      expected_errors({
136        "function Func_one line 1: Expected [0] but got {'0': 0}",
137        "function Func_one line 2: Expected False but got 'False'",
138        "function Func_one line 3: Expected True but got 'True'",
139        "function Func_two line 2: Expected True but got 'line two'",
140      })
141    end)
142
143    it('should have file names and passed messages', function()
144      source([[
145        call assert_equal(1, 100, 'equal assertion failed')
146        call assert_false('true', 'true  assertion failed')
147        call assert_true('false', 'false assertion failed')
148      ]])
149      source([[
150        call assert_true('', 'file two')
151      ]])
152      expected_errors({
153        'nvim_exec2() line 1: equal assertion failed: Expected 1 but got 100',
154        "nvim_exec2() line 2: true  assertion failed: Expected False but got 'true'",
155        "nvim_exec2() line 3: false assertion failed: Expected True but got 'false'",
156        "nvim_exec2() line 1: file two: Expected True but got ''",
157      })
158    end)
159  end)
160
161  -- assert_fails({cmd}, [, {error}])
162  describe('assert_fails', function()
163    it('should not change v:errors when cmd errors', function()
164      eq(0, eval([[assert_fails('NonexistentCmd')]]))
165      expected_empty()
166    end)
167
168    it('should change v:errors when cmd succeeds', function()
169      eq(1, eval([[assert_fails('call empty("")', '')]]))
170      expected_errors({ 'command did not fail: call empty("")' })
171    end)
172  end)
173end)