engine: util: Improve StructTagToFieldName and add tests
This improves this function to make it more generic.
This commit is contained in:
@@ -21,6 +21,7 @@ package util
|
||||
|
||||
import (
|
||||
"os/user"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
@@ -105,3 +106,61 @@ func TestCurrentUserGroupById(t *testing.T) {
|
||||
t.Errorf("gid didn't match current user's: %s vs %s", strconv.Itoa(gid), currentGID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructTagToFieldName0(t *testing.T) {
|
||||
type foo struct {
|
||||
A string `lang:"aaa"`
|
||||
B bool `lang:"bbb"`
|
||||
C int64 `lang:"ccc"`
|
||||
}
|
||||
f := &foo{ // a ptr!
|
||||
A: "hello",
|
||||
B: true,
|
||||
C: 13,
|
||||
}
|
||||
m, err := StructTagToFieldName(f) // (map[string]string, error)
|
||||
if err != nil {
|
||||
t.Errorf("got error: %+v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("got output: %+v", m)
|
||||
expected := map[string]string{
|
||||
"aaa": "A",
|
||||
"bbb": "B",
|
||||
"ccc": "C",
|
||||
}
|
||||
if !reflect.DeepEqual(m, expected) {
|
||||
t.Errorf("unexpected result")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestStructTagToFieldName1(t *testing.T) {
|
||||
type foo struct {
|
||||
A string `lang:"aaa"`
|
||||
B bool `lang:"bbb"`
|
||||
C int64 `lang:"ccc"`
|
||||
}
|
||||
f := foo{ // not a ptr!
|
||||
A: "hello",
|
||||
B: true,
|
||||
C: 13,
|
||||
}
|
||||
m, err := StructTagToFieldName(f) // (map[string]string, error)
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
//return
|
||||
}
|
||||
t.Logf("got output: %+v", m)
|
||||
t.Logf("got error: %+v", err)
|
||||
}
|
||||
|
||||
func TestStructTagToFieldName2(t *testing.T) {
|
||||
m, err := StructTagToFieldName(nil) // (map[string]string, error)
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
//return
|
||||
}
|
||||
t.Logf("got output: %+v", m)
|
||||
t.Logf("got error: %+v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user