summaryrefslogtreecommitdiff
path: root/tests/typ/code/block.typ
blob: 9122ea4862623d3d4c20fcce9f91e8bab56ba171 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Test code blocks.
// Ref: false

---
// Ref: true

// Evaluates to join of none, [My ] and the two loop bodies.
{
  let parts = ("my fri", "end.")
  [Hello, ]
  for s in parts [{s}]
}

// Evaluates to join of the templates and strings.
{
  [How]
  if true {
    " are"
  }
  [ ]
  if false [Nope]
  [you] + "?"
}

---
// Nothing evaluates to none.
#test({}, none)

// Let evaluates to none.
#test({ let v = 0 }, none)

// Evaluates to single expression.
#test({ "hello" }, "hello")

// Evaluates to string.
#test({ let x = "m"; x + "y" }, "my")

// Evaluated to int.
#test({
  let x = 1
  let y = 2
  x + y
}, 3)

// String is joined with trailing none, evaluates to string.
#test({
  type("")
  none
}, "string")

---
// Some things can't be joined.
{
  [A]
  // Error: 3-4 cannot join template with integer
  1
  [B]
}

---
// Block directly in template also creates a scope.
{ let x = 1 }

// Error: 7-8 unknown variable
#test(x, 1)

---
// Block in expression does create a scope.
#let a = {
  let b = 1
  b
}

#test(a, 1)

// Error: 2-3 unknown variable
{b}

---
// Double block creates a scope.
{{
  import b from "target.typ"
  test(b, 1)
}}

// Error: 2-3 unknown variable
{b}

---
// Multiple nested scopes.
{
  let a = "a1"
  {
    let a = "a2"
    {
      test(a, "a2")
      let a = "a3"
      test(a, "a3")
    }
    test(a, "a2")
  }
  test(a, "a1")
}

---
// Template also creates a scope.
[#let x = 1]

// Error: 2-3 unknown variable
{x}

---
// Multiple unseparated expressions in one line.

// Error: 2-4 expected expression, found invalid token
{1u}

// Should output `1`.
// Error: 3 expected semicolon or line break
{1 2}

// Should output `2`.
// Error: 12 expected semicolon or line break
// Error: 22 expected semicolon or line break
{let x = -1 let y = 3 x + y}

// Should output `3`.
{
  // Error: 7-10 expected identifier, found string
  for "v"

  // Error: 8 expected keyword `in`
  for v let z = 1 + 2

  z
}

---
// Error: 2 expected closing brace
{

---
// Error: 1-2 unexpected closing brace
}