Exemple #1
0
func TestNewFieldDef(t *testing.T) {
	ft := datadictionary.NewFieldType("aname", 11, "INT")

	var tests = []struct {
		required bool
	}{
		{false},
	}

	for _, test := range tests {
		fd := datadictionary.NewFieldDef(ft, test.required)
		assert.False(t, fd.IsGroup(), "field def is not a group")
		assert.Equal(t, "aname", fd.Name())
		assert.Equal(t, test.required, fd.Required())
	}
}
Exemple #2
0
func TestNewMessageDef(t *testing.T) {

	ft1 := datadictionary.NewFieldType("type1", 11, "STRING")
	ft2 := datadictionary.NewFieldType("type2", 12, "STRING")
	ft3 := datadictionary.NewFieldType("type3", 13, "INT")

	optionalfd1 := datadictionary.NewFieldDef(ft1, false)
	requiredfd1 := datadictionary.NewFieldDef(ft1, true)

	optionalfd2 := datadictionary.NewFieldDef(ft2, false)
	//	requiredfd2 := datadictionary.NewFieldDef(ft2, true)

	optionalGroup1 := datadictionary.NewGroupFieldDef(ft3, false, []datadictionary.MessagePart{requiredfd1, optionalfd2})
	requiredGroup1 := datadictionary.NewGroupFieldDef(ft3, true, []datadictionary.MessagePart{requiredfd1, optionalfd2})

	ct1 := datadictionary.NewComponentType("ct1", []datadictionary.MessagePart{requiredGroup1})

	optionalComp1 := datadictionary.NewComponent(ct1, false)

	var tests = []struct {
		parts                 []datadictionary.MessagePart
		expectedTags          datadictionary.TagSet
		expectedRequiredTags  datadictionary.TagSet
		expectedRequiredParts []datadictionary.MessagePart
	}{
		{
			parts:                 []datadictionary.MessagePart{},
			expectedTags:          datadictionary.TagSet{},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{optionalfd1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{requiredfd1, optionalfd2},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{11: struct{}{}},
			expectedRequiredParts: []datadictionary.MessagePart{requiredfd1},
		},
		{
			parts:                 []datadictionary.MessagePart{optionalGroup1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
		{
			parts:                 []datadictionary.MessagePart{requiredGroup1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{13: struct{}{}},
			expectedRequiredParts: []datadictionary.MessagePart{requiredGroup1},
		},
		{
			parts:                 []datadictionary.MessagePart{optionalComp1},
			expectedTags:          datadictionary.TagSet{11: struct{}{}, 12: struct{}{}, 13: struct{}{}},
			expectedRequiredTags:  datadictionary.TagSet{},
			expectedRequiredParts: []datadictionary.MessagePart(nil),
		},
	}

	for _, test := range tests {
		md := datadictionary.NewMessageDef("some message", "X", test.parts)

		assert.NotNil(t, md)
		assert.Equal(t, "some message", md.Name)
		assert.Equal(t, "X", md.MsgType)
		assert.Equal(t, test.expectedTags, md.Tags)
		assert.Equal(t, test.expectedRequiredTags, md.RequiredTags)
		assert.Equal(t, test.parts, md.Parts)
		assert.Equal(t, test.expectedRequiredParts, md.RequiredParts())
	}
}
func TestNewComponentType(t *testing.T) {
	ft1 := datadictionary.NewFieldType("aname1", 11, "INT")
	ft2 := datadictionary.NewFieldType("aname2", 12, "INT")

	optionalField1 := datadictionary.NewFieldDef(ft1, false)
	requiredField1 := datadictionary.NewFieldDef(ft1, true)

	optionalField2 := datadictionary.NewFieldDef(ft2, false)
	requiredField2 := datadictionary.NewFieldDef(ft2, true)

	requiredComp1 := datadictionary.NewComponent(
		datadictionary.NewComponentType("comp1", []datadictionary.MessagePart{requiredField1}),
		true)

	optionalComp1 := datadictionary.NewComponent(
		datadictionary.NewComponentType("comp1", []datadictionary.MessagePart{requiredField1}),
		false)

	var tests = []struct {
		testName               string
		parts                  []datadictionary.MessagePart
		expectedFields         []*datadictionary.FieldDef
		expectedRequiredParts  []datadictionary.MessagePart
		expectedRequiredFields []*datadictionary.FieldDef
	}{
		{
			testName:       "test1",
			parts:          []datadictionary.MessagePart{optionalField1},
			expectedFields: []*datadictionary.FieldDef{optionalField1},
		},
		{
			testName:               "test2",
			parts:                  []datadictionary.MessagePart{requiredField1},
			expectedFields:         []*datadictionary.FieldDef{requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField1},
		},
		{
			testName:               "test3",
			parts:                  []datadictionary.MessagePart{requiredField1, optionalField2},
			expectedFields:         []*datadictionary.FieldDef{requiredField1, optionalField2},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField1},
		},
		{
			testName:               "test4",
			parts:                  []datadictionary.MessagePart{requiredField2, optionalComp1},
			expectedFields:         []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField2},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField2},
		},
		{
			testName:               "test5",
			parts:                  []datadictionary.MessagePart{requiredField2, requiredComp1},
			expectedFields:         []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredFields: []*datadictionary.FieldDef{requiredField2, requiredField1},
			expectedRequiredParts:  []datadictionary.MessagePart{requiredField2, requiredComp1},
		},
	}

	for _, test := range tests {
		ct := datadictionary.NewComponentType("cname", test.parts)
		assert.NotNil(t, ct, test.testName)
		assert.Equal(t, "cname", ct.Name(), test.testName)
		assert.Equal(t, test.expectedFields, ct.Fields(), test.testName)
		assert.Equal(t, test.parts, ct.Parts(), test.testName)
		assert.Equal(t, test.expectedRequiredFields, ct.RequiredFields(), test.testName)
		assert.Equal(t, test.expectedRequiredParts, ct.RequiredParts(), test.testName)
	}
}