Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/toml/test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { assertEquals, assertThrows } from "../assert/mod.ts";import { existsSync } from "../fs/exists.ts";import * as path from "../path/mod.ts";import { parse, stringify } from "./mod.ts";
const moduleDir = path.dirname(path.fromFileUrl(import.meta.url));const testdataDir = path.resolve(moduleDir, "./testdata");
function parseFile(filePath: string): Record<string, unknown> { if (!existsSync(filePath)) { throw new Error(`File not found: ${filePath}`); } return parse(Deno.readTextFileSync(filePath));}
Deno.test({ name: "parse() handles strings", fn() { const expected = { strings: { str0: "deno", str1: "Roses are not Deno\n Violets are not Deno either", str2: "Roses are not Deno\nViolets are not Deno either", str3: "Roses are not Deno\r\nViolets are not Deno either", str4: 'this is a "quote"', str5: "The quick brown fox jumps over the lazy dog.", str6: "The quick brown fox jumps over the lazy dog.", str7: "Roses are red\tViolets are blue", str8: "Roses are red\fViolets are blue", str9: "Roses are red\bViolets are blue", str10: "Roses are red\\Violets are blue", str11: `double "quote"\nsingle 'quote'\n`, str12: 'Here are two quotation marks: "". Simple enough.', str13: 'Here are three quotation marks: """.', str14: 'Here are fifteen quotation marks: """"""""""""""".', str15: '"This," she said, "is just a pointless statement."', literal1: "The first newline is\ntrimmed in raw strings.\n All other whitespace\n is preserved.\n", literal2: '"\\n#=*{', literal3: "\\n\\t is 'literal'\\\n", literal4: 'Here are fifteen quotation marks: """""""""""""""', literal5: "Here are fifteen apostrophes: '''''''''''''''", literal6: "'That,' she said, 'is still pointless.'", withApostrophe: "What if it's not?", withSemicolon: `const message = 'hello world';`, withHexNumberLiteral: "Prevent bug from stripping string here ->0xabcdef", withUnicodeChar1: "あ", withUnicodeChar2: "Deno🦕", }, }; const actual = parseFile(path.join(testdataDir, "string.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles CRLF", fn() { const expected = { boolean: { bool1: true, bool2: false } }; const actual = parseFile(path.join(testdataDir, "CRLF.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles boolean", fn() { const expected = { boolean: { bool1: true, bool2: false, bool3: true } }; const actual = parseFile(path.join(testdataDir, "boolean.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles integer", fn() { const expected = { integer: { int1: 99, int2: 42, int3: 0, int4: -17, int5: 1000, int6: 5349221, int7: 12345, hex1: "0xDEADBEEF", hex2: "0xdeadbeef", hex3: "0xdead_beef", oct1: "0o01234567", oct2: "0o755", bin1: "0b11010110", }, }; const actual = parseFile(path.join(testdataDir, "integer.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles float", fn() { const expected = { float: { flt1: 1.0, flt2: 3.1415, flt3: -0.01, flt4: 5e22, flt5: 1e6, flt6: -2e-2, flt7: 6.626e-34, flt8: 224_617.445_991_228, sf1: Infinity, sf2: Infinity, sf3: -Infinity, sf4: NaN, sf5: NaN, sf6: NaN, }, }; const actual = parseFile(path.join(testdataDir, "float.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles arrays", fn() { const expected = { arrays: { data: [ ["gamma", "delta"], [1, 2], ], floats: [ 0.1, -1.25, ], hosts: ["alpha", "omega"], profiles: [ { "john@example.com": true, name: "John", }, { "doe@example.com": true, name: "Doe", }, ], }, }; const actual = parseFile(path.join(testdataDir, "arrays.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles table", fn() { const expected = { deeply: { nested: { object: { in: { the: { toml: { name: "Tom Preston-Werner", }, }, }, }, }, }, servers: { alpha: { ip: "10.0.0.1", dc: "eqdc10", }, beta: { ip: "10.0.0.2", dc: "eqdc20", }, }, dog: { "tater.man": { type: { name: "pug", }, }, }, }; const actual = parseFile(path.join(testdataDir, "table.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles various keys", fn() { const expected = { site: { "google.com": { bar: 1, baz: 1 } }, a: { b: { c: 1, d: 1 }, e: 1 }, "": 1, "127.0.0.1": 1, "ʎǝʞ": 1, 'this is "literal"': 1, 'double "quote"': 1, "basic__\n__": 1, "literal__\\n__": 1, }; const actual = parseFile(path.join(testdataDir, "keys.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles simple", fn() { const expected = { deno: "is", not: "[node]", regex: "<\\i\\c*\\s*>", NANI: "何?!", comment: "Comment inside # the comment", }; const actual = parseFile(path.join(testdataDir, "simple.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles datetime", fn() { const expected = { datetime: { odt1: new Date("1979-05-27T07:32:00Z"), odt2: new Date("1979-05-27T00:32:00-07:00"), odt3: new Date("1979-05-27T00:32:00.999999-07:00"), odt4: new Date("1979-05-27 07:32:00Z"), ld1: new Date("1979-05-27"), lt1: "07:32:00", lt2: "00:32:00.999999", }, }; const actual = parseFile(path.join(testdataDir, "datetime.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles inline table", fn() { const expected = { inlinetable: { nile: { also: { malevolant: { creation: { drum: { kit: "Tama", }, }, }, }, derek: { roddy: "drummer", }, }, name: { first: "Tom", last: "Preston-Werner", }, point: { x: 1, y: 2, }, dog: { type: { name: "pug", }, }, "tosin.abasi": "guitarist", animal: { as: { leaders: "tosin", }, }, annotation_filter: { "kubernetes.io/ingress.class": "nginx" }, literal_key: { "foo\\nbar": "foo\\nbar", }, nested: { parent: { "child.ren": [ "[", "]", ], children: [ "{", "}", ], }, }, empty: {}, }, }; const actual = parseFile(path.join(testdataDir, "inlineTable.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles array of tables", fn() { const expected = { bin: [ { name: "deno", path: "cli/main.rs" }, { name: "deno_core", path: "src/foo.rs" }, ], nib: [{ name: "node", path: "not_found" }], a: { c: { z: "z", }, }, b: [ { c: { z: "z", }, }, { c: { z: "z", }, }, ], aaa: [ { bbb: { asdf: "asdf", }, hi: "hi", }, ], }; const actual = parseFile(path.join(testdataDir, "arrayTable.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles cargo", fn() { const expected = { workspace: { members: ["./", "core"] }, bin: [{ name: "deno", path: "cli/main.rs" }], package: { name: "deno", version: "0.3.4", edition: "2018" }, dependencies: { deno_core: { path: "./core" }, atty: "0.2.11", dirs: "1.0.5", flatbuffers: "0.5.0", futures: "0.1.25", getopts: "0.2.18", http: "0.1.16", hyper: "0.12.24", "hyper-rustls": "0.16.0", "integer-atomics": "1.0.2", lazy_static: "1.3.0", libc: "0.2.49", log: "0.4.6", rand: "0.6.5", regex: "1.1.0", remove_dir_all: "0.5.2", ring: "0.14.6", rustyline: "3.0.0", serde_json: "1.0.38", "source-map-mappings": "0.5.0", tempfile: "3.0.7", tokio: "0.1.15", "tokio-executor": "0.1.6", "tokio-fs": "0.1.5", "tokio-io": "0.1.11", "tokio-process": "0.2.3", "tokio-threadpool": "0.1.11", url: "1.7.2", }, target: { "cfg(windows)": { dependencies: { winapi: "0.3.6" } }, "cfg(linux)": { dependencies: { winapi: "0.3.9" } }, }, }; const actual = parseFile(path.join(testdataDir, "cargo.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles stringify", fn() { const src = { foo: { bar: "deno" }, this: { is: { nested: "denonono" } }, "https://deno.land/std": { $: "dollar", }, "##": { deno: { "https://deno.land": { proto: "https", ":80": "port", }, }, }, arrayObjects: [{ stuff: "in" }, {}, { the: "array" }], deno: "is", not: "[node]", regex: "<ic*s*>", NANI: "何?!", comment: "Comment inside # the comment", int1: 99, int2: 42, int3: 0, int4: -17, int5: 1000, int6: 5349221, int7: 12345, flt1: 1.0, flt2: 3.1415, flt3: -0.01, flt4: 5e22, flt5: 1e6, flt6: -2e-2, flt7: 6.626e-34, odt1: new Date("1979-05-01T07:32:00Z"), odt2: new Date("1979-05-27T00:32:00-07:00"), odt3: new Date("1979-05-27T00:32:00.999999-07:00"), odt4: new Date("1979-05-27 07:32:00Z"), ld1: new Date("1979-05-27"), reg: /foo[bar]/, sf1: Infinity, sf2: Infinity, sf3: -Infinity, sf4: NaN, sf5: NaN, sf6: NaN, data: [ ["gamma", "delta"], [1, 2], ], hosts: ["alpha", "omega"], bool: true, bool2: false, }; const expected = `deno = "is"not = "[node]"regex = "<ic*s*>"NANI = "何?!"comment = "Comment inside # the comment"int1 = 99int2 = 42int3 = 0int4 = -17int5 = 1000int6 = 5349221int7 = 12345flt1 = 1flt2 = 3.1415flt3 = -0.01flt4 = 5e+22flt5 = 1000000flt6 = -0.02flt7 = 6.626e-34odt1 = 1979-05-01T07:32:00.000odt2 = 1979-05-27T07:32:00.000odt3 = 1979-05-27T07:32:00.999odt4 = 1979-05-27T07:32:00.000ld1 = 1979-05-27T00:00:00.000reg = "/foo[bar]/"sf1 = infsf2 = infsf3 = -infsf4 = NaNsf5 = NaNsf6 = NaNdata = [["gamma","delta"],[1,2]]hosts = ["alpha","omega"]bool = truebool2 = false
[foo]bar = "deno"
[this.is]nested = "denonono"
["https://deno.land/std"]"$" = "dollar"
["##".deno."https://deno.land"]proto = "https"":80" = "port"
[[arrayObjects]]stuff = "in"
[[arrayObjects]]
[[arrayObjects]]the = "array"`; const actual = stringify(src); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles mixed array", fn() { const src = { emptyArray: [], mixedArray1: [1, { b: 2 }], mixedArray2: [{ b: 2 }, 1], nestedArray1: [[{ b: 1, date: new Date("2022-05-13") }]], nestedArray2: [[[{ b: 1 }]]], nestedArray3: [[], [{ b: 1 }]], deepNested: { a: { b: [1, { c: 2, d: [{ e: 3 }, true] }], }, }, }; const expected = `emptyArray = []mixedArray1 = [1,{b = 2}]mixedArray2 = [{b = 2},1]nestedArray1 = [[{b = 1,date = "2022-05-13T00:00:00.000"}]]nestedArray2 = [[[{b = 1}]]]nestedArray3 = [[],[{b = 1}]]
[deepNested.a]b = [1,{c = 2,d = [{e = 3},true]}]`; const actual = stringify(src); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles stringify with string values", fn: () => { const src = { '"': '"', "'": "'", " ": " ", "\\": "\\", "\n": "\n", "\t": "\t", }; const expected = `"\\"" = "\\"""'" = "'"" " = " ""\\\\" = "\\\\""\\n" = "\\n""\\t" = "\\t"`.trim(); const actual = stringify(src).trim(); assertEquals(actual, expected); const parsed = parse(actual); assertEquals(src, parsed); },});
Deno.test({ name: "parse() handles comments", fn: () => { const expected = { str0: "value", str1: "# This is not a comment", str2: " # this is not a comment!\nA multiline string with a #\n# this is also not a comment\n", str3: '"# not a comment"\n\t# this is a real tab on purpose \n# not a comment\n', point0: { x: 1, y: 2, str0: "#not a comment", z: 3 }, point1: { x: 7, y: 8, z: 9, str0: "#not a comment" }, deno: { features: ["#secure by default", "supports typescript # not a comment"], url: "https://deno.land/", is_not_node: true, }, toml: { name: "Tom's Obvious, Minimal Language", objectives: ["easy to read", "minimal config file", "#not a comment"], }, }; const actual = parseFile(path.join(testdataDir, "comment.toml")); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles inline array of inline table", fn() { const expected = { inlineArray: { string: [{ var: "a string" }], my_points: [ { x: 1, y: 2, z: 3 }, { x: 7, y: 8, z: 9 }, { x: 2, y: 4, z: 8 }, ], points: [ { x: 1, y: 2, z: 3 }, { x: 7, y: 8, z: 9 }, { x: 2, y: 4, z: 8 }, ], }, }; const actual = parseFile( path.join(testdataDir, "inlineArrayOfInlineTable.toml"), ); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles parse malformed local time as string (#8433)", fn() { const expected = { sign: "2020-01-01x" }; const actual = parse(`sign='2020-01-01x'`); assertEquals(actual, expected); },});
Deno.test({ name: "parse() handles single-line string comment error", fn() { assertThrows( () => { parseFile(path.join(testdataDir, "error-open-string.toml")); }, Error, `Parse error on line 1, column 34: Single-line string cannot contain EOL`, ); },});
Deno.test({ name: "parse() handles invalid string format", fn() { assertThrows( () => { parseFile(path.join(testdataDir, "error-invalid-string.toml")); }, Error, `invalid data format`, ); },});
Deno.test({ name: "parse() handles invalid whitespaces", fn() { assertThrows( () => { parseFile(path.join(testdataDir, "error-invalid-whitespace1.toml")); }, Error, "Contains invalid whitespaces: `\\u3000`", ); assertThrows( () => { parseFile(path.join(testdataDir, "error-invalid-whitespace2.toml")); }, Error, "Contains invalid whitespaces: `\\u3000`", ); },});
// https://github.com/denoland/deno_std/issues/1067#issuecomment-907740319Deno.test({ name: "stringify() handles object value contains '='", fn() { const src = { "a": "a = 1", "helloooooooo": 1, };
const actual = stringify(src, { keyAlignment: true }); const expected = `a = "a = 1"helloooooooo = 1`; assertEquals(actual, expected); },});
Deno.test({ name: "stringify() handles key alignment", fn() { const src = { "a": 1, "aa": 1, "aaa": 1, "aaaa": 1, "aaaaa": 1, }; const actual = stringify(src, { keyAlignment: true }); const expected = `a = 1aa = 1aaa = 1aaaa = 1aaaaa = 1`; assertEquals(actual, expected); },});
Deno.test({ name: "stringify() handles empty key", fn() { const src = { "": "a", "b": { "": "c" }, }; const actual = stringify(src); const expected = `"" = "a"
[b]"" = "c"`; assertEquals(actual, expected); },});
Deno.test({ name: "stringify() handles empty object", fn() { const src = { "a": {}, "b": { "c": {} }, }; const actual = stringify(src); const expected = `[a]
[b.c]`; assertEquals(actual, expected); },});
Deno.test({ name: "stringify() handles special keys in inline object", fn() { const src = { "a": [{ "/": "b" }, "c"], }; const actual = stringify(src); const expected = 'a = [{"/" = "b"},"c"]\n'; assertEquals(actual, expected); },});
Deno.test({ name: "stringify() throws on invalid value", fn() { assertThrows( () => stringify({ a: [[null]] }), Error, "should never reach", ); assertThrows( () => stringify({ a: [[undefined]] }), Error, "should never reach", ); },});