go-ml.dev/pkg/base@v0.0.0-20200610162856-60c38abac71b/tests/option_test.go (about) 1 package tests 2 3 import ( 4 "go-ml.dev/pkg/base/fu" 5 "gotest.tools/assert" 6 "testing" 7 ) 8 9 type Option1 bool 10 type Option2 string 11 type Option3 int 12 type Option4 float64 13 14 func option1(o ...interface{}) bool { 15 return fu.Option(Option1(false), o).Bool() 16 } 17 18 func option2(o ...interface{}) string { 19 return fu.Option(Option2(""), o).String() 20 } 21 22 func option3(o ...interface{}) int { 23 return int(fu.Option(Option3(0), o).Int()) 24 } 25 26 func Test_Option1(t *testing.T) { 27 28 assert.Assert(t, option1(Option1(true)) == true) 29 assert.Assert(t, option1(Option1(true), Option1(false)) == true) 30 assert.Assert(t, option1(Option1(false), Option1(true)) == false) 31 assert.Assert(t, option1(Option2(0)) == false) 32 assert.Assert(t, option1() == false) 33 34 } 35 36 func Test_Option2(t *testing.T) { 37 assert.Assert(t, option2(Option2("hello")) == "hello") 38 assert.Assert(t, option2(Option1(false)) == "") 39 } 40 41 func Test_Option3(t *testing.T) { 42 assert.Assert(t, option3(Option3(42)) == 42) 43 assert.Assert(t, option3(Option1(false)) == 0) 44 } 45 46 func Test_Option4(t *testing.T) { 47 opts := []interface{}{Option3(42), Option2("hello"), Option1(true), Option4(1.0)} 48 assert.Assert(t, fu.IntOption(Option3(0), opts) == 42) 49 assert.Assert(t, fu.StrOption(Option2(""), opts) == "hello") 50 assert.Assert(t, fu.FloatOption(Option4(0), opts) == 1.0) 51 assert.Assert(t, fu.BoolOption(Option1(false), opts) == true) 52 }