Skip to content

Commit

Permalink
refactor: split tests and mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanjenkner committed Oct 5, 2024
1 parent c9c5897 commit d6076a0
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 85 deletions.
91 changes: 49 additions & 42 deletions internal/events/dataevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,77 @@ type DataEvent struct {
CaloriesPerHour *uint16
CaloriesTotal *uint16
}
type DataEventOption func(*DataEvent)

type DataEventBuilder struct {
dataEvent *DataEvent
}
func NewDataEvent(elapsedTime uint16, level uint8, opts ...DataEventOption) *DataEvent {
e := &DataEvent{
ElapsedTime: elapsedTime,
Level: level,
}

func NewDataEventBuilder(ElapsedTime uint16, Level uint8) *DataEventBuilder {
return &DataEventBuilder{
&DataEvent{
ElapsedTime: ElapsedTime,
Level: Level,
},
for _, opt := range opts {
opt(e)
}
}

func (b *DataEventBuilder) SetDistance(distance uint16) *DataEventBuilder {
b.dataEvent.Distance = &distance
return b
return e
}

func (b *DataEventBuilder) SetRemainingDistance(remainingDistance uint16) *DataEventBuilder {
b.dataEvent.RemainingDistance = &remainingDistance
return b
func WithDistance(distance uint16) DataEventOption {
return func(e *DataEvent) {
e.Distance = &distance
}
}

func (b *DataEventBuilder) SetTime500mSplit(time500mSplit uint16) *DataEventBuilder {
b.dataEvent.Time500mSplit = &time500mSplit
return b
func WithRemainingDistance(remainingDistance uint16) DataEventOption {
return func(e *DataEvent) {
e.RemainingDistance = &remainingDistance
}
}

func (b *DataEventBuilder) SetTime500mAverage(time500mAverage uint16) *DataEventBuilder {
b.dataEvent.Time500mAverage = &time500mAverage
return b
func WithTime500mSplit(time500mSplit uint16) DataEventOption {
return func(e *DataEvent) {
e.Time500mSplit = &time500mSplit
}
}

func (b *DataEventBuilder) SetStrokes(strokes uint16) *DataEventBuilder {
b.dataEvent.Strokes = &strokes
return b
func WithTime500mAverage(time500mAverage uint16) DataEventOption {
return func(e *DataEvent) {
e.Time500mAverage = &time500mAverage
}
}

func (b *DataEventBuilder) SetStrokesPerMinute(strokesPerMinute uint8) *DataEventBuilder {
b.dataEvent.StrokesPerMinute = &strokesPerMinute
return b
func WithStrokes(strokes uint16) DataEventOption {
return func(e *DataEvent) {
e.Strokes = &strokes
}
}

func (b *DataEventBuilder) SetWattsPreviousStroke(wattsPreviousStroke uint16) *DataEventBuilder {
b.dataEvent.WattsPreviousStroke = &wattsPreviousStroke
return b
func WithStrokesPerMinute(strokesPerMinute uint8) DataEventOption {
return func(e *DataEvent) {
e.StrokesPerMinute = &strokesPerMinute
}
}

func (b *DataEventBuilder) SetWattsAverage(wattsAverage uint16) *DataEventBuilder {
b.dataEvent.WattsAverage = &wattsAverage
return b
func WithWattsPreviousStroke(wattsPreviousStroke uint16) DataEventOption {
return func(e *DataEvent) {
e.WattsPreviousStroke = &wattsPreviousStroke
}
}

func (b *DataEventBuilder) SetCaloriesPerHour(caloriesPerHour uint16) *DataEventBuilder {
b.dataEvent.CaloriesPerHour = &caloriesPerHour
return b
func WithWattsAverage(wattsAverage uint16) DataEventOption {
return func(e *DataEvent) {
e.WattsAverage = &wattsAverage
}
}

func (b *DataEventBuilder) SetCaloriesTotal(caloriesTotal uint16) *DataEventBuilder {
b.dataEvent.CaloriesTotal = &caloriesTotal
return b
func WithCaloriesPerHour(caloriesPerHour uint16) DataEventOption {
return func(e *DataEvent) {
e.CaloriesPerHour = &caloriesPerHour
}
}

func (b *DataEventBuilder) Build() *DataEvent {
return b.dataEvent
func WithCaloriesTotal(caloriesTotal uint16) DataEventOption {
return func(e *DataEvent) {
e.CaloriesTotal = &caloriesTotal
}
}
4 changes: 4 additions & 0 deletions internal/events/statuschangeevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ const (
type StatusChangeEvent struct {
StatusChange StatusChange
}

func NewStatusChangeEvent(statusChange StatusChange) *StatusChangeEvent {
return &StatusChangeEvent{StatusChange: statusChange}
}
24 changes: 8 additions & 16 deletions internal/fitnessmachine/fitnessmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,14 @@ func TestFitnessMachine_rowerDataNotifyHandler(t *testing.T) {
f := &FitnessMachine{}
go f.rowerDataNotifyHandler(nil, notifierMock)
time.Sleep(1 * time.Second)
uint8Ptr := func(v uint8) *uint8 {
return &v
}
uint16Ptr := func(v uint16) *uint16 {
return &v
}
f.OnData(events.DataEvent{
ElapsedTime: 45,
Level: 0,
Distance: uint16Ptr(123),
Time500mSplit: uint16Ptr(115),
Strokes: uint16Ptr(23),
StrokesPerMinute: uint8Ptr(31),
WattsPreviousStroke: uint16Ptr(105),
CaloriesPerHour: uint16Ptr(987),
})
f.OnData(*events.NewDataEvent(45, 0,
events.WithDistance(123),
events.WithTime500mSplit(115),
events.WithStrokes(23),
events.WithStrokesPerMinute(31),
events.WithWattsPreviousStroke(105),
events.WithCaloriesPerHour(987),
))
time.Sleep(1 * time.Second)

(cancelFunc)()
Expand Down
34 changes: 18 additions & 16 deletions internal/serialmonitor/serialmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,40 @@ func (m *SerialMonitor) Run() {
case strings.HasPrefix(line, "A"):
capture := parse(line)
if capture.strokesPerMinute == 0 {
m.emitStatusChangeEvent(events.StatusChangeEvent{StatusChange: events.PausedOrStopped})
m.emitStatusChangeEvent(*events.NewStatusChangeEvent(events.PausedOrStopped))
isPausedOrStopped = true
} else if isPausedOrStopped {
m.emitStatusChangeEvent(events.StatusChangeEvent{StatusChange: events.Resumed})
m.emitStatusChangeEvent(*events.NewStatusChangeEvent(events.Resumed))
isPausedOrStopped = false
} else if strokes == 0 {
m.emitStatusChangeEvent(events.StatusChangeEvent{StatusChange: events.Started})
m.emitStatusChangeEvent(*events.NewStatusChangeEvent(events.Started))
}
builder := events.NewDataEventBuilder(capture.elapsedTime, capture.level)
if isPausedOrStopped {
builder.SetRemainingDistance(capture.distance)
builder.SetTime500mAverage(capture.time500m)
builder.SetWattsAverage(capture.watts)
builder.SetCaloriesTotal(capture.cals)
m.emitDataEvent(*events.NewDataEvent(capture.elapsedTime, capture.level,
events.WithRemainingDistance(capture.distance),
events.WithTime500mAverage(capture.time500m),
events.WithWattsAverage(capture.watts),
events.WithCaloriesTotal(capture.cals),
))
} else {
strokes++
builder.SetDistance(capture.distance)
builder.SetStrokes(strokes)
builder.SetStrokesPerMinute(capture.strokesPerMinute)
builder.SetTime500mSplit(capture.time500m)
builder.SetWattsPreviousStroke(capture.watts)
builder.SetCaloriesPerHour(capture.cals)
m.emitDataEvent(*events.NewDataEvent(capture.elapsedTime, capture.level,
events.WithDistance(capture.distance),
events.WithStrokes(strokes),
events.WithStrokesPerMinute(capture.strokesPerMinute),
events.WithTime500mSplit(capture.time500m),
events.WithWattsPreviousStroke(capture.watts),
events.WithCaloriesPerHour(capture.cals),
))
}
m.emitDataEvent(*builder.Build())

case strings.HasPrefix(line, "W"):
if err := m.writeLine("K"); err != nil {
log.Println(err)
}

case strings.HasPrefix(line, "R"):
m.emitStatusChangeEvent(events.StatusChangeEvent{StatusChange: events.Reset})
m.emitStatusChangeEvent(*events.NewStatusChangeEvent(events.Reset))
isPausedOrStopped = false
strokes = 0
}
Expand Down
39 changes: 28 additions & 11 deletions internal/serialmonitor/serialmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,29 @@ func TestSerialMonitor_RunCallsObserverForDataEvents(t *testing.T) {
serialMonitor.Run()

wantedDataEvents := []events.DataEvent{
*events.NewDataEventBuilder(4, 4).SetDistance(7).SetTime500mSplit(268).SetStrokes(1).SetStrokesPerMinute(14).SetWattsPreviousStroke(108).SetCaloriesPerHour(670).Build(),
*events.NewDataEventBuilder(6, 4).SetDistance(14).SetTime500mSplit(163).SetStrokes(2).SetStrokesPerMinute(28).SetWattsPreviousStroke(105).SetCaloriesPerHour(659).Build(),
*events.NewDataEventBuilder(8, 4).SetDistance(21).SetTime500mSplit(148).SetStrokes(3).SetStrokesPerMinute(29).SetWattsPreviousStroke(109).SetCaloriesPerHour(674).Build(),
*events.NewDataEventBuilder(2, 4).SetRemainingDistance(0).SetTime500mAverage(0).SetWattsAverage(0).SetCaloriesTotal(0).Build(),
*events.NewDataEventBuilder(5, 4).SetDistance(8).SetTime500mSplit(319).SetStrokes(1).SetStrokesPerMinute(11).SetWattsPreviousStroke(106).SetCaloriesPerHour(663).Build(),
*events.NewDataEventBuilder(1810, 4).SetRemainingDistance(6015).SetTime500mAverage(153).SetWattsAverage(109).SetCaloriesTotal(400).Build(),
*events.NewDataEvent(4, 4, events.WithDistance(7), events.WithTime500mSplit(268),
events.WithStrokes(1), events.WithStrokesPerMinute(14), events.WithWattsPreviousStroke(108),
events.WithCaloriesPerHour(670),
),
*events.NewDataEvent(6, 4, events.WithDistance(14), events.WithTime500mSplit(163),
events.WithStrokes(2), events.WithStrokesPerMinute(28), events.WithWattsPreviousStroke(105),
events.WithCaloriesPerHour(659),
),
*events.NewDataEvent(8, 4, events.WithDistance(21), events.WithTime500mSplit(148),
events.WithStrokes(3), events.WithStrokesPerMinute(29), events.WithWattsPreviousStroke(109),
events.WithCaloriesPerHour(674),
),
*events.NewDataEvent(2, 4, events.WithRemainingDistance(0), events.WithTime500mAverage(0),
events.WithWattsAverage(0), events.WithCaloriesTotal(0),
),
*events.NewDataEvent(5, 4, events.WithDistance(8), events.WithTime500mSplit(319),
events.WithStrokes(1), events.WithStrokesPerMinute(11), events.WithWattsPreviousStroke(106),
events.WithCaloriesPerHour(663),
),
*events.NewDataEvent(1810, 4, events.WithRemainingDistance(6015), events.WithTime500mAverage(153),
events.WithWattsAverage(109),
events.WithCaloriesTotal(400),
),
}

if got := len(mockObserver.DataEvents); len(wantedDataEvents) != got {
Expand Down Expand Up @@ -88,11 +105,11 @@ func TestSerialMonitor_RunCallsObserverForStatusChangeEvents(t *testing.T) {
serialMonitor.Run()

wantedStatusChangeEvents := []events.StatusChangeEvent{
{StatusChange: events.Started},
{StatusChange: events.Reset},
{StatusChange: events.PausedOrStopped},
{StatusChange: events.Resumed},
{StatusChange: events.PausedOrStopped},
*events.NewStatusChangeEvent(events.Started),
*events.NewStatusChangeEvent(events.Reset),
*events.NewStatusChangeEvent(events.PausedOrStopped),
*events.NewStatusChangeEvent(events.Resumed),
*events.NewStatusChangeEvent(events.PausedOrStopped),
}
if got := len(mockObserver.StatusChangeEvents); len(wantedStatusChangeEvents) != got {
t.Errorf("# statusChangeEvents = %v, wantedDataEvents %v", got, len(wantedStatusChangeEvents))
Expand Down

0 comments on commit d6076a0

Please sign in to comment.