func TestKeyStringValueTreeFind(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyStringValueTree(assert)
	list, err := tree.FindFirst(func(k, v string) (bool, error) {
		return k == "bravo", nil
	}).List()
	assert.Nil(err)
	assert.Equal(list, []collections.KeyStringValue{{"foo", "bar"}, {"bar", "foo"}})
	_, err = tree.FindFirst(func(k, v string) (bool, error) {
		return false, nil
	}).List()
	assert.ErrorMatch(err, ".* node not found")
	_, err = tree.FindFirst(func(k, v string) (bool, error) {
		return false, errors.New("ouch")
	}).List()
	assert.ErrorMatch(err, ".* cannot find first node: ouch")
	changers := tree.FindAll(func(k, v string) (bool, error) {
		return k == "bravo", nil
	})
	assert.Length(changers, 2)
	v, err := changers[0].Value()
	assert.Nil(err)
	assert.Equal(v, "three")
	v, err = changers[1].Value()
	assert.Nil(err)
	assert.Equal(v, "four")
	changers = tree.FindAll(func(k, v string) (bool, error) {
		return false, nil
	})
	assert.Length(changers, 0)
	changers = tree.FindAll(func(k, v string) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.Length(changers, 1)
	assert.ErrorMatch(changers[0].Error(), ".* cannot find all matching nodes: ouch")
}
TestKeyStringValueTreeFind tests the correct finding in
key/string value tree nodes.
package collections_test
import (
	"errors"
	"fmt"
	"strings"
	"testing"
	"github.com/tideland/golib/audit"
	"github.com/tideland/golib/collections"
)
func TestTreeCreate(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewTree("root", true)
	err := tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("charlie")
	assert.Nil(err)
	err = tree.Create("root", "delta", 1).Add(true)
	assert.Nil(err)
	assert.Length(tree, 8)
	tree.Deflate("toor")
	assert.Length(tree, 1)
	err = tree.At("foo").Add(0)
	assert.ErrorMatch(err, ".* node not found")
	err = tree.At("root", "foo").Add(0)
	assert.ErrorMatch(err, ".* node not found")
	tree = collections.NewTree("root", false)
	err = tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestTreeRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createTree(assert)
	err := tree.At("root", "alpha").Remove()
	assert.Nil(err)
	assert.Length(tree, 11)
	err = tree.At("root", "delta").Remove()
	assert.Nil(err)
	assert.Length(tree, 6)
}
func TestTreeSetValue(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createTree(assert)
	old, err := tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, "alpha")
	act, err := tree.At("root", "beta").Value()
	assert.Nil(err)
	assert.Equal(act, "beta")
	root, err := tree.Root().Value()
	assert.Nil(err)
	assert.Equal(root, "root")
	tree = collections.NewTree("root", false)
	err = tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("beta")
	assert.Nil(err)
	old, err = tree.At("root", "alpha").SetValue("beta")
	assert.Nil(old)
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestTreeFind(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createTree(assert)
	list, err := tree.FindFirst(func(v interface{}) (bool, error) {
		switch vt := v.(type) {
		case string:
			return vt == "bravo", nil
		default:
			return false, nil
		}
	}).List()
	assert.Nil(err)
	assert.Equal(list, []interface{}{"foo", "bar"})
	_, err = tree.FindFirst(func(v interface{}) (bool, error) {
		return false, nil
	}).List()
	assert.ErrorMatch(err, ".* node not found")
	_, err = tree.FindFirst(func(v interface{}) (bool, error) {
		return false, errors.New("ouch")
	}).List()
	assert.ErrorMatch(err, ".* cannot find first node: ouch")
	changers := tree.FindAll(func(v interface{}) (bool, error) {
		switch v.(type) {
		case int:
			return true, nil
		default:
			return false, nil
		}
	})
	assert.Length(changers, 2)
	v, err := changers[0].Value()
	assert.Nil(err)
	assert.Equal(v, 1)
	v, err = changers[1].Value()
	assert.Nil(err)
	assert.Equal(v, 2)
	changers = tree.FindAll(func(v interface{}) (bool, error) {
		return false, nil
	})
	assert.Length(changers, 0)
	changers = tree.FindAll(func(v interface{}) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.Length(changers, 1)
	assert.ErrorMatch(changers[0].Error(), ".* cannot find all matching nodes: ouch")
}
func TestTreeDo(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewTree("root", true)
	err := tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root", "bravo").Add("foo")
	assert.Nil(err)
	err = tree.At("root", "bravo").Add("bar")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("charlie")
	assert.Nil(err)
	err = tree.Create("root", "delta", 1).Add(true)
	assert.Nil(err)
	err = tree.Create("root", "delta", 2).Add(false)
	assert.Nil(err)
	var values []interface{}
	err = tree.DoAll(func(v interface{}) error {
		values = append(values, v)
		return nil
	})
	assert.Nil(err)
	assert.Length(values, 12)
	var all [][]interface{}
	err = tree.DoAllDeep(func(vs []interface{}) error {
		all = append(all, vs)
		return nil
	})
	assert.Nil(err)
	assert.Length(all, 12)
	for _, vs := range all {
		assert.True(len(vs) >= 1 && len(vs) <= 4)
	}
	err = tree.DoAll(func(v interface{}) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all nodes: ouch")
}
func TestTreeCopy(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewTree("root", true)
	err := tree.Create("root", "alpha").Add("a")
	assert.Nil(err)
	err = tree.Create("root", "beta").Add("b")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "one").Add("1")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "two").Add("2")
	assert.Nil(err)
	ctree := tree.Copy()
	assert.Length(ctree, 10)
	value, err := ctree.At("root", "alpha", "a").Value()
	assert.Nil(err)
	assert.Equal(value, "a")
	value, err = ctree.At("root", "gamma", "two", "2").Value()
	assert.Nil(err)
	assert.Equal(value, "2")
}
func TestStringTreeCreate(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewStringTree("root", true)
	err := tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("charlie")
	assert.Nil(err)
	err = tree.Create("root", "delta", "one").Add("true")
	assert.Nil(err)
	assert.Length(tree, 8)
	tree.Deflate("toor")
	assert.Length(tree, 1)
	err = tree.At("foo").Add("zero")
	assert.ErrorMatch(err, ".* node not found")
	err = tree.At("root", "foo").Add("zero")
	assert.ErrorMatch(err, ".* node not found")
	tree = collections.NewStringTree("root", false)
	err = tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.Nil(err)
	err = tree.At("root").Add("bravo")
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestStringTreeRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createStringTree(assert)
	err := tree.At("root", "alpha").Remove()
	assert.Nil(err)
	assert.Length(tree, 11)
	err = tree.At("root", "delta").Remove()
	assert.Nil(err)
	assert.Length(tree, 6)
}
func TestStringTreeSetValue(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createStringTree(assert)
	old, err := tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, "alpha")
	act, err := tree.At("root", "beta").Value()
	assert.Nil(err)
	assert.Equal(act, "beta")
	tree = collections.NewStringTree("root", false)
	err = tree.At("root").Add("alpha")
	assert.Nil(err)
	err = tree.At("root").Add("beta")
	assert.Nil(err)
	old, err = tree.At("root", "alpha").SetValue("beta")
	assert.Equal(old, "")
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestStringTreeFind(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createStringTree(assert)
	list, err := tree.FindFirst(func(v string) (bool, error) {
		return v == "bravo", nil
	}).List()
	assert.Nil(err)
	assert.Equal(list, []string{"foo", "bar"})
	_, err = tree.FindFirst(func(v string) (bool, error) {
		return false, nil
	}).List()
	assert.ErrorMatch(err, ".* node not found")
	_, err = tree.FindFirst(func(v string) (bool, error) {
		return false, errors.New("ouch")
	}).List()
	assert.ErrorMatch(err, ".* cannot find first node: ouch")
	changers := tree.FindAll(func(v string) (bool, error) {
		return v == "bravo", nil
	})
	assert.Length(changers, 2)
	v, err := changers[0].Value()
	assert.Nil(err)
	assert.Equal(v, "bravo")
	v, err = changers[1].Value()
	assert.Nil(err)
	assert.Equal(v, "bravo")
	changers = tree.FindAll(func(v string) (bool, error) {
		return false, nil
	})
	assert.Length(changers, 0)
	changers = tree.FindAll(func(v string) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.Length(changers, 1)
	assert.ErrorMatch(changers[0].Error(), ".* cannot find all matching nodes: ouch")
}
func TestStringTreeDo(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createStringTree(assert)
	var values []string
	err := tree.DoAll(func(v string) error {
		values = append(values, v)
		return nil
	})
	assert.Nil(err)
	assert.Length(values, 12)
	var all [][]string
	err = tree.DoAllDeep(func(vs []string) error {
		all = append(all, vs)
		return nil
	})
	assert.Nil(err)
	assert.Length(all, 12)
	for _, vs := range all {
		assert.True(len(vs) >= 1 && len(vs) <= 4)
	}
	err = tree.DoAll(func(v string) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all nodes: ouch")
}
func TestStringTreeCopy(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewStringTree("root", true)
	err := tree.Create("root", "alpha").Add("a")
	assert.Nil(err)
	err = tree.Create("root", "beta").Add("b")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "one").Add("1")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "two").Add("2")
	assert.Nil(err)
	ctree := tree.Copy()
	assert.Length(ctree, 10)
	value, err := ctree.At("root", "alpha", "a").Value()
	assert.Nil(err)
	assert.Equal(value, "a")
	value, err = ctree.At("root", "gamma", "two", "2").Value()
	assert.Nil(err)
	assert.Equal(value, "2")
}
func TestKeyValueTreeCreate(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewKeyValueTree("root", 1, true)
	err := tree.At("root").Add("alpha", 2)
	assert.Nil(err)
	err = tree.At("root").Add("bravo", 3)
	assert.Nil(err)
	err = tree.At("root").Add("bravo", true)
	assert.Nil(err)
	err = tree.At("root").Add("charlie", 1.0)
	assert.Nil(err)
	err = tree.Create("root", "delta", "one").Add("true", "false")
	assert.Nil(err)
	assert.Length(tree, 8)
	tree.Deflate("toor", 0)
	assert.Length(tree, 1)
	err = tree.At("foo").Add("zero", 0)
	assert.ErrorMatch(err, ".* node not found")
	err = tree.At("root", "foo").Add("zero", 0)
	assert.ErrorMatch(err, ".* node not found")
	tree = collections.NewKeyValueTree("root", 0, false)
	err = tree.At("root").Add("alpha", "a")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "b")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", 2)
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestKeyValueTreeRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyValueTree(assert)
	err := tree.At("root", "alpha").Remove()
	assert.Nil(err)
	assert.Length(tree, 11)
	err = tree.At("root", "delta").Remove()
	assert.Nil(err)
	assert.Length(tree, 6)
}
func TestKeyValueTreeSetKey(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyValueTree(assert)
	initialKey, err := tree.At("root", "alpha").Key()
	assert.Nil(err)
	assert.Equal(initialKey, "alpha")
	initialValue, err := tree.At("root", "alpha").Value()
	assert.Nil(err)
	currentKey, err := tree.At("root", "alpha").SetKey("beta")
	assert.Nil(err)
	assert.Equal(initialKey, currentKey)
	currentValue, err := tree.At("root", "beta").Value()
	assert.Nil(err)
	assert.Equal(currentValue, initialValue)
	tree = collections.NewKeyValueTree("root", 1, false)
	err = tree.At("root").Add("alpha", 2)
	assert.Nil(err)
	err = tree.At("root").Add("bravo", 3)
	assert.Nil(err)
	initialKey, err = tree.At("root", "alpha").SetKey("bravo")
	assert.Empty(initialKey)
	assert.ErrorMatch(err, ".* duplicates .*")
}
func TestKeyValueTreeSetValue(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyValueTree(assert)
	old, err := tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, 2)
	act, err := tree.At("root", "alpha").Value()
	assert.Nil(err)
	assert.Equal(act, "beta")
	tree = collections.NewKeyValueTree("root", 1, false)
	err = tree.At("root").Add("alpha", 2)
	assert.Nil(err)
	err = tree.At("root").Add("beta", 3)
	assert.Nil(err)
	old, err = tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, 2)
}
func TestKeyValueTreeFind(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyValueTree(assert)
	list, err := tree.FindFirst(func(k string, v interface{}) (bool, error) {
		return k == "bravo", nil
	}).List()
	assert.Nil(err)
	assert.Equal(list, []collections.KeyValue{{"foo", "bar"}, {"bar", "foo"}})
	_, err = tree.FindFirst(func(k string, v interface{}) (bool, error) {
		return false, nil
	}).List()
	assert.ErrorMatch(err, ".* node not found")
	_, err = tree.FindFirst(func(k string, v interface{}) (bool, error) {
		return false, errors.New("ouch")
	}).List()
	assert.ErrorMatch(err, ".* cannot find first node: ouch")
	changers := tree.FindAll(func(k string, v interface{}) (bool, error) {
		return k == "bravo", nil
	})
	assert.Length(changers, 2)
	v, err := changers[0].Value()
	assert.Nil(err)
	assert.Equal(v, 3)
	v, err = changers[1].Value()
	assert.Nil(err)
	assert.Equal(v, 4)
	changers = tree.FindAll(func(k string, v interface{}) (bool, error) {
		return false, nil
	})
	assert.Length(changers, 0)
	changers = tree.FindAll(func(k string, v interface{}) (bool, error) {
		return false, errors.New("ouch")
	})
	assert.Length(changers, 1)
	assert.ErrorMatch(changers[0].Error(), ".* cannot find all matching nodes: ouch")
}
func TestKeyValueTreeDo(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyValueTree(assert)
	var values []interface{}
	err := tree.DoAll(func(k string, v interface{}) error {
		values = append(values, v)
		return nil
	})
	assert.Nil(err)
	assert.Length(values, 12)
	keyValues := map[string]interface{}{}
	err = tree.DoAllDeep(func(ks []string, v interface{}) error {
		k := strings.Join(ks, "/") + " = " + fmt.Sprintf("%v", v)
		keyValues[k] = v
		return nil
	})
	assert.Nil(err)
	assert.Length(keyValues, 12)
	for k := range keyValues {
		ksv := strings.Split(k, " = ")
		assert.Length(ksv, 2)
		ks := strings.Split(ksv[0], "/")
		assert.True(len(ks) >= 1 && len(ks) <= 4)
	}
	err = tree.DoAll(func(k string, v interface{}) error {
		return errors.New("ouch")
	})
	assert.ErrorMatch(err, ".* cannot perform function on all nodes: ouch")
}
func TestKeyValueTreeCopy(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewKeyValueTree("root", "0", true)
	err := tree.Create("root", "alpha").Add("a", "1")
	assert.Nil(err)
	err = tree.Create("root", "beta").Add("b", "2")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "one").Add("1", "3.1")
	assert.Nil(err)
	err = tree.Create("root", "gamma", "two").Add("2", "3.2")
	assert.Nil(err)
	ctree := tree.Copy()
	assert.Length(ctree, 10)
	value, err := ctree.At("root", "alpha", "a").Value()
	assert.Nil(err)
	assert.Equal(value, "1")
	value, err = ctree.At("root", "gamma", "two", "2").Value()
	assert.Nil(err)
	assert.Equal(value, "3.2")
	catree, err := ctree.CopyAt("root", "gamma")
	assert.Nil(err)
	value, err = catree.At("gamma", "two", "2").Value()
	assert.Nil(err)
	assert.Equal(value, "3.2")
}
func TestKeyStringValueTreeCreate(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := collections.NewKeyStringValueTree("root", "one", true)
	err := tree.At("root").Add("alpha", "two")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "three")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "true")
	assert.Nil(err)
	err = tree.At("root").Add("charlie", "1.0")
	assert.Nil(err)
	err = tree.Create("root", "delta", "one").Add("true", "false")
	assert.Nil(err)
	assert.Length(tree, 8)
	tree.Deflate("toor", "zero")
	assert.Length(tree, 1)
	err = tree.At("foo").Add("zero", "0")
	assert.ErrorMatch(err, ".* node not found")
	err = tree.At("root", "foo").Add("zero", "0")
	assert.ErrorMatch(err, ".* node not found")
	tree = collections.NewKeyStringValueTree("root", "0", false)
	err = tree.At("root").Add("alpha", "a")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "b")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "2")
	assert.ErrorMatch(err, ".* duplicates are not allowed")
}
func TestKeyStringValueTreeRemove(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyStringValueTree(assert)
	err := tree.At("root", "alpha").Remove()
	assert.Nil(err)
	assert.Length(tree, 11)
	err = tree.At("root", "delta").Remove()
	assert.Nil(err)
	assert.Length(tree, 6)
}
func TestKeyStringValueTreeSetKey(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyStringValueTree(assert)
	initialKey, err := tree.At("root", "alpha").Key()
	assert.Nil(err)
	assert.Equal(initialKey, "alpha")
	initialValue, err := tree.At("root", "alpha").Value()
	assert.Nil(err)
	currentKey, err := tree.At("root", "alpha").SetKey("beta")
	assert.Nil(err)
	assert.Equal(initialKey, currentKey)
	currentValue, err := tree.At("root", "beta").Value()
	assert.Nil(err)
	assert.Equal(currentValue, initialValue)
	tree = collections.NewKeyStringValueTree("root", "one", false)
	err = tree.At("root").Add("alpha", "two")
	assert.Nil(err)
	err = tree.At("root").Add("bravo", "three")
	assert.Nil(err)
	initialKey, err = tree.At("root", "alpha").SetKey("bravo")
	assert.Empty(initialKey)
	assert.ErrorMatch(err, ".* duplicates .*")
}
func TestKeyStringValueTreeSetValue(t *testing.T) {
	assert := audit.NewTestingAssertion(t, true)
	tree := createKeyStringValueTree(assert)
	old, err := tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, "two")
	act, err := tree.At("root", "alpha").Value()
	assert.Nil(err)
	assert.Equal(act, "beta")
	tree = collections.NewKeyStringValueTree("root", "one", false)
	err = tree.At("root").Add("alpha", "two")
	assert.Nil(err)
	err = tree.At("root").Add("beta", "three")
	assert.Nil(err)
	old, err = tree.At("root", "alpha").SetValue("beta")
	assert.Nil(err)
	assert.Equal(old, "two")
}