github.com/mattn/anko@v0.1.10/core/testdata/toX_test.go (about) 1 package core 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 "time" 8 9 "github.com/mattn/anko/internal/testlib" 10 ) 11 12 func TestToX(t *testing.T) { 13 os.Setenv("ANKO_DEBUG", "1") 14 tests := []testlib.Test{ 15 {Script: `toBool(-2)`, RunOutput: false}, 16 {Script: `toBool(-1.5)`, RunOutput: false}, 17 {Script: `toBool(-1)`, RunOutput: false}, 18 {Script: `toBool(-0.4)`, RunOutput: false}, 19 {Script: `toBool(0)`, RunOutput: false}, 20 {Script: `toBool(0.4)`, RunOutput: true}, 21 {Script: `toBool(1)`, RunOutput: true}, 22 {Script: `toBool(1.5)`, RunOutput: true}, 23 {Script: `toBool(2)`, RunOutput: true}, 24 {Script: `toBool(true)`, RunOutput: true}, 25 {Script: `toBool(false)`, RunOutput: false}, 26 {Script: `toBool("true")`, RunOutput: true}, 27 {Script: `toBool("false")`, RunOutput: false}, 28 {Script: `toBool("yes")`, RunOutput: true}, 29 {Script: `toBool("ye")`, RunOutput: false}, 30 {Script: `toBool("y")`, RunOutput: true}, 31 {Script: `toBool("false")`, RunOutput: false}, 32 {Script: `toBool("f")`, RunOutput: false}, 33 {Script: `toBool("")`, RunOutput: false}, 34 {Script: `toBool(nil)`, RunOutput: false}, 35 {Script: `toBool({})`, RunOutput: false}, 36 {Script: `toBool([])`, RunOutput: false}, 37 {Script: `toBool([true])`, RunOutput: false}, 38 {Script: `toBool({"true": "true"})`, RunOutput: false}, 39 {Script: `toString(nil)`, RunOutput: "<nil>"}, 40 {Script: `toString("")`, RunOutput: ""}, 41 {Script: `toString(1)`, RunOutput: "1"}, 42 {Script: `toString(1.2)`, RunOutput: "1.2"}, 43 {Script: `toString(1/3)`, RunOutput: "0.3333333333333333"}, 44 {Script: `toString(false)`, RunOutput: "false"}, 45 {Script: `toString(true)`, RunOutput: "true"}, 46 {Script: `toString({})`, RunOutput: "map[]"}, 47 {Script: `toString({"foo": "bar"})`, RunOutput: "map[foo:bar]"}, 48 {Script: `toString([true,nil])`, RunOutput: "[true <nil>]"}, 49 {Script: `toString(toByteSlice("foo"))`, RunOutput: "foo"}, 50 {Script: `toInt(nil)`, RunOutput: int64(0)}, 51 {Script: `toInt(-2)`, RunOutput: int64(-2)}, 52 {Script: `toInt(-1.4)`, RunOutput: int64(-1)}, 53 {Script: `toInt(-1)`, RunOutput: int64(-1)}, 54 {Script: `toInt(0)`, RunOutput: int64(0)}, 55 {Script: `toInt(1)`, RunOutput: int64(1)}, 56 {Script: `toInt(1.4)`, RunOutput: int64(1)}, 57 {Script: `toInt(1.5)`, RunOutput: int64(1)}, 58 {Script: `toInt(1.9)`, RunOutput: int64(1)}, 59 {Script: `toInt(2)`, RunOutput: int64(2)}, 60 {Script: `toInt(2.1)`, RunOutput: int64(2)}, 61 {Script: `toInt("2")`, RunOutput: int64(2)}, 62 {Script: `toInt("2.1")`, RunOutput: int64(2)}, 63 {Script: `toInt(true)`, RunOutput: int64(1)}, 64 {Script: `toInt(false)`, RunOutput: int64(0)}, 65 {Script: `toInt({})`, RunOutput: int64(0)}, 66 {Script: `toInt([])`, RunOutput: int64(0)}, 67 {Script: `toFloat(nil)`, RunOutput: float64(0.0)}, 68 {Script: `toFloat(-2)`, RunOutput: float64(-2.0)}, 69 {Script: `toFloat(-1.4)`, RunOutput: float64(-1.4)}, 70 {Script: `toFloat(-1)`, RunOutput: float64(-1.0)}, 71 {Script: `toFloat(0)`, RunOutput: float64(0.0)}, 72 {Script: `toFloat(1)`, RunOutput: float64(1.0)}, 73 {Script: `toFloat(1.4)`, RunOutput: float64(1.4)}, 74 {Script: `toFloat(1.5)`, RunOutput: float64(1.5)}, 75 {Script: `toFloat(1.9)`, RunOutput: float64(1.9)}, 76 {Script: `toFloat(2)`, RunOutput: float64(2.0)}, 77 {Script: `toFloat(2.1)`, RunOutput: float64(2.1)}, 78 {Script: `toFloat("2")`, RunOutput: float64(2.0)}, 79 {Script: `toFloat("2.1")`, RunOutput: float64(2.1)}, 80 {Script: `toFloat(true)`, RunOutput: float64(1.0)}, 81 {Script: `toFloat(false)`, RunOutput: float64(0.0)}, 82 {Script: `toFloat({})`, RunOutput: float64(0.0)}, 83 {Script: `toFloat([])`, RunOutput: float64(0.0)}, 84 {Script: `toChar(0x1F431)`, RunOutput: "π±"}, 85 {Script: `toChar(0)`, RunOutput: "\x00"}, 86 {Script: `toRune("")`, RunOutput: rune(0)}, 87 {Script: `toRune("π±")`, RunOutput: rune(0x1F431)}, 88 {Script: `toBoolSlice(nil)`, RunOutput: []bool{}}, 89 {Script: `toBoolSlice(1)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type int64")}, 90 {Script: `toBoolSlice(1.2)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type float64")}, 91 {Script: `toBoolSlice(false)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type bool")}, 92 {Script: `toBoolSlice({})`, RunError: fmt.Errorf("function wants argument type []interface {} but received type map[interface {}]interface {}")}, 93 {Script: `toBoolSlice([])`, RunOutput: []bool{}}, 94 {Script: `toBoolSlice([nil])`, RunOutput: []bool{false}}, 95 {Script: `toBoolSlice([1])`, RunOutput: []bool{false}}, 96 {Script: `toBoolSlice([1.1])`, RunOutput: []bool{false}}, 97 {Script: `toBoolSlice([true])`, RunOutput: []bool{true}}, 98 {Script: `toBoolSlice([[]])`, RunOutput: []bool{false}}, 99 {Script: `toBoolSlice([{}])`, RunOutput: []bool{false}}, 100 {Script: `toIntSlice(nil)`, RunOutput: []int64{}}, 101 {Script: `toIntSlice(1)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type int64")}, 102 {Script: `toIntSlice(1.2)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type float64")}, 103 {Script: `toIntSlice(false)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type bool")}, 104 {Script: `toIntSlice({})`, RunError: fmt.Errorf("function wants argument type []interface {} but received type map[interface {}]interface {}")}, 105 {Script: `toIntSlice([])`, RunOutput: []int64{}}, 106 {Script: `toIntSlice([nil])`, RunOutput: []int64{0}}, 107 {Script: `toIntSlice([1])`, RunOutput: []int64{1}}, 108 {Script: `toIntSlice([1.1])`, RunOutput: []int64{1}}, 109 {Script: `toIntSlice([true])`, RunOutput: []int64{0}}, 110 {Script: `toIntSlice([[]])`, RunOutput: []int64{0}}, 111 {Script: `toIntSlice([{}])`, RunOutput: []int64{0}}, 112 {Script: `toFloatSlice(nil)`, RunOutput: []float64{}}, 113 {Script: `toFloatSlice(1)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type int64")}, 114 {Script: `toFloatSlice(1.2)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type float64")}, 115 {Script: `toFloatSlice(false)`, RunError: fmt.Errorf("function wants argument type []interface {} but received type bool")}, 116 {Script: `toFloatSlice({})`, RunError: fmt.Errorf("function wants argument type []interface {} but received type map[interface {}]interface {}")}, 117 {Script: `toFloatSlice([])`, RunOutput: []float64{}}, 118 {Script: `toFloatSlice([nil])`, RunOutput: []float64{0.0}}, 119 {Script: `toFloatSlice([1])`, RunOutput: []float64{1.0}}, 120 {Script: `toFloatSlice([1.1])`, RunOutput: []float64{1.1}}, 121 {Script: `toFloatSlice([true])`, RunOutput: []float64{0.0}}, 122 {Script: `toFloatSlice([[]])`, RunOutput: []float64{0.0}}, 123 {Script: `toFloatSlice([{}])`, RunOutput: []float64{0.0}}, 124 {Script: `toByteSlice(nil)`, RunOutput: []byte{}}, 125 {Script: `toByteSlice([])`, RunError: fmt.Errorf("function wants argument type string but received type []interface {}")}, 126 {Script: `toByteSlice(1)`, RunOutput: []byte{0x01}}, // FIXME? 127 {Script: `toByteSlice(1.1)`, RunError: fmt.Errorf("function wants argument type string but received type float64")}, 128 {Script: `toByteSlice(true)`, RunError: fmt.Errorf("function wants argument type string but received type bool")}, 129 {Script: `toByteSlice("foo")`, RunOutput: []byte{'f', 'o', 'o'}}, 130 {Script: `toByteSlice("δΈη")`, RunOutput: []byte{0xe4, 0xb8, 0x96, 0xe7, 0x95, 0x8c}}, 131 {Script: `toRuneSlice(nil)`, RunOutput: []rune{}}, 132 {Script: `toRuneSlice([])`, RunError: fmt.Errorf("function wants argument type string but received type []interface {}")}, 133 {Script: `toRuneSlice(1)`, RunOutput: []rune{0x01}}, // FIXME? 134 {Script: `toRuneSlice(1.1)`, RunError: fmt.Errorf("function wants argument type string but received type float64")}, 135 {Script: `toRuneSlice(true)`, RunError: fmt.Errorf("function wants argument type string but received type bool")}, 136 {Script: `toRuneSlice("foo")`, RunOutput: []rune{'f', 'o', 'o'}}, 137 {Script: `toRuneSlice("δΈη")`, RunOutput: []rune{'δΈ', 'η'}}, 138 {Script: `toStringSlice([true,false,1])`, RunOutput: []string{"", "", "\x01"}}, // FIXME? 139 {Script: `toDuration(nil)`, RunOutput: time.Duration(0)}, 140 {Script: `toDuration(0)`, RunOutput: time.Duration(0)}, 141 {Script: `toDuration(true)`, RunError: fmt.Errorf("function wants argument type int64 but received type bool")}, 142 {Script: `toDuration([])`, RunError: fmt.Errorf("function wants argument type int64 but received type []interface {}")}, 143 {Script: `toDuration({})`, RunError: fmt.Errorf("function wants argument type int64 but received type map[interface {}]interface {}")}, 144 {Script: `toDuration("")`, RunError: fmt.Errorf("function wants argument type int64 but received type string")}, 145 {Script: `toDuration("1s")`, RunError: fmt.Errorf("function wants argument type int64 but received type string")}, // TODO 146 {Script: `toDuration(a)`, Input: map[string]interface{}{"a": int64(time.Duration(123 * time.Minute))}, RunOutput: time.Duration(123 * time.Minute)}, 147 {Script: `toDuration(a)`, Input: map[string]interface{}{"a": float64(time.Duration(123 * time.Minute))}, RunOutput: time.Duration(123 * time.Minute)}, 148 {Script: `toDuration(a)`, Input: map[string]interface{}{"a": time.Duration(123 * time.Minute)}, RunOutput: time.Duration(123 * time.Minute)}, 149 } 150 testlib.Run(t, tests, &testlib.Options{EnvSetupFunc: &testCoreEnvSetupFunc}) 151 }