assert2 0.4.7 → 0.4.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/assert2/rjs.rb +131 -0
- data/lib/assert2/xhtml.rb +19 -16
- metadata +3 -2
data/lib/assert2/rjs.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'rkelly/visitors' # ERGO advise AP these requirers are broke!
|
2
|
+
require 'rkelly/visitable'
|
3
|
+
require 'rkelly/nodes/node'
|
4
|
+
require 'rkelly/nodes/postfix_node'
|
5
|
+
require 'rkelly'
|
6
|
+
require 'assert2/xhtml'
|
7
|
+
|
8
|
+
module Test; module Unit; module Assertions
|
9
|
+
|
10
|
+
class AssertRjs
|
11
|
+
def initialize(js, command, scope)
|
12
|
+
@js, @command, @scope = js, command, scope
|
13
|
+
end
|
14
|
+
|
15
|
+
attr_reader :command, :js, :scope
|
16
|
+
|
17
|
+
def match(kode)
|
18
|
+
RKelly.parse(js).pointcut(kode).
|
19
|
+
matches.each do |updater|
|
20
|
+
updater.grep(RKelly::Nodes::ArgumentsNode).each do |thang|
|
21
|
+
yield thang
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# TODO implement assert_no_rjs by upgrading scope to UnScope
|
27
|
+
|
28
|
+
def complain(about)
|
29
|
+
"#{ command } #{ about }\n#{ js }"
|
30
|
+
end
|
31
|
+
|
32
|
+
def flunk(about)
|
33
|
+
scope.flunk(complain(about))
|
34
|
+
end
|
35
|
+
|
36
|
+
def match_or_flunk(why)
|
37
|
+
@matcher = @matcher.to_s if @matcher.kind_of?(Symbol)
|
38
|
+
scope.assert_match @matcher, @text, complain(why)
|
39
|
+
end
|
40
|
+
|
41
|
+
def pwn_call *args, &block # TODO use or reject the block
|
42
|
+
target, matchers_backup = args[0], args[1..-1]
|
43
|
+
|
44
|
+
match "#{target}()" do |thang|
|
45
|
+
matchers = matchers_backup.dup
|
46
|
+
|
47
|
+
thang.value.each do |arg|
|
48
|
+
@text = eval(arg.value)
|
49
|
+
@matcher = matchers.first # or return @text
|
50
|
+
@matcher.to_s == @text or /#{ @matcher }/ =~ @text or break
|
51
|
+
matchers.shift
|
52
|
+
end
|
53
|
+
|
54
|
+
matchers.empty? and
|
55
|
+
matchers_backup.length == thang.value.length and
|
56
|
+
return @text
|
57
|
+
end
|
58
|
+
|
59
|
+
matchers = matchers_backup.inspect
|
60
|
+
|
61
|
+
scope.flunk("#{ command } to #{ target } with arguments #{
|
62
|
+
matchers } not found in #{ js }")
|
63
|
+
end
|
64
|
+
|
65
|
+
class ALERT < AssertRjs
|
66
|
+
def pwn *args, &block
|
67
|
+
@command = :call
|
68
|
+
pwn_call :alert, *args, &block
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class REMOVE < AssertRjs
|
73
|
+
def pwn *args, &block
|
74
|
+
@command = :call
|
75
|
+
pwn_call 'Element.remove', *args, &block
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class CALL < AssertRjs
|
80
|
+
def pwn *args, &block # TODO use or reject the block
|
81
|
+
pwn_call *args, &block
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class REPLACE_HTML < AssertRjs
|
86
|
+
def pwn *args, &block
|
87
|
+
target, @matcher = args
|
88
|
+
@matcher ||= //
|
89
|
+
|
90
|
+
match concept do |thang|
|
91
|
+
div_id, html = thang.value
|
92
|
+
|
93
|
+
if target and html
|
94
|
+
div_id = eval(div_id.value)
|
95
|
+
html = html.value.gsub('\u003C', '<').
|
96
|
+
gsub('\u003E', '>') # ERGO give a crap about encoding!
|
97
|
+
html = eval(html)
|
98
|
+
|
99
|
+
if div_id == target.to_s
|
100
|
+
cornplaint = complain("for ID #{ target } has incorrect payload, in")
|
101
|
+
scope.assert_match @matcher, html, cornplaint if @matcher
|
102
|
+
scope.assert_xhtml html, cornplaint, &block if block
|
103
|
+
return html
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
flunk "for ID #{ target } not found in"
|
109
|
+
end
|
110
|
+
def concept; 'Element.update()'; end
|
111
|
+
end
|
112
|
+
|
113
|
+
class REPLACE < REPLACE_HTML
|
114
|
+
def concept; 'Element.replace()'; end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def assert_rjs_(command, *args, &block)
|
119
|
+
klass = command.to_s.upcase
|
120
|
+
klass = eval("AssertRjs::#{klass}") rescue
|
121
|
+
flunk("#{command} not implemented!")
|
122
|
+
asserter = klass.new(@response.body, command, self)
|
123
|
+
return asserter.pwn(*args, &block)
|
124
|
+
end
|
125
|
+
|
126
|
+
# command == :replace_html or # TODO put me inside the method_missing!
|
127
|
+
# flunk("assert_rjs's alpha version only respects :replace_html")
|
128
|
+
# TODO also crack out the args correctly and gripe if they wrong
|
129
|
+
# TODO TDD the @matcher can be a string or regexp
|
130
|
+
|
131
|
+
end; end; end
|
data/lib/assert2/xhtml.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
=begin
|
2
|
-
One Yury Kotlyarov recently this Rails project as a question:
|
2
|
+
One Yury Kotlyarov recently posted this Rails project as a question:
|
3
3
|
|
4
4
|
http://github.com/yura/howto-rspec-custom-matchers/tree/master
|
5
5
|
|
@@ -71,13 +71,14 @@ class BeHtmlWith
|
|
71
71
|
attr_accessor :builder,
|
72
72
|
:doc,
|
73
73
|
:failure_message,
|
74
|
+
:message,
|
74
75
|
:scope
|
75
76
|
attr_reader :references
|
76
77
|
attr_writer :reference,
|
77
78
|
:sample
|
78
79
|
|
79
80
|
def matches?(stwing, &block)
|
80
|
-
@block
|
81
|
+
@block ||= block # ERGO test that ||= - preferrably with a real RSpec suite!
|
81
82
|
|
82
83
|
@scope.wrap_expectation self do
|
83
84
|
@doc = Nokogiri::HTML(stwing)
|
@@ -159,7 +160,6 @@ class BeHtmlWith
|
|
159
160
|
match_attributes and match_text
|
160
161
|
end
|
161
162
|
|
162
|
-
# TODO document without! and xpath! in the diagnostic
|
163
163
|
# TODO uh, indenting mebbe?
|
164
164
|
|
165
165
|
def match_attributes
|
@@ -235,7 +235,7 @@ class BeHtmlWith
|
|
235
235
|
|
236
236
|
def get_texts(element)
|
237
237
|
element.children.grep(Nokogiri::XML::Text).
|
238
|
-
map{|x|
|
238
|
+
map{|x|x.to_s.strip}.select{|x|x.any?}
|
239
239
|
end
|
240
240
|
|
241
241
|
def collect_best_sample(samples)
|
@@ -252,10 +252,12 @@ class BeHtmlWith
|
|
252
252
|
|
253
253
|
def complain( refered = @builder.doc,
|
254
254
|
sample = @best_sample || @doc.root )
|
255
|
-
|
256
|
-
|
255
|
+
# ERGO use to_xml? or what?
|
256
|
+
@failure_message = "#{message}\n".lstrip +
|
257
|
+
"\nCould not find this reference...\n\n" +
|
258
|
+
refered.to_xhtml.sub(/^\<\!DOCTYPE.*/, '') +
|
257
259
|
"\n\n...in this sample...\n\n" +
|
258
|
-
sample.
|
260
|
+
sample.to_xml
|
259
261
|
end
|
260
262
|
|
261
263
|
def build_deep_xpath_too(element)
|
@@ -297,13 +299,21 @@ module Test; module Unit; module Assertions
|
|
297
299
|
|
298
300
|
def wrap_expectation whatever; yield; end unless defined? wrap_expectation
|
299
301
|
|
300
|
-
def assert_xhtml(
|
302
|
+
def assert_xhtml(*args, &block) # ERGO merge
|
303
|
+
xhtml, message = args
|
304
|
+
|
305
|
+
if @response and message.nil?
|
306
|
+
message = xhtml
|
307
|
+
xhtml = @response.body
|
308
|
+
end
|
309
|
+
|
301
310
|
if block
|
302
311
|
matcher = BeHtmlWith.new(self, &block)
|
312
|
+
matcher.message = message
|
303
313
|
matcher.matches?(xhtml, &block)
|
304
314
|
message = matcher.failure_message
|
305
315
|
flunk message if message.to_s != ''
|
306
|
-
#
|
316
|
+
# return matcher.builder.doc.to_html # TODO return something reasonable
|
307
317
|
else
|
308
318
|
_assert_xml(xhtml)
|
309
319
|
return @xdoc
|
@@ -322,13 +332,6 @@ class Nokogiri::XML::Node
|
|
322
332
|
def content= string
|
323
333
|
self.native_content = encode_special_chars(string.to_s)
|
324
334
|
end
|
325
|
-
end
|
326
|
-
|
327
|
-
class Nokogiri::XML::Builder
|
328
|
-
def text(string)
|
329
|
-
node = Nokogiri::XML::Text.new(string.to_s, @doc)
|
330
|
-
insert(node)
|
331
|
-
end
|
332
335
|
end # ERGO retire these monkey patches as Nokogiri catches up
|
333
336
|
|
334
337
|
class Nokogiri::XML::Node
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assert2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phlip
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-04-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -24,6 +24,7 @@ extra_rdoc_files: []
|
|
24
24
|
files:
|
25
25
|
- lib/assert2
|
26
26
|
- lib/assert2/xhtml.rb~
|
27
|
+
- lib/assert2/rjs.rb
|
27
28
|
- lib/assert2/flunk.rb
|
28
29
|
- lib/assert2/rubynode_reflector.rb~
|
29
30
|
- lib/assert2/xpath.rb~
|