Skip to content

Commit

Permalink
Merge pull request #15 from yohamta/feat/f64duration
Browse files Browse the repository at this point in the history
Parsing float64 duration
  • Loading branch information
yohamta authored Dec 11, 2022
2 parents f619d96 + fb06e1a commit 586cca7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
31 changes: 30 additions & 1 deletion animation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,48 @@ func parseDurations(durations interface{}, frameCount int) []time.Duration {
for i := range val {
result[i] = val[i]
}
case []interface{}:
for i := range val {
result[i] = parseDurationValue(val[i])
}
case map[string]time.Duration:
for key, duration := range val {
min, max, step := parseInterval(key)
for i := min; i <= max; i += step {
result[i-1] = duration
}
}
case map[string]interface{}:
for key, duration := range val {
min, max, step := parseInterval(key)
for i := min; i <= max; i += step {
result[i-1] = parseDurationValue(duration)
}
}
case interface{}:
for i := 0; i < frameCount; i++ {
result[i] = parseDurationValue(val)
}
default:
log.Fatal(fmt.Sprintf("durations must be time.Duration or []time.Duration or map[string]time.Duration. was %v", durations))
log.Fatal(fmt.Sprintf("failed to parse durations: type=%T val=%+v", durations, durations))
}
return result
}

func parseDurationValue(value interface{}) time.Duration {
switch val := value.(type) {
case time.Duration:
return val
case int:
return time.Millisecond * time.Duration(val)
case float64:
return time.Millisecond * time.Duration(val)
default:
log.Fatal(fmt.Sprintf("failed to parse duration value: %+v", value))
}
return 0
}

func parseIntervals(durations []time.Duration) ([]time.Duration, time.Duration) {
result := []time.Duration{0}
var time time.Duration = 0
Expand Down
27 changes: 19 additions & 8 deletions animation_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ganim8_test

import (
"fmt"
"image"
"testing"
"time"
Expand Down Expand Up @@ -40,18 +41,28 @@ func assertEqualDurations(a, b []time.Duration) bool {

func TestParsingDuration(t *testing.T) {
var tests = []struct {
name string
args time.Duration
args []interface{}
want []time.Duration
}{
{"reads a simple array", 3, []time.Duration{3, 3, 3, 3}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
anim := ganim8.NewAnimation(mockSprite(4), tt.args, ganim8.Nop)
{
args: []interface{}{time.Duration(3)},
want: []time.Duration{3},
},
{
args: []interface{}{int(3)},
want: []time.Duration{time.Millisecond * 3},
},
{
args: []interface{}{float64(3.0)},
want: []time.Duration{time.Millisecond * 3},
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("testing-%d", i), func(t *testing.T) {
anim := ganim8.NewAnimation(mockSprite(1), tt.args, ganim8.Nop)
got := anim.Durations()
if assertEqualDurations(got, tt.want) == false {
t.Errorf("%s: got %v; want %v", tt.name, got, tt.want)
t.Errorf("got %v; want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit 586cca7

Please sign in to comment.