mirror of
https://github.com/Dadido3/go-typst.git
synced 2025-04-11 12:13:16 +00:00
Make TestInjectValues deterministic
This commit is contained in:
parent
2a1d2990e7
commit
6ecfb61e5b
7
util.go
7
util.go
@ -8,6 +8,8 @@ package typst
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"maps"
|
||||||
|
"slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InjectValues will write the given key-value pairs as Typst markup into output.
|
// InjectValues will write the given key-value pairs as Typst markup into output.
|
||||||
@ -23,7 +25,10 @@ import (
|
|||||||
func InjectValues(output io.Writer, values map[string]any) error {
|
func InjectValues(output io.Writer, values map[string]any) error {
|
||||||
enc := NewVariableEncoder(output)
|
enc := NewVariableEncoder(output)
|
||||||
|
|
||||||
for k, v := range values {
|
// We will have to iterate over the sorted list of map keys.
|
||||||
|
// Otherwise the output is not deterministic, and tests will fail randomly.
|
||||||
|
for _, k := range slices.Sorted(maps.Keys(values)) {
|
||||||
|
v := values[k]
|
||||||
if !IsIdentifier(k) {
|
if !IsIdentifier(k) {
|
||||||
return fmt.Errorf("%q is not a valid identifier", k)
|
return fmt.Errorf("%q is not a valid identifier", k)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ func TestInjectValues(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{"empty", args{values: nil}, "", false},
|
{"empty", args{values: nil}, "", false},
|
||||||
{"nil", args{values: map[string]any{"foo": nil}}, "#let foo = none\n", false},
|
{"nil", args{values: map[string]any{"foo": nil}}, "#let foo = none\n", false},
|
||||||
{"example", args{values: map[string]any{"foo": 1, "bar": 60 * time.Second}}, "#let foo = 1\n#let bar = duration(seconds: 60)\n", false},
|
{"example", args{values: map[string]any{"foo": 1, "bar": 60 * time.Second}}, "#let bar = duration(seconds: 60)\n#let foo = 1\n", false},
|
||||||
{"invalid identifier", args{values: map[string]any{"foo😀": 1}}, "", true},
|
{"invalid identifier", args{values: map[string]any{"foo😀": 1}}, "", true},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user