Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
concatenation of lists
Browse files Browse the repository at this point in the history
(( [a, b] [c, d] )) evaulates to [a, b, c, d]
  • Loading branch information
Alex Suraci and Dmitriy Kalinin committed Nov 1, 2013
1 parent 66aa9f4 commit b6cb3f4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
16 changes: 9 additions & 7 deletions dynaml/concatenation.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ func (e ConcatenationExpr) Evaluate(binding Binding) (yaml.Node, bool) {
return nil, false
}

astring, ok := a.(string)
if !ok {
return nil, false
astring, aok := a.(string)
bstring, bok := b.(string)
if aok && bok {
return astring + bstring, true
}

bstring, ok := b.(string)
if !ok {
return nil, false
alist, aok := a.([]yaml.Node)
blist, bok := b.([]yaml.Node)
if aok && bok {
return append(alist, blist...), true
}

return astring + bstring, true
return nil, false
}
61 changes: 40 additions & 21 deletions dynaml/concatenation_test.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
package dynaml

import (
"github.com/vito/spiff/yaml"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("concatenation", func() {
It("concatenates two strings", func() {
expr := ConcatenationExpr{
StringExpr{"one"},
StringExpr{"two"},
}
Context("when the left-hand side is a string", func() {
Context("and the right-hand side is a string", func() {
It("concatenates both strings", func() {
expr := ConcatenationExpr{
StringExpr{"one"},
StringExpr{"two"},
}

Expect(expr).To(EvaluateAs("onetwo", FakeBinding{}))
})
Expect(expr).To(EvaluateAs("onetwo", FakeBinding{}))
})
})

Context("when the left-hand side is not a string", func() {
It("fails", func() {
expr := ConcatenationExpr{
StringExpr{"one"},
IntegerExpr{42},
}
Context("and the right-hand side is NOT a string", func() {
It("fails", func() {
expr := ConcatenationExpr{
StringExpr{"two"},
IntegerExpr{42},
}

Expect(expr).To(FailToEvaluate(FakeBinding{}))
Expect(expr).To(FailToEvaluate(FakeBinding{}))
})
})
})

Context("when the right-hand side is not a string", func() {
It("fails", func() {
expr := ConcatenationExpr{
IntegerExpr{42},
StringExpr{"two"},
}
Context("when the left-hand side is a list", func() {
Context("and the right-hand side is a list", func() {
It("concatenates both lists", func() {
expr := ConcatenationExpr{
ListExpr{[]Expression{StringExpr{"one"}}},
ListExpr{[]Expression{StringExpr{"two"}}},
}

Expect(expr).To(EvaluateAs([]yaml.Node{"one", "two"}, FakeBinding{}))
})
})

Context("and the right-hand side is NOT a list", func() {
It("fails", func() {
expr := ConcatenationExpr{
IntegerExpr{42},
ListExpr{[]Expression{StringExpr{"two"}}},
}

Expect(expr).To(FailToEvaluate(FakeBinding{}))
Expect(expr).To(FailToEvaluate(FakeBinding{}))
})
})
})
})
1 change: 0 additions & 1 deletion spiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ foo: bar
Expect(err).NotTo(HaveOccured())
})


It("resolves the template and prints it out", func() {
Expect(merge).To(Say(`foo: bar`))
Expect(merge).To(ExitWith(0))
Expand Down

0 comments on commit b6cb3f4

Please sign in to comment.