Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed Mar 16, 2021
2 parents dee26b7 + 36b4331 commit b16a465
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/parser/human_readable_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func toHumanReadable(value reflect.Value, insertion int) string {
output := "(" + strconv.Itoa(value.Len()) + ") \n"
for _, key := range value.MapKeys() {
output += strings.Repeat(" ", insertion)
output += key.String() + ": "
output += toHumanReadable(key, insertion+1) + ": "
output += toHumanReadable(value.MapIndex(key), insertion+1)
output += "\n"
}
return output
return "\n" + strings.Repeat(" ", insertion) + strings.TrimSpace(output) + "\n"
case reflect.String:
return value.String()
case reflect.Int:
Expand Down
71 changes: 71 additions & 0 deletions core/parser/human_readable_parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package parser

import (
"github.com/stretchr/testify/assert"
"testing"
)

type TestStruct struct {
Number uint
Name string
Array []float64
}

func TestToHumanReadableString(t *testing.T) {
output, err := ToHumanReadable("foobla")
assert.Nil(t, err)
assert.Equal(t, "foobla", string(output))
}

func TestToHumanReadableFloat(t *testing.T) {
output, err := ToHumanReadable(0.1)
assert.Nil(t, err)
assert.Equal(t, "0.1", string(output))
}

func TestToHumanReadablePointer1(t *testing.T) {
var i uint64
i = 2
output, err := ToHumanReadable(&i)
assert.Nil(t, err)
assert.Equal(t, "2", string(output))
}

func TestToHumanReadablePointer2(t *testing.T) {
var str []string
str = nil
output, err := ToHumanReadable(&str)
assert.Nil(t, err)
assert.Equal(t, "", string(output))
}

func TestToHumanReadableArray(t *testing.T) {
output, err := ToHumanReadable([]string{"a", "b", "c"})
assert.Nil(t, err)
assert.Equal(t, "[3] a b c", string(output))
}

func TestToHumanReadableMap1(t *testing.T) {
output, err := ToHumanReadable(map[int]string{1: "one"})
assert.Nil(t, err)
assert.Equal(t, "(1) \n"+
"1: one", string(output))
}

func TestToHumanReadableMap2(t *testing.T) {
output, err := ToHumanReadable(map[int]TestStruct{
1: {Number: 5}})
assert.Nil(t, err)
assert.Equal(t, "(1) \n"+
"1: \n"+
" Number: 5", string(output))
}

func TestToHumanReadableStruct(t *testing.T) {
mystruct := TestStruct{Number: 5, Name: "MyName", Array: []float64{0.1, 0.2}}
output, err := ToHumanReadable(mystruct)
assert.Nil(t, err)
assert.Equal(t, "Number: 5\n"+
"Name: MyName\n"+
"Array: [2] 0.1 0.2", string(output))
}

0 comments on commit b16a465

Please sign in to comment.