You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/command/flags/flag_map_value_test.go

158 lines
2.7 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
package flags
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestFlagMapValueSet(t *testing.T) {
t.Parallel()
t.Run("missing =", func(t *testing.T) {
f := new(FlagMapValue)
if err := f.Set("foo"); err == nil {
t.Fatal("expected error, got nil")
}
})
t.Run("sets", func(t *testing.T) {
f := new(FlagMapValue)
if err := f.Set("foo=bar"); err != nil {
t.Fatal(err)
}
r, ok := (*f)["foo"]
if !ok {
t.Errorf("missing value: %#v", f)
}
if exp := "bar"; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
})
t.Run("sets multiple", func(t *testing.T) {
f := new(FlagMapValue)
r := map[string]string{
"foo": "bar",
"zip": "zap",
"cat": "dog",
}
for k, v := range r {
if err := f.Set(fmt.Sprintf("%s=%s", k, v)); err != nil {
t.Fatal(err)
}
}
for k, v := range r {
r, ok := (*f)[k]
if !ok {
t.Errorf("missing value %q: %#v", k, f)
}
if exp := v; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
}
})
t.Run("overwrites", func(t *testing.T) {
f := new(FlagMapValue)
if err := f.Set("foo=bar"); err != nil {
t.Fatal(err)
}
if err := f.Set("foo=zip"); err != nil {
t.Fatal(err)
}
r, ok := (*f)["foo"]
if !ok {
t.Errorf("missing value: %#v", f)
}
if exp := "zip"; r != exp {
t.Errorf("expected %q to be %q", r, exp)
}
})
}
func TestFlagMapValueMerge(t *testing.T) {
cases := map[string]struct {
src FlagMapValue
dst map[string]string
exp map[string]string
}{
"empty source and destination": {},
"empty source": {
dst: map[string]string{
"key": "val",
},
exp: map[string]string{
"key": "val",
},
},
"empty destination": {
src: map[string]string{
"key": "val",
},
dst: make(map[string]string),
exp: map[string]string{
"key": "val",
},
},
"non-overlapping keys": {
src: map[string]string{
"key1": "val1",
},
dst: map[string]string{
"key2": "val2",
},
exp: map[string]string{
"key1": "val1",
"key2": "val2",
},
},
"overlapping keys": {
src: map[string]string{
"key1": "val1",
},
dst: map[string]string{
"key1": "val2",
},
exp: map[string]string{
"key1": "val2",
},
},
"multiple keys": {
src: map[string]string{
"key1": "val1",
"key2": "val2",
},
dst: map[string]string{
"key1": "val2",
"key3": "val3",
},
exp: map[string]string{
"key1": "val2",
"key2": "val2",
"key3": "val3",
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
c.src.Merge(c.dst)
require.Equal(t, c.exp, c.dst)
})
}
}