File: convert_hash_test.rb

package info (click to toggle)
ruby-multi-xml 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 472 kB
  • sloc: ruby: 2,822; sh: 4; makefile: 2
file content (107 lines) | stat: -rw-r--r-- 2,803 bytes parent folder | download
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
require "test_helper"

# Tests convert_hash behavior for various type attributes
class ConvertHashTest < Minitest::Test
  cover "MultiXml*"
  include MultiXml::Helpers

  def test_with_array_type
    hash = {"type" => "array", "item" => %w[a b]}
    result = convert_hash(hash, "array", [])

    assert_equal %w[a b], result
  end

  def test_with_text_content
    hash = {"type" => "string", "__content__" => "hello"}
    result = convert_hash(hash, "string", [])

    assert_equal "hello", result
  end

  def test_returns_nil_for_empty_hash
    result = convert_hash({}, nil, [])

    assert_nil result
  end

  def test_typecasts_children_otherwise
    hash = {"child" => {"type" => "integer", "__content__" => "123"}}
    result = convert_hash(hash, nil, [])

    assert_equal({"child" => 123}, result)
  end

  def test_passes_disallowed_types_to_typecast_children
    hash = {"child" => {"type" => "yaml", "__content__" => "test"}}

    assert_raises(MultiXml::DisallowedTypeError) do
      convert_hash(hash, nil, ["yaml"])
    end
  end

  def test_passes_disallowed_types_to_extract_array
    hash = {"type" => "array", "item" => [{"type" => "yaml", "__content__" => "test"}]}

    assert_raises(MultiXml::DisallowedTypeError) do
      convert_hash(hash, "array", ["yaml"])
    end
  end

  def test_with_text_content_key_uses_convert_text_content
    hash = {MultiXml::TEXT_CONTENT_KEY => "42", "type" => "integer"}
    result = convert_hash(hash, "integer", [])

    assert_equal 42, result
  end

  def test_without_text_content_key_falls_through
    hash = {"type" => "string", "nil" => "false"}
    result = convert_hash(hash, "string", [])

    assert_equal "", result
  end

  def test_processes_non_array_non_text_content
    hash = {"child" => "value"}
    result = convert_hash(hash, nil, [])

    assert_equal({"child" => "value"}, result)
  end

  def test_integer_type_returns_nil_not_empty_string
    hash = {"type" => "integer"}
    result = convert_hash(hash, "integer", [])

    assert_nil result
  end

  def test_empty_value_receives_type
    hash = {"type" => "integer"}
    result = convert_hash(hash, "integer", [])

    assert_nil result
  end

  def test_empty_value_type_matters
    hash = {"key" => "value"}
    result_with_nil_type = convert_hash(hash, nil, [])

    assert_equal({"key" => "value"}, result_with_nil_type)
  end

  def test_calls_typecast_children_last
    hash = {"name" => {"type" => "integer", "__content__" => "123"}}
    result = convert_hash(hash, nil, [])

    assert_equal({"name" => 123}, result)
  end

  def test_passes_disallowed_types_through_all_paths
    hash = {"nested" => {"type" => "symbol", "__content__" => "test"}}

    assert_raises(MultiXml::DisallowedTypeError) do
      convert_hash(hash, nil, ["symbol"])
    end
  end
end