github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/internal/assert/assert_test.go (about)

     1  // Copyright 2024 Google LLC
     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 assert_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/google/yamlfmt/internal/assert"
    22  )
    23  
    24  type tMock struct {
    25  	logs   []string
    26  	failed bool
    27  	err    error
    28  }
    29  
    30  func newTMock() *tMock {
    31  	return &tMock{
    32  		logs: []string{},
    33  	}
    34  }
    35  
    36  func (t *tMock) Helper() {}
    37  
    38  func (t *tMock) Fatal(...any) {
    39  	t.failed = true
    40  }
    41  
    42  func (t *tMock) Fatalf(msg string, args ...any) {
    43  	t.logs = append(t.logs, fmt.Sprintf(msg, args...))
    44  	t.Fatal()
    45  }
    46  
    47  func (t *tMock) Errorf(msg string, args ...any) {
    48  	t.failed = true
    49  	t.err = fmt.Errorf(msg, args...)
    50  }
    51  
    52  func TestAssertFail(t *testing.T) {
    53  	testInstance := newTMock()
    54  	failMsg := "expected %d to equal %d"
    55  	a := 1
    56  	b := 2
    57  	assert.Assert(testInstance, a == b, failMsg, a, b)
    58  	if !testInstance.failed {
    59  		t.Fatalf("Assert failed. %v", *testInstance)
    60  	}
    61  	if len(testInstance.logs) != 1 {
    62  		t.Fatalf("Found %d logs. %v", len(testInstance.logs), testInstance.logs)
    63  	}
    64  	expectedFailLog := fmt.Sprintf(failMsg, a, b)
    65  	if testInstance.logs[0] != expectedFailLog {
    66  		t.Fatalf(
    67  			"Failure log didn't match.\nexpected: %s\ngot: %s",
    68  			expectedFailLog,
    69  			testInstance.logs[0],
    70  		)
    71  	}
    72  }
    73  
    74  func TestEqualFail(t *testing.T) {
    75  	testInstance := newTMock()
    76  	failMsg := "expected %v to equal %v"
    77  	expected := 1
    78  	got := 2
    79  	assert.EqualMsg(testInstance, expected, got, failMsg)
    80  	if len(testInstance.logs) != 1 {
    81  		t.Fatalf("Found %d logs. %v", len(testInstance.logs), testInstance.logs)
    82  	}
    83  	expectedFailLog := fmt.Sprintf(failMsg, expected, got)
    84  	if testInstance.logs[0] != expectedFailLog {
    85  		t.Fatalf(
    86  			"Failure log didn't match.\nexpected: %s\ngot: %s",
    87  			expectedFailLog,
    88  			testInstance.logs[0],
    89  		)
    90  	}
    91  }
    92  
    93  func TestDereferenceEqualErr(t *testing.T) {
    94  	testInstance := newTMock()
    95  	expected := &struct{}{}
    96  	errMsg := "nil pointer %v %v"
    97  	assert.DereferenceEqualMsg(testInstance, expected, nil, errMsg, "does not matter")
    98  	if testInstance.err == nil {
    99  		t.Fatalf("DereferenceEqual should have failed")
   100  	}
   101  	expectedErr := fmt.Errorf(errMsg, expected, nil)
   102  	if testInstance.err.Error() != expectedErr.Error() {
   103  		t.Fatalf(
   104  			"Errors didn't match.\nexpected: %s\ngot: %s",
   105  			expectedErr,
   106  			testInstance.err,
   107  		)
   108  	}
   109  }
   110  
   111  func TestDerefenceEqualFail(t *testing.T) {
   112  	testInstance := newTMock()
   113  	type x struct {
   114  		num int
   115  	}
   116  	failMsg := "%v not equal %v"
   117  	expected := &x{num: 1}
   118  	got := &x{num: 2}
   119  	assert.DereferenceEqualMsg(testInstance, expected, got, "does not matter", failMsg)
   120  	if len(testInstance.logs) != 1 {
   121  		t.Fatalf("Found %d logs. %v", len(testInstance.logs), testInstance.logs)
   122  	}
   123  	expectedFailLog := fmt.Sprintf(failMsg, *expected, *got)
   124  	if testInstance.logs[0] != expectedFailLog {
   125  		t.Fatalf(
   126  			"Failure log didn't match.\nexpected: %s\ngot: %s",
   127  			expectedFailLog,
   128  			testInstance.logs[0],
   129  		)
   130  	}
   131  }
   132  
   133  func TestDereferenceEqualPass(t *testing.T) {
   134  	testInstance := newTMock()
   135  	type x struct {
   136  		num int
   137  	}
   138  	expected := &x{num: 1}
   139  	got := &x{num: 1}
   140  	assert.DereferenceEqualMsg(testInstance, expected, got, "doesn't matter", "doesn't matter")
   141  	if testInstance.failed {
   142  		t.Fatalf("test failed when it should have passed")
   143  	}
   144  	if len(testInstance.logs) != 0 {
   145  		t.Fatalf("test instance had logs when it shouldn't: %v", testInstance.logs)
   146  	}
   147  }
   148  
   149  func TestSliceEqualFailDiffSize(t *testing.T) {
   150  	testInstance := newTMock()
   151  	failSizeMsg := "%v and %v"
   152  	expected := []int{1, 2, 3, 4}
   153  	got := []int{1, 2, 3}
   154  	assert.SliceEqualMsg(testInstance, expected, got, failSizeMsg, "something else")
   155  	if len(testInstance.logs) != 1 {
   156  		t.Fatalf("Found %d logs. %v", len(testInstance.logs), testInstance.logs)
   157  	}
   158  	expectedFailLog := fmt.Sprintf(failSizeMsg, len(expected), len(got))
   159  	if testInstance.logs[0] != expectedFailLog {
   160  		t.Fatalf(
   161  			"Failure log didn't match.\nexpected: %s\ngot: %s",
   162  			expectedFailLog,
   163  			testInstance.logs[0],
   164  		)
   165  	}
   166  }
   167  
   168  func TestSliceEqualMismatch(t *testing.T) {
   169  	testInstance := newTMock()
   170  	failMismatchMsg := "at index %v: %v and %v"
   171  	expected := []int{1, 2, 4}
   172  	got := []int{1, 2, 3}
   173  	assert.SliceEqualMsg(testInstance, expected, got, "something else", failMismatchMsg)
   174  	if len(testInstance.logs) != 1 {
   175  		t.Fatalf("Found %d logs. %v", len(testInstance.logs), testInstance.logs)
   176  	}
   177  	expectedFailLog := fmt.Sprintf(failMismatchMsg, 2, expected[2], got[2])
   178  	if testInstance.logs[0] != expectedFailLog {
   179  		t.Fatalf(
   180  			"Failure log didn't match.\nexpected: %s\ngot: %s",
   181  			expectedFailLog,
   182  			testInstance.logs[0],
   183  		)
   184  	}
   185  }