Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from aouyang1/master
Override slices if WithOverride is specified
- Loading branch information
Showing
2 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package mergo | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type Student struct { | ||
Name string | ||
Books []string | ||
} | ||
|
||
var testData = []struct { | ||
S1 Student | ||
S2 Student | ||
ExpectedSlice []string | ||
}{ | ||
{Student{"Jack", []string{"a", "B"}}, Student{"Tom", []string{"1"}}, []string{"a", "B"}}, | ||
{Student{"Jack", []string{"a", "B"}}, Student{"Tom", []string{}}, []string{"a", "B"}}, | ||
{Student{"Jack", []string{}}, Student{"Tom", []string{"1"}}, []string{"1"}}, | ||
{Student{"Jack", []string{}}, Student{"Tom", []string{}}, []string{}}, | ||
} | ||
|
||
func TestIssue64MergeSliceWithOverride(t *testing.T) { | ||
for _, data := range testData { | ||
err := Merge(&data.S2, data.S1, WithOverride) | ||
if err != nil { | ||
t.Errorf("Error while merging %s", err) | ||
} | ||
if len(data.S2.Books) != len(data.ExpectedSlice) { | ||
t.Fatalf("Got %d elements in slice, but expected %d", len(data.S2.Books), len(data.ExpectedSlice)) | ||
} | ||
for i, val := range data.S2.Books { | ||
if val != data.ExpectedSlice[i] { | ||
t.Fatalf("Expected %s, but got %s while merging slice with override", data.ExpectedSlice[i], val) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters