2022-12-29 13:49:01 +03:00
// Test reading structured data and files.
2022-09-25 18:35:56 +03:00
// Ref: false
2022-07-27 01:09:15 +03:00
2022-12-29 13:49:01 +03:00
---
// Test reading plain text files
2023-06-28 11:35:44 +03:00
#let data = read("/files/hello.txt")
2023-08-06 01:15:29 +03:00
#test(data, "Hello, world!\n")
2022-12-29 13:49:01 +03:00
---
2023-06-28 11:35:44 +03:00
// Error: 18-38 file not found (searched at files/missing.txt)
#let data = read("/files/missing.txt")
2022-12-29 13:49:01 +03:00
---
2023-06-28 11:35:44 +03:00
// Error: 18-34 file is not valid utf-8
#let data = read("/files/bad.txt")
2022-12-29 13:49:01 +03:00
2022-07-27 01:09:15 +03:00
---
2022-09-25 18:35:56 +03:00
// Test reading CSV data.
// Ref: true
2022-07-27 01:09:15 +03:00
#set page(width: auto)
2023-06-28 11:35:44 +03:00
#let data = csv("/files/zoo.csv")
2022-12-30 17:13:28 +03:00
#let cells = data.at(0).map(strong) + data.slice(1).flatten()
#table(columns: data.at(0).len(), ..cells)
2022-07-27 01:09:15 +03:00
---
2022-11-29 15:37:25 +03:00
// Error: 6-16 file not found (searched at typ/compute/nope.csv)
2022-07-27 01:09:15 +03:00
#csv("nope.csv")
---
2023-09-11 13:04:37 +03:00
// Error: 6-22 failed to parse CSV (found 3 instead of 2 fields in line 3)
2023-06-28 11:35:44 +03:00
#csv("/files/bad.csv")
2022-09-25 18:35:56 +03:00
---
// Test reading JSON data.
2023-06-28 11:35:44 +03:00
#let data = json("/files/zoo.json")
2022-09-25 18:35:56 +03:00
#test(data.len(), 3)
2022-12-30 17:13:28 +03:00
#test(data.at(0).name, "Debby")
#test(data.at(2).weight, 150)
2022-09-25 18:35:56 +03:00
---
2023-09-11 13:04:37 +03:00
// Error: 7-24 failed to parse JSON (expected value at line 3 column 14)
2023-06-28 11:35:44 +03:00
#json("/files/bad.json")
2022-09-25 19:20:39 +03:00
2023-04-26 12:31:32 +03:00
---
// Test reading TOML data.
2023-06-28 11:35:44 +03:00
#let data = toml("/files/toml-types.toml")
2023-04-26 12:31:32 +03:00
#test(data.string, "wonderful")
#test(data.integer, 42)
#test(data.float, 3.14)
#test(data.boolean, true)
#test(data.array, (1, "string", 3.0, false))
#test(data.inline_table, ("first": "amazing", "second": "greater") )
#test(data.table.element, 5)
#test(data.table.others, (false, "indeed", 7))
2023-06-09 16:40:27 +03:00
#test(data.date_time, datetime(
year: 2023,
month: 2,
day: 1,
hour: 15,
minute: 38,
second: 57,
))
2023-08-25 15:31:03 +03:00
#test(data.date_time2, datetime(
year: 2023,
month: 2,
day: 1,
hour: 15,
minute: 38,
second: 57,
))
#test(data.date, datetime(
year: 2023,
month: 2,
day: 1,
))
#test(data.time, datetime(
hour: 15,
minute: 38,
second: 57,
))
2023-04-26 12:31:32 +03:00
---
2023-09-11 13:04:37 +03:00
// Error: 7-24 failed to parse TOML (expected `.`, `=` at line 1 column 16)
2023-06-28 11:35:44 +03:00
#toml("/files/bad.toml")
2023-04-26 12:31:32 +03:00
2023-04-01 15:33:42 +03:00
---
// Test reading YAML data
2023-06-28 11:35:44 +03:00
#let data = yaml("/files/yaml-types.yaml")
2023-08-30 18:15:49 +03:00
#test(data.len(), 9)
2023-04-01 15:33:42 +03:00
#test(data.null_key, (none, none))
#test(data.string, "text")
#test(data.integer, 5)
#test(data.float, 1.12)
#test(data.mapping, ("1": "one", "2": "two"))
#test(data.seq, (1,2,3,4))
#test(data.bool, false)
2023-08-25 15:31:03 +03:00
#test(data.keys().contains("true"), true)
2023-08-30 18:15:49 +03:00
#test(data.at("1"), "ok")
2023-04-01 15:33:42 +03:00
---
2023-09-13 15:04:12 +03:00
// Error: 7-24 failed to parse YAML (did not find expected ',' or ']' at line 2 column 1, while parsing a flow sequence at line 1 column 18)
2023-06-28 11:35:44 +03:00
#yaml("/files/bad.yaml")
2023-04-01 15:33:42 +03:00
2022-09-25 19:20:39 +03:00
---
// Test reading XML data.
2023-06-28 11:35:44 +03:00
#let data = xml("/files/data.xml")
2022-09-25 19:20:39 +03:00
#test(data, ((
tag: "data",
attrs: (:),
children: (
"\n ",
(tag: "hello", attrs: (name: "hi"), children: ("1",)),
"\n ",
(
tag: "data",
attrs: (:),
children: (
"\n ",
(tag: "hello", attrs: (:), children: ("World",)),
"\n ",
(tag: "hello", attrs: (:), children: ("World",)),
"\n ",
),
),
"\n",
),
),))
---
2023-09-11 13:04:37 +03:00
// Error: 6-22 failed to parse XML (found closing tag 'data' instead of 'hello' in line 3)
2023-06-28 11:35:44 +03:00
#xml("/files/bad.xml")