diff --git a/mcmp/series.go b/mcmp/series.go index 644fa6d..f4f94af 100644 --- a/mcmp/series.go +++ b/mcmp/series.go @@ -65,6 +65,15 @@ func SeriesElements(c *Component, key interface{}) []SeriesElement { return els } +// SeriesGetElement returns the ith element in the series at the given key. +func SeriesGetElement(c *Component, key interface{}, i int) (SeriesElement, bool) { + els, _ := getSeriesElements(c, key) + if i >= len(els) { + return SeriesElement{}, false + } + return els[i], true +} + // SeriesValues returns the sequence of values that have been added to the // Component under the given key via AddSeriesValue, in the same order the // values were added. diff --git a/mcmp/series_test.go b/mcmp/series_test.go index 32fb6d7..7e366fb 100644 --- a/mcmp/series_test.go +++ b/mcmp/series_test.go @@ -8,12 +8,24 @@ import ( func TestSeries(t *T) { key := "foo" + c := new(Component) + + assertGetElement := func(i int, expEl SeriesElement) massert.Assertion { + el, ok := SeriesGetElement(c, key, i) + if expEl == (SeriesElement{}) { + return massert.Equal(false, ok) + } + return massert.All( + massert.Equal(expEl, el), + massert.Equal(true, ok), + ) + } // test empty state - c := new(Component) massert.Require(t, massert.Length(SeriesElements(c, key), 0), massert.Length(SeriesValues(c, key), 0), + assertGetElement(0, SeriesElement{}), ) // test after a single value has been added @@ -21,6 +33,8 @@ func TestSeries(t *T) { massert.Require(t, massert.Equal([]SeriesElement{{Value: 1}}, SeriesElements(c, key)), massert.Equal([]interface{}{1}, SeriesValues(c, key)), + assertGetElement(0, SeriesElement{Value: 1}), + assertGetElement(1, SeriesElement{}), ) // test after a child has been added @@ -31,6 +45,9 @@ func TestSeries(t *T) { SeriesElements(c, key), ), massert.Equal([]interface{}{1}, SeriesValues(c, key)), + assertGetElement(0, SeriesElement{Value: 1}), + assertGetElement(1, SeriesElement{Child: childA}), + assertGetElement(2, SeriesElement{}), ) // test after another value has been added @@ -41,5 +58,9 @@ func TestSeries(t *T) { SeriesElements(c, key), ), massert.Equal([]interface{}{1, 2}, SeriesValues(c, key)), + assertGetElement(0, SeriesElement{Value: 1}), + assertGetElement(1, SeriesElement{Child: childA}), + assertGetElement(2, SeriesElement{Value: 2}), + assertGetElement(3, SeriesElement{}), ) }