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

	// Start with an empty stack.
	sa := collections.NewStringStack()
	assert.Length(sa, 0)
	sa.Push("foo")
	sa.Push("bar")
	assert.Length(sa, 2)
	sa.Push()
	assert.Length(sa, 2)
	sa.Push("baz", "yadda")
	assert.Length(sa, 4)
	v, err := sa.Peek()
	assert.Nil(err)
	assert.Equal(v, "yadda")
	v, err = sa.Pop()
	assert.Nil(err)
	assert.Equal(v, "yadda")
	assert.Length(sa, 3)

	// Start with a filled stack.
	sb := collections.NewStringStack("a", "b", "c")
	assert.Length(sb, 3)
	v, err = sb.Pop()
	assert.Nil(err)
	assert.Equal(v, "c")
	assert.Length(sb, 2)
	v, err = sb.Pop()
	assert.Nil(err)
	assert.Equal(v, "b")
	assert.Length(sb, 1)
	v, err = sb.Pop()
	assert.Nil(err)
	assert.Equal(v, "a")
	assert.Length(sb, 0)

	// Popping the last one returns an error.
	v, err = sb.Pop()
	assert.ErrorMatch(err, ".*collection is empty")

	// And now deflate the first one.
	sa.Deflate()
	assert.Length(sa, 0)
}
Exemple #2
0
// BeginTagNode implements the sml.Builder interface.
func (b *configBuilder) BeginTagNode(tag string) error {
	switch {
	case b.values == nil && tag != "config":
		return errors.New(ErrIllegalConfigSource, errorMessages, `does not start with "config" node`)
	case b.values == nil:
		b.stack = collections.NewStringStack(tag)
		b.values = collections.NewKeyStringValueTree(tag, "", false)
	default:
		b.stack.Push(tag)
		changer := b.values.Create(b.stack.All()...)
		if changer.Error() != nil {
			return errors.New(ErrIllegalConfigSource, errorMessages, changer.Error())
		}
	}
	return nil
}
Exemple #3
0
// BeginTagNode implements the Builder interface.
func (tb *KeyStringValueTreeBuilder) BeginTagNode(tag string) error {
	if tb.done {
		return errors.New(ErrBuilder, errorMessages, "building is already done")
	}
	switch {
	case tb.tree == nil:
		tb.stack = collections.NewStringStack(tag)
		tb.tree = collections.NewKeyStringValueTree(tag, "", false)
	default:
		tb.stack.Push(tag)
		changer := tb.tree.Create(tb.stack.All()...)
		if err := changer.Error(); err != nil {
			return errors.Annotate(err, ErrBuilder, errorMessages)
		}
	}
	return nil
}