Пример #1
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")
		}
	}

}
Пример #2
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))

}
Пример #3
0
func Test_Mock_On(t *testing.T) {

	// make a test impl object
	var mockedService *TestExampleImplementation = new(TestExampleImplementation)

	assert.Equal(t, mockedService.Mock.On("TheExampleMethod"), &mockedService.Mock)
	assert.Equal(t, "TheExampleMethod", mockedService.Mock.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 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)

}
Пример #7
0
func Test_Mock_On_WithArgs(t *testing.T) {

	// make a test impl object
	var mockedService *TestExampleImplementation = new(TestExampleImplementation)

	assert.Equal(t, mockedService.Mock.On("TheExampleMethod", 1, 2, 3), &mockedService.Mock)
	assert.Equal(t, "TheExampleMethod", mockedService.Mock.onMethodName)
	assert.Equal(t, 1, mockedService.Mock.onMethodArguments[0])
	assert.Equal(t, 2, mockedService.Mock.onMethodArguments[1])
	assert.Equal(t, 3, mockedService.Mock.onMethodArguments[2])

}
Пример #8
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)

}
Пример #9
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"))

}
Пример #10
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())

}
Пример #11
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")

}
Пример #12
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")

}
Пример #13
0
func TestPingPong(t *testing.T) {
	// t.Parallel()
	logs.ResetLogger()

	withTwoEndpoints(t, func(A, B *Endpoint) {
		var (
			assert = assert.New(t)
			c      *Channel
			ident  *Identity
			pkt    *lob.Packet
			err    error
		)

		go func() {
			c, err := A.Listen("ping", false).AcceptChannel()

			c.SetDeadline(time.Now().Add(10 * time.Second))

			if assert.NoError(err) && assert.NotNil(c) {
				defer c.Close()

				pkt, err = c.ReadPacket()
				if assert.NoError(err) && assert.NotNil(pkt) {
					assert.Equal("ping", string(pkt.Body(nil)))

					err = c.WritePacket(lob.New([]byte("pong")))
					assert.NoError(err)
				}
			}
		}()

		ident, err = A.LocalIdentity()
		assert.NoError(err)

		c, err = B.Open(ident, "ping", false)
		assert.NoError(err)
		if assert.NotNil(c) {
			defer c.Close()

			c.SetDeadline(time.Now().Add(10 * time.Second))

			err = c.WritePacket(lob.New([]byte("ping")))
			assert.NoError(err)

			pkt, err = c.ReadPacket()
			if assert.NoError(err) && assert.NotNil(pkt) {
				assert.Equal("pong", string(pkt.Body(nil)))
			}
		}
	})
}
Пример #14
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())

}
Пример #15
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")

}
Пример #16
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))

}
Пример #17
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")

}
Пример #18
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))

}
Пример #19
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))

}
Пример #20
0
func TestAccessorsAccessSetSingleFieldNotExisting(t *testing.T) {

	current := map[string]interface{}{}
	access(current, "name", "Mat", true, false)
	assert.Equal(t, current["name"], "Mat")

}
Пример #21
0
func TestAccessorsSet(t *testing.T) {

	current := New(map[string]interface{}{"name": "Tyler"})
	current.Set("name", "Mat")
	assert.Equal(t, "Mat", current.Get("name").data)

}
Пример #22
0
func TestAccessorsAccessSetDeepDeep(t *testing.T) {

	current := map[string]interface{}{"one": map[string]interface{}{"two": map[string]interface{}{"three": map[string]interface{}{"four": 4}}}}

	access(current, "one.two.three.four", 5, true, true)

	assert.Equal(t, 5, access(current, "one.two.three.four", nil, false, true))

}
Пример #23
0
// AssertNumberOfCalls asserts that the method was called expectedCalls times.
func (m *Mock) AssertNumberOfCalls(t TestingT, methodName string, expectedCalls int) bool {
	var actualCalls int = 0
	for _, call := range m.Calls {
		if call.Method == methodName {
			actualCalls++
		}
	}
	return assert.Equal(t, actualCalls, expectedCalls, fmt.Sprintf("Expected number of calls (%d) does not match the actual number of calls (%d).", expectedCalls, actualCalls))
}
Пример #24
0
func Test_Mock_findExpectedCall(t *testing.T) {

	m := new(Mock)
	m.On("One", 1).Return("one")
	m.On("Two", 2).Return("two")
	m.On("Two", 3).Return("three")

	f, c := m.findExpectedCall("Two", 3)

	if assert.Equal(t, 2, f) {
		if assert.NotNil(t, c) {
			assert.Equal(t, "Two", c.Method)
			assert.Equal(t, 3, c.Arguments[0])
			assert.Equal(t, "three", c.ReturnArguments[0])
		}
	}

}
Пример #25
0
func Test_Arguments_Diff_WithAnythingArgument_InActualToo(t *testing.T) {

	var args Arguments = []interface{}{"string", Anything, true}
	var count int
	_, count = args.Diff([]interface{}{"string", 123, true})

	assert.Equal(t, 0, count)

}
Пример #26
0
func TestAccessorsAccessSetArray(t *testing.T) {

	current := map[string]interface{}{"names": []interface{}{"Tyler"}}

	access(current, "names[0]", "Mat", true, true)

	assert.Equal(t, "Mat", access(current, "names[0]", nil, false, true))

}
Пример #27
0
func Test_Arguments_Diff_WithAnythingOfTypeArgument(t *testing.T) {

	var args Arguments = []interface{}{"string", AnythingOfType("int"), true}
	var count int
	_, count = args.Diff([]interface{}{"string", 123, true})

	assert.Equal(t, 0, count)

}
Пример #28
0
func TestMergeHere(t *testing.T) {

	d := make(map[string]interface{})
	d["name"] = "Mat"

	d1 := make(map[string]interface{})
	d1["name"] = "Tyler"
	d1["location"] = "UT"

	dObj := New(d)
	d1Obj := New(d1)

	merged := dObj.MergeHere(d1Obj)

	assert.Equal(t, dObj, merged, "With MergeHere, it should return the first modified map")
	assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
	assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
	assert.Equal(t, merged.Get("location").Str(), dObj.Get("location").Str())
}
Пример #29
0
func TestMerge(t *testing.T) {

	d := make(map[string]interface{})
	d["name"] = "Mat"

	d1 := make(map[string]interface{})
	d1["name"] = "Tyler"
	d1["location"] = "UT"

	dObj := New(d)
	d1Obj := New(d1)

	merged := dObj.Merge(d1Obj)

	assert.Equal(t, merged.Get("name").Str(), d1Obj.Get("name").Str())
	assert.Equal(t, merged.Get("location").Str(), d1Obj.Get("location").Str())
	assert.Empty(t, dObj.Get("location").Str())

}
Пример #30
0
func Test_Arguments_Diff_WithAnythingOfTypeArgument_Failing(t *testing.T) {

	var args Arguments = []interface{}{"string", AnythingOfType("string"), true}
	var count int
	var diff string
	diff, count = args.Diff([]interface{}{"string", 123, true})

	assert.Equal(t, 1, count)
	assert.Contains(t, diff, `string != type int - %!s(int=123)`)

}