...

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

Documentation: github.com/stretchr/testify/assert

     1  package assert
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  func TestIsIncreasing(t *testing.T) {
     9  	mockT := new(testing.T)
    10  
    11  	if !IsIncreasing(mockT, []int{1, 2}) {
    12  		t.Error("IsIncreasing should return true")
    13  	}
    14  
    15  	if !IsIncreasing(mockT, []int{1, 2, 3, 4, 5}) {
    16  		t.Error("IsIncreasing should return true")
    17  	}
    18  
    19  	if IsIncreasing(mockT, []int{1, 1}) {
    20  		t.Error("IsIncreasing should return false")
    21  	}
    22  
    23  	if IsIncreasing(mockT, []int{2, 1}) {
    24  		t.Error("IsIncreasing should return false")
    25  	}
    26  
    27  	// Check error report
    28  	for _, currCase := range []struct {
    29  		collection interface{}
    30  		msg        string
    31  	}{
    32  		{collection: []string{"b", "a"}, msg: `"b" is not less than "a"`},
    33  		{collection: []int{2, 1}, msg: `"2" is not less than "1"`},
    34  		{collection: []int{2, 1, 3, 4, 5, 6, 7}, msg: `"2" is not less than "1"`},
    35  		{collection: []int{-1, 0, 2, 1}, msg: `"2" is not less than "1"`},
    36  		{collection: []int8{2, 1}, msg: `"2" is not less than "1"`},
    37  		{collection: []int16{2, 1}, msg: `"2" is not less than "1"`},
    38  		{collection: []int32{2, 1}, msg: `"2" is not less than "1"`},
    39  		{collection: []int64{2, 1}, msg: `"2" is not less than "1"`},
    40  		{collection: []uint8{2, 1}, msg: `"2" is not less than "1"`},
    41  		{collection: []uint16{2, 1}, msg: `"2" is not less than "1"`},
    42  		{collection: []uint32{2, 1}, msg: `"2" is not less than "1"`},
    43  		{collection: []uint64{2, 1}, msg: `"2" is not less than "1"`},
    44  		{collection: []float32{2.34, 1.23}, msg: `"2.34" is not less than "1.23"`},
    45  		{collection: []float64{2.34, 1.23}, msg: `"2.34" is not less than "1.23"`},
    46  	} {
    47  		out := &outputT{buf: bytes.NewBuffer(nil)}
    48  		False(t, IsIncreasing(out, currCase.collection))
    49  		Contains(t, out.buf.String(), currCase.msg)
    50  	}
    51  }
    52  
    53  func TestIsNonIncreasing(t *testing.T) {
    54  	mockT := new(testing.T)
    55  
    56  	if !IsNonIncreasing(mockT, []int{2, 1}) {
    57  		t.Error("IsNonIncreasing should return true")
    58  	}
    59  
    60  	if !IsNonIncreasing(mockT, []int{5, 4, 4, 3, 2, 1}) {
    61  		t.Error("IsNonIncreasing should return true")
    62  	}
    63  
    64  	if !IsNonIncreasing(mockT, []int{1, 1}) {
    65  		t.Error("IsNonIncreasing should return true")
    66  	}
    67  
    68  	if IsNonIncreasing(mockT, []int{1, 2}) {
    69  		t.Error("IsNonIncreasing should return false")
    70  	}
    71  
    72  	// Check error report
    73  	for _, currCase := range []struct {
    74  		collection interface{}
    75  		msg        string
    76  	}{
    77  		{collection: []string{"a", "b"}, msg: `"a" is not greater than or equal to "b"`},
    78  		{collection: []int{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    79  		{collection: []int{1, 2, 7, 6, 5, 4, 3}, msg: `"1" is not greater than or equal to "2"`},
    80  		{collection: []int{5, 4, 3, 1, 2}, msg: `"1" is not greater than or equal to "2"`},
    81  		{collection: []int8{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    82  		{collection: []int16{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    83  		{collection: []int32{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    84  		{collection: []int64{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    85  		{collection: []uint8{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    86  		{collection: []uint16{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    87  		{collection: []uint32{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    88  		{collection: []uint64{1, 2}, msg: `"1" is not greater than or equal to "2"`},
    89  		{collection: []float32{1.23, 2.34}, msg: `"1.23" is not greater than or equal to "2.34"`},
    90  		{collection: []float64{1.23, 2.34}, msg: `"1.23" is not greater than or equal to "2.34"`},
    91  	} {
    92  		out := &outputT{buf: bytes.NewBuffer(nil)}
    93  		False(t, IsNonIncreasing(out, currCase.collection))
    94  		Contains(t, out.buf.String(), currCase.msg)
    95  	}
    96  }
    97  
    98  func TestIsDecreasing(t *testing.T) {
    99  	mockT := new(testing.T)
   100  
   101  	if !IsDecreasing(mockT, []int{2, 1}) {
   102  		t.Error("IsDecreasing should return true")
   103  	}
   104  
   105  	if !IsDecreasing(mockT, []int{5, 4, 3, 2, 1}) {
   106  		t.Error("IsDecreasing should return true")
   107  	}
   108  
   109  	if IsDecreasing(mockT, []int{1, 1}) {
   110  		t.Error("IsDecreasing should return false")
   111  	}
   112  
   113  	if IsDecreasing(mockT, []int{1, 2}) {
   114  		t.Error("IsDecreasing should return false")
   115  	}
   116  
   117  	// Check error report
   118  	for _, currCase := range []struct {
   119  		collection interface{}
   120  		msg        string
   121  	}{
   122  		{collection: []string{"a", "b"}, msg: `"a" is not greater than "b"`},
   123  		{collection: []int{1, 2}, msg: `"1" is not greater than "2"`},
   124  		{collection: []int{1, 2, 7, 6, 5, 4, 3}, msg: `"1" is not greater than "2"`},
   125  		{collection: []int{5, 4, 3, 1, 2}, msg: `"1" is not greater than "2"`},
   126  		{collection: []int8{1, 2}, msg: `"1" is not greater than "2"`},
   127  		{collection: []int16{1, 2}, msg: `"1" is not greater than "2"`},
   128  		{collection: []int32{1, 2}, msg: `"1" is not greater than "2"`},
   129  		{collection: []int64{1, 2}, msg: `"1" is not greater than "2"`},
   130  		{collection: []uint8{1, 2}, msg: `"1" is not greater than "2"`},
   131  		{collection: []uint16{1, 2}, msg: `"1" is not greater than "2"`},
   132  		{collection: []uint32{1, 2}, msg: `"1" is not greater than "2"`},
   133  		{collection: []uint64{1, 2}, msg: `"1" is not greater than "2"`},
   134  		{collection: []float32{1.23, 2.34}, msg: `"1.23" is not greater than "2.34"`},
   135  		{collection: []float64{1.23, 2.34}, msg: `"1.23" is not greater than "2.34"`},
   136  	} {
   137  		out := &outputT{buf: bytes.NewBuffer(nil)}
   138  		False(t, IsDecreasing(out, currCase.collection))
   139  		Contains(t, out.buf.String(), currCase.msg)
   140  	}
   141  }
   142  
   143  func TestIsNonDecreasing(t *testing.T) {
   144  	mockT := new(testing.T)
   145  
   146  	if !IsNonDecreasing(mockT, []int{1, 2}) {
   147  		t.Error("IsNonDecreasing should return true")
   148  	}
   149  
   150  	if !IsNonDecreasing(mockT, []int{1, 1, 2, 3, 4, 5}) {
   151  		t.Error("IsNonDecreasing should return true")
   152  	}
   153  
   154  	if !IsNonDecreasing(mockT, []int{1, 1}) {
   155  		t.Error("IsNonDecreasing should return false")
   156  	}
   157  
   158  	if IsNonDecreasing(mockT, []int{2, 1}) {
   159  		t.Error("IsNonDecreasing should return false")
   160  	}
   161  
   162  	// Check error report
   163  	for _, currCase := range []struct {
   164  		collection interface{}
   165  		msg        string
   166  	}{
   167  		{collection: []string{"b", "a"}, msg: `"b" is not less than or equal to "a"`},
   168  		{collection: []int{2, 1}, msg: `"2" is not less than or equal to "1"`},
   169  		{collection: []int{2, 1, 3, 4, 5, 6, 7}, msg: `"2" is not less than or equal to "1"`},
   170  		{collection: []int{-1, 0, 2, 1}, msg: `"2" is not less than or equal to "1"`},
   171  		{collection: []int8{2, 1}, msg: `"2" is not less than or equal to "1"`},
   172  		{collection: []int16{2, 1}, msg: `"2" is not less than or equal to "1"`},
   173  		{collection: []int32{2, 1}, msg: `"2" is not less than or equal to "1"`},
   174  		{collection: []int64{2, 1}, msg: `"2" is not less than or equal to "1"`},
   175  		{collection: []uint8{2, 1}, msg: `"2" is not less than or equal to "1"`},
   176  		{collection: []uint16{2, 1}, msg: `"2" is not less than or equal to "1"`},
   177  		{collection: []uint32{2, 1}, msg: `"2" is not less than or equal to "1"`},
   178  		{collection: []uint64{2, 1}, msg: `"2" is not less than or equal to "1"`},
   179  		{collection: []float32{2.34, 1.23}, msg: `"2.34" is not less than or equal to "1.23"`},
   180  		{collection: []float64{2.34, 1.23}, msg: `"2.34" is not less than or equal to "1.23"`},
   181  	} {
   182  		out := &outputT{buf: bytes.NewBuffer(nil)}
   183  		False(t, IsNonDecreasing(out, currCase.collection))
   184  		Contains(t, out.buf.String(), currCase.msg)
   185  	}
   186  }
   187  
   188  func TestOrderingMsgAndArgsForwarding(t *testing.T) {
   189  	msgAndArgs := []interface{}{"format %s %x", "this", 0xc001}
   190  	expectedOutput := "format this c001\n"
   191  	collection := []int{1, 2, 1}
   192  	funcs := []func(t TestingT){
   193  		func(t TestingT) { IsIncreasing(t, collection, msgAndArgs...) },
   194  		func(t TestingT) { IsNonIncreasing(t, collection, msgAndArgs...) },
   195  		func(t TestingT) { IsDecreasing(t, collection, msgAndArgs...) },
   196  		func(t TestingT) { IsNonDecreasing(t, collection, msgAndArgs...) },
   197  	}
   198  	for _, f := range funcs {
   199  		out := &outputT{buf: bytes.NewBuffer(nil)}
   200  		f(out)
   201  		Contains(t, out.buf.String(), expectedOutput)
   202  	}
   203  }
   204  

View as plain text