assert_xpath 0.3.1 → 0.4.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.
Files changed (3) hide show
  1. data/lib/assert_javascript.rb +30 -15
  2. data/lib/assert_xpath.rb +38 -12
  3. metadata +4 -21
@@ -96,8 +96,8 @@ module AssertJavaScript
96
96
  stuff = {}
97
97
 
98
98
  @xdoc.each_element(sit_and_spin) do |node|
99
- name = REXML::XPath.first(node, './/Identifier').text
100
- number = REXML::XPath.first(node, './/Number').text
99
+ name = node.get_path('.//Identifier').text
100
+ number = node.get_path('.//Number').text
101
101
  stuff[name.to_sym] = number.to_f
102
102
  # ERGO is to_f best?
103
103
  # ERGO simpler way to add an ostruct member?
@@ -172,29 +172,29 @@ module AssertJavaScript
172
172
  end # ERGO use assert_any_xpath
173
173
 
174
174
  jsonic.each_element('descendant-or-self::PropertyNameAndValueList/PropertyPair') do |node|
175
- name = REXML::XPath.first(node, 'PropertyName/*')
175
+ name = node.get_path('PropertyName/*')
176
176
  name = name.text.to_sym
177
177
 
178
178
  json[name] =
179
179
  case
180
- when b = REXML::XPath.first(node, 'ObjectLiteral/PropertyNameAndValueList')
180
+ when b = node.get_path('ObjectLiteral/PropertyNameAndValueList')
181
181
  assert_json(b)
182
182
 
183
- when b = REXML::XPath.first(node, 'Boolean')
183
+ when b = node.get_path('Boolean')
184
184
  b.text == '1'
185
185
 
186
- when b = REXML::XPath.first(node, 'Number')
186
+ when b = node.get_path('Number')
187
187
  b.text
188
188
 
189
189
  # ERGO how to do an or in an XPath??
190
190
 
191
- when b = REXML::XPath.first(node, 'String')
191
+ when b = node.get_path('String')
192
192
  b.text
193
193
 
194
- when b = REXML::XPath.first(node, 'Identifier')
194
+ when b = node.get_path('Identifier')
195
195
  b.text # ERGO test me!
196
196
 
197
- when b = REXML::XPath.first(node, 'ArrayLiteral/ElementList')
197
+ when b = node.get_path('ArrayLiteral/ElementList')
198
198
  # ERGO recurse here
199
199
  returning [] do |list|
200
200
  b.each_element('*') do |item|
@@ -241,7 +241,8 @@ module AssertJavaScript
241
241
  params = {}
242
242
 
243
243
  if query
244
- query.split('&').each do |item|
244
+ splitter = using(:libxml?) ? '&' : '&'
245
+ query.split(splitter).each do |item|
245
246
  key, value = item.split('=')
246
247
  params[key.to_sym] = CGI::unescape(value)
247
248
  end
@@ -292,10 +293,10 @@ module AssertJavaScript
292
293
  #
293
294
  def deny_js_replace_html(element_id, matcher = nil, diagnostic = nil)
294
295
  path = object_method_xpath('Element', 'update', element_id)
295
-
296
+
296
297
  # ERGO don't let subsequent updates with the same element_id confuse the matcher!
297
-
298
- if matcher and node = REXML::XPath.first(@xdoc, path)
298
+
299
+ if matcher and node = @xdoc.get_path(path)
299
300
  stash_xdoc do
300
301
  @xdoc = node
301
302
  assert_no_match matcher, assert_js_argument(2), diagnostic
@@ -364,8 +365,11 @@ module AssertJavaScript
364
365
  if (tag_id.class != String and tag_id.class != Regexp) or
365
366
  /#{tag_id}/ =~ assert_js_argument(2)
366
367
  if action
367
- assert_equal action.to_s.gsub('&', '&'),
368
- assert_js_argument(1),
368
+
369
+ # ERGO move those GCI::unescapeHTMLs up the food chain?
370
+
371
+ assert_equal CGI::unescapeHTML(action),
372
+ CGI::unescapeHTML(assert_js_argument(1)),
369
373
  diagnostic
