Skip to content

Commit

Permalink
feat: add new api
Browse files Browse the repository at this point in the history
  • Loading branch information
mmadfox committed Aug 18, 2023
1 parent 9577176 commit 410049f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
7 changes: 7 additions & 0 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ func (d *Device) AddRoute(routes ...*navigator.Route) error {
return err
}

// Routes returns a copy of the routes associated with the device.
func (d *Device) Routes() []*navigator.Route {
d.mu.RLock()
defer d.mu.RUnlock()
return d.navigator.Routes()
}

// RemoveRoute removes a route from the device's navigator by its ID.
func (d *Device) RemoveRoute(routeID string) bool {
d.mu.Lock()
Expand Down
8 changes: 8 additions & 0 deletions device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1244,3 +1244,11 @@ func TestDevice_SetUserID(t *testing.T) {
d.SetUserID(expectedUserID)
require.Equal(t, expectedUserID, d.UserID())
}

func TestDevice_Routes(t *testing.T) {
expectedRoutes := testRoutes()
d := NewBicycleTracker()
d.AddRoute(expectedRoutes...)
require.Equal(t, expectedRoutes, d.Routes())

}
7 changes: 7 additions & 0 deletions navigator/navigator.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ func (n *Navigator) IsFinish() bool {
return false
}

// Routes returns a copy of the navigator's routes slice.
func (n *Navigator) Routes() []*Route {
routes := make([]*Route, len(n.routes))
copy(routes, n.routes)
return routes
}

// CurrentRoute returns the current route.
func (n *Navigator) CurrentRoute() *Route {
if len(n.routes) == 0 {
Expand Down
10 changes: 10 additions & 0 deletions navigator/navigator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,16 @@ func TestNavigator_RouteByID(t *testing.T) {
}
}

func TestNavigator_Routes(t *testing.T) {
n, err := New()
require.NoError(t, err)

route := RouteFromTracks(track1km2segment, track3km7segments)
n.AddRoute(route)

require.Len(t, n.Routes(), 1)
}

func TestNavigator_EachRoute(t *testing.T) {
routes := routes(10)
n, _ := New()
Expand Down

0 comments on commit 410049f

Please sign in to comment.