Skip to content

Commit

Permalink
tests for #84
Browse files Browse the repository at this point in the history
  • Loading branch information
requilence committed Jun 15, 2018
1 parent d581347 commit f01cd5a
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions issue84_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package mergo

import (
"testing"
)

type DstStructIssue84 struct {
A int
B int
C int
}

type DstNestedStructIssue84 struct {
A struct {
A int
B int
C int
}
B int
C int
}

func TestIssue84MergeMapWithNilValueToStructWithOverride(t *testing.T) {
p1 := DstStructIssue84{
A: 0, B: 1, C: 2,
}
p2 := map[string]interface{}{
"A": 3, "B": 4, "C": 0,
}
if err := Map(&p1, p2, WithOverride); err != nil {
t.Fatalf("Error during the merge: %v", err)
}
if p1.C != 0 {
t.Error("C field should become '0'")
}
}

func TestIssue84MergeMapWithoutKeyExistsToStructWithOverride(t *testing.T) {
p1 := DstStructIssue84{
A: 0, B: 1, C: 2,
}
p2 := map[string]interface{}{
"A": 3, "B": 4,
}
if err := Map(&p1, p2, WithOverride); err != nil {
t.Fatalf("Error during the merge: %v", err)
}
if p1.C != 2 {
t.Error("C field should be '2'")
}
}

func TestIssue84MergeNestedMapWithNilValueToStructWithOverride(t *testing.T) {
p1 := DstNestedStructIssue84{
A: struct {
A int
B int
C int
}{A: 1, B: 2, C: 0},
B: 0,
C: 2,
}
p2 := map[string]interface{}{
"A": map[string]interface{}{
"A": 0, "B": 0, "C": 5,
}, "B": 4, "C": 0,
}
if err := Map(&p1, p2, WithOverride); err != nil {
t.Fatalf("Error during the merge: %v", err)
}
if p1.B != 4 {
t.Error("A.C field should become '4'")
}

if p1.A.C != 5 {
t.Error("A.C field should become '5'")
}

if p1.A.B != 0 || p1.A.A != 0 {
t.Error("A.A and A.B field should become '0'")
}
}

0 comments on commit f01cd5a

Please sign in to comment.