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

     1  // Copyright 2024 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 results
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"go.chromium.org/luci/common/testing/typed"
    22  )
    23  
    24  // Check that calling NewResultBuilder does something remotely reasonable.
    25  func TestNewResultBuilderSmokeTest(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	res := NewResultBuilder().Result()
    29  	if diff := typed.Diff(res, nil); diff != "" {
    30  		t.Errorf("unexpected diff (-want +got): %s", diff)
    31  	}
    32  }
    33  
    34  // TestNewResultBuilder tests using a ResultBuilder to build a result.
    35  func TestNewResultBuilder(t *testing.T) {
    36  	t.Parallel()
    37  
    38  	cases := []struct {
    39  		name     string
    40  		expected *Result
    41  		actual   *Result
    42  	}{
    43  		{
    44  			name:     "equal",
    45  			expected: NewResultBuilder().SetName("equal").Result(),
    46  			actual: &Result{
    47  				failed: true,
    48  				header: resultHeader{
    49  					comparison: "equal",
    50  				},
    51  			},
    52  		},
    53  		{
    54  			name:     "equal[int]",
    55  			expected: NewResultBuilder().SetName("equal", reflect.TypeOf(0)).Result(),
    56  			actual: &Result{
    57  				failed: true,
    58  				header: resultHeader{
    59  					comparison: "equal",
    60  					types:      []string{"int"},
    61  				},
    62  			},
    63  		},
    64  	}
    65  
    66  	for _, tt := range cases {
    67  		tt := tt
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			t.Parallel()
    70  			if diff := typed.Diff(tt.expected, tt.actual); diff != "" {
    71  				t.Errorf("unexpected diff (-want +got): %s", diff)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  // TestBecause tests setting the because field of a Result.
    78  func TestBecause(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	cases := []struct {
    82  		name   string
    83  		format string
    84  		args   []interface{}
    85  		result *Result
    86  	}{
    87  		{
    88  			name:   "because",
    89  			format: "%s",
    90  			args:   []interface{}{7},
    91  			result: &Result{
    92  				failed: true,
    93  				values: []value{{
    94  					name:  "Because",
    95  					value: 7,
    96  				}},
    97  			},
    98  		},
    99  	}
   100  
   101  	for _, tt := range cases {
   102  		tt := tt
   103  		t.Run(tt.name, func(t *testing.T) {
   104  			t.Parallel()
   105  			expected := tt.result
   106  			actual := NewResultBuilder().Because(tt.format, tt.args...).Result()
   107  			if diff := typed.Diff(expected, actual); diff != "" {
   108  				t.Errorf("unexpected diff (-want +got): %s", diff)
   109  			}
   110  		})
   111  	}
   112  }