summaryrefslogtreecommitdiff
path: root/test/rexml/test_jaxen.rb
blob: ab66139cb092ced2c60f11569bf8fb216e7f9893 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
require_relative 'rexml_test_utils'

require "rexml/document"
require "rexml/xpath"

# Harness to test REXML's capabilities against the test suite from Jaxen
# ryan.a.cox@gmail.com

module REXMLTests
  class JaxenTester < Test::Unit::TestCase
    include REXMLTestUtils
    include REXML

    def test_axis ; test("axis") ; end
    def test_basic ; test("basic") ; end
    def test_basicupdate ; test("basicupdate") ; end
    def test_contents ; test("contents") ; end
    def test_defaultNamespace ; test("defaultNamespace") ; end
    def test_fibo ; test("fibo") ; end
    def test_id ; test("id") ; end
    def test_jaxen24 ; test("jaxen24") ; end
    def test_lang ; test("lang") ; end
    def test_message ; test("message") ; end
    def test_moreover ; test("moreover") ; end
    def test_much_ado ; test("much_ado") ; end
    def test_namespaces ; test("namespaces") ; end
    def test_nitf ; test("nitf") ; end
    def test_numbers ; test("numbers") ; end
    def test_pi ; test("pi") ; end
    def test_pi2 ; test("pi2") ; end
    def test_simple ; test("simple") ; end
    def test_testNamespaces ; test("testNamespaces") ; end
    def test_text ; test("text") ; end
    def test_underscore ; test("underscore") ; end
    def test_web ; test("web") ; end
    def test_web2 ; test("web2") ; end

    private
    def test( fname )
#      Dir.entries( xml_dir ).each { |fname|
#        if fname =~ /\.xml$/
          file = File.new(fixture_path(fname+".xml"))
          doc = Document.new( file )
          XPath.each( doc, "/tests/document" ) {|e| handleDocument(e)}
#        end
#      }
    end

    # processes a tests/document/context node
    def handleContext( testDoc, ctxElement)
      testCtx = XPath.match( testDoc, ctxElement.attributes["select"] )[0]
      namespaces = {}
      if testCtx.class == Element
        testCtx.prefixes.each { |pre| handleNamespace( testCtx, pre, namespaces ) }
      end
      variables = {}
      XPath.each( ctxElement, "@*[namespace-uri() = 'http://jaxen.org/test-harness/var']") { |attrib| handleVariable(testCtx, variables, attrib) }
      XPath.each( ctxElement, "valueOf") { |e| handleValueOf(testCtx, variables, namespaces, e) }
      XPath.each( ctxElement, "test[not(@exception) or (@exception != 'true') ]") { |e| handleNominalTest(testCtx,variables, namespaces, e) }
      XPath.each( ctxElement, "test[@exception = 'true']") { |e| handleExceptionalTest(testCtx,variables, namespaces, e) }
    end

    # processes a tests/document/context/valueOf or tests/document/context/test/valueOf node
    def handleValueOf(ctx,variables, namespaces, valueOfElement)
      expected = valueOfElement.text
      got = XPath.match( ctx, valueOfElement.attributes["select"], namespaces, variables )[0]
      assert_true( (got.nil? && expected.nil?) || !got.nil? )
      case got.class
      when Element
        assert_equal( got.class, Element )
      when Attribute, Text, Comment, TrueClass, FalseClass
        assert_equal( expected, got.to_s )
      when Instruction
        assert_equal( expected, got.content )
      when Fixnum
        assert_equal( exected.to_f, got )
      when String
        # normalize values for comparison
        got = "" if got == nil or got == ""
        expected = "" if expected == nil or expected == ""
        assert_equal( expected, got )
      else
        assert_fail( "Wassup?" )
      end
    end


    # processes a tests/document/context/test node ( where @exception is false or doesn't exist )
    def handleNominalTest(ctx, variables, namespaces, testElement)
      expected = testElement.attributes["count"]
      got = XPath.match( ctx, testElement.attributes["select"], namespaces, variables )
      # might be a test with no count attribute, but nested valueOf elements
      assert( expected == got.size.to_s ) if !expected.nil?

      XPath.each( testElement, "valueOf") { |e|
        handleValueOf(got, variables, namespaces, e)
      }
    end

    # processes a tests/document/context/test node ( where @exception is true )
    def handleExceptionalTest(ctx, variables, namespaces, testElement)
      assert_raise( Exception ) {
        XPath.match( ctx, testElement.attributes["select"], namespaces, variables )
      }
    end

    # processes a tests/document node
    def handleDocument(docElement)
      puts "- Processing document: #{docElement.attributes['url']}"
      testFile = File.new( docElement.attributes["url"] )
      testDoc = Document.new testFile
      XPath.each( docElement, "context") { |e| handleContext(testDoc, e) }
    end

    # processes a variable definition in a namespace like <test var:foo="bar">
    def handleVariable( ctx, variables, attrib )
      puts "--- Found attribute: #{attrib.name}"
      variables[attrib.name] = attrib.value
    end

    # processes a namespace definition like <test xmlns:foo="fiz:bang:bam">
    def handleNamespace( ctx, prefix, namespaces )
      puts "--- Found namespace: #{prefix}"
      namespaces[prefix] = ctx.namespaces[prefix]
    end

  end
end