Package list golang-github-imdario-mergo / 47853799-ea8e-48db-ae84-6f8bdbd2f952/main issue89_test.go
47853799-ea8e-48db-ae84-6f8bdbd2f952/main

Tree @47853799-ea8e-48db-ae84-6f8bdbd2f952/main (Download .tar.gz)

issue89_test.go @47853799-ea8e-48db-ae84-6f8bdbd2f952/mainraw · history · blame

package mergo_test

import (
	"testing"

	"github.com/imdario/mergo"
)

func TestIssue89Boolean(t *testing.T) {
	type Foo struct {
		Bar bool `json:"bar"`
	}

	src := Foo{Bar: true}
	dst := Foo{Bar: false}

	if err := mergo.Merge(&dst, src); err != nil {
		t.Error(err)
	}
	if dst.Bar == false {
		t.Errorf("expected true, got false")
	}
}

func TestIssue89MergeWithEmptyValue(t *testing.T) {
	p1 := map[string]interface{}{
		"A": 3, "B": "note", "C": true,
	}
	p2 := map[string]interface{}{
		"B": "", "C": false,
	}
	if err := mergo.Merge(&p1, p2, mergo.WithOverwriteWithEmptyValue); err != nil {
		t.Error(err)
	}
	testCases := []struct {
		key      string
		expected interface{}
	}{
		{
			"A",
			3,
		},
		{
			"B",
			"",
		},
		{
			"C",
			false,
		},
	}
	for _, tC := range testCases {
		if p1[tC.key] != tC.expected {
			t.Errorf("expected %v in p1[%q], got %v", tC.expected, tC.key, p1[tC.key])
		}
	}
}