// TestStringTreeCreate tests the correct creation of a string tree. func TestStringTreeCreate(t *testing.T) { assert := audit.NewTestingAssertion(t, true) // String tree with duplicates, no errors. 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) // Deflate tree. tree.Deflate("toor") assert.Length(tree, 1) // Navigate with illegal paths. 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 without duplicates, so also with errors. 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 createStringTree(assert audit.Assertion) collections.StringTree { 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", "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", "one").Add("true") assert.Nil(err) err = tree.Create("root", "delta", "two").Add("false") assert.Nil(err) assert.Length(tree, 12) return tree }
// TestStringTreeCopy tests the copy of a string tree. 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") }
// TestStringTreeSetValue tests the setting of a string tree nodes value. func TestStringTreeSetValue(t *testing.T) { assert := audit.NewTestingAssertion(t, true) tree := createStringTree(assert) // Tree with duplicates. 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 without duplicates. 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") }