Add List method to Routes singletone returning the list of numbered routes

This commit is contained in:
Roberto Abdelkader Martínez Pérez
2019-10-09 09:54:15 +02:00
parent 4164f3e3de
commit 6810cb99ee
2 changed files with 33 additions and 0 deletions
+8
View File
@@ -35,3 +35,11 @@ func (srl *safeRouteList) Snapshot() []model.Route {
copy(rs, srl.rs)
return rs
}
func (srl *safeRouteList) List() []model.Route {
rs := srl.Snapshot()
for i := 0; i < len(rs); i++ {
rs[i].Index = i
}
return rs
}
+25
View File
@@ -165,3 +165,28 @@ func TestAppendReturnsTheInsertedRoutedWithTheActualIndexWhenPopulated(t *testin
t.Errorf("Index of the returned route is not the last one, i.e., 41, but %d", r.Index)
}
}
func TestListReturnsTheSameNumberOfRoutesThanSnapshot(t *testing.T) {
srl := New()
srl.Append(model.Route{ID: "FOO"})
if len(srl.List()) != len(srl.Snapshot()) {
t.Error("The number of routes returned is not correct")
}
}
func TestListReturnsANumberedListOfRoutes(t *testing.T) {
srl := New()
for i := 0; i < 42; i++ {
srl.Append(model.Route{})
}
l := srl.List()
for i, r := range l {
if i != r.Index {
t.Fatalf("Route is correctly numbered. Got %v, expected %v", r.Index, i)
}
}
}