]> git.gir.st - VimFx.git/blob - extension/test/assert.coffee
Fix the `yy` command in Reader View
[VimFx.git] / extension / test / assert.coffee
1 ###
2 # Copyright Simon Lydell 2016.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 # This file provides a basic assertion library.
21
22 createError = (description, message = '') ->
23 formattedMessage = if message then "\nMessage: #{message}" else ''
24 error = new Error("Expected #{description}.#{formattedMessage}")
25 error.stack = error.stack.split('\n')[1..]
26 return error
27
28 format = (value) ->
29 try
30 string = JSON.stringify(value)
31 if string? and not
32 (string == '{}' and Object::toString.call(value) != '[object Object]')
33 return string
34 return String(value)
35
36 arrayEqual = (actual, expected, message = '') ->
37 unless Array.isArray(actual) and Array.isArray(expected)
38 throw createError(
39 "two arrays to compare. Got: #{format(actual)} and #{format(expected)}",
40 message
41 )
42 unless actual.length == expected.length and
43 actual.every((actualItem, index) -> actualItem == expected[index])
44 throw createError(
45 "#{format(actual)} to array-equal #{format(expected)}",
46 message
47 )
48
49 equal = (actual, expected, message = '') ->
50 return if actual == expected
51 throw createError("#{format(actual)} to equal #{format(expected)}", message)
52
53 notEqual = (actual, expected, message = '') ->
54 return if actual != expected
55 throw createError(
56 "#{format(actual)} NOT to equal #{format(expected)}",
57 message
58 )
59
60 ok = (actual, message = '') ->
61 return if actual
62 throw createError("#{format(actual)} to be truthy", message)
63
64 throws = (regex, badValue, fn) ->
65 try fn() catch error
66 start = 'VimFx:'
67 unless error.message.startsWith(start)
68 throw createError(
69 "thrown error message #{format(error.message)} to start with
70 #{format(start)}")
71 end = ": #{badValue}"
72 unless error.message.endsWith(end)
73 throw createError(
74 "thrown error message #{format(error.message)} to end with
75 #{format(end)}")
76 unless regex.test(error.message)
77 throw createError(
78 "thrown error message #{format(error.message)} to match the regex
79 #{format(regex)}")
80 return
81 throw createError("function to throw, but it did not: #{format(fn)}")
82
83 module.exports = {
84 arrayEqual
85 equal
86 notEqual
87 ok
88 throws
89 }
Imprint / Impressum