github.com/go-spring/spring-base@v1.1.3/assert/assert_test.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package assert_test
    18  
    19  import (
    20  	"bytes"
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"testing"
    25  
    26  	"github.com/go-spring/spring-base/assert"
    27  	"github.com/golang/mock/gomock"
    28  )
    29  
    30  func runCase(t *testing.T, f func(g *assert.MockT)) {
    31  	ctrl := gomock.NewController(t)
    32  	defer ctrl.Finish()
    33  	g := assert.NewMockT(ctrl)
    34  	g.EXPECT().Helper().AnyTimes()
    35  	f(g)
    36  }
    37  
    38  func TestTrue(t *testing.T) {
    39  	runCase(t, func(g *assert.MockT) {
    40  		assert.True(g, true)
    41  	})
    42  	runCase(t, func(g *assert.MockT) {
    43  		g.EXPECT().Error([]interface{}{"got false but expect true"})
    44  		assert.True(g, false)
    45  	})
    46  	runCase(t, func(g *assert.MockT) {
    47  		g.EXPECT().Error([]interface{}{"got false but expect true; param (index=0)"})
    48  		assert.True(g, false, "param (index=0)")
    49  	})
    50  }
    51  
    52  func TestFalse(t *testing.T) {
    53  	runCase(t, func(g *assert.MockT) {
    54  		assert.False(g, false)
    55  	})
    56  	runCase(t, func(g *assert.MockT) {
    57  		g.EXPECT().Error([]interface{}{"got true but expect false"})
    58  		assert.False(g, true)
    59  	})
    60  	runCase(t, func(g *assert.MockT) {
    61  		g.EXPECT().Error([]interface{}{"got true but expect false; param (index=0)"})
    62  		assert.False(g, true, "param (index=0)")
    63  	})
    64  }
    65  
    66  func TestNil(t *testing.T) {
    67  	runCase(t, func(g *assert.MockT) {
    68  		assert.Nil(g, nil)
    69  	})
    70  	runCase(t, func(g *assert.MockT) {
    71  		assert.Nil(g, (*int)(nil))
    72  	})
    73  	runCase(t, func(g *assert.MockT) {
    74  		var a []string
    75  		assert.Nil(g, a)
    76  	})
    77  	runCase(t, func(g *assert.MockT) {
    78  		var m map[string]string
    79  		assert.Nil(g, m)
    80  	})
    81  	runCase(t, func(g *assert.MockT) {
    82  		g.EXPECT().Error([]interface{}{"got (int) 3 but expect nil"})
    83  		assert.Nil(g, 3)
    84  	})
    85  	runCase(t, func(g *assert.MockT) {
    86  		g.EXPECT().Error([]interface{}{"got (int) 3 but expect nil; param (index=0)"})
    87  		assert.Nil(g, 3, "param (index=0)")
    88  	})
    89  }
    90  
    91  func TestNotNil(t *testing.T) {
    92  	runCase(t, func(g *assert.MockT) {
    93  		assert.NotNil(g, 3)
    94  	})
    95  	runCase(t, func(g *assert.MockT) {
    96  		a := make([]string, 0)
    97  		assert.NotNil(g, a)
    98  	})
    99  	runCase(t, func(g *assert.MockT) {
   100  		m := make(map[string]string)
   101  		assert.NotNil(g, m)
   102  	})
   103  	runCase(t, func(g *assert.MockT) {
   104  		g.EXPECT().Error([]interface{}{"got nil but expect not nil"})
   105  		assert.NotNil(g, nil)
   106  	})
   107  	runCase(t, func(g *assert.MockT) {
   108  		g.EXPECT().Error([]interface{}{"got nil but expect not nil; param (index=0)"})
   109  		assert.NotNil(g, nil, "param (index=0)")
   110  	})
   111  }
   112  
   113  func TestEqual(t *testing.T) {
   114  	runCase(t, func(g *assert.MockT) {
   115  		assert.Equal(g, 0, 0)
   116  	})
   117  	runCase(t, func(g *assert.MockT) {
   118  		assert.Equal(g, []string{"a"}, []string{"a"})
   119  	})
   120  	runCase(t, func(g *assert.MockT) {
   121  		assert.Equal(g, struct {
   122  			text string
   123  		}{text: "a"}, struct {
   124  			text string
   125  		}{text: "a"})
   126  	})
   127  	runCase(t, func(g *assert.MockT) {
   128  		g.EXPECT().Error([]interface{}{"got (struct { Text string }) {a} but expect (struct { Text string \"json:\\\"text\\\"\" }) {a}"})
   129  		assert.Equal(g, struct {
   130  			Text string
   131  		}{Text: "a"}, struct {
   132  			Text string `json:"text"`
   133  		}{Text: "a"})
   134  	})
   135  	runCase(t, func(g *assert.MockT) {
   136  		g.EXPECT().Error([]interface{}{"got (struct { text string }) {a} but expect (struct { msg string }) {a}"})
   137  		assert.Equal(g, struct {
   138  			text string
   139  		}{text: "a"}, struct {
   140  			msg string
   141  		}{msg: "a"})
   142  	})
   143  	runCase(t, func(g *assert.MockT) {
   144  		g.EXPECT().Error([]interface{}{"got (int) 0 but expect (string) 0"})
   145  		assert.Equal(g, 0, "0")
   146  	})
   147  	runCase(t, func(g *assert.MockT) {
   148  		g.EXPECT().Error([]interface{}{"got (int) 0 but expect (string) 0; param (index=0)"})
   149  		assert.Equal(g, 0, "0", "param (index=0)")
   150  	})
   151  }
   152  
   153  func TestNotEqual(t *testing.T) {
   154  	runCase(t, func(g *assert.MockT) {
   155  		assert.NotEqual(g, "0", 0)
   156  	})
   157  	runCase(t, func(g *assert.MockT) {
   158  		g.EXPECT().Error([]interface{}{"got ([]string) [a] but expect not ([]string) [a]"})
   159  		assert.NotEqual(g, []string{"a"}, []string{"a"})
   160  	})
   161  	runCase(t, func(g *assert.MockT) {
   162  		g.EXPECT().Error([]interface{}{"got (string) 0 but expect not (string) 0"})
   163  		assert.NotEqual(g, "0", "0")
   164  	})
   165  	runCase(t, func(g *assert.MockT) {
   166  		g.EXPECT().Error([]interface{}{"got (string) 0 but expect not (string) 0; param (index=0)"})
   167  		assert.NotEqual(g, "0", "0", "param (index=0)")
   168  	})
   169  }
   170  
   171  func TestJsonEqual(t *testing.T) {
   172  	runCase(t, func(g *assert.MockT) {
   173  		assert.JsonEqual(g, `{"a":0,"b":1}`, `{"b":1,"a":0}`)
   174  	})
   175  	runCase(t, func(g *assert.MockT) {
   176  		g.EXPECT().Error([]interface{}{"invalid character 'h' in literal true (expecting 'r')"})
   177  		assert.JsonEqual(g, `this is an error`, `[{"b":1},{"a":0}]`)
   178  	})
   179  	runCase(t, func(g *assert.MockT) {
   180  		g.EXPECT().Error([]interface{}{"invalid character 'h' in literal true (expecting 'r')"})
   181  		assert.JsonEqual(g, `{"a":0,"b":1}`, `this is an error`)
   182  	})
   183  	runCase(t, func(g *assert.MockT) {
   184  		g.EXPECT().Error([]interface{}{"got (string) {\"a\":0,\"b\":1} but expect (string) [{\"b\":1},{\"a\":0}]"})
   185  		assert.JsonEqual(g, `{"a":0,"b":1}`, `[{"b":1},{"a":0}]`)
   186  	})
   187  	runCase(t, func(g *assert.MockT) {
   188  		g.EXPECT().Error([]interface{}{"got (string) {\"a\":0} but expect (string) {\"a\":1}; param (index=0)"})
   189  		assert.JsonEqual(g, `{"a":0}`, `{"a":1}`, "param (index=0)")
   190  	})
   191  }
   192  
   193  func TestSame(t *testing.T) {
   194  	runCase(t, func(g *assert.MockT) {
   195  		assert.Same(g, "0", "0")
   196  	})
   197  	runCase(t, func(g *assert.MockT) {
   198  		g.EXPECT().Error([]interface{}{"got (int) 0 but expect (string) 0"})
   199  		assert.Same(g, 0, "0")
   200  	})
   201  	runCase(t, func(g *assert.MockT) {
   202  		g.EXPECT().Error([]interface{}{"got (int) 0 but expect (string) 0; param (index=0)"})
   203  		assert.Same(g, 0, "0", "param (index=0)")
   204  	})
   205  }
   206  
   207  func TestNotSame(t *testing.T) {
   208  	runCase(t, func(g *assert.MockT) {
   209  		assert.NotSame(g, "0", 0)
   210  	})
   211  	runCase(t, func(g *assert.MockT) {
   212  		g.EXPECT().Error([]interface{}{"expect not (string) 0"})
   213  		assert.NotSame(g, "0", "0")
   214  	})
   215  	runCase(t, func(g *assert.MockT) {
   216  		g.EXPECT().Error([]interface{}{"expect not (string) 0; param (index=0)"})
   217  		assert.NotSame(g, "0", "0", "param (index=0)")
   218  	})
   219  }
   220  
   221  func TestPanic(t *testing.T) {
   222  	runCase(t, func(g *assert.MockT) {
   223  		assert.Panic(g, func() { panic("this is an error") }, "an error")
   224  	})
   225  	runCase(t, func(g *assert.MockT) {
   226  		g.EXPECT().Error([]interface{}{"did not panic"})
   227  		assert.Panic(g, func() {}, "an error")
   228  	})
   229  	runCase(t, func(g *assert.MockT) {
   230  		g.EXPECT().Error([]interface{}{"invalid pattern"})
   231  		assert.Panic(g, func() { panic("this is an error") }, "an error \\")
   232  	})
   233  	runCase(t, func(g *assert.MockT) {
   234  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\""})
   235  		assert.Panic(g, func() { panic("there's no error") }, "an error")
   236  	})
   237  	runCase(t, func(g *assert.MockT) {
   238  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\"; param (index=0)"})
   239  		assert.Panic(g, func() { panic("there's no error") }, "an error", "param (index=0)")
   240  	})
   241  	runCase(t, func(g *assert.MockT) {
   242  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\""})
   243  		assert.Panic(g, func() { panic(errors.New("there's no error")) }, "an error")
   244  	})
   245  	runCase(t, func(g *assert.MockT) {
   246  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\""})
   247  		assert.Panic(g, func() { panic(bytes.NewBufferString("there's no error")) }, "an error")
   248  	})
   249  	runCase(t, func(g *assert.MockT) {
   250  		g.EXPECT().Error([]interface{}{"got \"[there's no error]\" which does not match \"an error\""})
   251  		assert.Panic(g, func() { panic([]string{"there's no error"}) }, "an error")
   252  	})
   253  }
   254  
   255  func TestMatches(t *testing.T) {
   256  	runCase(t, func(g *assert.MockT) {
   257  		assert.Matches(g, "this is an error", "this is an error")
   258  	})
   259  	runCase(t, func(g *assert.MockT) {
   260  		g.EXPECT().Error([]interface{}{"invalid pattern"})
   261  		assert.Matches(g, "this is an error", "an error \\")
   262  	})
   263  	runCase(t, func(g *assert.MockT) {
   264  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\""})
   265  		assert.Matches(g, "there's no error", "an error")
   266  	})
   267  	runCase(t, func(g *assert.MockT) {
   268  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\"; param (index=0)"})
   269  		assert.Matches(g, "there's no error", "an error", "param (index=0)")
   270  	})
   271  }
   272  
   273  func TestError(t *testing.T) {
   274  	runCase(t, func(g *assert.MockT) {
   275  		assert.Error(g, errors.New("this is an error"), "an error")
   276  	})
   277  	runCase(t, func(g *assert.MockT) {
   278  		g.EXPECT().Error([]interface{}{"invalid pattern"})
   279  		assert.Error(g, errors.New("there's no error"), "an error \\")
   280  	})
   281  	runCase(t, func(g *assert.MockT) {
   282  		g.EXPECT().Error([]interface{}{"expect not nil error"})
   283  		assert.Error(g, nil, "an error")
   284  	})
   285  	runCase(t, func(g *assert.MockT) {
   286  		g.EXPECT().Error([]interface{}{"expect not nil error; param (index=0)"})
   287  		assert.Error(g, nil, "an error", "param (index=0)")
   288  	})
   289  	runCase(t, func(g *assert.MockT) {
   290  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\""})
   291  		assert.Error(g, errors.New("there's no error"), "an error")
   292  	})
   293  	runCase(t, func(g *assert.MockT) {
   294  		g.EXPECT().Error([]interface{}{"got \"there's no error\" which does not match \"an error\"; param (index=0)"})
   295  		assert.Error(g, errors.New("there's no error"), "an error", "param (index=0)")
   296  	})
   297  }
   298  
   299  func TestTypeOf(t *testing.T) {
   300  	runCase(t, func(g *assert.MockT) {
   301  		assert.TypeOf(g, new(int), (*int)(nil))
   302  	})
   303  	runCase(t, func(g *assert.MockT) {
   304  		g.EXPECT().Error([]interface{}{"got type (string) but expect type (fmt.Stringer)"})
   305  		assert.TypeOf(g, "string", (*fmt.Stringer)(nil))
   306  	})
   307  }
   308  
   309  func TestImplements(t *testing.T) {
   310  	runCase(t, func(g *assert.MockT) {
   311  		assert.Implements(g, errors.New("error"), (*error)(nil))
   312  	})
   313  	runCase(t, func(g *assert.MockT) {
   314  		g.EXPECT().Error([]interface{}{"expect should be interface"})
   315  		assert.Implements(g, new(int), (*int)(nil))
   316  	})
   317  	runCase(t, func(g *assert.MockT) {
   318  		g.EXPECT().Error([]interface{}{"got type (*int) but expect type (io.Reader)"})
   319  		assert.Implements(g, new(int), (*io.Reader)(nil))
   320  	})
   321  }