1// Copyright (C) MongoDB, Inc. 2014-present. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); you may 4// not use this file except in compliance with the License. You may obtain 5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 6 7package util 8 9import ( 10 "reflect" 11 "testing" 12 13 "github.com/mongodb/mongo-tools/legacy/testtype" 14 . "github.com/smartystreets/goconvey/convey" 15) 16 17func TestMaxInt(t *testing.T) { 18 19 testtype.SkipUnlessTestType(t, testtype.UnitTestType) 20 21 Convey("When finding the maximum of two ints", t, func() { 22 23 Convey("the larger int should be returned", func() { 24 25 So(MaxInt(1, 2), ShouldEqual, 2) 26 So(MaxInt(2, 1), ShouldEqual, 2) 27 28 }) 29 30 }) 31} 32 33func TestNumberConverter(t *testing.T) { 34 35 testtype.SkipUnlessTestType(t, testtype.UnitTestType) 36 37 Convey("With a number converter for float32", t, func() { 38 floatConverter := newNumberConverter(reflect.TypeOf(float32(0))) 39 40 Convey("numeric values should be convertable", func() { 41 out, err := floatConverter(21) 42 So(err, ShouldEqual, nil) 43 So(out, ShouldEqual, 21.0) 44 out, err = floatConverter(uint64(21)) 45 So(err, ShouldEqual, nil) 46 So(out, ShouldEqual, 21.0) 47 out, err = floatConverter(float64(27.52)) 48 So(err, ShouldEqual, nil) 49 So(out, ShouldEqual, 27.52) 50 }) 51 52 Convey("non-numeric values should fail", func() { 53 _, err := floatConverter("I AM A STRING") 54 So(err, ShouldNotBeNil) 55 _, err = floatConverter(struct{ int }{12}) 56 So(err, ShouldNotBeNil) 57 _, err = floatConverter(nil) 58 So(err, ShouldNotBeNil) 59 }) 60 }) 61} 62 63func TestUInt32Converter(t *testing.T) { 64 65 testtype.SkipUnlessTestType(t, testtype.UnitTestType) 66 67 Convey("With a series of test values, conversions should pass", t, func() { 68 out, err := ToUInt32(int64(99)) 69 So(err, ShouldEqual, nil) 70 So(out, ShouldEqual, uint32(99)) 71 out, err = ToUInt32(int32(99)) 72 So(err, ShouldEqual, nil) 73 So(out, ShouldEqual, uint32(99)) 74 out, err = ToUInt32(float32(99)) 75 So(err, ShouldEqual, nil) 76 So(out, ShouldEqual, uint32(99)) 77 out, err = ToUInt32(float64(99)) 78 So(err, ShouldEqual, nil) 79 So(out, ShouldEqual, uint32(99)) 80 out, err = ToUInt32(uint64(99)) 81 So(err, ShouldEqual, nil) 82 So(out, ShouldEqual, uint32(99)) 83 out, err = ToUInt32(uint32(99)) 84 So(err, ShouldEqual, nil) 85 So(out, ShouldEqual, uint32(99)) 86 87 Convey("but non-numeric inputs will fail", func() { 88 _, err = ToUInt32(nil) 89 So(err, ShouldNotBeNil) 90 _, err = ToUInt32("string") 91 So(err, ShouldNotBeNil) 92 _, err = ToUInt32([]byte{1, 2, 3, 4}) 93 So(err, ShouldNotBeNil) 94 }) 95 }) 96} 97