2 # Copyright Simon Lydell 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
20 # This file provides a basic assertion library.
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..]
30 string = JSON.stringify(value)
32 (string == '{}' and Object::toString.call(value) != '[object Object]')
36 arrayEqual = (actual, expected, message = '') ->
37 unless Array.isArray(actual) and Array.isArray(expected)
39 "two arrays to compare. Got: #{format(actual)} and #{format(expected)}",
42 unless actual.length == expected.length and
43 actual.every((actualItem, index) -> actualItem == expected[index])
45 "#{format(actual)} to array-equal #{format(expected)}",
49 equal = (actual, expected, message = '') ->
50 return if actual == expected
51 throw createError("#{format(actual)} to equal #{format(expected)}", message)
53 notEqual = (actual, expected, message = '') ->
54 return if actual != expected
56 "#{format(actual)} NOT to equal #{format(expected)}",
60 ok = (actual, message = '') ->
62 throw createError("#{format(actual)} to be truthy", message)
64 throws = (regex, badValue, fn) ->
67 unless error.message.startsWith(start)
69 "thrown error message #{format(error.message)} to start with
72 unless error.message.endsWith(end)
74 "thrown error message #{format(error.message)} to end with
76 unless regex.test(error.message)
78 "thrown error message #{format(error.message)} to match the regex
81 throw createError("function to throw, but it did not: #{format(fn)}")