src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/vals/repr_test.go (about) 1 package vals 2 3 import ( 4 "fmt" 5 "math/big" 6 "os" 7 "testing" 8 9 "src.elv.sh/pkg/tt" 10 ) 11 12 type reprer struct{} 13 14 func (reprer) Repr(int) string { return "<reprer>" } 15 16 type nonReprer struct{} 17 18 func TestReprPlain(t *testing.T) { 19 tt.Test(t, ReprPlain, 20 Args(nil).Rets("$nil"), 21 22 Args(false).Rets("$false"), 23 Args(true).Rets("$true"), 24 25 Args("foo").Rets("foo"), 26 27 Args(1).Rets("(num 1)"), 28 Args(bigInt(z)).Rets("(num "+z+")"), 29 Args(big.NewRat(1, 2)).Rets("(num 1/2)"), 30 Args(1.0).Rets("(num 1.0)"), 31 Args(1e10).Rets("(num 10000000000.0)"), 32 33 Args(os.Stdin).Rets( 34 fmt.Sprintf("<file{%s %d}>", os.Stdin.Name(), os.Stdin.Fd())), 35 36 Args(EmptyList).Rets("[]"), 37 Args(MakeList("foo", "bar")).Rets("[foo bar]"), 38 39 Args(EmptyMap).Rets("[&]"), 40 Args(MakeMap("foo", "bar")).Rets("[&foo=bar]"), 41 // Keys of the same type are sorted. 42 Args(MakeMap("b", "second", "a", "first", "c", "third")). 43 Rets("[&a=first &b=second &c=third]"), 44 Args(MakeMap(2, "second", 1, "first", 3, "third")). 45 Rets("[&(num 1)=first &(num 2)=second &(num 3)=third]"), 46 // Keys of mixed types tested in a different test. 47 48 Args(reprer{}).Rets("<reprer>"), 49 Args(nonReprer{}).Rets("<unknown {}>"), 50 ) 51 } 52 53 func TestReprPlain_MapWithKeysOfMixedTypes(t *testing.T) { 54 m := MakeMap( 55 "b", "second", "a", "first", "c", "third", 56 2, "second", 1, "first", 3, "third") 57 strPart := "&a=first &b=second &c=third" 58 numPart := "&(num 1)=first &(num 2)=second &(num 3)=third" 59 want1 := "[" + strPart + " " + numPart + "]" 60 want2 := "[" + numPart + " " + strPart + "]" 61 got := ReprPlain(m) 62 if got != want1 && got != want2 { 63 t.Errorf("got %q, want %q or %q", got, want1, want2) 64 } 65 }