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
|
// Copyright (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package util
import (
"reflect"
"testing"
"github.com/mongodb/mongo-tools/common/testtype"
. "github.com/smartystreets/goconvey/convey"
)
func TestMaxInt(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
Convey("When finding the maximum of two ints", t, func() {
Convey("the larger int should be returned", func() {
So(MaxInt(1, 2), ShouldEqual, 2)
So(MaxInt(2, 1), ShouldEqual, 2)
})
})
}
func TestNumberConverter(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
Convey("With a number converter for float32", t, func() {
floatConverter := newNumberConverter(reflect.TypeOf(float32(0)))
Convey("numeric values should be convertable", func() {
out, err := floatConverter(21)
So(err, ShouldEqual, nil)
So(out, ShouldEqual, 21.0)
out, err = floatConverter(uint64(21))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, 21.0)
out, err = floatConverter(float64(27.52))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, 27.52)
})
Convey("non-numeric values should fail", func() {
_, err := floatConverter("I AM A STRING")
So(err, ShouldNotBeNil)
_, err = floatConverter(struct{ int }{12})
So(err, ShouldNotBeNil)
_, err = floatConverter(nil)
So(err, ShouldNotBeNil)
})
})
}
func TestUInt32Converter(t *testing.T) {
testtype.SkipUnlessTestType(t, testtype.UnitTestType)
Convey("With a series of test values, conversions should pass", t, func() {
out, err := ToUInt32(int64(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
out, err = ToUInt32(int32(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
out, err = ToUInt32(float32(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
out, err = ToUInt32(float64(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
out, err = ToUInt32(uint64(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
out, err = ToUInt32(uint32(99))
So(err, ShouldEqual, nil)
So(out, ShouldEqual, uint32(99))
Convey("but non-numeric inputs will fail", func() {
_, err = ToUInt32(nil)
So(err, ShouldNotBeNil)
_, err = ToUInt32("string")
So(err, ShouldNotBeNil)
_, err = ToUInt32([]byte{1, 2, 3, 4})
So(err, ShouldNotBeNil)
})
})
}
|