示例#1
0
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))

}
示例#2
0
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")
		}
	}

}
示例#3
0
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)

}
示例#4
0
/*
	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))

}
示例#5
0
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())
	}

}
示例#6
0
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)

}
示例#7
0
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)

}
示例#8
0
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)

}
示例#9
0
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])

}
示例#10
0
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)

}
示例#11
0
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())

}
示例#12
0
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"))

}
示例#13
0
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")

}
示例#14
0
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")

}
示例#15
0
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))

}
示例#16
0
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())

}
示例#17
0
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)

}
示例#18
0
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")

}
示例#19
0
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)
}
示例#20
0
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)

}
示例#21
0
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))

}
示例#22
0
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)
}
示例#23
0
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")

}
示例#24
0
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)

}
示例#25
0
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)

}
示例#26
0
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)

}
示例#27
0
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)

}
示例#28
0
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)

}
示例#29
0
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))
}
示例#30
0
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))

}