toml-rb 1.1.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,244 +0,0 @@
1
- ################################################################################
2
- ## Comment
3
-
4
- # Speak your mind with the hash symbol. They go from the symbol to the end of
5
- # the line.
6
-
7
-
8
- ################################################################################
9
- ## Table
10
-
11
- # Tables (also known as hash tables or dictionaries) are collections of
12
- # key/value pairs. They appear in square brackets on a line by themselves.
13
-
14
- [table]
15
-
16
- key = "value" # Yeah, you can do this.
17
-
18
- # Nested tables are denoted by table names with dots in them. Name your tables
19
- # whatever crap you please, just don't use #, ., [ or ].
20
-
21
- [table.subtable]
22
-
23
- key = "another value"
24
-
25
- # You don't need to specify all the super-tables if you don't want to. TomlRB
26
- # knows how to do it for you.
27
-
28
- # [x] you
29
- # [x.y] don't
30
- # [x.y.z] need these
31
- [x.y.z.w] # for this to work
32
-
33
-
34
- ################################################################################
35
- ## Inline Table
36
-
37
- # Inline tables provide a more compact syntax for expressing tables. They are
38
- # especially useful for grouped data that can otherwise quickly become verbose.
39
- # Inline tables are enclosed in curly braces `{` and `}`. No newlines are
40
- # allowed between the curly braces unless they are valid within a value.
41
-
42
- [table.inline]
43
-
44
- name = { first = "Tom", last = "Preston-Werner" }
45
- point = { x = 1, y = 2 }
46
-
47
-
48
- ################################################################################
49
- ## String
50
-
51
- # There are four ways to express strings: basic, multi-line basic, literal, and
52
- # multi-line literal. All strings must contain only valid UTF-8 characters.
53
-
54
- [string.basic]
55
-
56
- basic = "I'm a string. \"You can quote me\". Name\tJos\u00E9\nLocation\tSF."
57
-
58
- [string.multiline]
59
-
60
- # The following strings are byte-for-byte equivalent:
61
- key1 = "One\nTwo"
62
- key2 = """One\nTwo"""
63
- key3 = """
64
- One
65
- Two"""
66
-
67
- [string.multiline.continued]
68
-
69
- # The following strings are byte-for-byte equivalent:
70
- key1 = "The quick brown fox jumps over the lazy dog."
71
-
72
- key2 = """
73
- The quick brown \
74
-
75
-
76
- fox jumps over \
77
- the lazy dog."""
78
-
79
- key3 = """\
80
- The quick brown \
81
- fox jumps over \
82
- the lazy dog.\
83
- """
84
-
85
- [string.literal]
86
-
87
- # What you see is what you get.
88
- winpath = 'C:\Users\nodejs\templates'
89
- winpath2 = '\\ServerX\admin$\system32\'
90
- quoted = 'Tom "Dubs" Preston-Werner'
91
- regex = '<\i\c*\s*>'
92
-
93
-
94
- [string.literal.multiline]
95
-
96
- regex2 = '''I [dw]on't need \d{2} apples'''
97
- lines = '''
98
- The first newline is
99
- trimmed in raw strings.
100
- All other whitespace
101
- is preserved.
102
- '''
103
-
104
-
105
- ################################################################################
106
- ## Integer
107
-
108
- # Integers are whole numbers. Positive numbers may be prefixed with a plus sign.
109
- # Negative numbers are prefixed with a minus sign.
110
-
111
- [integer]
112
-
113
- key1 = +99
114
- key2 = 42
115
- key3 = 0
116
- key4 = -17
117
-
118
- [integer.underscores]
119
-
120
- # For large numbers, you may use underscores to enhance readability. Each
121
- # underscore must be surrounded by at least one digit.
122
- key1 = 1_000
123
- key2 = 5_349_221
124
- key3 = 1_2_3_4_5 # valid but inadvisable
125
-
126
-
127
- ################################################################################
128
- ## Float
129
-
130
- # A float consists of an integer part (which may be prefixed with a plus or
131
- # minus sign) followed by a fractional part and/or an exponent part.
132
-
133
- [float.fractional]
134
-
135
- key1 = +1.0
136
- key2 = 3.1415
137
- key3 = -0.01
138
-
139
- [float.exponent]
140
-
141
- key1 = 5e+22
142
- key2 = 1e6
143
- key3 = -2E-2
144
-
145
- [float.both]
146
-
147
- key = 6.626e-34
148
-
149
- [float.underscores]
150
-
151
- key1 = 9_224_617.445_991_228_313
152
- key2 = 1e1_000
153
-
154
-
155
- ################################################################################
156
- ## Boolean
157
-
158
- # Booleans are just the tokens you're used to. Always lowercase.
159
-
160
- [boolean]
161
-
162
- True = true
163
- False = false
164
-
165
-
166
- ################################################################################
167
- ## Datetime
168
-
169
- # Datetimes are RFC 3339 dates.
170
-
171
- [datetime]
172
-
173
- key1 = 1979-05-27T07:32:00Z
174
- key2 = 1979-05-27T00:32:00-07:00
175
- key3 = 1979-05-27T00:32:00.999999-07:00
176
-
177
-
178
- ################################################################################
179
- ## Array
180
-
181
- # Arrays are square brackets with other primitives inside. Whitespace is
182
- # ignored. Elements are separated by commas. Data types may not be mixed.
183
-
184
- [array]
185
-
186
- key1 = [ 1, 2, 3 ]
187
- key2 = [ "red", "yellow", "green" ]
188
- key3 = [ [ 1, 2 ], [3, 4, 5] ]
189
- key4 = [ [ 1, 2 ], ["a", "b", "c"] ] # this is ok
190
-
191
- # Arrays can also be multiline. So in addition to ignoring whitespace, arrays
192
- # also ignore newlines between the brackets. Terminating commas are ok before
193
- # the closing bracket.
194
-
195
- key5 = [
196
- 1, 2, 3
197
- ]
198
- key6 = [
199
- 1,
200
- 2, # this is ok
201
- ]
202
-
203
-
204
- ################################################################################
205
- ## Array of Tables
206
-
207
- # These can be expressed by using a table name in double brackets. Each table
208
- # with the same double bracketed name will be an element in the array. The
209
- # tables are inserted in the order encountered.
210
-
211
- [[products]]
212
-
213
- name = "Hammer"
214
- sku = 738594937
215
-
216
- [[products]]
217
-
218
- [[products]]
219
-
220
- name = "Nail"
221
- sku = 284758393
222
- color = "gray"
223
-
224
-
225
- # You can create nested arrays of tables as well.
226
-
227
- [[fruit]]
228
- name = "apple"
229
-
230
- [fruit.physical]
231
- color = "red"
232
- shape = "round"
233
-
234
- [[fruit.variety]]
235
- name = "red delicious"
236
-
237
- [[fruit.variety]]
238
- name = "granny smith"
239
-
240
- [[fruit]]
241
- name = "banana"
242
-
243
- [[fruit.variety]]
244
- name = "plantain"
@@ -1,49 +0,0 @@
1
- # This is a TomlRB document. Boom.
2
-
3
- title = "TomlRB Example"
4
-
5
- [owner]
6
- name = "Tom Preston-Werner"
7
- organization = "GitHub"
8
- bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
9
- dob = 1979-05-27T07:32:00Z # First class dates? Why not?
10
-
11
- [database]
12
- server = "192.168.1.1"
13
- ports = [ 8001, 8001, 8002 ]
14
- connection_max = 5000
15
- enabled = true
16
-
17
- [servers]
18
-
19
- # You can indent as you please. Tabs or spaces. TomlRB don't care.
20
- [servers.alpha]
21
- ip = "10.0.0.1"
22
- dc = "eqdc10"
23
-
24
- [servers.beta]
25
- ip = "10.0.0.2"
26
- dc = "eqdc10"
27
-
28
- [clients]
29
- data = [ ["gamma", "delta"], [1, 2] ]
30
-
31
- # Line breaks are OK when inside arrays
32
- hosts = [
33
- "alpha",
34
- "omega"
35
- ]
36
-
37
- [amqp]
38
- exchange = {durable = true, auto_delete = false}
39
-
40
- [[products]]
41
- name = "Hammer"
42
- sku = 738594937
43
-
44
- [[products]]
45
-
46
- [[products]]
47
- name = "Nail"
48
- sku = 284758393
49
- color = "gray"
@@ -1,239 +0,0 @@
1
- # encoding: utf-8
2
- require_relative 'helper'
3
-
4
- class GrammarTest < Minitest::Test
5
- def test_comment
6
- match = TomlRB::Document.parse(' # A comment', root: :comment)
7
- assert_nil(match.value)
8
- end
9
-
10
- def test_key
11
- match = TomlRB::Document.parse('bad_key-', root: :key)
12
- assert_equal('bad_key-', match.value)
13
-
14
- match = TomlRB::Document.parse('"123.ʎǝʞ.#?"', root: :key)
15
- assert_equal('123.ʎǝʞ.#?', match.value)
16
- end
17
-
18
- def test_keygroup
19
- indentation_alternatives_for('[akey]') do |str|
20
- match = TomlRB::Document.parse(str, root: :keygroup)
21
- assert_equal(TomlRB::Keygroup, match.value.class)
22
- assert_equal(['akey'], match.value.instance_variable_get('@nested_keys'))
23
- end
24
-
25
- match = TomlRB::Document.parse('[owner.emancu]', root: :keygroup)
26
- assert_equal(%w(owner emancu),
27
- match.value.instance_variable_get('@nested_keys'))
28
-
29
- match = TomlRB::Document.parse('["owner.emancu"]', root: :keygroup)
30
- assert_equal(%w(owner.emancu),
31
- match.value.instance_variable_get('@nested_keys'))
32
-
33
- match = TomlRB::Document.parse('["first key"."second key"]', root: :keygroup)
34
- assert_equal(['first key', 'second key'],
35
- match.value.instance_variable_get('@nested_keys'))
36
-
37
- match = TomlRB::Document.parse('[ owner . emancu ]', root: :keygroup)
38
- assert_equal(%w(owner emancu),
39
- match.value.instance_variable_get('@nested_keys'))
40
-
41
- assert_raises Citrus::ParseError do
42
- TomlRB::Document.parse('[ owner emancu ]', root: :keygroup)
43
- end
44
- end
45
-
46
- def test_keyvalue
47
- indentation_alternatives_for('key = "value"') do |str|
48
- match = TomlRB::Document.parse(str, root: :keyvalue)
49
- assert_equal(TomlRB::Keyvalue, match.value.class)
50
-
51
- keyvalue = match.value
52
- assert_equal('key', keyvalue.instance_variable_get('@key'))
53
- assert_equal('value', keyvalue.instance_variable_get('@value'))
54
- end
55
- end
56
-
57
- def test_string
58
- match = TomlRB::Document.parse('"TomlRB-Example, should work."', root: :string)
59
- assert_equal('TomlRB-Example, should work.', match.value)
60
- end
61
-
62
- def test_multiline_string
63
- match = TomlRB::Document.parse('"""\tOne\nTwo"""', root: :multiline_string)
64
- assert_equal "\tOne\nTwo", match.value
65
-
66
- to_parse = '"""\
67
- One \
68
- Two\
69
- """'
70
-
71
- match = TomlRB::Document.parse(to_parse, root: :multiline_string)
72
- assert_equal "One Two", match.value
73
- end
74
-
75
- def test_empty_multiline_string
76
- to_parse = '""""""'
77
-
78
- match = TomlRB::Document.parse(to_parse, root: :multiline_string)
79
- assert_equal '', match.value
80
- end
81
-
82
- def test_special_characters
83
- match = TomlRB::Document.parse('"\0 \" \t \n \r"', root: :string)
84
- assert_equal("\0 \" \t \n \r", match.value)
85
-
86
- match = TomlRB::Document.parse('"C:\\\\Documents\\\\nada.exe"', root: :string)
87
- assert_equal('C:\\Documents\\nada.exe', match.value)
88
- end
89
-
90
- def test_bool
91
- match = TomlRB::Document.parse('true', root: :bool)
92
- assert_equal(true, match.value)
93
-
94
- match = TomlRB::Document.parse('false', root: :bool)
95
- assert_equal(false, match.value)
96
- end
97
-
98
- def test_integer
99
- match = TomlRB::Document.parse('26', root: :number)
100
- assert_equal(26, match.value)
101
-
102
- match = TomlRB::Document.parse('1_200_000_999', root: :number)
103
- assert_equal(1_200_000_999, match.value)
104
- end
105
-
106
- def test_float
107
- match = TomlRB::Document.parse('1.69', root: :number)
108
- assert_equal(1.69, match.value)
109
-
110
- match = TomlRB::Document.parse('1_000.69', root: :number)
111
- assert_equal(1000.69, match.value)
112
-
113
- match = TomlRB::Document.parse('1e6', root: :number)
114
- assert_equal(1e6, match.value)
115
-
116
- match = TomlRB::Document.parse('1.02e-46', root: :number)
117
- assert_equal(1.02e-46, match.value)
118
-
119
- match = TomlRB::Document.parse('+1e4_000_000', root: :number)
120
- assert_equal(1e4_000_000, match.value)
121
- end
122
-
123
- def test_signed_numbers
124
- match = TomlRB::Document.parse('+26', root: :number)
125
- assert_equal(26, match.value)
126
-
127
- match = TomlRB::Document.parse('-26', root: :number)
128
- assert_equal(-26, match.value)
129
-
130
- match = TomlRB::Document.parse('1.69', root: :number)
131
- assert_equal(1.69, match.value)
132
-
133
- match = TomlRB::Document.parse('-1.69', root: :number)
134
- assert_equal(-1.69, match.value)
135
- end
136
-
137
- def test_expressions_with_comments
138
- match = TomlRB::Document.parse('[shouldwork] # with comment', root: :keygroup)
139
- assert_equal(['shouldwork'],
140
- match.value.instance_variable_get('@nested_keys'))
141
-
142
- match = TomlRB::Document.parse('works = true # with comment', root: :keyvalue).value
143
- assert_equal('works', match.instance_variable_get('@key'))
144
- assert_equal(true, match.instance_variable_get('@value'))
145
- end
146
-
147
- def test_array
148
- match = TomlRB::Document.parse('[]', root: :array)
149
- assert_equal([], match.value)
150
-
151
- match = TomlRB::Document.parse('[ 2, 4]', root: :array)
152
- assert_equal([2, 4], match.value)
153
-
154
- match = TomlRB::Document.parse('[ 2.4, 4.72]', root: :array)
155
- assert_equal([2.4, 4.72], match.value)
156
-
157
- match = TomlRB::Document.parse('[ "hey", "TomlRB"]', root: :array)
158
- assert_equal(%w(hey TomlRB), match.value)
159
-
160
- match = TomlRB::Document.parse('[ ["hey", "TomlRB"], [2,4] ]', root: :array)
161
- assert_equal([%w(hey TomlRB), [2, 4]], match.value)
162
-
163
- match = TomlRB::Document.parse('[ { one = 1 }, { two = 2, three = 3} ]',
164
- root: :inline_table_array)
165
- assert_equal([{ 'one' => 1 }, { 'two' => 2, 'three' => 3 }], match.value)
166
- end
167
-
168
- def test_empty_array
169
- # test that [] is parsed as array and not as inline table array
170
- match = TomlRB::Document.parse("a = []", root: :keyvalue).value
171
- assert_equal [], match.value
172
- end
173
-
174
- def test_multiline_array
175
- multiline_array = "[ \"hey\",\n \"ho\",\n\t \"lets\", \"go\",\n ]"
176
- match = TomlRB::Document.parse(multiline_array, root: :array)
177
- assert_equal(%w(hey ho lets go), match.value)
178
-
179
- multiline_array = "[\n#1,\n2,\n# 3\n]"
180
- match = TomlRB::Document.parse(multiline_array, root: :array)
181
- assert_equal([2], match.value)
182
-
183
- multiline_array = "[\n# comment\n#, more comments\n4]"
184
- match = TomlRB::Document.parse(multiline_array, root: :array)
185
- assert_equal([4], match.value)
186
-
187
- multiline_array = "[\n 1,\n # 2,\n 3 ,\n]"
188
- match = TomlRB::Document.parse(multiline_array, root: :array)
189
- assert_equal([1, 3], match.value)
190
-
191
- multiline_array = "[\n 1 , # useless comment\n # 2,\n 3 #other comment\n]"
192
- match = TomlRB::Document.parse(multiline_array, root: :array)
193
- assert_equal([1, 3], match.value)
194
- end
195
-
196
- # Dates are really hard to test from JSON, due the imposibility to represent
197
- # datetimes without quotes.
198
- def test_datetime
199
- match = TomlRB::Document.parse('1986-08-28T15:15:00Z', root: :datetime)
200
- assert_equal(Time.utc(1986, 8, 28, 15, 15), match.value)
201
-
202
- match = TomlRB::Document.parse('1986-08-28T15:15:00-03:00', root: :datetime)
203
- assert_equal(Time.utc(1986, 8, 28, 18, 15), match.value)
204
-
205
- match = TomlRB::Document.parse('1986-08-28T15:15:00.123-03:00', root: :datetime)
206
- assert_equal(Time.utc(1986, 8, 28, 18, 15, 0.123), match.value)
207
-
208
- match = TomlRB::Document.parse('1986-08-28', root: :datetime)
209
- assert_equal(Time.utc(1986, 8, 28, 0, 0, 0), match.value)
210
-
211
- match = TomlRB::Document.parse('1986-08-28T15:15:00', root: :datetime)
212
- assert_equal(Time.utc(1986, 8, 28, 15, 15), match.value)
213
-
214
- match = TomlRB::Document.parse('1986-08-28T15:15:00.999999', root: :datetime)
215
- assert_equal(Time.utc(1986, 8, 28, 15, 15, 0.999999), match.value)
216
- end
217
-
218
- def test_inline_table
219
- match = TomlRB::Document.parse('{ }', root: :inline_table)
220
- assert_equal({}, match.value.value)
221
-
222
- match = TomlRB::Document.parse('{ simple = true, params = 2 }', root: :inline_table)
223
- assert_equal({ 'simple' => true, 'params' => 2 }, match.value.value)
224
-
225
- match = TomlRB::Document.parse('{ nest = { really = { hard = true } } }',
226
- root: :inline_table)
227
- assert_equal({ 'nest' => { 'really' => { 'hard' => true } } }, match.value.value)
228
- assert_equal({ nest: { really: { hard: true } } }, match.value.value(true))
229
- end
230
-
231
- private
232
-
233
- # Creates all the alternatives of valid indentations to test
234
- def indentation_alternatives_for(str)
235
- [str, " #{str}", "\t#{str}", "\t\t#{str}"].each do |alternative|
236
- yield(alternative)
237
- end
238
- end
239
- end