summaryrefslogtreecommitdiff
path: root/tests/typ/compute/data.typ
blob: f13f7da814c43149402907c54b51739f0e80b73b (plain) (blame)
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
// Test reading structured data and files.
// Ref: false

---
// Test reading plain text files
#let data = read("/files/hello.txt")
#test(data, "Hello, world!")

---
// Error: 18-38 file not found (searched at files/missing.txt)
#let data = read("/files/missing.txt")

---
// Error: 18-34 file is not valid utf-8
#let data = read("/files/bad.txt")

---
// Test reading CSV data.
// Ref: true
#set page(width: auto)
#let data = csv("/files/zoo.csv")
#let cells = data.at(0).map(strong) + data.slice(1).flatten()
#table(columns: data.at(0).len(), ..cells)

---
// Error: 6-16 file not found (searched at typ/compute/nope.csv)
#csv("nope.csv")

---
// Error: 6-22 failed to parse csv file: found 3 instead of 2 fields in line 3
#csv("/files/bad.csv")

---
// Test reading JSON data.
#let data = json("/files/zoo.json")
#test(data.len(), 3)
#test(data.at(0).name, "Debby")
#test(data.at(2).weight, 150)

---
// Error: 7-24 failed to parse json file: syntax error in line 3
#json("/files/bad.json")

---
// Test reading TOML data.
#let data = toml("/files/toml-types.toml")
#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))
#test(data.date_time, datetime(
  year: 2023,
  month: 2,
  day: 1,
  hour: 15,
  minute: 38,
  second: 57,
))

---
// Error: 7-24 failed to parse toml file: expected `.`, `=`, index 15-15
#toml("/files/bad.toml")

---
// Test reading YAML data
#let data = yaml("/files/yaml-types.yaml")
#test(data.len(), 7)
#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)
#test(data.keys().contains("true"), false)
---

---
// Error: 7-24 failed to parse yaml file: while parsing a flow sequence, expected ',' or ']' at line 2 column 1
#yaml("/files/bad.yaml")

---
// Test reading XML data.
#let data = xml("/files/data.xml")
#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",
  ),
),))

---
// Error: 6-22 failed to parse xml file: found closing tag 'data' instead of 'hello' in line 3
#xml("/files/bad.xml")