blob: 4abb691e08d228a41d971cd9f2cff188e04bb71b [file]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package flagmisc
import (
"flag"
"testing"
)
func TestFlagImplementations(t *testing.T) {
// Sanity check that these types do indeed implement flag.Value. This would
// otherwise give a compile-time error.
var _ flag.Value = (*StringsValue)(nil)
var _ flag.Value = (*StringMapValue)(nil)
}
func assertEqual(t *testing.T, a, b string) {
if a != b {
t.Errorf("expected the following to be the same:\n%q\n%q\n", a, b)
}
}
func setAndCompare(t *testing.T, f flag.Value, val, expected string) {
if err := f.Set(val); err != nil {
t.Fatalf("could not set flag value %q on %v", val, f)
}
assertEqual(t, expected, f.String())
}
func TestStringsValue(t *testing.T) {
rs := &StringsValue{}
assertEqual(t, rs.String(), "")
setAndCompare(t, rs, "a", "a")
setAndCompare(t, rs, "b", "a, b")
setAndCompare(t, rs, "c", "a, b, c")
}
func TestStringMapValue(t *testing.T) {
mf := &StringMapValue{}
assertEqual(t, mf.String(), "map[]")
*mf = nil
setAndCompare(t, mf, "a=b", "map[a:b]")
setAndCompare(t, mf, "c=d", "map[a:b c:d]")
setAndCompare(t, mf, "e=f", "map[a:b c:d e:f]")
}