File: typecast_array_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 (86 lines) | stat: -rw-r--r-- 2,229 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
require "test_helper"

# Tests typecast_array behavior including unwrapping single elements
class TypecastArrayTest < Minitest::Test
  cover "MultiXml*"
  include MultiXml::Helpers

  def test_with_single_element_returns_first
    assert_equal({"key" => "value"}, typecast_array([{"key" => "value"}], []))
  end

  def test_with_multiple_elements_returns_array
    assert_equal [{"a" => 1}, {"b" => 2}], typecast_array([{"a" => 1}, {"b" => 2}], [])
  end

  def test_with_empty_array
    assert_empty typecast_array([], [])
  end

  def test_recursively_typecasts
    assert_equal 42, typecast_array([{"type" => "integer", "__content__" => "42"}], [])
  end

  def test_mutates_original
    input = [{"type" => "integer", "__content__" => "42"}]
    original_first = input.first
    typecast_array(input, [])

    assert_equal 42, input.first
    refute_same original_first, input.first
  end

  def test_one_returns_false_for_empty
    result = typecast_array([], [])

    assert_empty result
  end

  def test_one_returns_true_for_single
    result = typecast_array(["only"], [])

    assert_equal "only", result
  end

  def test_one_returns_false_for_multiple
    result = typecast_array(%w[one two], [])

    assert_equal %w[one two], result
  end

  def test_passes_disallowed_types_to_nested_calls
    input = [{"type" => "yaml", "__content__" => "test: value"}]
    result = typecast_array(input, [])

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

  def test_with_custom_disallowed_types
    input = [{"type" => "integer", "__content__" => "42"}]

    assert_raises(MultiXml::DisallowedTypeError) do
      typecast_array(input, ["integer"])
    end
  end

  def test_preserves_array_with_nil_and_value
    input = [{}, {"__content__" => "value"}]
    result = typecast_array(input, [])

    assert_equal [nil, "value"], result
  end

  def test_preserves_array_with_multiple_nils
    input = [{}, {}]
    result = typecast_array(input, [])

    assert_equal [nil, nil], result
  end

  def test_preserves_array_with_false_and_value
    input = [{"type" => "boolean", "__content__" => "false"}, {"__content__" => "value"}]
    result = typecast_array(input, [])

    assert_equal [false, "value"], result
  end
end