Skip to content

Commit

Permalink
ADD: config: test json unmarshal into prefilled struct
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisnian committed May 25, 2024
1 parent 0d9a690 commit 23d5bca
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions config/json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package config_test

import (
"encoding/json"
"reflect"
"testing"
"time"
)

type PrefilledSimple struct {
IntEmpty int
IntKeep int
IntOverride int

StringEmpty string
StringKeep string
StringOverride string

DurationEmpty time.Duration
DurationKeep time.Duration
DurationOverride time.Duration

BytesEmpty []byte
BytesKeep []byte
BytesOverride []byte
}

func TestPrefilled_Simple(t *testing.T) {
input := PrefilledSimple{
IntKeep: 10,
IntOverride: 10,
StringKeep: "hello",
StringOverride: "hello",
DurationKeep: time.Second,
DurationOverride: time.Second,
BytesKeep: []byte("_nian_"),
BytesOverride: []byte("_nian_"),
}
data := []byte(`{"IntOverride":20,"StringOverride":"world","DurationOverride":60000000000,"BytesOverride":"X3h4eHhf"}`)
want := PrefilledSimple{
IntKeep: 10,
IntOverride: 20,
StringKeep: "hello",
StringOverride: "world",
DurationKeep: time.Second,
DurationOverride: time.Minute,
BytesKeep: []byte("_nian_"),
BytesOverride: []byte("_xxxx_"),
}

if err := json.Unmarshal(data, &input); err != nil {
t.Fatalf("json.Unmarshal() error: %v", err)
}
if !reflect.DeepEqual(input, want) {
t.Fatalf("json.Unmarshal() result:\n get %+v\n want %+v", input, want)
}
}

type IntBox struct {
IntEmpty int
IntKeep int
IntOverride int
}

type IntBox2 struct {
BoxEmpty IntBox
BoxKeep IntBox
BoxOverride IntBox
}

type PrefilledNested struct {
BoxEmpty IntBox
BoxKeep IntBox
BoxOverride IntBox

Box2Empty IntBox2
Box2Keep IntBox2
Box2Override IntBox2
}

func TestPrefilled_Nested(t *testing.T) {
input := PrefilledNested{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 10},
Box2Keep: IntBox2{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 10},
},
Box2Override: IntBox2{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 10},
},
}
data := []byte(`{"BoxOverride":{"IntOverride":20},"Box2Override":{"BoxOverride":{"IntOverride":20}}}`)
want := PrefilledNested{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 20},
Box2Keep: IntBox2{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 10},
},
Box2Override: IntBox2{
BoxKeep: IntBox{IntKeep: 10, IntOverride: 10},
BoxOverride: IntBox{IntKeep: 10, IntOverride: 20},
},
}

if err := json.Unmarshal(data, &input); err != nil {
t.Fatalf("json.Unmarshal() error: %v", err)
}
if !reflect.DeepEqual(input, want) {
t.Fatalf("json.Unmarshal() result:\n get %+v\n want %+v", input, want)
}
}

0 comments on commit 23d5bca

Please sign in to comment.