Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256import Foundationimport Testing
@testable import ParcelConfig
@Suite struct TOMLParserTests { @Test(arguments: [ ("0", Int64(0)), ("+0", 0), ("-0", 0), ("42", 42), ("+42", 42), ("-17", -17), ("1_000_000", 1_000_000), ("0xDEAD_BEEF", 3_735_928_559), ("0o755", 493), ("0b1101_0010", 210), ("0x0", 0), ("0o0", 0), ("0b0", 0), ("9223372036854775807", Int64.max), ("-9223372036854775808", Int64.min), ("0x7fffffffffffffff", Int64.max), ]) func integers(input: String, expected: Int64) throws { #expect(try scalar(input) == .integer(expected)) }
@Test(arguments: [ ("0.0", 0.0), ("+1.0", 1.0), ("-0.5", -0.5), ("1e3", 1000), ("1E+3", 1000), ("1e-3", 0.001), ("3.14_15", 3.1415), ("1_000.5_0e+0_2", 100050), ("-2E2", -200), ]) func floats(input: String, expected: Double) throws { #expect(try scalar(input) == .float(expected)) }
@Test(arguments: ["inf", "+inf", "-inf", "nan", "+nan", "-nan"]) func specialFloats(input: String) throws { guard case .float(let value) = try scalar(input) else { Issue.record("Expected a float") return } if input.hasSuffix("nan") { #expect(value.isNaN) } else { #expect(value.isInfinite) #expect(value.sign == (input.hasPrefix("-") ? .minus : .plus)) } }
@Test(arguments: [ (#""hello""#, "hello"), (#"'hello'"#, "hello"), (#""""#, ""), ("''", ""), (#""\b\t\n\f\r\"\\""#, "\u{08}\t\n\u{0C}\r\"\\"), (#""\u0041\U0001F600""#, "A😀"), (#"'C:\Users\test'"#, "C:\\Users\\test"), (#""café 🪟""#, "café 🪟"), (#""""hello""""#, "hello"), ("'''\nhello\nworld'''", "hello\nworld"), ("\"\"\"\nhello\nworld\"\"\"", "hello\nworld"), ("\"\"\"\r\nhello\r\nworld\"\"\"", "hello\nworld"), ("'''\r\nhello\r\nworld'''", "hello\nworld"), ("\"\"\"one\\\n two\"\"\"", "onetwo"), ("\"\"\"one\\ \t\n \n\t two\"\"\"", "onetwo"), (#""""a"b""c""""#, "a\"b\"\"c"), (#""""a"""""#, "a\""), (#""""a""""""#, "a\"\""), ("'''a''''", "a'"), ("'''a'''''", "a''"), ("\"tab\tcharacter\"", "tab\tcharacter"), ("'tab\tcharacter'", "tab\tcharacter"), ]) func strings(input: String, expected: String) throws { #expect(try scalar(input) == .string(expected)) }
@Test(arguments: ["true", "false"]) func booleans(input: String) throws { #expect(try scalar(input) == .bool(input == "true")) }
@Test(arguments: [ "", " \t", "# hello", "# hello\r\n", "\n\n", "\t # comment\n", ]) func emptyDocuments(source: String) throws { #expect(try TOMLParser.parse(source).root.value == .table([:])) }
@Test(arguments: ["[1, 2, 3]", "[1,2,3,]", "[\n1, # one\n2,\n3,\n]"]) func arrays(input: String) throws { guard case .array(let entries) = try scalar(input) else { Issue.record("Expected an array") return } #expect(entries.map(\.value) == [.integer(1), .integer(2), .integer(3)]) }
@Test func mixedArrays() throws { guard case .array(let entries) = try scalar("[1, 'two', true, [], {}]") else { Issue.record("Expected an array") return } #expect(entries.map(\.value) == [.integer(1), .string("two"), .bool(true), .array([]), .table([:])]) }
@Test func inlineTablesAndDottedKeys() throws { let document = try TOMLDocument(parsing: "point = { x = 1, details.name = 'desk', y = 2 }") #expect(document.node(forKey: ["point", "x"])?.value == .integer(1)) #expect(document.node(forKey: ["point", "details", "name"])?.value == .string("desk")) }
@Test func inlineTablesCanContainMultilineValues() throws { let document = try TOMLDocument(parsing: "v = { a = [\n1,\n2], b = '''\nthree\nfour''' }") #expect(document.node(forKey: ["v", "b"])?.value == .string("three\nfour")) }
@Test func quotedAndBareKeys() throws { let source = "\"\" = 1\n'hello.world' = 2\n\"🪟\" . 'inner gap' = 3\n123 = 4\nfoo-bar_baz = 5" let document = try TOMLDocument(parsing: source) #expect(document.root[""]?.value == .integer(1)) #expect(document.root["hello.world"]?.value == .integer(2)) #expect(document.node(forKey: ["🪟", "inner gap"])?.value == .integer(3)) #expect(document.root["123"]?.value == .integer(4)) #expect(document.root["foo-bar_baz"]?.value == .integer(5)) }
@Test func parentTablesMayFollowChildren() throws { let document = try TOMLDocument(parsing: "[a.b.c]\nx=1\n[a]\ny=2\n[a.b]\nz=3") #expect(document.node(forKey: ["a", "b", "c", "x"])?.value == .integer(1)) #expect(document.node(forKey: ["a", "y"])?.value == .integer(2)) #expect(document.node(forKey: ["a", "b", "z"])?.value == .integer(3)) }
@Test func dottedTablesCanHaveNewSubtables() throws { let document = try TOMLDocument(parsing: "a.b.c = 1\na.b.d = 2\n[a.b.extra]\ne = 3") #expect(document.node(forKey: ["a", "b", "extra", "e"])?.value == .integer(3)) }
@Test func arraysOfTablesAttachToLatestElement() throws { let document = try TOMLDocument( parsing: """ [[displays]] name = 'first' [displays.gaps] inner = 8 [[displays.rules]] role = 'one' [[displays.rules]] role = 'two' [[displays]] name = 'second' [displays.gaps] inner = 12 """) #expect(document.node(at: [.key("displays"), .index(0), .key("gaps"), .key("inner")])?.value == .integer(8)) #expect(document.node(at: [.key("displays"), .index(1), .key("gaps"), .key("inner")])?.value == .integer(12)) #expect( document.node(at: [.key("displays"), .index(0), .key("rules"), .index(1), .key("role")])?.value == .string("two")) }
@Test func unicodeScalarLocations() throws { let document = try TOMLDocument(parsing: "# 🪟\r\n\"é\" = 'café' # note") let node = try #require(document.root["é"]) #expect(node.range.start == SourceLocation(line: 2, column: 8, offset: 12)) #expect(node.keyRange?.start == SourceLocation(line: 2, column: 1, offset: 5)) #expect(node.range.end.column == 14) }
@Test func duplicateIncludesBothPositions() throws { do { _ = try TOMLParser.parse("[layout]\ngap = 8\ngap = 12") Issue.record("Expected a duplicate error") } catch let diagnostic as ConfigDiagnostic { #expect(diagnostic.location.line == 3) #expect(diagnostic.location.column == 1) #expect(diagnostic.relatedLocation?.line == 2) #expect(diagnostic.description.contains("line 3, column 1")) #expect(diagnostic.message.contains("line 2, column 1")) } }
@Test(arguments: [ "a = 01", "a = -01", "a = +01", "a = 1_", "a = _1", "a = 1__2", "a = 0x_1", "a = 0b2", "a = 0o8", "a = 0XFF", "a = +0x1", "a = -0x1", "a = 0x", "a = .5", "a = 1.", "a = 1.e2", "a = 01.2", "a = 1e", "a = 1e_2", "a = 1_e2", "a = 1e+", "a = 1e9999", "a = 9223372036854775808", "a = -9223372036854775809", "a = 0xffffffffffffffff", "a = True", "a = FALSE", "a = text", "a = null", "a =", "a = 1 b = 2", "a = 1\rb = 2", "a = [1 2]", "a = [1,,2]", "a = [,]", "a = [1", "a = {x=1,}", "a = {x=1\ny=2}", "a = {\nx=1}", "a = {x=1 # nope\n}", "a = 'unfinished", "a = \"unfinished", "a = 'one\ntwo'", "a = \"one\ntwo\"", #"a = "\x00""#, #"a = "\uD800""#, #"a = "\U00110000""#, #"a = "\u123""#, #"a = "\q""#, "a = '\u{00}'", "a = '\u{7F}'", "# bad\u{01}", "a = \"\"\"x\\ y\"\"\"", "a = \"\"\"x\"\"\"\"\"\"", "=1", "a..b=1", "a.=1", "a b=1", "🪟=1", "\"\"\"a\"\"\"=1", "[a", "[]", "[[a]", "[a] a=1", "a=1\na=2", "a.b=1\na=2", "a=1\na.b=2", "a={b=1}\na.c=2", "a={b=1}\n[a.c]", "a.b=1\n[a]", "[a]\n[a]", "a=[]\n[[a]]", "a=[{}]\n[[a]]", "[[a]]\n[a]", "[a]\n[[a]]", "[[a]]\nx=1\nx=2", "a={x=1,x=2}", "a={x={y=1},x.z=2}", "[a.b]\nx=1\n[a]\nb.y=2", ]) func invalidDocuments(source: String) { #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: source) } }
@Test(arguments: ["1979-05-27", "1979-05-27T07:32:00Z", "07:32:00", "1979-05-27 07:32:00"]) func datesHaveClearErrors(input: String) throws { do { _ = try scalar(input) Issue.record("Expected an unsupported date error") } catch let diagnostic as ConfigDiagnostic { #expect(diagnostic.message.contains("Dates and times")) #expect(diagnostic.location.column == 5) } }
@Test(arguments: [ ("a = [1,\n nope]", 2, 3), ("a = \"bad\\q\"", 1, 9), ("# comment\n=1", 2, 1), ("a=1\r", 1, 4), ("a = 'x\ny'", 1, 7), ("a = {x=1,}", 1, 10), ("[a]\nx = 1\nx = 2", 3, 1), ]) func errorPositions(source: String, line: Int, column: Int) throws { do { _ = try TOMLDocument(parsing: source) Issue.record("Expected an error") } catch let diagnostic as ConfigDiagnostic { #expect(diagnostic.location.line == line) #expect(diagnostic.location.column == column) } }
@Test func excessiveNestingIsDiagnosed() { let source = "value = " + String(repeating: "[", count: 129) + "1" + String(repeating: "]", count: 129) #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: source) } }
@Test(arguments: [ "[a.b.c]\nx=1\n[a]\nb.d=2", "[a.b.c]\nx=1\n[a]\nb.d=2\n[a.b.e]\nz=3", "a.b.c=1\n[a.b.d]\nx=2", "[[a.b]]\nx=1\n[a]\ny=2", "[[a]]\nx=1\n[[a.b]]\ny=2\n[[a]]\nx=2\n[[a.b]]\ny=3", ]) func implicitTableExtensions(source: String) throws { _ = try TOMLDocument(parsing: source) }
@Test(arguments: [ "[a.b.c]\nx=1\n[a]\nb.d=2\n[a.b]\nz=3", "[[a]]\n[a.b]\nx=1\n[a.b]\ny=2", "[[a]]\nb.c=1\n[a.b]\ny=2", "[a.b.c]\nx=1\n[a]\nb.d.e=2\n[a.b.d]\ny=3", ]) func dottedUseDeclaresImplicitTables(source: String) { #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: source) } }
@Test func excessiveDottedNestingIsDiagnosed() { let key = Array(repeating: "a", count: 129).joined(separator: ".") #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: key + "=1") } #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: "[" + key + "]") } }
@Test func inlineDottedNestingSharesDepthBudget() { let prefix = Array(repeating: "a", count: 64).joined(separator: ".") let source = "v={" + prefix + "={" + prefix + "={x=1}}}" #expect(throws: ConfigDiagnostic.self) { try TOMLDocument(parsing: source) } }
private func scalar(_ source: String) throws -> TOMLValue { let document = try TOMLDocument(parsing: "v = " + source) return try #require(document.root["v"]?.value) }}