mcmp: implement SeriesGetElement

This commit is contained in:
Brian Picciano 2019-07-09 20:53:27 -04:00
parent b2aa2f73bd
commit de56d14739
2 changed files with 31 additions and 1 deletions

View File

@ -65,6 +65,15 @@ func SeriesElements(c *Component, key interface{}) []SeriesElement {
return els 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 // SeriesValues returns the sequence of values that have been added to the
// Component under the given key via AddSeriesValue, in the same order the // Component under the given key via AddSeriesValue, in the same order the
// values were added. // values were added.

View File

@ -8,12 +8,24 @@ import (
func TestSeries(t *T) { func TestSeries(t *T) {
key := "foo" 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 // test empty state
c := new(Component)
massert.Require(t, massert.Require(t,
massert.Length(SeriesElements(c, key), 0), massert.Length(SeriesElements(c, key), 0),
massert.Length(SeriesValues(c, key), 0), massert.Length(SeriesValues(c, key), 0),
assertGetElement(0, SeriesElement{}),
) )
// test after a single value has been added // test after a single value has been added
@ -21,6 +33,8 @@ func TestSeries(t *T) {
massert.Require(t, massert.Require(t,
massert.Equal([]SeriesElement{{Value: 1}}, SeriesElements(c, key)), massert.Equal([]SeriesElement{{Value: 1}}, SeriesElements(c, key)),
massert.Equal([]interface{}{1}, SeriesValues(c, key)), massert.Equal([]interface{}{1}, SeriesValues(c, key)),
assertGetElement(0, SeriesElement{Value: 1}),
assertGetElement(1, SeriesElement{}),
) )
// test after a child has been added // test after a child has been added
@ -31,6 +45,9 @@ func TestSeries(t *T) {
SeriesElements(c, key), SeriesElements(c, key),
), ),
massert.Equal([]interface{}{1}, SeriesValues(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 // test after another value has been added
@ -41,5 +58,9 @@ func TestSeries(t *T) {
SeriesElements(c, key), SeriesElements(c, key),
), ),
massert.Equal([]interface{}{1, 2}, SeriesValues(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{}),
) )
} }