Exemple #1
0
// TestSetsAddRemove tests the core set methods.
func TestSetsAddRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", 42, true)
	assert.Length(set, 3)
	set.Add("foo", "bar", 123)
	assert.Length(set, 5)
	all := set.All()
	assert.Length(all, 5)
	set.Remove("yadda")
	assert.Length(set, 5)
	set.Remove("bar", 42)
	assert.Length(set, 3)
	set.Remove(false, "foo")
	assert.Length(set, 2)
	set.Deflate()
	assert.Length(set, 0)
}
Exemple #2
0
// TestSetsDoAll tests the iteration over set values.
func TestSetsDoAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", "bar", 42, true, "yadda", 12345)
	sl := 0
	err := set.DoAll(func(v interface{}) error {
		if s, ok := v.(string); ok {
			sl += len(s)
		}
		return nil
	})
	assert.Nil(err)
	assert.Equal(sl, 11)

	err = set.DoAll(func(v interface{}) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all values: ouch")
}
Exemple #3
0
// TestSetsFindAll tests the finding of set values.
func TestSetsFindAll(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)

	set := collections.NewSet("foo", "bar", 42, true, "yadda", 12345)
	vs, err := set.FindAll(func(v interface{}) (bool, error) {
		switch v.(type) {
		case string:
			return true, nil
		default:
			return false, nil
		}
	})
	assert.Nil(err)
	assert.Length(vs, 3)

	vs, err = set.FindAll(func(v interface{}) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot find all matching values: ouch")
	assert.Length(vs, 0)
}