animation data: methods for edition animation data records count

This commit is contained in:
M. Sz 2021-03-04 11:33:44 +01:00
parent b72017dbd9
commit c603eaafbc
2 changed files with 76 additions and 1 deletions

View File

@ -61,6 +61,34 @@ func (ad *AnimationData) GetRecordsCount() int {
return len(ad.entries)
}
func (ad *AnimationData) PushRecord(name string) {
ad.entries[name] = append(
ad.entries[name],
&AnimationDataRecord{
name: name,
},
)
}
func (ad *AnimationData) DeleteRecord(name string, recordIdx int) error {
newRecords := make([]*AnimationDataRecord, 0)
for n, i := range ad.entries[name] {
if n == recordIdx {
continue
}
newRecords = append(newRecords, i)
}
if len(ad.entries[name]) == len(newRecords) {
return fmt.Errorf("index %d not found", recordIdx)
}
ad.entries[name] = newRecords
return nil
}
// Load loads the data into an AnimationData struct
//nolint:gocognit,funlen // can't reduce
func Load(data []byte) (*AnimationData, error) {

View File

@ -155,7 +155,7 @@ func TestAnimationDataRecord_FPS(t *testing.T) {
}
}
func TestAnimationDataRecord_Marshal(t *testing.T) {
func TestAnimationData_Marshal(t *testing.T) {
file, fileErr := os.Open("testdata/AnimData.d2")
if fileErr != nil {
t.Error("cannot open test data file")
@ -209,3 +209,50 @@ func TestAnimationDataRecord_Marshal(t *testing.T) {
}
}
}
func TestAnimationData_DeleteRecord(t *testing.T) {
ad := &AnimationData{
entries: map[string][]*AnimationDataRecord{
"a": {
{name: "a", speed: 1, framesPerDirection: 1},
{name: "a", speed: 2, framesPerDirection: 2},
{name: "a", speed: 3, framesPerDirection: 3},
},
},
}
err := ad.DeleteRecord("a", 1)
if err != nil {
t.Error(err)
}
if len(ad.entries["a"]) != 2 {
t.Fatal("Delete record error")
}
if ad.entries["a"][1].speed != 3 {
t.Fatal("Invalid index deleted")
}
}
func TestAnimationData_PushRecord(t *testing.T) {
ad := &AnimationData{
entries: map[string][]*AnimationDataRecord{
"a": {
{name: "a", speed: 1, framesPerDirection: 1},
{name: "a", speed: 2, framesPerDirection: 2},
},
},
}
ad.PushRecord("a")
if len(ad.entries["a"]) != 3 {
t.Fatal("No record was pushed")
}
if ad.entries["a"][2].name != "a" {
t.Fatal("unexpected name of new record was set")
}
}