Beispiel #1
0
func TestUnMarshal_ObjxMap(t *testing.T) {

	obj1 := objx.MSI("name", "Mat", "age", 30, "language", "en")
	obj2 := objx.MSI("obj", obj1)
	obj3 := objx.MSI("another_obj", obj2)

	csvCodec := new(CsvCodec)
	bytes, _ := csvCodec.Marshal(obj3, nil)

	log.Printf("bytes = %s", string(bytes))

	// unmarshal it back
	var obj interface{}
	csvCodec.Unmarshal(bytes, &obj)

	if objmap, ok := obj.(map[string]interface{}); ok {
		if objmap2, ok := objmap["another_obj"].(map[string]interface{}); ok {
			if objmap3, ok := objmap2["obj"].(map[string]interface{}); ok {

				assert.Equal(t, "Mat", objmap3["name"])
				assert.Equal(t, 30, objmap3["age"])
				assert.Equal(t, "en", objmap3["language"])

			} else {
				assert.True(t, false, "another_obj.obj should be msi")
			}
		} else {
			assert.True(t, false, "another_obj should be msi")
		}
	} else {
		assert.True(t, false, "obj should be msi")
	}

}
func TestMarshalWithCodec_WithError(t *testing.T) {

	// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	object := objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testCodec.On("Marshal", object, args).Return(nil, assert.AnError)

	_, err := service.MarshalWithCodec(testCodec, object, args)

	assert.Equal(t, assert.AnError, err, "The error should get returned")

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock)

}
func TestMarshalWithCodec_WithFacade(t *testing.T) {

	// func (s *WebCodecService) MarshalWithCodec(codec codecs.Codec, object interface{}, options ...interface{}) ([]byte, error) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	var bytesToReturn []byte = []byte("Hello World")
	testObjectWithFacade := new(test.TestObjectWithFacade)
	object := objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testObjectWithFacade.On("PublicData", args).Return(object, nil)
	testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)

	bytes, err := service.MarshalWithCodec(testCodec, testObjectWithFacade, args)

	if assert.Nil(t, err) {
		assert.Equal(t, string(bytesToReturn), string(bytes))
	}

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock, testObjectWithFacade.Mock)

}
func TestMarshalWithCodec(t *testing.T) {

	testCodec := new(test.TestCodec)
	service := NewWebCodecService()

	// make some test stuff
	var bytesToReturn []byte = []byte("Hello World")
	var object objx.Map = objx.MSI("Name", "Mat")
	var option1 string = "Option One"
	var option2 string = "Option Two"

	args := map[string]interface{}{option1: option1, option2: option2}

	// setup expectations
	testCodec.On("Marshal", object, args).Return(bytesToReturn, nil)

	bytes, err := service.MarshalWithCodec(testCodec, object, args)

	if assert.Nil(t, err) {
		assert.Equal(t, string(bytesToReturn), string(bytes))
	}

	// assert that our expectations were met
	mock.AssertExpectationsForObjects(t, testCodec.Mock)

}
Beispiel #5
0
func TestMarshal_mapWithTypes(t *testing.T) {

	data := map[string]interface{}{"name": "Mat", "age": 30, "yesOrNo": true}
	options := objx.MSI(OptionIncludeTypeAttributes, true)
	bytes, marshalErr := marshal(data, false, 0, options)

	if assert.NoError(t, marshalErr) {
		assert.Equal(t, "<object><name type=\"string\">Mat</name><age type=\"int\">30</age><yesOrNo type=\"bool\">true</yesOrNo></object>", string(bytes), "Output")
	}

}