| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import * as Toml from "effect/encoding/Toml" |
| 3 | |
| 4 | describe("Toml", () => { |
| 5 | it("parses tables, dotted keys, arrays, and inline tables", () => { |
| 6 | assert.deepStrictEqual( |
| 7 | Toml.parse(` |
| 8 | title = "Effect" |
| 9 | ports = [8000, 8001] |
| 10 | database.connection.timeout = 30 |
| 11 | |
| 12 | [database] |
| 13 | enabled = true |
| 14 | credentials = { user = "root", roles = ["admin", "writer"] } |
| 15 | `), |
| 16 | { |
| 17 | title: "Effect", |
| 18 | ports: [8000, 8001], |
| 19 | database: { |
| 20 | connection: { timeout: 30 }, |
| 21 | enabled: true, |
| 22 | credentials: { user: "root", roles: ["admin", "writer"] } |
| 23 | } |
| 24 | } |
| 25 | ) |
| 26 | }) |
| 27 | |
| 28 | it("parses arrays of tables and date-time values", () => { |
| 29 | assert.deepStrictEqual( |
| 30 | Toml.parse(` |
| 31 | [[servers]] |
| 32 | name = "alpha" |
| 33 | started = 2026-08-05T01:02:03Z |
| 34 | |
| 35 | [[servers]] |
| 36 | name = "beta" |
| 37 | started = 2026-08-05 |
| 38 | `), |
| 39 | { |
| 40 | servers: [ |
| 41 | { name: "alpha", started: new Date("2026-08-05T01:02:03Z") }, |
| 42 | { name: "beta", started: "2026-08-05" } |
| 43 | ] |
| 44 | } |
| 45 | ) |
| 46 | }) |
| 47 | |
| 48 | it("parses child tables under distinct array entries", () => { |
| 49 | assert.deepStrictEqual( |
| 50 | Toml.parse(` |
| 51 | [[servers]] |
| 52 | [servers.tls] |
| 53 | |
| 54 | [[servers]] |
| 55 | [servers.tls] |
| 56 | `), |
| 57 | { servers: [{ tls: {} }, { tls: {} }] } |
| 58 | ) |
| 59 | }) |
| 60 | |
| 61 | it("parses multiline strings and numeric formats", () => { |
| 62 | assert.deepStrictEqual( |
| 63 | Toml.parse(` |
| 64 | message = """ |
| 65 | hello \\ |
| 66 | world""" |
| 67 | hex = 0xDEAD_BEEF |
| 68 | fraction = 1_000.5 |
| 69 | local = 2026-08-05 01:02:03 |
| 70 | `), |
| 71 | { |
| 72 | message: "hello world", |
| 73 | hex: 0xdeadbeef, |
| 74 | fraction: 1000.5, |
| 75 | local: "2026-08-05T01:02:03" |
| 76 | } |
| 77 | ) |
| 78 | }) |
| 79 | |
| 80 | it("rejects duplicate keys", () => { |
| 81 | assert.throws(() => Toml.parse("key = 1\nkey = 2\n")) |
| 82 | assert.throws(() => Toml.parse("key = 1__000\n")) |
| 83 | }) |
| 84 | }) |