func TestAccessorsAccessGetDeep(t *testing.T) { current := map[string]interface{}{"name": map[string]interface{}{"first": "Tyler", "last": "Bunnell"}} assert.Equal(t, "Tyler", access(current, "name.first", nil, false, true)) assert.Equal(t, "Bunnell", access(current, "name.last", nil, false, true)) }
func TestMapCreation(t *testing.T) { o := New(nil) assert.Nil(t, o) o = New("Tyler") assert.Nil(t, o) unconvertable := &Unconvertable{name: "Tyler"} o = New(unconvertable) assert.Nil(t, o) convertable := &Convertable{name: "Tyler"} o = New(convertable) if assert.NotNil(t, convertable) { assert.Equal(t, "Tyler", o["name"], "Tyler") } o = MSI() if assert.NotNil(t, o) { assert.NotNil(t, o) } o = MSI("name", "Tyler") if assert.NotNil(t, o) { if assert.NotNil(t, o) { assert.Equal(t, o["name"], "Tyler") } } }
func Test_Mock_On(t *testing.T) { // make a test impl object var mockedService *TestExampleImplementation = new(TestExampleImplementation) assert.Equal(t, mockedService.On("TheExampleMethod"), &mockedService.Mock) assert.Equal(t, "TheExampleMethod", mockedService.onMethodName) }
/* Arguments helper methods */ func Test_Arguments_Get(t *testing.T) { var args Arguments = []interface{}{"string", 123, true} assert.Equal(t, "string", args.Get(0).(string)) assert.Equal(t, 123, args.Get(1).(int)) assert.Equal(t, true, args.Get(2).(bool)) }
func TestMapFromURLQuery(t *testing.T) { m, err := FromURLQuery("name=tyler&state=UT") if assert.NoError(t, err) && assert.NotNil(t, m) { assert.Equal(t, "tyler", m.Get("name").Str()) assert.Equal(t, "UT", m.Get("state").Str()) } }
func TestInfluxDBConfShouldDefaultEmpty(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{} conf.Default() assert.Equal("localhost", conf.Host) assert.Equal(uint(8083), conf.Port) }
func TestAccessorsAccessSetSingleField(t *testing.T) { current := map[string]interface{}{"name": "Tyler"} access(current, "name", "Mat", true, false) assert.Equal(t, current["name"], "Mat") access(current, "age", 29, true, true) assert.Equal(t, current["age"], 29) }
func TestRiemannConfShouldDefault(t *testing.T) { assert := assert.New(t) rc := riemann.RiemannConf{} rc.Default() assert.Equal("localhost", rc.Host) assert.Equal(uint(5555), rc.Port) }
func Test_Mock_On_WithArgs(t *testing.T) { // make a test impl object var mockedService *TestExampleImplementation = new(TestExampleImplementation) assert.Equal(t, mockedService.On("TheExampleMethod", 1, 2, 3), &mockedService.Mock) assert.Equal(t, "TheExampleMethod", mockedService.onMethodName) assert.Equal(t, 1, mockedService.onMethodArguments[0]) assert.Equal(t, 2, mockedService.onMethodArguments[1]) assert.Equal(t, 3, mockedService.onMethodArguments[2]) }
func TestAccessorsAccessGetFromArrayWithInt(t *testing.T) { current := []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}} one := access(current, 0, nil, false, false) two := access(current, 1, nil, false, false) three := access(current, 2, nil, false, false) assert.Equal(t, "Tyler", one.(map[string]interface{})["first"]) assert.Equal(t, "Capitol", two.(map[string]interface{})["first"]) assert.Nil(t, three) }
func TestConversionBase64(t *testing.T) { o := New(map[string]interface{}{"name": "Mat"}) result, err := o.Base64() if assert.NoError(t, err) { assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", result) } assert.Equal(t, "eyJuYW1lIjoiTWF0In0=", o.MustBase64()) }
func TestConversionSignedBase64(t *testing.T) { o := New(map[string]interface{}{"name": "Mat"}) result, err := o.SignedBase64("key") if assert.NoError(t, err) { assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", result) } assert.Equal(t, "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6", o.MustSignedBase64("key")) }
func TestMapFromBase64String(t *testing.T) { base64String := "eyJuYW1lIjoiTWF0In0=" o, err := FromBase64(base64String) if assert.NoError(t, err) { assert.Equal(t, o.Get("name").Str(), "Mat") } assert.Equal(t, MustFromBase64(base64String).Get("name").Str(), "Mat") }
func TestMapFromSignedBase64String(t *testing.T) { base64String := "eyJuYW1lIjoiTWF0In0=_67ee82916f90b2c0d68c903266e8998c9ef0c3d6" o, err := FromSignedBase64(base64String, "key") if assert.NoError(t, err) { assert.Equal(t, o.Get("name").Str(), "Mat") } assert.Equal(t, MustFromSignedBase64(base64String, "key").Get("name").Str(), "Mat") }
func TestAccessorsAccessGetInsideArray(t *testing.T) { current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}} assert.Equal(t, "Tyler", access(current, "names[0].first", nil, false, true)) assert.Equal(t, "Bunnell", access(current, "names[0].last", nil, false, true)) assert.Equal(t, "Capitol", access(current, "names[1].first", nil, false, true)) assert.Equal(t, "Bollocks", access(current, "names[1].last", nil, false, true)) assert.Panics(t, func() { access(current, "names[2]", nil, false, true) }) assert.Nil(t, access(current, "names[2]", nil, false, false)) }
func TestConversionJSON(t *testing.T) { jsonString := `{"name":"Mat"}` o := MustFromJSON(jsonString) result, err := o.JSON() if assert.NoError(t, err) { assert.Equal(t, jsonString, result) } assert.Equal(t, jsonString, o.MustJSON()) }
func TestInfluxDBConfShouldDefaultNonEmpty(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{ Port: 65536, Database: "morgoth", } conf.Default() assert.Equal("localhost", conf.Host) assert.Equal(uint(8083), conf.Port) assert.Equal("morgoth", conf.Database) }
func TestExclude(t *testing.T) { d := make(Map) d["name"] = "Mat" d["age"] = 29 d["secret"] = "ABC" excluded := d.Exclude([]string{"secret"}) assert.Equal(t, d["name"], excluded["name"]) assert.Equal(t, d["age"], excluded["age"]) assert.False(t, excluded.Has("secret"), "secret should be excluded") }
func TestDefaultShouldDefaultMultipleFields(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"i am a string"` B string C int `default:"1"` } s := S{} assert.Equal(s.A, "") assert.Equal(s.B, "") assert.Equal(s.C, 0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, "i am a string") assert.Equal(s.A, v) _, err = SetDefault(&s, "B") assert.NotNil(err) assert.Equal(s.B, "") v, err = SetDefault(&s, "C") assert.Nil(err) assert.Equal(s.C, 1) assert.Equal(s.C, v) }
func TestRiemannConfShouldValidate(t *testing.T) { assert := assert.New(t) rc := riemann.RiemannConf{ Host: "example.com", Port: 42, } err := rc.Validate() assert.Nil(err) assert.Equal("example.com", rc.Host) assert.Equal(uint(42), rc.Port) }
func TestAccessorsAccessSetInsideArray(t *testing.T) { current := map[string]interface{}{"names": []interface{}{map[string]interface{}{"first": "Tyler", "last": "Bunnell"}, map[string]interface{}{"first": "Capitol", "last": "Bollocks"}}} access(current, "names[0].first", "Mat", true, true) access(current, "names[0].last", "Ryer", true, true) access(current, "names[1].first", "Captain", true, true) access(current, "names[1].last", "Underpants", true, true) assert.Equal(t, "Mat", access(current, "names[0].first", nil, false, true)) assert.Equal(t, "Ryer", access(current, "names[0].last", nil, false, true)) assert.Equal(t, "Captain", access(current, "names[1].first", nil, false, true)) assert.Equal(t, "Underpants", access(current, "names[1].last", nil, false, true)) }
func TestDefaultShouldDefaultString(t *testing.T) { assert := assert.New(t) type S struct { A string `default:"1.0"` } s := S{} assert.Equal(s.A, "") v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, "1.0") assert.Equal(s.A, v) }
func TestCopy(t *testing.T) { d1 := make(map[string]interface{}) d1["name"] = "Tyler" d1["location"] = "UT" d1Obj := New(d1) d2Obj := d1Obj.Copy() d2Obj["name"] = "Mat" assert.Equal(t, d1Obj.Get("name").Str(), "Tyler") assert.Equal(t, d2Obj.Get("name").Str(), "Mat") }
func TestInfluxDBConfDefaultShouldIgnoreValidFields(t *testing.T) { assert := assert.New(t) conf := influxdb.InfluxDBConf{ Host: "influx", Port: 42, Database: "morgoth", } conf.Default() assert.Equal("influx", conf.Host) assert.Equal(uint(42), conf.Port) assert.Equal("morgoth", conf.Database) }
func TestDefaultShouldDefaultInt(t *testing.T) { assert := assert.New(t) type S struct { A int `default:"-1"` } s := S{} assert.Equal(s.A, 0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, -1) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultBool(t *testing.T) { assert := assert.New(t) type S struct { A bool `default:"true"` } s := S{} assert.Equal(s.A, false) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, true) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultFloat(t *testing.T) { assert := assert.New(t) type S struct { A float64 `default:"1.0"` } s := S{} assert.Equal(s.A, 0.0) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, 1.0) assert.Equal(s.A, v) }
func TestDefaultShouldDefaultUint(t *testing.T) { assert := assert.New(t) type S struct { A uint `default:"1"` } s := S{} assert.Equal(s.A, uint(0)) v, err := SetDefault(&s, "A") assert.Nil(err) assert.Equal(s.A, uint(1)) assert.Equal(s.A, v) }
func TestScheduleShouldStartAndStop(t *testing.T) { defer glog.Flush() assert := assert.New(t) unit := 10 * time.Millisecond calledCount := 0 testF := func(start time.Time, dur time.Time) { remainder := start.Nanosecond() % int(unit) assert.Equal(0, remainder, "Start not truncated to 10ms") calledCount++ } s := schedule.Schedule{ Callback: testF, Period: unit, } assert.NotNil(s) sleepCount := 3 start := time.Now() err := s.Start() assert.Nil(err) time.Sleep(unit * time.Duration(sleepCount)) s.Stop() elapsed := time.Since(start) assert.True(calledCount >= sleepCount && calledCount <= int(elapsed/unit)) }
func Test_Arguments_Error(t *testing.T) { var err error = errors.New("An Error") var args Arguments = []interface{}{"string", 123, true, err} assert.Equal(t, err, args.Error(3)) }