package protocol import ( "fmt" "path" "testing" "gitea.mmo.to/ppForge/ppforge/globals" ) func TestUpdateMetaData(t *testing.T) { var ( name string = "test" version string = "1.0.0" osilayer uint = 3 tcpiplayer uint = 2 extensionTo string = "" desc string = "description" ) prot := NewProtocolStructure() UpdateMetaData( prot, name, version, tcpiplayer, osilayer, extensionTo, desc, ) if prot.Metadata.Name != name || prot.Metadata.Description != desc || prot.Metadata.ExtensionTo != extensionTo || prot.Metadata.TCPIPLayer != tcpiplayer || prot.Metadata.Version != version || prot.Metadata.LowerLayerIdentification != nil || prot.Metadata.RequiredJSFunctions != nil { t.Log("Data hasn't been set correctly!") t.Fail() } } func GenerateFields() []Field { slc := make([]Field, 0) for i := 0; i < 10; i++ { field := NewField( fmt.Sprintf("testfield%d", i), fmt.Sprintf("Description %d", i), "", int(i*8), nil, false, false, ) slc = append(slc, *field) } return slc } func GenerateStructure() *Protocol { p := NewProtocolStructure() slc := GenerateFields() p.AppendFields(slc...) for i, e := range p.Structure { fmt.Printf("%d %s\n", i, e.ToJson()) } fmt.Println("Generated Structure.") return p } func TestFieldAppend(t *testing.T) { f := NewField( "testfield", "Description", "", 8, nil, false, false, ) p := NewProtocolStructure() p.AppendField(*f) if p.Structure[0].Name != "testfield" { t.Log("Append failed.") t.Fail() } } func TestFieldAdd(t *testing.T) { f := NewField( "testfield", "Description", "", 8, nil, false, false, ) p := NewProtocolStructure() p.AddField(0, f) p.AddField(0, f) } func TestUpdateFieldByName(t *testing.T) { p := GenerateStructure() f := NewField( "UpdatedField", "", "", 42, nil, false, true, ) p.UpdateFieldByName("testfield3", f) for i, e := range p.Structure { fmt.Printf("%d %s\n", i, e.ToJson()) } if p.Structure[3].Desc != "" || p.Structure[3].Size != 42 { t.Log("Update field by Ref failed!") t.Fail() } } // func TestUpdateFieldByElement(t *testing.T) { p := GenerateStructure() f := NewField( "UpdatedField", "", "", 42, nil, false, true, ) p.UpdateFieldByElement(5, f) for i, e := range p.Structure { fmt.Printf("%d %s\n", i, e.ToJson()) } if p.Structure[5].Desc != "" || p.Structure[5].Size != 42 { t.Log("Update field by Ref failed!") t.Fail() } } func TestRemoveFieldByName(t *testing.T) { p := GenerateStructure() p.RemoveFieldByName("testfield3") for i, e := range p.Structure { fmt.Printf("%d %s\n", i, e.ToJson()) } if p.Structure[3].Name != "testfield4" { t.Log("Remove by name failed!") t.Fail() } } // func TestRemoveFieldByElement(t *testing.T) { p := GenerateStructure() p.RemoveFieldByElement(3) for i, e := range p.Structure { fmt.Printf("%d %s\n", i, e.ToJson()) } if p.Structure[3].Name != "testfield4" { t.Log("Remove by element failed!") t.Fail() } } func TestToJson(t *testing.T) { p := GenerateStructure() fmt.Print(p.ToJSON()) } func TestSaveAndLoad(t *testing.T) { p := GenerateStructure() wd, err := globals.GetRepoDir(globals.GetCurrentDir()) if err != nil { t.Fail() } path := path.Join(wd, "test", "protocolsavetest.protocoljson") p.Save(path) p2 := NewProtocolStructure() p2.Load(path) if len(p2.Structure) == 0 { t.Fail() } p3, err := LoadNew(path) if len(p3.Structure) == 0 { t.Fail() } }