go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/testing/assert/assert_test.go (about)

     1  // Copyright 2023 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  package assert
    15  
    16  import (
    17  	"testing"
    18  
    19  	"go.chromium.org/luci/common/testing/assert/results"
    20  	"go.chromium.org/luci/common/testing/assert/testsupport"
    21  	"go.chromium.org/luci/common/testing/typed"
    22  )
    23  
    24  func TestCheck(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	isEmpty := func(x string) *results.Result {
    28  		if x == "" {
    29  			return nil
    30  		}
    31  		return results.NewResultBuilder().SetName("string not empty").Result()
    32  	}
    33  
    34  	cases := []struct {
    35  		name    string
    36  		input   string
    37  		compare results.Comparison[string]
    38  		ok      bool
    39  	}{
    40  		{
    41  			name:    "empty string",
    42  			input:   "",
    43  			compare: isEmpty,
    44  			ok:      true,
    45  		},
    46  		{
    47  			name:    "empty string",
    48  			input:   "a",
    49  			compare: isEmpty,
    50  			ok:      false,
    51  		},
    52  	}
    53  
    54  	for _, tt := range cases {
    55  		tt := tt
    56  		t.Run(tt.name, func(t *testing.T) {
    57  			t.Parallel()
    58  
    59  			got := Check(testsupport.ZeroTB{}, tt.input, tt.compare)
    60  
    61  			if diff := typed.Diff(got, tt.ok); diff != "" {
    62  				t.Errorf("unexpected diff (-want +got): %s", diff)
    63  			}
    64  		})
    65  	}
    66  }