Skip to content

Commit

Permalink
progress tracker: SetValue() to set a custom value (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored Dec 4, 2018
1 parent b8cb96c commit f6b258e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions progress/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ func (t *Tracker) Reset() {
t.value = 0
}

// SetValue sets the value of the tracker and re-calculates if the tracker is
// "done".
func (t *Tracker) SetValue(value int64) {
t.Reset()
t.Increment(value)
}

func (t *Tracker) start() {
t.done = false
t.timeStart = time.Now()
Expand Down
14 changes: 14 additions & 0 deletions progress/tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ func TestTracker_Reset(t *testing.T) {
assert.Equal(t, time.Time{}, tracker.timeStop)
assert.Equal(t, int64(0), tracker.value)
}

func TestTracker_SetValue(t *testing.T) {
tracker := Tracker{Total: 100}
assert.Equal(t, int64(0), tracker.value)
assert.False(t, tracker.done)

tracker.SetValue(5)
assert.Equal(t, int64(5), tracker.value)
assert.False(t, tracker.done)

tracker.SetValue(tracker.Total)
assert.Equal(t, tracker.Total, tracker.value)
assert.True(t, tracker.done)
}

0 comments on commit f6b258e

Please sign in to comment.