go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/flagpb/marshal_test.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package flagpb
    16  
    17  import (
    18  	"strings"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func msg(keysValues ...any) map[string]any {
    25  	m := make(map[string]any, len(keysValues)/2)
    26  	for i := 0; i < len(keysValues); i += 2 {
    27  		m[keysValues[i].(string)] = keysValues[i+1]
    28  	}
    29  	return m
    30  }
    31  
    32  func repeated(a ...any) []any {
    33  	return a
    34  }
    35  
    36  func TestMarshal(t *testing.T) {
    37  	t.Parallel()
    38  
    39  	Convey("Marshal", t, func() {
    40  		test := func(m map[string]any, flags ...string) {
    41  			Convey(strings.Join(flags, " "), func() {
    42  				actualFlags, err := MarshalUntyped(m)
    43  				So(err, ShouldBeNil)
    44  				So(actualFlags, ShouldResemble, flags)
    45  			})
    46  		}
    47  
    48  		test(nil)
    49  		test(
    50  			msg("x", 1),
    51  			"-x", "1")
    52  		test(
    53  			msg("x", "a b"),
    54  			"-x", "a b")
    55  		test(
    56  			msg("x", repeated(1, 2)),
    57  			"-x", "1", "-x", "2")
    58  		test(
    59  			msg("b", true),
    60  			"-b")
    61  		test(
    62  			msg("b", false),
    63  			"-b=false")
    64  		test(
    65  			msg("m", msg("x", 1)),
    66  			"-m.x", "1")
    67  		test(
    68  			msg(
    69  				"m", repeated(msg("x", 1), msg("x", 2)),
    70  			),
    71  			"-m.x", "1", "-m", "-m.x", "2")
    72  	})
    73  }