File: xml_block_test.rb

package info (click to toggle)
ruby-roxml 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 800 kB
  • sloc: ruby: 4,133; xml: 1,013; makefile: 7
file content (82 lines) | stat: -rw-r--r-- 1,943 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
74
75
76
77
78
79
80
81
82
require_relative './../test_helper'
require 'minitest/autorun'

class ArrayWithBlockShorthand
  include ROXML

  xml_reader :array, :as => [Integer], :from => 'number'
end

class ArrayWithBlock
  include ROXML

  xml_reader :array, :as => [], :from => 'number' do |arr|
    arr.map(&:to_i).reverse
  end
end

class TestXMLBlocks < Minitest::Test
  def test_block_is_applied
    muffins = Muffins.from_xml(fixture(:muffins))

    assert muffins.count > 0
    assert_equal 0, muffins.count % 13
  end

  def test_block_is_applied_to_hash
    numerology = Numerology.from_xml(fixture(:numerology))

    assert !numerology.predictions.keys.empty?
    assert numerology.predictions.keys.all? {|k| k.is_a? Integer }
    assert numerology.predictions.values.all? {|k| k.is_a? String }
  end

  def test_stacked_blocks_are_applied
    muffins = MuffinsWithStackedBlocks.from_xml(fixture(:muffins))

    assert muffins.count > 0
    assert_equal 0, muffins.count % 13
  end

  def test_block_shorthand_applied_properly_to_array
    obj = ArrayWithBlockShorthand.from_xml(%{
      <array_with_block_shorthand>
        <number>1</number>
        <number>2</number>
        <number>3</number>
      </array_with_block_shorthand>
    })

    assert_equal [1, 2, 3], obj.array
  end

  def test_block_applied_properly_to_array
    obj = ArrayWithBlock.from_xml(%{
      <array_with_block>
        <number>1</number>
        <number>2</number>
        <number>3</number>
      </array_with_block>
    })

    assert_equal [3, 2, 1], obj.array
  end

  def test_block_shorthand_applied_properly_to_empty_array
    obj = ArrayWithBlockShorthand.from_xml(%{
      <array_with_block_shorthand>
      </array_with_block_shorthand>
    })

    assert_equal [], obj.array
  end

  def test_block_applied_properly_to_empty_array
    obj = ArrayWithBlock.from_xml(%{
      <array_with_block>
      </array_with_block>
    })

    assert_equal [], obj.array
  end
end