示例#1
0
func TestUnmarshal_LiteralsOptional(t *testing.T) {
	type Message struct {
		FIXMsgType           string     `fix:"0"`
		StringPtrField       *string    `fix:"112"`
		UnsetStringPtrField  *string    `fix:"113"`
		IntPtrField          *int       `fix:"114"`
		BoolPtrField         *bool      `fix:"115"`
		FloatPtrField        *float64   `fix:"116"`
		UTCTimestampPtrField *time.Time `fix:"117"`
	}

	fixMsg := quickfix.NewMessage()
	fixMsg.Body.SetField(quickfix.Tag(112), quickfix.FIXString("world"))
	fixMsg.Body.SetField(quickfix.Tag(114), quickfix.FIXInt(3))
	fixMsg.Body.SetField(quickfix.Tag(115), quickfix.FIXBoolean(false))
	fixMsg.Body.SetField(quickfix.Tag(116), quickfix.FIXFloat(500.123))
	tVal, _ := time.Parse("2006-Jan-02", "2014-Jun-16")
	fixMsg.Body.SetField(quickfix.Tag(117), quickfix.FIXUTCTimestamp{Value: tVal})

	var msgOut Message
	quickfix.Unmarshal(fixMsg, &msgOut)

	var tests = []struct {
		testName      string
		expectNil     bool
		actualIsNil   bool
		expectedDeref interface{}
		actualDeref   interface{}
	}{
		{"set string ptr", false, msgOut.StringPtrField == nil, "world", *msgOut.StringPtrField},
		{"unset string ptr", true, msgOut.UnsetStringPtrField == nil, "", "unset"},
		{"int ptr", false, msgOut.IntPtrField == nil, 3, *msgOut.IntPtrField},
		{"bool ptr", false, msgOut.BoolPtrField == nil, false, *msgOut.BoolPtrField},
		{"float ptr", false, msgOut.FloatPtrField == nil, 500.123, *msgOut.FloatPtrField},
		{"timestamp ptr", false, msgOut.UTCTimestampPtrField == nil, tVal, *msgOut.UTCTimestampPtrField},
	}

	for _, test := range tests {
		if test.expectNil {
			if !test.actualIsNil {
				t.Errorf("%v: Expected nil not %v", test.testName, test.actualDeref)
			}
			continue
		}

		if test.actualIsNil {
			t.Errorf("%v: Did not expect nil", test.testName)
			continue
		}

		if test.expectedDeref != test.actualDeref {
			t.Errorf("Expected '%v' got '%v'", test.expectedDeref, test.actualDeref)
		}
	}
}
示例#2
0
func TestUnmarshal_Literals(t *testing.T) {
	type Message struct {
		FIXMsgType        string    `fix:"0"`
		StringField       string    `fix:"114"`
		IntField          int       `fix:"115"`
		UnsetIntField     int       `fix:"116"`
		BoolField         bool      `fix:"117"`
		FloatField        float64   `fix:"118"`
		UTCTimestampField time.Time `fix:"119"`
	}

	fixMsg := quickfix.NewMessage()
	fixMsg.Body.SetField(quickfix.Tag(114), quickfix.FIXString("world"))
	fixMsg.Body.SetField(quickfix.Tag(115), quickfix.FIXInt(3))
	fixMsg.Body.SetField(quickfix.Tag(117), quickfix.FIXBoolean(true))
	fixMsg.Body.SetField(quickfix.Tag(118), quickfix.FIXFloat(500.123))
	tVal, _ := time.Parse("2006-Jan-02", "2014-Jun-16")
	fixMsg.Body.SetField(quickfix.Tag(119), quickfix.FIXUTCTimestamp{Value: tVal})

	var msgOut Message
	quickfix.Unmarshal(fixMsg, &msgOut)

	var tests = []struct {
		expected interface{}
		actual   interface{}
	}{
		{"world", msgOut.StringField},
		{3, msgOut.IntField},
		{0, msgOut.UnsetIntField},
		{true, msgOut.BoolField},
		{500.123, msgOut.FloatField},
		{tVal, msgOut.UTCTimestampField},
	}

	for _, test := range tests {
		if test.expected != test.actual {
			t.Errorf("Expected %v got %v", test.expected, test.actual)
		}
	}
}