...

Source file src/github.com/stretchr/testify/assert/forward_assertions_test.go

Documentation: github.com/stretchr/testify/assert

     1  package assert
     2  
     3  import (
     4  	"errors"
     5  	"regexp"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestImplementsWrapper(t *testing.T) {
    11  	assert := New(new(testing.T))
    12  
    13  	if !assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {
    14  		t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")
    15  	}
    16  	if assert.Implements((*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {
    17  		t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")
    18  	}
    19  }
    20  
    21  func TestIsTypeWrapper(t *testing.T) {
    22  	assert := New(new(testing.T))
    23  
    24  	if !assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {
    25  		t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")
    26  	}
    27  	if assert.IsType(new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {
    28  		t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")
    29  	}
    30  
    31  }
    32  
    33  func TestEqualWrapper(t *testing.T) {
    34  	assert := New(new(testing.T))
    35  
    36  	if !assert.Equal("Hello World", "Hello World") {
    37  		t.Error("Equal should return true")
    38  	}
    39  	if !assert.Equal(123, 123) {
    40  		t.Error("Equal should return true")
    41  	}
    42  	if !assert.Equal(123.5, 123.5) {
    43  		t.Error("Equal should return true")
    44  	}
    45  	if !assert.Equal([]byte("Hello World"), []byte("Hello World")) {
    46  		t.Error("Equal should return true")
    47  	}
    48  	if !assert.Equal(nil, nil) {
    49  		t.Error("Equal should return true")
    50  	}
    51  }
    52  
    53  func TestEqualValuesWrapper(t *testing.T) {
    54  	assert := New(new(testing.T))
    55  
    56  	if !assert.EqualValues(uint32(10), int32(10)) {
    57  		t.Error("EqualValues should return true")
    58  	}
    59  }
    60  
    61  func TestNotNilWrapper(t *testing.T) {
    62  	assert := New(new(testing.T))
    63  
    64  	if !assert.NotNil(new(AssertionTesterConformingObject)) {
    65  		t.Error("NotNil should return true: object is not nil")
    66  	}
    67  	if assert.NotNil(nil) {
    68  		t.Error("NotNil should return false: object is nil")
    69  	}
    70  
    71  }
    72  
    73  func TestNilWrapper(t *testing.T) {
    74  	assert := New(new(testing.T))
    75  
    76  	if !assert.Nil(nil) {
    77  		t.Error("Nil should return true: object is nil")
    78  	}
    79  	if assert.Nil(new(AssertionTesterConformingObject)) {
    80  		t.Error("Nil should return false: object is not nil")
    81  	}
    82  
    83  }
    84  
    85  func TestTrueWrapper(t *testing.T) {
    86  	assert := New(new(testing.T))
    87  
    88  	if !assert.True(true) {
    89  		t.Error("True should return true")
    90  	}
    91  	if assert.True(false) {
    92  		t.Error("True should return false")
    93  	}
    94  
    95  }
    96  
    97  func TestFalseWrapper(t *testing.T) {
    98  	assert := New(new(testing.T))
    99  
   100  	if !assert.False(false) {
   101  		t.Error("False should return true")
   102  	}
   103  	if assert.False(true) {
   104  		t.Error("False should return false")
   105  	}
   106  
   107  }
   108  
   109  func TestExactlyWrapper(t *testing.T) {
   110  	assert := New(new(testing.T))
   111  
   112  	a := float32(1)
   113  	b := float64(1)
   114  	c := float32(1)
   115  	d := float32(2)
   116  
   117  	if assert.Exactly(a, b) {
   118  		t.Error("Exactly should return false")
   119  	}
   120  	if assert.Exactly(a, d) {
   121  		t.Error("Exactly should return false")
   122  	}
   123  	if !assert.Exactly(a, c) {
   124  		t.Error("Exactly should return true")
   125  	}
   126  
   127  	if assert.Exactly(nil, a) {
   128  		t.Error("Exactly should return false")
   129  	}
   130  	if assert.Exactly(a, nil) {
   131  		t.Error("Exactly should return false")
   132  	}
   133  
   134  }
   135  
   136  func TestNotEqualWrapper(t *testing.T) {
   137  
   138  	assert := New(new(testing.T))
   139  
   140  	if !assert.NotEqual("Hello World", "Hello World!") {
   141  		t.Error("NotEqual should return true")
   142  	}
   143  	if !assert.NotEqual(123, 1234) {
   144  		t.Error("NotEqual should return true")
   145  	}
   146  	if !assert.NotEqual(123.5, 123.55) {
   147  		t.Error("NotEqual should return true")
   148  	}
   149  	if !assert.NotEqual([]byte("Hello World"), []byte("Hello World!")) {
   150  		t.Error("NotEqual should return true")
   151  	}
   152  	if !assert.NotEqual(nil, new(AssertionTesterConformingObject)) {
   153  		t.Error("NotEqual should return true")
   154  	}
   155  }
   156  
   157  func TestNotEqualValuesWrapper(t *testing.T) {
   158  
   159  	assert := New(new(testing.T))
   160  
   161  	if !assert.NotEqualValues("Hello World", "Hello World!") {
   162  		t.Error("NotEqualValues should return true")
   163  	}
   164  	if !assert.NotEqualValues(123, 1234) {
   165  		t.Error("NotEqualValues should return true")
   166  	}
   167  	if !assert.NotEqualValues(123.5, 123.55) {
   168  		t.Error("NotEqualValues should return true")
   169  	}
   170  	if !assert.NotEqualValues([]byte("Hello World"), []byte("Hello World!")) {
   171  		t.Error("NotEqualValues should return true")
   172  	}
   173  	if !assert.NotEqualValues(nil, new(AssertionTesterConformingObject)) {
   174  		t.Error("NotEqualValues should return true")
   175  	}
   176  	if assert.NotEqualValues(10, uint(10)) {
   177  		t.Error("NotEqualValues should return false")
   178  	}
   179  }
   180  
   181  func TestContainsWrapper(t *testing.T) {
   182  
   183  	assert := New(new(testing.T))
   184  	list := []string{"Foo", "Bar"}
   185  
   186  	if !assert.Contains("Hello World", "Hello") {
   187  		t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")
   188  	}
   189  	if assert.Contains("Hello World", "Salut") {
   190  		t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")
   191  	}
   192  
   193  	if !assert.Contains(list, "Foo") {
   194  		t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
   195  	}
   196  	if assert.Contains(list, "Salut") {
   197  		t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")
   198  	}
   199  
   200  }
   201  
   202  func TestNotContainsWrapper(t *testing.T) {
   203  
   204  	assert := New(new(testing.T))
   205  	list := []string{"Foo", "Bar"}
   206  
   207  	if !assert.NotContains("Hello World", "Hello!") {
   208  		t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")
   209  	}
   210  	if assert.NotContains("Hello World", "Hello") {
   211  		t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")
   212  	}
   213  
   214  	if !assert.NotContains(list, "Foo!") {
   215  		t.Error("NotContains should return true: \"[\"Foo\", \"Bar\"]\" does not contain \"Foo!\"")
   216  	}
   217  	if assert.NotContains(list, "Foo") {
   218  		t.Error("NotContains should return false: \"[\"Foo\", \"Bar\"]\" contains \"Foo\"")
   219  	}
   220  
   221  }
   222  
   223  func TestConditionWrapper(t *testing.T) {
   224  
   225  	assert := New(new(testing.T))
   226  
   227  	if !assert.Condition(func() bool { return true }, "Truth") {
   228  		t.Error("Condition should return true")
   229  	}
   230  
   231  	if assert.Condition(func() bool { return false }, "Lie") {
   232  		t.Error("Condition should return false")
   233  	}
   234  
   235  }
   236  
   237  func TestDidPanicWrapper(t *testing.T) {
   238  
   239  	if funcDidPanic, _, _ := didPanic(func() {
   240  		panic("Panic!")
   241  	}); !funcDidPanic {
   242  		t.Error("didPanic should return true")
   243  	}
   244  
   245  	if funcDidPanic, _, _ := didPanic(func() {
   246  	}); funcDidPanic {
   247  		t.Error("didPanic should return false")
   248  	}
   249  
   250  }
   251  
   252  func TestPanicsWrapper(t *testing.T) {
   253  
   254  	assert := New(new(testing.T))
   255  
   256  	if !assert.Panics(func() {
   257  		panic("Panic!")
   258  	}) {
   259  		t.Error("Panics should return true")
   260  	}
   261  
   262  	if assert.Panics(func() {
   263  	}) {
   264  		t.Error("Panics should return false")
   265  	}
   266  
   267  }
   268  
   269  func TestNotPanicsWrapper(t *testing.T) {
   270  
   271  	assert := New(new(testing.T))
   272  
   273  	if !assert.NotPanics(func() {
   274  	}) {
   275  		t.Error("NotPanics should return true")
   276  	}
   277  
   278  	if assert.NotPanics(func() {
   279  		panic("Panic!")
   280  	}) {
   281  		t.Error("NotPanics should return false")
   282  	}
   283  
   284  }
   285  
   286  func TestNoErrorWrapper(t *testing.T) {
   287  	assert := New(t)
   288  	mockAssert := New(new(testing.T))
   289  
   290  	// start with a nil error
   291  	var err error
   292  
   293  	assert.True(mockAssert.NoError(err), "NoError should return True for nil arg")
   294  
   295  	// now set an error
   296  	err = errors.New("Some error")
   297  
   298  	assert.False(mockAssert.NoError(err), "NoError with error should return False")
   299  
   300  }
   301  
   302  func TestErrorWrapper(t *testing.T) {
   303  	assert := New(t)
   304  	mockAssert := New(new(testing.T))
   305  
   306  	// start with a nil error
   307  	var err error
   308  
   309  	assert.False(mockAssert.Error(err), "Error should return False for nil arg")
   310  
   311  	// now set an error
   312  	err = errors.New("Some error")
   313  
   314  	assert.True(mockAssert.Error(err), "Error with error should return True")
   315  
   316  }
   317  
   318  func TestErrorContainsWrapper(t *testing.T) {
   319  	assert := New(t)
   320  	mockAssert := New(new(testing.T))
   321  
   322  	// start with a nil error
   323  	var err error
   324  	assert.False(mockAssert.ErrorContains(err, ""),
   325  		"ErrorContains should return false for nil arg")
   326  
   327  	// now set an error
   328  	err = errors.New("some error: another error")
   329  	assert.False(mockAssert.ErrorContains(err, "different error"),
   330  		"ErrorContains should return false for different error string")
   331  	assert.True(mockAssert.ErrorContains(err, "some error"),
   332  		"ErrorContains should return true")
   333  	assert.True(mockAssert.ErrorContains(err, "another error"),
   334  		"ErrorContains should return true")
   335  }
   336  
   337  func TestEqualErrorWrapper(t *testing.T) {
   338  	assert := New(t)
   339  	mockAssert := New(new(testing.T))
   340  
   341  	// start with a nil error
   342  	var err error
   343  	assert.False(mockAssert.EqualError(err, ""),
   344  		"EqualError should return false for nil arg")
   345  
   346  	// now set an error
   347  	err = errors.New("some error")
   348  	assert.False(mockAssert.EqualError(err, "Not some error"),
   349  		"EqualError should return false for different error string")
   350  	assert.True(mockAssert.EqualError(err, "some error"),
   351  		"EqualError should return true")
   352  }
   353  
   354  func TestEmptyWrapper(t *testing.T) {
   355  	assert := New(t)
   356  	mockAssert := New(new(testing.T))
   357  
   358  	assert.True(mockAssert.Empty(""), "Empty string is empty")
   359  	assert.True(mockAssert.Empty(nil), "Nil is empty")
   360  	assert.True(mockAssert.Empty([]string{}), "Empty string array is empty")
   361  	assert.True(mockAssert.Empty(0), "Zero int value is empty")
   362  	assert.True(mockAssert.Empty(false), "False value is empty")
   363  
   364  	assert.False(mockAssert.Empty("something"), "Non Empty string is not empty")
   365  	assert.False(mockAssert.Empty(errors.New("something")), "Non nil object is not empty")
   366  	assert.False(mockAssert.Empty([]string{"something"}), "Non empty string array is not empty")
   367  	assert.False(mockAssert.Empty(1), "Non-zero int value is not empty")
   368  	assert.False(mockAssert.Empty(true), "True value is not empty")
   369  
   370  }
   371  
   372  func TestNotEmptyWrapper(t *testing.T) {
   373  	assert := New(t)
   374  	mockAssert := New(new(testing.T))
   375  
   376  	assert.False(mockAssert.NotEmpty(""), "Empty string is empty")
   377  	assert.False(mockAssert.NotEmpty(nil), "Nil is empty")
   378  	assert.False(mockAssert.NotEmpty([]string{}), "Empty string array is empty")
   379  	assert.False(mockAssert.NotEmpty(0), "Zero int value is empty")
   380  	assert.False(mockAssert.NotEmpty(false), "False value is empty")
   381  
   382  	assert.True(mockAssert.NotEmpty("something"), "Non Empty string is not empty")
   383  	assert.True(mockAssert.NotEmpty(errors.New("something")), "Non nil object is not empty")
   384  	assert.True(mockAssert.NotEmpty([]string{"something"}), "Non empty string array is not empty")
   385  	assert.True(mockAssert.NotEmpty(1), "Non-zero int value is not empty")
   386  	assert.True(mockAssert.NotEmpty(true), "True value is not empty")
   387  
   388  }
   389  
   390  func TestLenWrapper(t *testing.T) {
   391  	assert := New(t)
   392  	mockAssert := New(new(testing.T))
   393  
   394  	assert.False(mockAssert.Len(nil, 0), "nil does not have length")
   395  	assert.False(mockAssert.Len(0, 0), "int does not have length")
   396  	assert.False(mockAssert.Len(true, 0), "true does not have length")
   397  	assert.False(mockAssert.Len(false, 0), "false does not have length")
   398  	assert.False(mockAssert.Len('A', 0), "Rune does not have length")
   399  	assert.False(mockAssert.Len(struct{}{}, 0), "Struct does not have length")
   400  
   401  	ch := make(chan int, 5)
   402  	ch <- 1
   403  	ch <- 2
   404  	ch <- 3
   405  
   406  	cases := []struct {
   407  		v interface{}
   408  		l int
   409  	}{
   410  		{[]int{1, 2, 3}, 3},
   411  		{[...]int{1, 2, 3}, 3},
   412  		{"ABC", 3},
   413  		{map[int]int{1: 2, 2: 4, 3: 6}, 3},
   414  		{ch, 3},
   415  
   416  		{[]int{}, 0},
   417  		{map[int]int{}, 0},
   418  		{make(chan int), 0},
   419  
   420  		{[]int(nil), 0},
   421  		{map[int]int(nil), 0},
   422  		{(chan int)(nil), 0},
   423  	}
   424  
   425  	for _, c := range cases {
   426  		assert.True(mockAssert.Len(c.v, c.l), "%#v have %d items", c.v, c.l)
   427  	}
   428  }
   429  
   430  func TestWithinDurationWrapper(t *testing.T) {
   431  	assert := New(t)
   432  	mockAssert := New(new(testing.T))
   433  	a := time.Now()
   434  	b := a.Add(10 * time.Second)
   435  
   436  	assert.True(mockAssert.WithinDuration(a, b, 10*time.Second), "A 10s difference is within a 10s time difference")
   437  	assert.True(mockAssert.WithinDuration(b, a, 10*time.Second), "A 10s difference is within a 10s time difference")
   438  
   439  	assert.False(mockAssert.WithinDuration(a, b, 9*time.Second), "A 10s difference is not within a 9s time difference")
   440  	assert.False(mockAssert.WithinDuration(b, a, 9*time.Second), "A 10s difference is not within a 9s time difference")
   441  
   442  	assert.False(mockAssert.WithinDuration(a, b, -9*time.Second), "A 10s difference is not within a 9s time difference")
   443  	assert.False(mockAssert.WithinDuration(b, a, -9*time.Second), "A 10s difference is not within a 9s time difference")
   444  
   445  	assert.False(mockAssert.WithinDuration(a, b, -11*time.Second), "A 10s difference is not within a 9s time difference")
   446  	assert.False(mockAssert.WithinDuration(b, a, -11*time.Second), "A 10s difference is not within a 9s time difference")
   447  }
   448  
   449  func TestInDeltaWrapper(t *testing.T) {
   450  	assert := New(new(testing.T))
   451  
   452  	True(t, assert.InDelta(1.001, 1, 0.01), "|1.001 - 1| <= 0.01")
   453  	True(t, assert.InDelta(1, 1.001, 0.01), "|1 - 1.001| <= 0.01")
   454  	True(t, assert.InDelta(1, 2, 1), "|1 - 2| <= 1")
   455  	False(t, assert.InDelta(1, 2, 0.5), "Expected |1 - 2| <= 0.5 to fail")
   456  	False(t, assert.InDelta(2, 1, 0.5), "Expected |2 - 1| <= 0.5 to fail")
   457  	False(t, assert.InDelta("", nil, 1), "Expected non numerals to fail")
   458  
   459  	cases := []struct {
   460  		a, b  interface{}
   461  		delta float64
   462  	}{
   463  		{uint8(2), uint8(1), 1},
   464  		{uint16(2), uint16(1), 1},
   465  		{uint32(2), uint32(1), 1},
   466  		{uint64(2), uint64(1), 1},
   467  
   468  		{int(2), int(1), 1},
   469  		{int8(2), int8(1), 1},
   470  		{int16(2), int16(1), 1},
   471  		{int32(2), int32(1), 1},
   472  		{int64(2), int64(1), 1},
   473  
   474  		{float32(2), float32(1), 1},
   475  		{float64(2), float64(1), 1},
   476  	}
   477  
   478  	for _, tc := range cases {
   479  		True(t, assert.InDelta(tc.a, tc.b, tc.delta), "Expected |%V - %V| <= %v", tc.a, tc.b, tc.delta)
   480  	}
   481  }
   482  
   483  func TestInEpsilonWrapper(t *testing.T) {
   484  	assert := New(new(testing.T))
   485  
   486  	cases := []struct {
   487  		a, b    interface{}
   488  		epsilon float64
   489  	}{
   490  		{uint8(2), uint16(2), .001},
   491  		{2.1, 2.2, 0.1},
   492  		{2.2, 2.1, 0.1},
   493  		{-2.1, -2.2, 0.1},
   494  		{-2.2, -2.1, 0.1},
   495  		{uint64(100), uint8(101), 0.01},
   496  		{0.1, -0.1, 2},
   497  	}
   498  
   499  	for _, tc := range cases {
   500  		True(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
   501  	}
   502  
   503  	cases = []struct {
   504  		a, b    interface{}
   505  		epsilon float64
   506  	}{
   507  		{uint8(2), int16(-2), .001},
   508  		{uint64(100), uint8(102), 0.01},
   509  		{2.1, 2.2, 0.001},
   510  		{2.2, 2.1, 0.001},
   511  		{2.1, -2.2, 1},
   512  		{2.1, "bla-bla", 0},
   513  		{0.1, -0.1, 1.99},
   514  	}
   515  
   516  	for _, tc := range cases {
   517  		False(t, assert.InEpsilon(tc.a, tc.b, tc.epsilon, "Expected %V and %V to have a relative difference of %v", tc.a, tc.b, tc.epsilon))
   518  	}
   519  }
   520  
   521  func TestRegexpWrapper(t *testing.T) {
   522  
   523  	assert := New(new(testing.T))
   524  
   525  	cases := []struct {
   526  		rx, str string
   527  	}{
   528  		{"^start", "start of the line"},
   529  		{"end$", "in the end"},
   530  		{"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12.34"},
   531  	}
   532  
   533  	for _, tc := range cases {
   534  		True(t, assert.Regexp(tc.rx, tc.str))
   535  		True(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
   536  		False(t, assert.NotRegexp(tc.rx, tc.str))
   537  		False(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
   538  	}
   539  
   540  	cases = []struct {
   541  		rx, str string
   542  	}{
   543  		{"^asdfastart", "Not the start of the line"},
   544  		{"end$", "in the end."},
   545  		{"[0-9]{3}[.-]?[0-9]{2}[.-]?[0-9]{2}", "My phone number is 650.12a.34"},
   546  	}
   547  
   548  	for _, tc := range cases {
   549  		False(t, assert.Regexp(tc.rx, tc.str), "Expected \"%s\" to not match \"%s\"", tc.rx, tc.str)
   550  		False(t, assert.Regexp(regexp.MustCompile(tc.rx), tc.str))
   551  		True(t, assert.NotRegexp(tc.rx, tc.str))
   552  		True(t, assert.NotRegexp(regexp.MustCompile(tc.rx), tc.str))
   553  	}
   554  }
   555  
   556  func TestZeroWrapper(t *testing.T) {
   557  	assert := New(t)
   558  	mockAssert := New(new(testing.T))
   559  
   560  	for _, test := range zeros {
   561  		assert.True(mockAssert.Zero(test), "Zero should return true for %v", test)
   562  	}
   563  
   564  	for _, test := range nonZeros {
   565  		assert.False(mockAssert.Zero(test), "Zero should return false for %v", test)
   566  	}
   567  }
   568  
   569  func TestNotZeroWrapper(t *testing.T) {
   570  	assert := New(t)
   571  	mockAssert := New(new(testing.T))
   572  
   573  	for _, test := range zeros {
   574  		assert.False(mockAssert.NotZero(test), "Zero should return true for %v", test)
   575  	}
   576  
   577  	for _, test := range nonZeros {
   578  		assert.True(mockAssert.NotZero(test), "Zero should return false for %v", test)
   579  	}
   580  }
   581  
   582  func TestJSONEqWrapper_EqualSONString(t *testing.T) {
   583  	assert := New(new(testing.T))
   584  	if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) {
   585  		t.Error("JSONEq should return true")
   586  	}
   587  
   588  }
   589  
   590  func TestJSONEqWrapper_EquivalentButNotEqual(t *testing.T) {
   591  	assert := New(new(testing.T))
   592  	if !assert.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
   593  		t.Error("JSONEq should return true")
   594  	}
   595  
   596  }
   597  
   598  func TestJSONEqWrapper_HashOfArraysAndHashes(t *testing.T) {
   599  	assert := New(new(testing.T))
   600  	if !assert.JSONEq("{\r\n\t\"numeric\": 1.5,\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]],\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\"\r\n}",
   601  		"{\r\n\t\"numeric\": 1.5,\r\n\t\"hash\": {\"nested\": \"hash\", \"nested_slice\": [\"this\", \"is\", \"nested\"]},\r\n\t\"string\": \"foo\",\r\n\t\"array\": [{\"foo\": \"bar\"}, 1, \"string\", [\"nested\", \"array\", 5.5]]\r\n}") {
   602  		t.Error("JSONEq should return true")
   603  	}
   604  }
   605  
   606  func TestJSONEqWrapper_Array(t *testing.T) {
   607  	assert := New(new(testing.T))
   608  	if !assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) {
   609  		t.Error("JSONEq should return true")
   610  	}
   611  
   612  }
   613  
   614  func TestJSONEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
   615  	assert := New(new(testing.T))
   616  	if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) {
   617  		t.Error("JSONEq should return false")
   618  	}
   619  }
   620  
   621  func TestJSONEqWrapper_HashesNotEquivalent(t *testing.T) {
   622  	assert := New(new(testing.T))
   623  	if assert.JSONEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
   624  		t.Error("JSONEq should return false")
   625  	}
   626  }
   627  
   628  func TestJSONEqWrapper_ActualIsNotJSON(t *testing.T) {
   629  	assert := New(new(testing.T))
   630  	if assert.JSONEq(`{"foo": "bar"}`, "Not JSON") {
   631  		t.Error("JSONEq should return false")
   632  	}
   633  }
   634  
   635  func TestJSONEqWrapper_ExpectedIsNotJSON(t *testing.T) {
   636  	assert := New(new(testing.T))
   637  	if assert.JSONEq("Not JSON", `{"foo": "bar", "hello": "world"}`) {
   638  		t.Error("JSONEq should return false")
   639  	}
   640  }
   641  
   642  func TestJSONEqWrapper_ExpectedAndActualNotJSON(t *testing.T) {
   643  	assert := New(new(testing.T))
   644  	if assert.JSONEq("Not JSON", "Not JSON") {
   645  		t.Error("JSONEq should return false")
   646  	}
   647  }
   648  
   649  func TestJSONEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
   650  	assert := New(new(testing.T))
   651  	if assert.JSONEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) {
   652  		t.Error("JSONEq should return false")
   653  	}
   654  }
   655  
   656  func TestYAMLEqWrapper_EqualYAMLString(t *testing.T) {
   657  	assert := New(new(testing.T))
   658  	if !assert.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"hello": "world", "foo": "bar"}`) {
   659  		t.Error("YAMLEq should return true")
   660  	}
   661  
   662  }
   663  
   664  func TestYAMLEqWrapper_EquivalentButNotEqual(t *testing.T) {
   665  	assert := New(new(testing.T))
   666  	if !assert.YAMLEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
   667  		t.Error("YAMLEq should return true")
   668  	}
   669  
   670  }
   671  
   672  func TestYAMLEqWrapper_HashOfArraysAndHashes(t *testing.T) {
   673  	assert := New(new(testing.T))
   674  	expected := `
   675  numeric: 1.5
   676  array:
   677    - foo: bar
   678    - 1
   679    - "string"
   680    - ["nested", "array", 5.5]
   681  hash:
   682    nested: hash
   683    nested_slice: [this, is, nested]
   684  string: "foo"
   685  `
   686  
   687  	actual := `
   688  numeric: 1.5
   689  hash:
   690    nested: hash
   691    nested_slice: [this, is, nested]
   692  string: "foo"
   693  array:
   694    - foo: bar
   695    - 1
   696    - "string"
   697    - ["nested", "array", 5.5]
   698  `
   699  	if !assert.YAMLEq(expected, actual) {
   700  		t.Error("YAMLEq should return true")
   701  	}
   702  }
   703  
   704  func TestYAMLEqWrapper_Array(t *testing.T) {
   705  	assert := New(new(testing.T))
   706  	if !assert.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `["foo", {"nested": "hash", "hello": "world"}]`) {
   707  		t.Error("YAMLEq should return true")
   708  	}
   709  
   710  }
   711  
   712  func TestYAMLEqWrapper_HashAndArrayNotEquivalent(t *testing.T) {
   713  	assert := New(new(testing.T))
   714  	if assert.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `{"foo": "bar", {"nested": "hash", "hello": "world"}}`) {
   715  		t.Error("YAMLEq should return false")
   716  	}
   717  }
   718  
   719  func TestYAMLEqWrapper_HashesNotEquivalent(t *testing.T) {
   720  	assert := New(new(testing.T))
   721  	if assert.YAMLEq(`{"foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) {
   722  		t.Error("YAMLEq should return false")
   723  	}
   724  }
   725  
   726  func TestYAMLEqWrapper_ActualIsSimpleString(t *testing.T) {
   727  	assert := New(new(testing.T))
   728  	if assert.YAMLEq(`{"foo": "bar"}`, "Simple String") {
   729  		t.Error("YAMLEq should return false")
   730  	}
   731  }
   732  
   733  func TestYAMLEqWrapper_ExpectedIsSimpleString(t *testing.T) {
   734  	assert := New(new(testing.T))
   735  	if assert.YAMLEq("Simple String", `{"foo": "bar", "hello": "world"}`) {
   736  		t.Error("YAMLEq should return false")
   737  	}
   738  }
   739  
   740  func TestYAMLEqWrapper_ExpectedAndActualSimpleString(t *testing.T) {
   741  	assert := New(new(testing.T))
   742  	if !assert.YAMLEq("Simple String", "Simple String") {
   743  		t.Error("YAMLEq should return true")
   744  	}
   745  }
   746  
   747  func TestYAMLEqWrapper_ArraysOfDifferentOrder(t *testing.T) {
   748  	assert := New(new(testing.T))
   749  	if assert.YAMLEq(`["foo", {"hello": "world", "nested": "hash"}]`, `[{ "hello": "world", "nested": "hash"}, "foo"]`) {
   750  		t.Error("YAMLEq should return false")
   751  	}
   752  }
   753  

View as plain text