370
374
  end
371
375
  # ERGO a better system to extract GET parameters
@@ -389,7 +393,12 @@ module AssertJavaScript
389
393
  # Not ready for public use!
390
394
  #
391
395
  def assert_js_xml(q) # ERGO explain this beast; hide inside assert_js_*
396
+
397
+ # FIXME test this with an &entity;!
398
+
392
399
  xml = eval('"' + q + '"')
400
+ xml = '<html><body>' + CGI::unescapeHTML(xml) + '</body></html>' if using :libxml?
401
+
393
402
  assert_xml xml # ERGO what are the errors if these fail?
394
403
  end # ERGO test the error recoverer in assert_xml
395
404
 
@@ -462,6 +471,12 @@ module AssertJavaScript
462
471
 
463
472
  def javascript_to_xml(source, diagnostic)
464
473
  source ||= default_js_source(diagnostic)
474
+ source = source.to_s
475
+ source.gsub!('<![CDATA[>', '')
476
+ source.gsub!('<![CDATA[', '')
477
+ source.gsub!('//]]]]>', '')
478
+ source.gsub!(']]>', '') # ERGO fix these things at their source
479
+
465
480
  here = File.dirname(__FILE__)
466
481
  jsToXml_pl = File.join(here, 'jsToXml.pl')
467
482
 
data/lib/assert_xpath.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rexml/document'
2
2
  require 'stringio'
3
+ require 'libxml' # FIXME soften that requirement!
3
4
 
4
5
  RAILS_ENV = ENV.fetch('RAILS_ENV', 'test') unless defined?(RAILS_ENV)
5
6
  AFE = Test::Unit::AssertionFailedError unless defined?(AFE)
@@ -185,7 +186,7 @@ module AssertXPath
185
186
  def rexml? ; false end # becase diverse libraries are a "boundary"
186
187
  def hpricot? ; false end # situation. We can't control their contents!
187
188
  end
188
-
189
+
189
190
  class HpricotHelper < XmlHelper #:nodoc:
190
191
  def hpricot? ; true end
191
192
  def symbol_to_xpath(tag) tag.to_s end
@@ -232,7 +233,7 @@ module AssertXPath
232
233
  # run entire suites in this mode.
233
234
  #
234
235
  def invoke_libxml(favorite_flavor = :html)
235
- @_favorite_flavor = favorite_flavor
236
+ @_favorite_flavor = favorite_flavor
236
237
  @xdoc = nil
237
238
  @helper = LibxmlHelper.new
238
239
  end
@@ -244,12 +245,12 @@ module AssertXPath
244
245
  :xhtml => '<!DOCTYPE html PUBLIC ' +
245
246
  '"-//W3C//DTD XHTML 1.0 Transitional//EN" ' +
246
247
  '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >',
247
- :xml => ''
248
+ :xml => nil
248
249
  }.freeze
249
250
  end
250
251
  private :_doc_type
251
252
 
252
- # FIXME what happens to assert_js_replace_html bearing entities??
253
+ # ERGO what happens to assert_js_replace_html bearing entities??
253
254
 
254
255
  # Subsequent +assert_xml+ calls will use REXML. See
255
256
  # +invoke_hpricot+ to learn the various differences between the
@@ -310,6 +311,8 @@ module AssertXPath
310
311
  contents.gsub!('//<![CDATA[', '')
311
312
  contents.gsub!('//]]>', '')
312
313
  contents.gsub!('//]>', '')
314
+ contents.gsub!('//]]', '')
315
+ contents.gsub!('//]', '')
313
316
 
314
317
  begin
315
318
  @xdoc = REXML::Document.new(contents)
@@ -323,18 +326,38 @@ module AssertXPath
323
326
  return (assert_xpath('/*') rescue nil) if @xdoc
324
327
  end
325
328
 
329
+ # Temporarily sets the validation type to :xml, :html, or :xhtml
330
+ #
331
+ def validate_as(type) # FIXME use or lose this
332
+ @_favorite_flavor, formerly = type, @_favorite_flavor
333
+ yield
334
+ ensure
335
+ @_favorite_flavor = type
336
+ end # ERGO more documentation!
337
+
326
338
  def assert_libxml(*args, &block)
