2019-06-15 18:36:10 +00:00
|
|
|
package mcmp
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSeries(t *T) {
|
|
|
|
key := "foo"
|
2019-07-10 00:53:27 +00:00
|
|
|
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),
|
|
|
|
)
|
|
|
|
}
|
2019-06-15 18:36:10 +00:00
|
|
|
|
|
|
|
// test empty state
|
|
|
|
massert.Require(t,
|
2019-06-15 23:52:55 +00:00
|
|
|
massert.Length(SeriesElements(c, key), 0),
|
|
|
|
massert.Length(SeriesValues(c, key), 0),
|
2019-07-10 00:53:27 +00:00
|
|
|
assertGetElement(0, SeriesElement{}),
|
2019-06-15 18:36:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test after a single value has been added
|
|
|
|
AddSeriesValue(c, key, 1)
|
|
|
|
massert.Require(t,
|
2019-06-15 23:52:55 +00:00
|
|
|
massert.Equal([]SeriesElement{{Value: 1}}, SeriesElements(c, key)),
|
|
|
|
massert.Equal([]interface{}{1}, SeriesValues(c, key)),
|
2019-07-10 00:53:27 +00:00
|
|
|
assertGetElement(0, SeriesElement{Value: 1}),
|
|
|
|
assertGetElement(1, SeriesElement{}),
|
2019-06-15 18:36:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test after a child has been added
|
|
|
|
childA := c.Child("a")
|
|
|
|
massert.Require(t,
|
|
|
|
massert.Equal(
|
|
|
|
[]SeriesElement{{Value: 1}, {Child: childA}},
|
2019-06-15 23:52:55 +00:00
|
|
|
SeriesElements(c, key),
|
2019-06-15 18:36:10 +00:00
|
|
|
),
|
2019-06-15 23:52:55 +00:00
|
|
|
massert.Equal([]interface{}{1}, SeriesValues(c, key)),
|
2019-07-10 00:53:27 +00:00
|
|
|
assertGetElement(0, SeriesElement{Value: 1}),
|
|
|
|
assertGetElement(1, SeriesElement{Child: childA}),
|
|
|
|
assertGetElement(2, SeriesElement{}),
|
2019-06-15 18:36:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// test after another value has been added
|
|
|
|
AddSeriesValue(c, key, 2)
|
|
|
|
massert.Require(t,
|
|
|
|
massert.Equal(
|
|
|
|
[]SeriesElement{{Value: 1}, {Child: childA}, {Value: 2}},
|
2019-06-15 23:52:55 +00:00
|
|
|
SeriesElements(c, key),
|
2019-06-15 18:36:10 +00:00
|
|
|
),
|
2019-06-15 23:52:55 +00:00
|
|
|
massert.Equal([]interface{}{1, 2}, SeriesValues(c, key)),
|
2019-07-10 00:53:27 +00:00
|
|
|
assertGetElement(0, SeriesElement{Value: 1}),
|
|
|
|
assertGetElement(1, SeriesElement{Child: childA}),
|
|
|
|
assertGetElement(2, SeriesElement{Value: 2}),
|
|
|
|
assertGetElement(3, SeriesElement{}),
|
2019-06-15 18:36:10 +00:00
|
|
|
)
|
|
|
|
}
|