Skip to content

Commit

Permalink
add fields unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Jan 7, 2025
1 parent 037b1b9 commit ae9db59
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/unit/elements/fields/all-any/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# added by typst-test
Binary file added test/unit/elements/fields/all-any/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test/unit/elements/fields/all-any/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
check: _ => x => if type(x) == bool { true } else { x >= 0.0 },
error: _ => _ => "float must be positive or zero"
)

#let all-fields = (
field("color", types.any, required: true),
field("backcolor", types.any, required: true),
field("sign", types.any, default: 3em),
field("name", types.any, default: 3em),
)

#let (door, door-e) = element(
"door",
display: it => {},
fields: all-fields
)

#let (udoor, udoor-e) = element(
"udoor",
display: it => {},
fields: all-fields,
allow-unknown-fields: true
)

#door(red, 50)
#udoor(5pt, 60)

#door(10pt, 80, sign: "a", name: "b")
#let u = udoor(10pt, 80, sign: "a", name: "b", asdfasdf: 50)

#assert.eq(e.data(u).fields.asdfasdf, 50)
1 change: 1 addition & 0 deletions test/unit/elements/fields/empty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# added by typst-test
Binary file added test/unit/elements/fields/empty/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions test/unit/elements/fields/empty/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types

#let (door, door-e) = element(
"door",
display: it => {},
fields: ()
)

#let (udoor, udoor-e) = element(
"udoor",
display: it => {},
fields: (),
allow-unknown-fields: true
)

#door()
#udoor(abc: 5pt, def: 60)

#door()
#let u = udoor(g: 10pt, sign: "a", name: "b", asdfasdf: 50)

#assert.eq(e.data(u).fields.asdfasdf, 50)
1 change: 1 addition & 0 deletions test/unit/elements/fields/not-typechecked/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# added by typst-test
Binary file added test/unit/elements/fields/not-typechecked/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions test/unit/elements/fields/not-typechecked/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
check: _ => x => if type(x) == bool { true } else { x >= 0.0 },
error: _ => _ => "float must be positive or zero"
)

#let all-fields = (
field("color", color, required: true),
field("backcolor", color, required: true),
field("extracolor", types.option(color), named: false),
field("sign", types.smart(content)),
field("name", types.option(content)),
field("family", types.union(int, str), default: 5),
field("cool", bool, required: true, named: true),
field("sad", bool, required: true, named: true),
field("verysad", types.union(bool), named: true, default: true),
field("fruit", types.union(types.literal("bana"), types.literal("nana")), default: "nana"),
field("real-quantity", types.union(float, content), default: 5.5),
field("quantity", types.exact(types.union(float, content)), default: 5.5),
field("quantity-lit", types.exact(types.literal(10)), default: 10),
field("anything", types.exact(types.any), default: 50),
field("nothing", types.option(types.never), default: none),
field("somethings", array, default: (5, 4)),
field("matype", type, default: int),
field("float-or-content", types.union(color, float, content), default: red),
field("just-float", types.union(color, float), default: red),
field("just-content", types.union(color, content), default: red),
field("just-true", types.literal(true), default: true),
field("bool-or-pos-float", bool-or-pos-float, default: 5.0)
)

#let (door, door-e) = element(
"door",
display: it => {
rect(fill: it.color)[#it]
},
typecheck: false,
fields: all-fields
)

#let (udoor, udoor-e) = element(
"udoor",
display: it => {
rect(fill: it.color)[#it]
},
typecheck: false,
fields: all-fields,
allow-unknown-fields: true
)

#door(red, blue, cool: true, sad: false, family: "family")
#let d = door(red, blue, cool: true, sad: false, family: 0, verysad: "b")

#assert.eq(e.data(d).fields.verysad, "b")

#let u = udoor(red, blue, cool: true, sad: false, family: 0, verysad: true, real-quantity: "a", nope-not-existing: 50)

#assert.eq(e.data(u).fields.nope-not-existing, 50)
#assert.eq(e.data(u).fields.real-quantity, "a")

#show: e.set_(door, 50, verysad: red)

#door(red, blue, cool: true, sad: true)
#(door-e.get)(v => assert.eq(v.extracolor, 50))
#(door-e.get)(v => assert.eq(v.verysad, red))

#(door-e.where)(extracolor: yellow, sad: false, yellow-not-sad-doors => {
show yellow-not-sad-doors: [yep, yellow door, not sad]

door(red, blue, cool: true, sad: false)
door(red, blue, green, cool: true, sad: true)
})

#door(red, blue, cool: true, sad: "abc", real-quantity: 5)
#door(red, blue, cool: "not checked", sad: 5, real-quantity: "abc")
#door(red, blue, cool: true, sad: false, quantity: 5.5)
#door(red, blue, cool: true, sad: false, anything: (a: 5))
#door(red, 3, cool: true, sad: 50, quantity: red)


