File: toml_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 (148 lines) | stat: -rw-r--r-- 4,248 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
require_relative "helper"
require_relative "toml_examples"
require "json"

class TomlTest < Minitest::Test
  def test_file_v_0_4_0
    path = File.join(File.dirname(__FILE__), "example-v0.4.0.toml")
    parsed = TomlRB.load_file(path)
    hash = TomlRB::Examples.example_v_0_4_0

    assert_equal hash["array"], parsed["array"]
    assert_equal hash["boolean"], parsed["boolean"]
    assert_equal hash["datetime"], parsed["datetime"]
    assert_equal hash["float"], parsed["float"]
    assert_equal hash["integer"], parsed["integer"]
    assert_equal hash["string"], parsed["string"]
    assert_equal hash["table"], parsed["table"]
    assert_equal hash["products"], parsed["products"]
    assert_equal hash["fruit"], parsed["fruit"]
  end

  def test_file_v_0_5_0
    path = File.join(File.dirname(__FILE__), "example-v0.5.0.toml")
    parsed = TomlRB.load_file(path)
    hash = TomlRB::Examples.example_v_0_5_0
    assert_equal hash["keys"], parsed["keys"]
    assert_equal hash["string"], parsed["string"]
    assert_equal hash["integer"], parsed["integer"]
    assert_equal hash["float"], parsed["float"]
    assert_equal hash["boolean"], parsed["boolean"]
    assert_equal hash["offset-date-time"], parsed["offset-date-time"]
    assert_equal hash["local-date-time"], parsed["local-date-time"]
    assert_equal hash["local-date"], parsed["local-date"]
    assert_equal hash["local-time"], parsed["local-time"]
    assert_equal hash["array"], parsed["array"]
    assert_equal hash["table"], parsed["table"]
    assert_equal hash["inline-table"], parsed["inline-table"]
    assert_equal hash["array-of-tables"], parsed["array-of-tables"]
  end

  def test_file
    path = File.join(File.dirname(__FILE__), "example.toml")
    parsed = TomlRB.load_file(path)

    assert_equal TomlRB::Examples.example, parsed
  end

  def test_hard_example
    path = File.join(File.dirname(__FILE__), "hard_example.toml")
    parsed = TomlRB.load_file(path)

    assert_equal TomlRB::Examples.hard_example, parsed
  end

  def test_symbolize_keys
    path = File.join(File.dirname(__FILE__), "example.toml")
    parsed = TomlRB.load_file(path, symbolize_keys: true)

    hash = {
      title: "TomlRB Example",

      owner: {
        name: "Tom Preston-Werner",
        organization: "GitHub",
        bio: "GitHub Cofounder & CEO\nLikes tater tots and beer.",
        dob: Time.utc(1979, 0o5, 27, 0o7, 32, 0o0)
      },

      database: {
        server: "192.168.1.1",
        ports: [8001, 8001, 8002],
        connection_max: 5000,
        enabled: true
      },

      servers: {
        alpha: {
          ip: "10.0.0.1",
          dc: "eqdc10"
        },
        beta: {
          ip: "10.0.0.2",
          dc: "eqdc10"
        }
      },

      clients: {
        data: [%w[gamma delta], [1, 2]],
        hosts: %w[alpha omega]
      },

      amqp: {
        exchange: {
          durable: true,
          auto_delete: false
        }
      },

      products: [
        {name: "Hammer", sku: 738_594_937},
        {},
        {name: "Nail", sku: 284_758_393, color: "gray"}
      ]

    }

    assert_equal(hash, parsed)
  end

  def test_line_break
    parsed = TomlRB.parse("hello = 'world'\r\nline_break = true")
    assert_equal({"hello" => "world", "line_break" => true}, parsed)
  end

  def test_trailing_whitespace_after_keyvalue
    parsed = TomlRB.parse("a = 1   ")
    assert_equal({"a" => 1}, parsed)
  end

  def test_trailing_whitespace_after_line_break
    parsed = TomlRB.parse("[table]\r\na = 1\r\n  ")
    assert_equal({"table" => {"a" => 1}}, parsed)
  end

  def test_valid_cases
    Dir["test/examples/valid/**/*.json"].each do |json_file|
      toml_file = File.join(File.dirname(json_file),
        File.basename(json_file, ".json")) + ".toml"
      begin
        toml = TomlRB.load_file(toml_file)
      rescue TomlRB::Error => e
        assert false, "Error: #{e} in #{toml_file}"
      end

      json = JSON.parse(File.read(json_file))

      assert_equal json, toml, "In file '#{toml_file}'"
    end
  end

  def test_invalid_cases
    Dir["test/examples/invalid/**/*.toml"].each do |toml_file|
      assert_raises(TomlRB::Error, "For file #{toml_file}") do
        TomlRB.load_file(toml_file)
      end
    end
  end
end