From 6ecfb61e5b0068b25c364601dfb326ae359a8482 Mon Sep 17 00:00:00 2001 From: David Vogel Date: Thu, 27 Feb 2025 16:28:19 +0100 Subject: [PATCH] Make TestInjectValues deterministic --- util.go | 7 ++++++- util_test.go | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 7479c23..7d534e4 100644 --- a/util.go +++ b/util.go @@ -8,6 +8,8 @@ package typst import ( "fmt" "io" + "maps" + "slices" ) // 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 { 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) { return fmt.Errorf("%q is not a valid identifier", k) } diff --git a/util_test.go b/util_test.go index ffa5f2f..c046d6c 100644 --- a/util_test.go +++ b/util_test.go @@ -18,7 +18,7 @@ func TestInjectValues(t *testing.T) { }{ {"empty", args{values: nil}, "", 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}, } for _, tt := range tests {