File: dumper_test.rb

package info (click to toggle)
ruby-toml-rb 4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,840 kB
  • sloc: ruby: 1,350; makefile: 6
file content (149 lines) | stat: -rw-r--r-- 4,119 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require_relative "helper"
require "date"

class DumperTest < Minitest::Test
  def test_dump_empty
    dumped = TomlRB.dump({})
    assert_equal("", dumped)
  end

  def test_dump_types
    dumped = TomlRB.dump(string: 'TomlRB "dump"')
    assert_equal("string = \"TomlRB \\\"dump\\\"\"\n", dumped)

    dumped = TomlRB.dump(float: -13.24)
    assert_equal("float = -13.24\n", dumped)

    dumped = TomlRB.dump(int: 1234)
    assert_equal("int = 1234\n", dumped)

    dumped = TomlRB.dump(truthy: true)
    assert_equal("truthy = true\n", dumped)

    dumped = TomlRB.dump(falsey: false)
    assert_equal("falsey = false\n", dumped)

    dumped = TomlRB.dump(array: [1, 2, 3])
    assert_equal("array = [1, 2, 3]\n", dumped)

    dumped = TomlRB.dump(array: [[1, 2], %w[weird one]])
    assert_equal("array = [[1, 2], [\"weird\", \"one\"]]\n", dumped)

    dumped = TomlRB.dump(array: %w[#$ #@ #{}])
    assert_equal("array = [\"\#$\", \"\#@\", \"\#{}\"]\n", dumped)

    dumped = TomlRB.dump(time: Time.utc(1986, 8, 28, 15, 15))
    assert_equal("time = 1986-08-28T15:15:00Z\n", dumped)

    dumped = TomlRB.dump(datetime: DateTime.new(1986, 8, 28, 15, 15))
    assert_equal("datetime = 1986-08-28T15:15:00Z\n", dumped)

    dumped = TomlRB.dump(date: Date.new(1986, 8, 28))
    assert_equal("date = 1986-08-28\n", dumped)

    dumped = TomlRB.dump(regexp: /abc\n*\{/)
    assert_equal("regexp = \"/abc\\\\n*\\\\{/\"\n", dumped)
  end

  def test_dump_nested_attributes
    hash = {nested: {hash: {deep: true}}}
    dumped = TomlRB.dump(hash)
    assert_equal("[nested.hash]\ndeep = true\n", dumped)

    hash[:nested][:other] = 12
    dumped = TomlRB.dump(hash)
    assert_equal("[nested]\nother = 12\n[nested.hash]\ndeep = true\n", dumped)

    hash[:nested][:nest] = {again: "it never ends"}
    dumped = TomlRB.dump(hash)
    toml = <<-EOS.gsub(/^ {6}/, "")
      [nested]
      other = 12
      [nested.hash]
      deep = true
      [nested.nest]
      again = "it never ends"
    EOS

    assert_equal(toml, dumped)

    hash = {non: {'bare."keys"' => {"works" => true}}}
    dumped = TomlRB.dump(hash)
    assert_equal("[non.\"bare.\\\"keys\\\"\"]\nworks = true\n", dumped)

    hash = {hola: [{chau: 4}, {chau: 3}]}
    dumped = TomlRB.dump(hash)
    assert_equal("[[hola]]\nchau = 4\n[[hola]]\nchau = 3\n", dumped)
  end

  def test_print_empty_tables
    hash = {plugins: {cpu: {foo: "bar", baz: 1234}, disk: {}, io: {}}}
    dumped = TomlRB.dump(hash)
    toml = <<-EOS.gsub(/^ {6}/, "")
      [plugins.cpu]
      baz = 1234
      foo = "bar"
      [plugins.disk]
      [plugins.io]
    EOS

    assert_equal toml, dumped
  end

  def test_dump_array_tables
    hash = {fruit: [{physical: {color: "red"}}, {physical: {color: "blue"}}]}
    dumped = TomlRB.dump(hash)
    toml = <<-EOS.gsub(/^ {6}/, "")
      [[fruit]]
      [fruit.physical]
      color = "red"
      [[fruit]]
      [fruit.physical]
      color = "blue"
    EOS

    assert_equal toml, dumped
  end

  def test_dump_interpolation_curly
    hash = {"key" => "includes \#{variable}"}
    dumped = TomlRB.dump(hash)

    assert_equal %(key = "includes \#{variable}") + "\n", dumped
  end

  def test_dump_interpolation_at
    hash = {"key" => 'includes #@variable'}
    dumped = TomlRB.dump(hash)

    assert_equal 'key = "includes #@variable"' + "\n", dumped
  end

  def test_dump_interpolation_dollar
    hash = {"key" => 'includes #$variable'}
    dumped = TomlRB.dump(hash)

    assert_equal 'key = "includes #$variable"' + "\n", dumped
  end

  def test_dump_special_chars_in_literals
    hash = {'\t' => "escape special chars in string literals"}
    dumped = TomlRB.dump(hash)

    assert_equal %("\\\\t" = "escape special chars in string literals") + "\n", dumped
  end

  def test_dump_special_chars_in_strings
    hash = {"\t" => "escape special chars in strings"}
    dumped = TomlRB.dump(hash)

    assert_equal %("\\t" = "escape special chars in strings") + "\n", dumped
  end

  def test_dump_empty_key
    hash = {"" => "empty key"}
    dumped = TomlRB.dump(hash)

    assert_equal %("" = "empty key") + "\n", dumped
  end
end