1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
package assert
import (
"reflect"
"testing"
)
func TestCompare(t *testing.T) {
for _, currCase := range []struct {
less interface{}
greater interface{}
cType string
}{
{less: "a", greater: "b", cType: "string"},
{less: int(1), greater: int(2), cType: "int"},
{less: int8(1), greater: int8(2), cType: "int8"},
{less: int16(1), greater: int16(2), cType: "int16"},
{less: int32(1), greater: int32(2), cType: "int32"},
{less: int64(1), greater: int64(2), cType: "int64"},
{less: uint8(1), greater: uint8(2), cType: "uint8"},
{less: uint16(1), greater: uint16(2), cType: "uint16"},
{less: uint32(1), greater: uint32(2), cType: "uint32"},
{less: uint64(1), greater: uint64(2), cType: "uint64"},
{less: float32(1), greater: float32(2), cType: "float32"},
{less: float64(1), greater: float64(2), cType: "float64"},
} {
resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
if !isComparable {
t.Error("object should be comparable for type " + currCase.cType)
}
if resLess != 1 {
t.Errorf("object less should be less than greater for type " + currCase.cType)
}
resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
if !isComparable {
t.Error("object are comparable for type " + currCase.cType)
}
if resGreater != -1 {
t.Errorf("object greater should be greater than less for type " + currCase.cType)
}
resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind())
if !isComparable {
t.Error("object are comparable for type " + currCase.cType)
}
if resEqual != 0 {
t.Errorf("objects should be equal for type " + currCase.cType)
}
}
}
func TestGreater(t *testing.T) {
mockT := new(testing.T)
if !Greater(mockT, 2, 1) {
t.Error("Greater should return true")
}
if Greater(mockT, 1, 1) {
t.Error("Greater should return false")
}
if Greater(mockT, 1, 2) {
t.Error("Greater should return false")
}
}
func TestGreaterOrEqual(t *testing.T) {
mockT := new(testing.T)
if !GreaterOrEqual(mockT, 2, 1) {
t.Error("Greater should return true")
}
if !GreaterOrEqual(mockT, 1, 1) {
t.Error("Greater should return true")
}
if GreaterOrEqual(mockT, 1, 2) {
t.Error("Greater should return false")
}
}
func TestLess(t *testing.T) {
mockT := new(testing.T)
if !Less(mockT, 1, 2) {
t.Error("Less should return true")
}
if Less(mockT, 1, 1) {
t.Error("Less should return false")
}
if Less(mockT, 2, 1) {
t.Error("Less should return false")
}
}
func TestLessOrEqual(t *testing.T) {
mockT := new(testing.T)
if !LessOrEqual(mockT, 1, 2) {
t.Error("Greater should return true")
}
if !LessOrEqual(mockT, 1, 1) {
t.Error("Greater should return true")
}
if LessOrEqual(mockT, 2, 1) {
t.Error("Greater should return false")
}
}
|