From 10da5e0424c9da4f9e1e263a76eef50e1a0c70f5 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Tue, 17 Jan 2023 21:55:36 +0800 Subject: [PATCH] chore: add more tests (#2801) --- core/lang/lang.go | 2 +- core/lang/lang_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/core/lang/lang.go b/core/lang/lang.go index 3e2932fd..6ca50973 100644 --- a/core/lang/lang.go +++ b/core/lang/lang.go @@ -29,7 +29,7 @@ func Repr(v interface{}) string { } val := reflect.ValueOf(v) - if val.Kind() == reflect.Ptr && !val.IsNil() { + for val.Kind() == reflect.Ptr && !val.IsNil() { val = val.Elem() } diff --git a/core/lang/lang_test.go b/core/lang/lang_test.go index 1527defd..2c28a5e0 100644 --- a/core/lang/lang_test.go +++ b/core/lang/lang_test.go @@ -1,6 +1,9 @@ package lang import ( + "encoding/json" + "errors" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -110,6 +113,28 @@ func TestRepr(t *testing.T) { } } +func TestReprOfValue(t *testing.T) { + t.Run("error", func(t *testing.T) { + assert.Equal(t, "error", reprOfValue(reflect.ValueOf(errors.New("error")))) + }) + + t.Run("stringer", func(t *testing.T) { + assert.Equal(t, "1.23", reprOfValue(reflect.ValueOf(json.Number("1.23")))) + }) + + t.Run("int", func(t *testing.T) { + assert.Equal(t, "1", reprOfValue(reflect.ValueOf(1))) + }) + + t.Run("int", func(t *testing.T) { + assert.Equal(t, "1", reprOfValue(reflect.ValueOf("1"))) + }) + + t.Run("int", func(t *testing.T) { + assert.Equal(t, "1", reprOfValue(reflect.ValueOf(uint(1)))) + }) +} + type mockStringable struct{} func (m mockStringable) String() string {