327
339
  xml = args.shift || @xdoc || @response.body
328
- require 'xml/libxml'
329
- xp = XML::Parser.new()
330
340
  xhtml = xml.to_s
331
341
 
342
+ # CONSIDER fix this like at the source??
343
+ xhtml.gsub!('<![CDATA[>', '')
344
+ xhtml.gsub!('<![CDATA[', '')
345
+ xhtml.gsub!('//]]]]>', '')
346
+ xhtml.gsub!(']]>', '')
347
+
332
348
  if xhtml !~ /^\<\!DOCTYPE\b/ and xhtml !~ /\<\?xml\b/
333
- xhtml = _doc_type[@_favorite_flavor || :html] + "\n" + xhtml
349
+ xhtml = _doc_type[@_favorite_flavor || :html] + "\n" + xhtml if _doc_type[@_favorite_flavor]
334
350
  end # ERGO document we pass HTML level into invoker
335
351
 
352
+ if xhtml.index('<?xml version="1" standalone="yes"?>') == 0
353
+ xhtml.gsub!('<?xml version="1" standalone="yes"?>', '')
354
+ xhtml.strip! # ERGO what is libxml's problem with that line???
355
+ end
356
+
336
357
  # # FIXME blog that libxml will fully validate your ass...
337
358
 
359
+ xp = xhtml =~ /\<\!DOCTYPE/ ? XML::HTMLParser.new() : XML::Parser.new()
360
+ xhtml = '<xml/>' unless xhtml.any?
338
361
  xp.string = xhtml
339
362
  # FIXME blog we don't work with libxml-ruby 3.8.4
340
363
  # XML::Parser.default_load_external_dtd = false
@@ -541,6 +564,8 @@ module AssertXPath
541
564
  end
542
565
  end
543
566
 
567
+ # FIXME @helper -> @_helper
568
+
544
569
  # Wraps the common idiom <code>assert_xpath('descendant-or-self::./<em>my_tag</em>[ @id = "<em>my_id</em>" ]')</code>. Depends on +assert_xml+
545
570
  # * +tag+ - an XML node name, such as +div+ or +input+.
546
571
  # If this is a <code>:symbol</code>, we prefix "<code>.//</code>"
@@ -672,7 +697,7 @@ module AssertXPath
672
697
  # ERGO then update documentation of those who use this
673
698
  def symbol_to_xpath(tag)
674
699
  return tag unless tag.class == Symbol
675
- using :libxml? # prop-ulates @helper
700
+ @helper or using :libxml? # prop-ulates @helper
676
701
  return @helper.symbol_to_xpath(tag)
677
702
  end
678
703
 
@@ -859,6 +884,7 @@ module XML
859
884
  end
860
885
  return [find_first(xpath, "x:http://www.w3.org/1999/xhtml")]
861
886
  end
887
+ alias each_element search
862
888
 
863
889
  def text
864
890
  #p text?
@@ -910,10 +936,10 @@ end
910
936
  class REXML::Element
911
937
  include AssertXPath::CommonXPathExtensions
912
938
 
913
- # Semi-private method to match Hpricotic abilities
914
- def search(xpath)
915
- return self.each_element( xpath ){}
916
- end
939
+ # Semi-private method to match Hpricotic abilities
940
+ def search(xpath)
941
+ return self.each_element( xpath ){}
942
+ end
917
943
 
918
944
  def method_missing(*args, &block) #:nodoc:
919
945
  symbol = args.shift
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_xpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phlip
@@ -9,27 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-08 00:00:00 -08:00
12
+ date: 2008-03-10 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rubynode
17
- version_requirement:
18
- version_requirements: !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: "0"
23
- version:
24
- - !ruby/object:Gem::Dependency
25
- name: assert2
26
- version_requirement:
27
- version_requirements: !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: "0"
32
- version:
14
+ dependencies: []
15
+
33
16
  description:
34
17
  email: phlip2005@gmail.com
35
18
  executables: []