hamcrest4qunit 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/hamcrest4qunit/description.js.coffee +23 -0
- data/app/assets/javascripts/hamcrest4qunit/h4q.js.coffee +158 -0
- data/app/assets/javascripts/hamcrest4qunit/index.js.coffee +7 -0
- data/app/assets/javascripts/hamcrest4qunit/matcher.js.coffee +31 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/arrays.js.coffee +221 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/compounds.js.coffee +134 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/core.js.coffee +126 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/dates.js.coffee +166 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/dom.js.coffee +73 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/functions.js.coffee +142 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/index.js.coffee +1 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/numbers.js.coffee +177 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/objects.js.coffee +249 -0
- data/app/assets/javascripts/hamcrest4qunit/matchers/strings.js.coffee +123 -0
- data/app/assets/javascripts/hamcrest4qunit/test/index.js +920 -0
- data/hamcrest4qunit.gemspec +20 -0
- data/lib/hamcrest4qunit.rb +11 -0
- data/lib/hamcrest4qunit/engine.rb +9 -0
- data/lib/hamcrest4qunit/version.rb +3 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Hamcrest4qunit
|
2
|
+
|
3
|
+
Hamcrest4QUnit is a port of the [Hamcrest](http://code.google.com/p/hamcrest/)
|
4
|
+
matchers set intended to work with [QUnit](http://docs.jquery.com/Qunit).
|
5
|
+
|
6
|
+
Sarting with the version 1.3.0, `hamcrest4qunit` is provided as a ruby gem.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'hamcrest4qunit'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install hamcrest4qunit
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Add this line to your tests manifest:
|
25
|
+
|
26
|
+
//= require hamcrest4qunit
|
27
|
+
|
28
|
+
To include the `hamcrest4qunit` test suite in your own tests, add this line to your tests manifest:
|
29
|
+
|
30
|
+
//= require hamcrest4qunit/test
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class h4q.Description
|
2
|
+
constructor: (@message="", @diff=null) ->
|
3
|
+
|
4
|
+
toString: -> @message
|
5
|
+
hasDiff: -> @diff?
|
6
|
+
clearMessage: -> @message = ""; this
|
7
|
+
appendText: (msg) -> @append msg; this
|
8
|
+
|
9
|
+
appendValue: (val) ->
|
10
|
+
@append "<span class='value'>#{QUnit.jsDump.parse val}</span>"
|
11
|
+
this
|
12
|
+
|
13
|
+
appendRawValue: (val) ->
|
14
|
+
@append "<span class='value'>#{val}</span>"
|
15
|
+
this
|
16
|
+
|
17
|
+
appendDescriptionOf: (matcher) ->
|
18
|
+
matcher.describeTo this
|
19
|
+
this
|
20
|
+
|
21
|
+
append: (s) ->
|
22
|
+
@message += " " if @message isnt ""
|
23
|
+
@message += s
|
@@ -0,0 +1,158 @@
|
|
1
|
+
TAB_STRING = " "
|
2
|
+
|
3
|
+
h4q = {}
|
4
|
+
window.h4q = h4q
|
5
|
+
|
6
|
+
h4q.tokenReplace = (s) ->
|
7
|
+
args = h4q.argumentsToArray arguments
|
8
|
+
args.shift()
|
9
|
+
|
10
|
+
if args.length > 0 and args[0] instanceof Object
|
11
|
+
kwargs = args[0]
|
12
|
+
s = s.replace /\${([^}]+)}/g, ->
|
13
|
+
key = arguments[1]
|
14
|
+
if kwargs.hasOwnProperty(key) then kwargs[key] else arguments[0]
|
15
|
+
|
16
|
+
args.shift()
|
17
|
+
|
18
|
+
s.replace /\$([0-9]+)/g, ->
|
19
|
+
index = parseInt arguments[1]
|
20
|
+
if index < args.length then args[index] else arguments[0]
|
21
|
+
|
22
|
+
h4q.argumentsToArray = (args) -> arg for arg in args
|
23
|
+
|
24
|
+
h4q.sortProperties = (o) ->
|
25
|
+
b = (k for k of o)
|
26
|
+
b.sort()
|
27
|
+
b
|
28
|
+
|
29
|
+
h4q.getType = (thing) ->
|
30
|
+
return "Null" unless thing?
|
31
|
+
|
32
|
+
results = /\[object (.{1,})\]/.exec Object.prototype.toString.call thing
|
33
|
+
|
34
|
+
if results and results.length > 1 then results[1] else ""
|
35
|
+
|
36
|
+
h4q.getTypeName = (type) ->
|
37
|
+
results = /function (.{1,})\(/.exec type.toString()
|
38
|
+
if results and results.length > 1 then results[1] else ""
|
39
|
+
|
40
|
+
h4q.aPrefix = (s) -> if s.substr(0,1) in "aeiouAEIOU" then "an" else "a"
|
41
|
+
|
42
|
+
h4q.escapeHtml = (s) ->
|
43
|
+
return "" unless s?
|
44
|
+
|
45
|
+
s = String(s)
|
46
|
+
s.replace /[\&"<>\\]/g, (s) ->
|
47
|
+
switch s
|
48
|
+
when "&" then "&"
|
49
|
+
when "\\" then "\\\\"
|
50
|
+
when '"' then '\"'
|
51
|
+
when "<" then "<"
|
52
|
+
when ">" then ">"
|
53
|
+
else s
|
54
|
+
|
55
|
+
h4q.nodeToString = (node, indent="") ->
|
56
|
+
unless node? and node instanceof Node
|
57
|
+
throw new Error
|
58
|
+
message: "nodeToString expect a valid node as argument"
|
59
|
+
|
60
|
+
isRootCall = indent is ""
|
61
|
+
tag = String(node.nodeName).toLowerCase()
|
62
|
+
attr = h4q.attrToString node
|
63
|
+
content = h4q.contentToString node, indent
|
64
|
+
|
65
|
+
s = "#{indent}<#{tag}#{attr}>#{content}</#{tag}>"
|
66
|
+
|
67
|
+
if isRootCall then h4q.escapeHtml s else s
|
68
|
+
|
69
|
+
h4q.attrToString = (node) ->
|
70
|
+
exludedAttrs = [
|
71
|
+
"length", "item", "getNamedItem",
|
72
|
+
"setNamedItem", "removeNamedItem", "getNamedItemNS",
|
73
|
+
"setNamedItemNS", "removeNamedItemNS"
|
74
|
+
]
|
75
|
+
|
76
|
+
s = ""
|
77
|
+
alreadyPrinted = []
|
78
|
+
for attr of node.attributes
|
79
|
+
name = node.attributes[attr].name
|
80
|
+
|
81
|
+
unless attr in exludedAttrs or name in alreadyPrinted
|
82
|
+
s += " #{name}=\"#{node.attributes[attr].value}\""
|
83
|
+
alreadyPrinted.push name
|
84
|
+
|
85
|
+
return s
|
86
|
+
|
87
|
+
h4q.contentToString = (node, indent) ->
|
88
|
+
s = ""
|
89
|
+
nodes = node.childNodes
|
90
|
+
return "" unless nodes?
|
91
|
+
|
92
|
+
l = nodes.length
|
93
|
+
contentIsOnlyText = true
|
94
|
+
res = []
|
95
|
+
for node in nodes
|
96
|
+
|
97
|
+
if node.nodeType is 3 then res.push node.textContent
|
98
|
+
else
|
99
|
+
res.push h4q.nodeToString node, indent + TAB_STRING
|
100
|
+
contentIsOnlyText = false
|
101
|
+
|
102
|
+
if contentIsOnlyText then s += res.join ""
|
103
|
+
else
|
104
|
+
res = res.map (o, i) ->
|
105
|
+
if /</.exec o then o else indent + TAB_STRING + o
|
106
|
+
|
107
|
+
s += "\n#{res.join "\n"}"
|
108
|
+
|
109
|
+
s + (if l > 0 and not contentIsOnlyText then "\n#{indent}" else "")
|
110
|
+
|
111
|
+
h4q.push = (result, mismatchMsg, expectedMsg, message) ->
|
112
|
+
details =
|
113
|
+
result: result
|
114
|
+
message: message
|
115
|
+
|
116
|
+
resultMsg = "<tr class='test-actual'>
|
117
|
+
<th>#{if result? then "And:" else "But:"}</th>
|
118
|
+
<td><pre>#{mismatchMsg.message}</pre></td>
|
119
|
+
</tr>"
|
120
|
+
|
121
|
+
diffMsg = if mismatchMsg.hasDiff()
|
122
|
+
"<tr class='test-diff'>
|
123
|
+
<th>Diff: </th>
|
124
|
+
<td><pre>#{mismatchMsg.diff}</pre></td>
|
125
|
+
</tr>"
|
126
|
+
else
|
127
|
+
""
|
128
|
+
|
129
|
+
output = "<span class='test-message'>#{message}</span>
|
130
|
+
<table>
|
131
|
+
<tr class='test-expected'>
|
132
|
+
<th>Expected: </th>
|
133
|
+
<td><pre>#{expectedMsg.message}</pre></td>
|
134
|
+
</tr>
|
135
|
+
#{resultMsg}
|
136
|
+
#{diffMsg}
|
137
|
+
</table>"
|
138
|
+
|
139
|
+
QUnit.log details
|
140
|
+
QUnit.config.current.assertions.push
|
141
|
+
result: result,
|
142
|
+
message: output
|
143
|
+
|
144
|
+
h4q.assertThat = (target, matcher=h4q.equalTo(true), description=null) ->
|
145
|
+
throw new Error message: "Unsupported assertion" if arguments.length is 0
|
146
|
+
|
147
|
+
expectedMsg = new Description()
|
148
|
+
mismatchMsg = new Description()
|
149
|
+
|
150
|
+
matcher = h4q.equalTo matcher unless matcher instanceof Matcher
|
151
|
+
|
152
|
+
expectedMsg.appendDescriptionOf( matcher )
|
153
|
+
result = matcher.matches( target, mismatchMsg )
|
154
|
+
|
155
|
+
h4q.push result,
|
156
|
+
mismatchMsg,
|
157
|
+
expectedMsg,
|
158
|
+
if message? then message else if result? then "okay" else "failed"
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class h4q.Matcher
|
2
|
+
constructor: (options) ->
|
3
|
+
|
4
|
+
unless options._matches
|
5
|
+
throw new Error
|
6
|
+
message: "Improperly configured matcher, the matcher
|
7
|
+
expect a _matches method"
|
8
|
+
|
9
|
+
if typeof options._matches isnt "function"
|
10
|
+
throw new Error
|
11
|
+
message: "Improperly configured matcher, the _matches
|
12
|
+
argument must be a function"
|
13
|
+
|
14
|
+
unless options._describeTo
|
15
|
+
throw new Error
|
16
|
+
message: "Improperly configured matcher, the matcher expect
|
17
|
+
a _describeTo method"
|
18
|
+
|
19
|
+
if typeof options._describeTo isnt "function"
|
20
|
+
throw new Error
|
21
|
+
message: "Improperly configured matcher, the _describeTo
|
22
|
+
argument must be a function"
|
23
|
+
|
24
|
+
@[k] = v for k, v of options
|
25
|
+
|
26
|
+
matches: (v, mismatchMsg) ->
|
27
|
+
@_matches.call this, v, mismatchMsg
|
28
|
+
|
29
|
+
describeTo: ( msg ) ->
|
30
|
+
@_describeTo.call this, msg
|
31
|
+
|
@@ -0,0 +1,221 @@
|
|
1
|
+
h4q.arrayWithLength = (l) ->
|
2
|
+
if isNaN l
|
3
|
+
throw new Error message: "length argument must be a valid number"
|
4
|
+
if l < 0
|
5
|
+
throw new Error
|
6
|
+
message: "length argument must be greater than or equal to 0"
|
7
|
+
|
8
|
+
new Matcher
|
9
|
+
length: l
|
10
|
+
_matches: (v,msg) ->
|
11
|
+
msg.appendText("was").appendValue(v)
|
12
|
+
v? and
|
13
|
+
v instanceof Array and
|
14
|
+
v.length == @length
|
15
|
+
|
16
|
+
_describeTo: (msg) ->
|
17
|
+
msg.appendText("an Array with")
|
18
|
+
.appendValue(@length)
|
19
|
+
.appendText(if @length > 1 then "items" else "item")
|
20
|
+
|
21
|
+
h4q.emptyArray = ->
|
22
|
+
new Matcher
|
23
|
+
_matches: (v,msg) ->
|
24
|
+
msg.appendText("was").appendValue(v)
|
25
|
+
v? and
|
26
|
+
v instanceof Array and
|
27
|
+
v.length is 0
|
28
|
+
|
29
|
+
_describeTo: (msg) ->
|
30
|
+
msg.appendText "an empty Array"
|
31
|
+
|
32
|
+
h4q.array = ->
|
33
|
+
args = h4q.argumentsToArray arguments
|
34
|
+
matchers = []
|
35
|
+
l = args.length
|
36
|
+
|
37
|
+
return h4q.emptyArray() if l is 0
|
38
|
+
|
39
|
+
for m in args
|
40
|
+
if m instanceof Matcher
|
41
|
+
matchers.push m
|
42
|
+
else
|
43
|
+
matchers.push equalTo m
|
44
|
+
|
45
|
+
new Matcher
|
46
|
+
matchers: matchers
|
47
|
+
_matches: (v,msg) ->
|
48
|
+
l = @matchers.length
|
49
|
+
|
50
|
+
unless v?
|
51
|
+
msg.appendText("was").appendValue(null)
|
52
|
+
false
|
53
|
+
else unless v instanceof Array
|
54
|
+
msg.appendText("was").appendValue(v)
|
55
|
+
false
|
56
|
+
else if v.length isnt l
|
57
|
+
msg.appendText("was an Array with")
|
58
|
+
.appendValue(v.length)
|
59
|
+
.appendText(if v.length > 1 then "items" else "item")
|
60
|
+
false
|
61
|
+
else
|
62
|
+
a = @matchers
|
63
|
+
hasError = false
|
64
|
+
msg.appendText("was").appendRawValue("[").appendText("\n")
|
65
|
+
|
66
|
+
v.forEach (o,i) ->
|
67
|
+
msgtmp = new Description()
|
68
|
+
res = a[i].matches o, msgtmp
|
69
|
+
msg.appendText " "
|
70
|
+
if res
|
71
|
+
msg.appendValue o
|
72
|
+
else
|
73
|
+
msg.appendText msgtmp.message
|
74
|
+
hasError = true
|
75
|
+
|
76
|
+
msg.appendText ",\n" if i < l-1
|
77
|
+
|
78
|
+
msg.appendRawValue "\n]"
|
79
|
+
|
80
|
+
not hasError
|
81
|
+
|
82
|
+
_describeTo: (msg) ->
|
83
|
+
l = @matchers.length
|
84
|
+
|
85
|
+
msg.appendText("an Array with")
|
86
|
+
.appendValue(l)
|
87
|
+
.appendText(if l > 1 then "items" else "item")
|
88
|
+
.appendText("like").appendRawValue("[").appendText("\n ")
|
89
|
+
|
90
|
+
for matcher, i in @matchers
|
91
|
+
msg.appendDescriptionOf matcher
|
92
|
+
msg.appendText(",\n ") if i < l-1
|
93
|
+
|
94
|
+
msg.appendRawValue("\n]")
|
95
|
+
|
96
|
+
h4q.everyItem = (m) ->
|
97
|
+
unless m?
|
98
|
+
throw new Error message: "everyItem must receive an argument"
|
99
|
+
|
100
|
+
m = equalTo m unless m instanceof Matcher
|
101
|
+
|
102
|
+
new Matcher
|
103
|
+
matcher: m
|
104
|
+
_matches: (v, msg) ->
|
105
|
+
unless v?
|
106
|
+
msg.appendText("was").appendValue(null)
|
107
|
+
false
|
108
|
+
else unless v instanceof Array
|
109
|
+
msg.appendText("was").appendValue(v)
|
110
|
+
false
|
111
|
+
else
|
112
|
+
for val, i in v
|
113
|
+
msgtmp = new Description()
|
114
|
+
unless @matcher.matches val, msgtmp
|
115
|
+
msg.appendText(msgtmp.message)
|
116
|
+
.appendText("at index")
|
117
|
+
.appendValue(i)
|
118
|
+
return false
|
119
|
+
|
120
|
+
msg.appendText("was").appendValue(v)
|
121
|
+
true
|
122
|
+
|
123
|
+
_describeTo: (msg) ->
|
124
|
+
msg.appendText("an Array of which every item is")
|
125
|
+
.appendDescriptionOf( @matcher )
|
126
|
+
|
127
|
+
h4q.hasItem = (m) ->
|
128
|
+
unless m?
|
129
|
+
throw new Error message: "hasItem must receive an argument"
|
130
|
+
|
131
|
+
m = equalTo m unless m instanceof Matcher
|
132
|
+
|
133
|
+
new Matcher
|
134
|
+
matcher: m
|
135
|
+
_matches: (v,msg) ->
|
136
|
+
unless v?
|
137
|
+
msg.appendText("was").appendValue(null)
|
138
|
+
false
|
139
|
+
unless v instanceof Array
|
140
|
+
msg.appendText("was").appendValue(v)
|
141
|
+
false
|
142
|
+
else
|
143
|
+
l = v.length
|
144
|
+
for val in v
|
145
|
+
msgtmp = new Description()
|
146
|
+
if @matcher.matches val, msgtmp
|
147
|
+
msg.appendText("was").appendValue(v)
|
148
|
+
return true
|
149
|
+
|
150
|
+
msg.appendText "can't find any"
|
151
|
+
false
|
152
|
+
|
153
|
+
_describeTo: (msg) ->
|
154
|
+
msg.appendText("an Array containing").appendDescriptionOf(@matcher)
|
155
|
+
|
156
|
+
h4q.hasItems = ->
|
157
|
+
args = h4q.argumentsToArray arguments
|
158
|
+
if args.length is 0
|
159
|
+
throw new Error message: "hasItems must receive at least one argument"
|
160
|
+
|
161
|
+
matchers = []
|
162
|
+
for m in args
|
163
|
+
if m instanceof Matcher
|
164
|
+
matchers.push m
|
165
|
+
else
|
166
|
+
matchers.push equalTo m
|
167
|
+
|
168
|
+
new Matcher
|
169
|
+
matchers: matchers
|
170
|
+
_matches: (v,msg) ->
|
171
|
+
unless v?
|
172
|
+
msg.appendText("was").appendValue(null)
|
173
|
+
false
|
174
|
+
unless v instanceof Array
|
175
|
+
msg.appendText("was").appendValue(v)
|
176
|
+
false
|
177
|
+
else
|
178
|
+
l = @matchers.length
|
179
|
+
k = v.length
|
180
|
+
gres = true
|
181
|
+
mismsg = []
|
182
|
+
msgtmp = new Description()
|
183
|
+
for m in @matchers
|
184
|
+
res = false
|
185
|
+
for val, j in v
|
186
|
+
res = m.matches(val, msgtmp) or res
|
187
|
+
|
188
|
+
unless res
|
189
|
+
d = new Description()
|
190
|
+
d.appendDescriptionOf m
|
191
|
+
mismsg.push d
|
192
|
+
|
193
|
+
gres = res and gres
|
194
|
+
|
195
|
+
if gres
|
196
|
+
msg.appendText("was").appendValue(v)
|
197
|
+
else
|
198
|
+
l = mismsg.length
|
199
|
+
if l is 1
|
200
|
+
msg.appendText("can't find").appendText( mismsg )
|
201
|
+
else
|
202
|
+
msg.appendText("can't find neither")
|
203
|
+
for v, i in mismsg
|
204
|
+
if i is l-1
|
205
|
+
msg.appendText "nor"
|
206
|
+
else if i > 0
|
207
|
+
msg.appendText ","
|
208
|
+
|
209
|
+
msg.appendText v
|
210
|
+
gres
|
211
|
+
|
212
|
+
_describeTo: (msg) ->
|
213
|
+
msg.appendText "an Array containing"
|
214
|
+
for m, i in @matchers
|
215
|
+
if i is @matchers.length - 1
|
216
|
+
msg.appendText "and"
|
217
|
+
else if i > 0
|
218
|
+
msg.appendText ","
|
219
|
+
|
220
|
+
msg.appendDescriptionOf m
|
221
|
+
|