go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/protowalk/attributes_test.go (about)

     1  // Copyright 2022 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 protowalk
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestRecurseAttr(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	Convey(`RecurseAttr`, t, func() {
    27  		Convey(`isMap`, func() {
    28  			So(recurseNone.isMap(), ShouldBeFalse)
    29  			So(recurseOne.isMap(), ShouldBeFalse)
    30  			So(recurseRepeated.isMap(), ShouldBeFalse)
    31  
    32  			So(recurseMapKind.isMap(), ShouldBeTrue)
    33  			So(recurseMapBool.isMap(), ShouldBeTrue)
    34  			So(recurseMapInt.isMap(), ShouldBeTrue)
    35  			So(recurseMapUint.isMap(), ShouldBeTrue)
    36  			So(recurseMapString.isMap(), ShouldBeTrue)
    37  		})
    38  
    39  		Convey(`set`, func() {
    40  			r := recurseNone
    41  
    42  			Convey(`OK`, func() {
    43  				So(r.set(recurseOne), ShouldBeTrue)
    44  				So(r, ShouldEqual, recurseOne)
    45  				So(r.set(recurseOne), ShouldBeTrue)
    46  				So(r, ShouldEqual, recurseOne)
    47  			})
    48  
    49  			Convey(`bad`, func() {
    50  				So(r.set(recurseOne), ShouldBeTrue)
    51  				So(r, ShouldEqual, recurseOne)
    52  				So(func() { r.set(recurseRepeated) }, ShouldPanic)
    53  			})
    54  		})
    55  	})
    56  
    57  }
    58  
    59  func TestProcessAttr(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	Convey(`ProcessAttr`, t, func() {
    63  		Convey(`applies`, func() {
    64  			So(ProcessNever.applies(false), ShouldBeFalse)
    65  			So(ProcessNever.applies(true), ShouldBeFalse)
    66  
    67  			So(ProcessIfSet.applies(false), ShouldBeFalse)
    68  			So(ProcessIfSet.applies(true), ShouldBeTrue)
    69  
    70  			So(ProcessIfUnset.applies(false), ShouldBeTrue)
    71  			So(ProcessIfUnset.applies(true), ShouldBeFalse)
    72  
    73  			So(ProcessAlways.applies(false), ShouldBeTrue)
    74  			So(ProcessAlways.applies(true), ShouldBeTrue)
    75  		})
    76  
    77  		Convey(`Valid`, func() {
    78  			So(ProcessNever.Valid(), ShouldBeTrue)
    79  			So(ProcessIfSet.Valid(), ShouldBeTrue)
    80  			So(ProcessIfUnset.Valid(), ShouldBeTrue)
    81  			So(ProcessAlways.Valid(), ShouldBeTrue)
    82  
    83  			So(ProcessAttr(217).Valid(), ShouldBeFalse)
    84  		})
    85  	})
    86  
    87  }