go-superstruct

[library] uses reflection to merge structs
git clone https://hhvn.uk/go-superstruct
git clone git://hhvn.uk/go-superstruct
Log | Files | Refs | LICENSE

superstruct_test.go (738B)


      1 package superstruct
      2 
      3 import (
      4 	"strings"
      5 	"testing"
      6 
      7 	_ "embed"
      8 )
      9 
     10 type A struct {
     11 	A string
     12 	C string
     13 }
     14 
     15 type B struct {
     16 	B string
     17 	C string
     18 	D *string
     19 	E interface{}
     20 	F int16
     21 	G uint32
     22 	H float64
     23 	I [5]int
     24 	J []int
     25 	K map[string]int
     26 }
     27 
     28 //go:embed test_output.txt
     29 var expected string
     30 
     31 func Test(t *testing.T) {
     32 	m := make(map[string]int)
     33 	m["l"] = 99
     34 	m["m"] = 98
     35 
     36 	str := "5"
     37 	arr := [5]int{10, 9, 8, 7, 6}
     38 	sl  := []int{3, 2}
     39 	a := A{"1", "2"}
     40 	b := B{"3", "4", &str, "hi", 16, 32, 64.0, arr, sl, m}
     41 
     42 	c, err := CreateAndCopy(a, b)
     43 	if err != nil {
     44 		t.Fatalf("%v", err)
     45 	}
     46 
     47 	expected = strings.TrimSuffix(expected, "\n")
     48 	got := SortedStructString(c)
     49 
     50 	if expected != got {
     51 		t.Fatalf("\nExpected:\n%v\nGot:\n%v", expected, got)
     52 	}
     53 }