File: base_spec.rb

package info (click to toggle)
ruby-fog-xml 0.1.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: ruby: 246; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,603 bytes parent folder | download | duplicates (3)
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
require "minitest_helper"

describe Fog::Parsers::Base do
  
  def parse(input, parser_class)
    document = parser_class.new
    parser = Nokogiri::XML::SAX::Parser.new(document)
    parser.parse input
    document.response
  end

  describe 'value' do
    class ValueTest < Fog::Parsers::Base

      def start_element(name, attrs)
        @stack.push({})
        super
      end

      def reset
        super
        @stack = [@response]
      end

      def end_element name
        top = @stack.pop
        if top.empty?
          @stack.last[name] = value
        else
          @stack.last[name] = top if @stack.any?
        end
      end

    end

    it 'extracts the characters for the current element' do
      doc = <<-XML
      <Test>
        <Foo>FooValue</Foo>
        <Bar>BarValue</Bar>
      </Test>
      XML
      assert_equal({'Test'=> {'Foo' => 'FooValue', 'Bar' => 'BarValue'}}, parse(doc, ValueTest))
    end
  end

  describe 'attr_value' do
    class AttrParser < Fog::Parsers::Base
      def start_element(name, attrs=[])
        super
        @response[name] = {'foo' => attr_value('foo', attrs)}
      end
    end

    describe 'the attribute exists' do
      it 'returns the value' do
        doc = <<-XML
        <Test foo="bar" />
        XML
        assert_equal({'Test'=> {'foo' => 'bar'}}, parse(doc, AttrParser))
      end
    end

    describe 'the attribute does not exist' do
      it 'returns nil' do
        doc = <<-XML
        <Test notfoo="bar" />
        XML
        assert_equal({'Test'=> {'foo' => nil}}, parse(doc, AttrParser))
      end
    end
  end
end