#[
#show: e.set_(door, just-true: true, float-or-content: 50, just-float: 5, just-content: "abc", bool-or-pos-float: false)
#show: e.set_(udoor, just-true: true, float-or-content: 50, just-float: 5, just-content: "abc", bool-or-pos-float: false)

#(door-e.get)(v => {
// No casting without typechecking
assert(type(v.float-or-content) == int and v.float-or-content == 50)
assert(type(v.just-float) == int and v.just-float == 5)
assert(v.just-content == "abc")
})

#(udoor-e.get)(v => {
// No casting without typechecking
assert(type(v.float-or-content) == int and v.float-or-content == 50)
assert(type(v.just-float) == int and v.just-float == 5)
assert(v.just-content == "abc")
})
]
1 change: 1 addition & 0 deletions test/unit/elements/fields/typechecked/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# added by typst-test
Binary file added test/unit/elements/fields/typechecked/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
100 changes: 100 additions & 0 deletions test/unit/elements/fields/typechecked/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#import "/test/unit/base.typ": empty
#show: empty

#import "/src/lib.typ" as e: element, field, types

#let bool-or-pos-float = types.wrap(
types.union(bool, float),
check: _ => x => if type(x) == bool { true } else { x >= 0.0 },
error: _ => _ => "float must be positive or zero"
)

#let all-fields = (
field("color", color, required: true),
field("backcolor", color, required: true),
field("extracolor", types.option(color), named: false),
field("sign", types.smart(content)),
field("name", types.option(content)),
field("family", types.union(int, str), default: 5),
field("cool", bool, required: true, named: true),
field("sad", bool, required: true, named: true),
field("verysad", types.union(bool), named: true, default: true),
field("fruit", types.union(types.literal("bana"), types.literal("nana")), default: "nana"),
field("real-quantity", types.union(float, content), default: 5.5),
field("quantity", types.exact(types.union(float, content)), default: 5.5),
field("quantity-lit", types.exact(types.literal(10)), default: 10),
field("anything", types.exact(types.any), default: 50),
field("nothing", types.option(types.never), default: none),
field("somethings", array, default: (5, 4)),
field("matype", type, default: int),
field("float-or-content", types.union(color, float, content), default: red),
field("just-float", types.union(color, float), default: red),
field("just-content", types.union(color, content), default: red),
field("just-true", types.literal(true), default: true),
field("bool-or-pos-float", bool-or-pos-float, default: 5.0),
field("stroke", stroke),
)

#let (door, door-e) = element(
"door",
display: it => {
rect(fill: it.color)[#it]
},
fields: all-fields
)

#let (udoor, udoor-e) = element(
"udoor",
display: it => {
rect(fill: it.color)[#it]
},
fields: all-fields,
allow-unknown-fields: true
)

#door(red, blue, cool: true, sad: false, family: "family")
#door(red, blue, cool: true, sad: false, family: 0, verysad: false)

#udoor(red, blue, cool: true, sad: false, family: 0, verysad: false)
#let u = udoor(red, blue, cool: true, sad: false, family: 0, verysad: true, nope-not-existing: 50)

#assert.eq(e.data(u).fields.nope-not-existing, 50)

#show: e.set_(door, yellow)
#show: e.set_(udoor, yellow)
#show: e.set_(udoor, this-does-not-exist: [abc])

#(udoor-e.get)(v => assert.eq(v.this-does-not-exist, [abc]))

#door(red, blue, cool: true, sad: true)
#(door-e.get)(v => assert.eq(v.extracolor, yellow))

#(door-e.where)(extracolor: yellow, sad: false, yellow-not-sad-doors => {
show yellow-not-sad-doors: [yep, yellow door, not sad]

door(red, blue, cool: true, sad: false)
door(red, blue, green, cool: true, sad: true)
})

// Test casting
#door(red, blue, cool: true, sad: false, real-quantity: 5)
#door(red, blue, cool: true, sad: false, real-quantity: "abc")
#door(red, blue, cool: true, sad: false, quantity: 5.5)
#door(red, blue, cool: true, sad: false, anything: (a: 5))

#[
#show: e.set_(door, just-true: true, float-or-content: 50, just-float: 5, just-content: "abc", bool-or-pos-float: false)

#(door-e.get)(v => {
assert(type(v.float-or-content) == float and v.float-or-content == 50.0)
assert(type(v.just-float) == float and v.just-float == 5)
assert(v.just-content == [abc])
})
]

// Test folding
#show: e.set_(udoor, stroke: 4pt, unknown-field-here: 50)
#show: e.set_(udoor, stroke: black, unknown-field-here: 80)

#(e.data(udoor).get)(u => assert.eq(u.stroke, 4pt + black))
#(e.data(udoor).get)(u => assert.eq(u.unknown-field-here, 80))

0 comments on commit ae9db59

Please sign in to comment.