1 package mergo_test
2
3 import (
4 "testing"
5
6 "github.com/imdario/mergo"
7 )
8
9 type foz struct {
10 A *bool
11 B string
12 C *bool
13 D *bool
14 E *bool
15 }
16
17 func TestIssue131MergeWithOverwriteWithEmptyValue(t *testing.T) {
18 src := foz{
19 A: func(v bool) *bool { return &v }(false),
20 B: "src",
21 }
22 dest := foz{
23 A: func(v bool) *bool { return &v }(true),
24 B: "dest",
25 }
26 if err := mergo.Merge(&dest, src, mergo.WithOverwriteWithEmptyValue); err != nil {
27 t.Error(err)
28 }
29 if *src.A != *dest.A {
30 t.Errorf("dest.A not merged in properly: %v != %v", *src.A, *dest.A)
31 }
32 if src.B != dest.B {
33 t.Errorf("dest.B not merged in properly: %v != %v", src.B, dest.B)
34 }
35 }
36
37 func TestIssue131MergeWithoutDereferenceWithOverride(t *testing.T) {
38 src := foz{
39 A: func(v bool) *bool { return &v }(false),
40 B: "src",
41 C: nil,
42 D: func(v bool) *bool { return &v }(false),
43 E: func(v bool) *bool { return &v }(true),
44 }
45 dest := foz{
46 A: func(v bool) *bool { return &v }(true),
47 B: "dest",
48 C: func(v bool) *bool { return &v }(false),
49 D: nil,
50 E: func(v bool) *bool { return &v }(false),
51 }
52 if err := mergo.Merge(&dest, src, mergo.WithoutDereference, mergo.WithOverride); err != nil {
53 t.Error(err)
54 }
55 if *src.A != *dest.A {
56 t.Errorf("dest.A not merged in properly: %v != %v", *src.A, *dest.A)
57 }
58 if src.B != dest.B {
59 t.Errorf("dest.B not merged in properly: %v != %v", src.B, dest.B)
60 }
61 if *dest.C != false {
62 t.Errorf("dest.C not merged in properly: %v != %v", *src.C, *dest.C)
63 }
64 if *dest.D != false {
65 t.Errorf("dest.D not merged in properly: %v != %v", src.D, *dest.D)
66 }
67 if *dest.E != true {
68 t.Errorf("dest.E not merged in properly: %v != %v", *src.E, *dest.E)
69 }
70 }
71
72 func TestIssue131MergeWithoutDereference(t *testing.T) {
73 src := foz{
74 A: func(v bool) *bool { return &v }(false),
75 B: "src",
76 C: nil,
77 D: func(v bool) *bool { return &v }(false),
78 E: func(v bool) *bool { return &v }(true),
79 }
80 dest := foz{
81 A: func(v bool) *bool { return &v }(true),
82 B: "dest",
83 C: func(v bool) *bool { return &v }(false),
84 D: nil,
85 E: func(v bool) *bool { return &v }(false),
86 }
87 if err := mergo.Merge(&dest, src, mergo.WithoutDereference); err != nil {
88 t.Error(err)
89 }
90 if *src.A == *dest.A {
91 t.Errorf("dest.A should not have been merged: %v == %v", *src.A, *dest.A)
92 }
93 if src.B == dest.B {
94 t.Errorf("dest.B should not have been merged: %v == %v", src.B, dest.B)
95 }
96 if *dest.C != false {
97 t.Errorf("dest.C not merged in properly: %v != %v", *src.C, *dest.C)
98 }
99 if *dest.D != false {
100 t.Errorf("dest.D not merged in properly: %v != %v", src.D, *dest.D)
101 }
102 if *dest.E == true {
103 t.Errorf("dest.Eshould not have been merged: %v == %v", *src.E, *dest.E)
104 }
105 }
106
View as plain text