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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
require_relative "helper"
class GrammarTest < Minitest::Test
def test_comment
match = TomlRB::Document.parse(" # A comment", root: :comment)
assert_nil(match.value)
end
def test_key
match = TomlRB::Document.parse("bad_key-", root: :key)
assert_equal("bad_key-", match.value.first)
match = TomlRB::Document.parse('"123.ʎǝʞ.#?"', root: :key)
assert_equal("123.ʎǝʞ.#?", match.value.first)
end
def test_table
indentation_alternatives_for("[akey]") do |str|
match = TomlRB::Document.parse(str, root: :table)
assert_equal(TomlRB::Table, match.value.class)
assert_equal(["akey"], match.value.instance_variable_get(:@dotted_keys))
end
match = TomlRB::Document.parse("[owner.emancu]", root: :table)
assert_equal(%w[owner emancu],
match.value.instance_variable_get(:@dotted_keys))
match = TomlRB::Document.parse('["owner.emancu"]', root: :table)
assert_equal(%w[owner.emancu],
match.value.instance_variable_get(:@dotted_keys))
match = TomlRB::Document.parse('["first key"."second key"]', root: :table)
assert_equal(["first key", "second key"],
match.value.instance_variable_get(:@dotted_keys))
match = TomlRB::Document.parse("[ owner . emancu ]", root: :table)
assert_equal(%w[owner emancu],
match.value.instance_variable_get(:@dotted_keys))
assert_raises Citrus::ParseError do
TomlRB::Document.parse("[ owner emancu ]", root: :table)
end
end
def test_keyvalue
indentation_alternatives_for('key = "value"') do |str|
match = TomlRB::Document.parse(str, root: :keyvalue)
assert_equal(TomlRB::Keyvalue, match.value.class)
keyvalue = match.value
assert_equal("key", keyvalue.instance_variable_get(:@dotted_keys).first)
assert_equal("value", keyvalue.instance_variable_get(:@value))
end
indentation_alternatives_for('key1."key2".key3 = "value"') do |str|
match = TomlRB::Document.parse(str, root: :keyvalue)
assert_equal(TomlRB::Keyvalue, match.value.class)
keyvalue = match.value
assert_equal("key1", keyvalue.instance_variable_get(:@dotted_keys)[0])
assert_equal("key2", keyvalue.instance_variable_get(:@dotted_keys)[1])
assert_equal("key3", keyvalue.instance_variable_get(:@dotted_keys)[2])
assert_equal("value", keyvalue.instance_variable_get(:@value))
end
end
def test_string
match = TomlRB::Document.parse('"TomlRB-Example, should work."', root: :string)
assert_equal("TomlRB-Example, should work.", match.value)
end
def test_multiline_string
match = TomlRB::Document.parse('"""\tOne\nTwo"""', root: :multiline_string)
assert_equal "\tOne\nTwo", match.value
to_parse = '"""\
One \
Two\
"""'
match = TomlRB::Document.parse(to_parse, root: :multiline_string)
assert_equal "One Two", match.value
end
def test_empty_multiline_string
to_parse = '""""""'
match = TomlRB::Document.parse(to_parse, root: :multiline_string)
assert_equal "", match.value
end
def test_special_characters
match = TomlRB::Document.parse('"\0 \" \t \n \r"', root: :string)
assert_equal("\0 \" \t \n \r", match.value)
match = TomlRB::Document.parse('"C:\\\\Documents\\\\nada.exe"', root: :string)
assert_equal("C:\\Documents\\nada.exe", match.value)
end
def test_bool
match = TomlRB::Document.parse("true", root: :bool)
assert_equal(true, match.value)
match = TomlRB::Document.parse("false", root: :bool)
assert_equal(false, match.value)
end
def test_integer
match = TomlRB::Document.parse("+99", root: :integer)
assert_equal(99, match.value)
match = TomlRB::Document.parse("42", root: :integer)
assert_equal(42, match.value)
match = TomlRB::Document.parse("0", root: :integer)
assert_equal(0, match.value)
match = TomlRB::Document.parse("-17", root: :integer)
assert_equal(-17, match.value)
match = TomlRB::Document.parse("1_000", root: :integer)
assert_equal(1_000, match.value)
match = TomlRB::Document.parse("5_349_221", root: :integer)
assert_equal(5_349_221, match.value)
match = TomlRB::Document.parse("1_2_3_4_5", root: :integer)
assert_equal(1_2_3_4_5, match.value)
match = TomlRB::Document.parse("0xDEADBEEF", root: :integer)
assert_equal(0xDEADBEEF, match.value)
match = TomlRB::Document.parse("0xdeadbeef", root: :integer)
assert_equal(0xdeadbeef, match.value)
match = TomlRB::Document.parse("0xdead_beef", root: :integer)
assert_equal(0xdead_beef, match.value)
match = TomlRB::Document.parse("0o01234567", root: :integer)
assert_equal(0o01234567, match.value)
match = TomlRB::Document.parse("0o755", root: :integer)
assert_equal(0o755, match.value)
match = TomlRB::Document.parse("0b11010110", root: :integer)
assert_equal(0b11010110, match.value)
end
def test_float
match = TomlRB::Document.parse("+1.0", root: :float)
assert_equal(+1.0, match.value)
match = TomlRB::Document.parse("3.1415", root: :float)
assert_equal(3.1415, match.value)
match = TomlRB::Document.parse("-0.01", root: :float)
assert_equal(-0.01, match.value)
match = TomlRB::Document.parse("5e+22", root: :float)
assert_equal(5e+22, match.value)
match = TomlRB::Document.parse("1e6", root: :float)
assert_equal(1e6, match.value)
match = TomlRB::Document.parse("-2E-2", root: :float)
assert_equal(-2E-2, match.value)
match = TomlRB::Document.parse("6.626e-34", root: :float)
assert_equal(6.626e-34, match.value)
match = TomlRB::Document.parse("224_617.445_991_228", root: :float)
assert_equal(224_617.445_991_228, match.value)
match = TomlRB::Document.parse("inf", root: :float)
assert_equal(Float::INFINITY, match.value)
match = TomlRB::Document.parse("+inf", root: :float)
assert_equal(Float::INFINITY, match.value)
match = TomlRB::Document.parse("-inf", root: :float)
assert_equal(-Float::INFINITY, match.value)
match = TomlRB::Document.parse("nan", root: :float)
assert(match.value.nan?)
match = TomlRB::Document.parse("+nan", root: :float)
assert(match.value.nan?)
match = TomlRB::Document.parse("-nan", root: :float)
assert(match.value.nan?)
end
def test_signed_numbers
match = TomlRB::Document.parse("+26", root: :number)
assert_equal(26, match.value)
match = TomlRB::Document.parse("-26", root: :number)
assert_equal(-26, match.value)
match = TomlRB::Document.parse("1.69", root: :number)
assert_equal(1.69, match.value)
match = TomlRB::Document.parse("-1.69", root: :number)
assert_equal(-1.69, match.value)
end
def test_expressions_with_comments
match = TomlRB::Document.parse("[shouldwork] # with comment", root: :table)
assert_equal(["shouldwork"],
match.value.instance_variable_get(:@dotted_keys))
match = TomlRB::Document.parse("works = true # with comment", root: :keyvalue).value
assert_equal("works", match.instance_variable_get(:@dotted_keys).first)
assert_equal(true, match.instance_variable_get(:@value))
end
def test_array
match = TomlRB::Document.parse("[]", root: :array)
assert_equal([], match.value)
match = TomlRB::Document.parse("[ 2, 4]", root: :array)
assert_equal([2, 4], match.value)
match = TomlRB::Document.parse("[ 2.4, 4.72]", root: :array)
assert_equal([2.4, 4.72], match.value)
match = TomlRB::Document.parse('[ "hey", "TomlRB"]', root: :array)
assert_equal(%w[hey TomlRB], match.value)
match = TomlRB::Document.parse('[ ["hey", "TomlRB"], [2,4] ]', root: :array)
assert_equal([%w[hey TomlRB], [2, 4]], match.value)
match = TomlRB::Document.parse("[ { one = 1 }, { two = 2, three = 3} ]",
root: :array)
assert_equal([{"one" => 1}, {"two" => 2, "three" => 3}], match.value)
end
def test_empty_array
# test that [] is parsed as array and not as inline table array
match = TomlRB::Document.parse("a = []", root: :keyvalue).value
assert_equal [], match.value
end
def test_multiline_array
multiline_array = "[ \"hey\",\n \"ho\",\n\t \"lets\", \"go\",\n ]"
match = TomlRB::Document.parse(multiline_array, root: :array)
assert_equal(%w[hey ho lets go], match.value)
multiline_array = "[\n#1,\n2,\n# 3\n]"
match = TomlRB::Document.parse(multiline_array, root: :array)
assert_equal([2], match.value)
multiline_array = "[\n# comment\n#, more comments\n4]"
match = TomlRB::Document.parse(multiline_array, root: :array)
assert_equal([4], match.value)
multiline_array = "[\n 1,\n # 2,\n 3 ,\n]"
match = TomlRB::Document.parse(multiline_array, root: :array)
assert_equal([1, 3], match.value)
multiline_array = "[\n 1 , # useless comment\n # 2,\n 3 #other comment\n]"
match = TomlRB::Document.parse(multiline_array, root: :array)
assert_equal([1, 3], match.value)
end
# Dates are really hard to test from JSON, due the imposibility to represent
# datetimes without quotes.
def test_datetime
match = TomlRB::Document.parse("1979-05-27T07:32:00Z", root: :datetime)
assert_equal(Time.utc(1979, 5, 27, 7, 32, 0), match.value)
match = TomlRB::Document.parse("1979-05-27T00:32:00-07:00", root: :datetime)
assert_equal(Time.new(1979, 5, 27, 0, 32, 0, "-07:00"), match.value)
match = TomlRB::Document.parse("1979-05-27T00:32:00.999999-07:00", root: :datetime)
assert_equal(Time.new(1979, 5, 27, 0, 32, 0.999999, "-07:00"), match.value)
match = TomlRB::Document.parse("1979-05-27 07:32:00Z", root: :datetime)
assert_equal(Time.utc(1979, 5, 27, 7, 32, 0), match.value)
match = TomlRB::Document.parse("1979-05-27T07:32:00", root: :datetime)
assert_equal(Time.local(1979, 5, 27, 7, 32, 0), match.value)
match = TomlRB::Document.parse("1979-05-27T00:32:00.999999", root: :datetime)
assert_equal(Time.local(1979, 5, 27, 0, 32, 0, 999999), match.value)
match = TomlRB::Document.parse("1979-05-27", root: :datetime)
assert_equal(Time.local(1979, 5, 27), match.value)
match = TomlRB::Document.parse("07:32:00", root: :datetime)
assert_equal(Time.at(3600 * 7 + 60 * 32), match.value)
match = TomlRB::Document.parse("00:32:00.999999", root: :datetime)
assert_equal(Time.at(60 * 32, 999999), match.value)
end
def test_inline_table
match = TomlRB::Document.parse("{ }", root: :inline_table)
assert_equal({}, match.value.value)
match = TomlRB::Document.parse("{ simple = true, params = 2 }", root: :inline_table)
assert_equal({"simple" => true, "params" => 2}, match.value.value)
match = TomlRB::Document.parse("{ nest = { really = { hard = true } } }",
root: :inline_table)
assert_equal({"nest" => {"really" => {"hard" => true}}}, match.value.value)
assert_equal({nest: {really: {hard: true}}}, match.value.value(true))
end
private
# Creates all the alternatives of valid indentations to test
def indentation_alternatives_for(str)
[str, " #{str}", "\t#{str}", "\t\t#{str}"].each do |alternative|
yield(alternative)
end
end
end
|