Skip to content

Commit

Permalink
The StaticCheck helper shouldn't run parsing, but only the check
Browse files Browse the repository at this point in the history
  • Loading branch information
bandesz committed Nov 7, 2018
1 parent 6e42e8e commit 3e2b83c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 52 deletions.
7 changes: 2 additions & 5 deletions parsley/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ func Evaluate(ctx *Context, p Parser, evalCtx interface{}) (interface{}, error)
return nil, parseErr
}

switch n := node.(type) {
case StaticCheckable:
if err := n.StaticCheck(evalCtx); err != nil {
return nil, ctx.FileSet().ErrorWithPosition(err)
}
if checkErr := StaticCheck(ctx, node, evalCtx); checkErr != nil {
return nil, checkErr
}

value, evalErr := node.Value(evalCtx)
Expand Down
7 changes: 1 addition & 6 deletions parsley/static_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ type StaticCheckableNode interface {

// StaticCheck parses the given input and runs static analysis on it. It expects a reader, the root parser and the evaluation context.
// If there are multiple possible parse trees only the first one is used for the analysis.
func StaticCheck(ctx *Context, p Parser, evalCtx interface{}) error {
node, parseErr := Parse(ctx, p)
if parseErr != nil {
return parseErr
}

func StaticCheck(ctx *Context, node Node, evalCtx interface{}) error {
switch n := node.(type) {
case StaticCheckable:
if err := n.StaticCheck(evalCtx); err != nil {
Expand Down
53 changes: 12 additions & 41 deletions parsley/static_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ package parsley_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/opsidian/parsley/data"
"github.com/opsidian/parsley/parsley"
"github.com/opsidian/parsley/parsley/parsleyfakes"
)

var _ = Describe("StaticCheck", func() {
var (
r *parsleyfakes.FakeReader
p *parsleyfakes.FakeParser
ctx *parsley.Context
parserRes *parsleyfakes.FakeStaticCheckableNode
parserErr parsley.Error
evalCtx interface{}
checkErr parsley.Error
err error
r *parsleyfakes.FakeReader
ctx *parsley.Context
node *parsleyfakes.FakeStaticCheckableNode
evalCtx interface{}
checkErr parsley.Error
err error
)

BeforeEach(func() {
Expand All @@ -37,46 +34,20 @@ var _ = Describe("StaticCheck", func() {
r = &parsleyfakes.FakeReader{}
ctx = parsley.NewContext(fs, r)
r.PosReturns(parsley.Pos(1))
p = &parsleyfakes.FakeParser{}
parserRes = &parsleyfakes.FakeStaticCheckableNode{}
node = &parsleyfakes.FakeStaticCheckableNode{}
evalCtx = "context"
parserErr = nil
checkErr = nil
err = nil
})

JustBeforeEach(func() {
p.ParseReturns(parserRes, data.EmptyIntSet, parserErr)
if parserRes != nil {
parserRes.StaticCheckReturns(checkErr)
}
err = parsley.StaticCheck(ctx, p, evalCtx)
node.StaticCheckReturns(checkErr)
err = parsley.StaticCheck(ctx, node, evalCtx)
})

It("gets the zero position from the reader", func() {
Expect(r.PosCallCount()).To(Equal(1))
Expect(r.PosArgsForCall(0)).To(Equal(0))
})

It("calls the parser", func() {
Expect(p.ParseCallCount()).To(Equal(1))
passedCtx, passedLeftRecCtx, passedPos := p.ParseArgsForCall(0)
Expect(passedCtx).To(BeEquivalentTo(ctx))
Expect(passedLeftRecCtx).To(BeEquivalentTo(data.EmptyIntMap))
Expect(passedPos).To(Equal(parsley.Pos(1)))
Expect(err).ToNot(HaveOccurred())
})

Context("if the parser has an error", func() {
BeforeEach(func() {
parserRes = nil
err := &parsleyfakes.FakeError{}
err.PosReturns(parsley.Pos(1))
err.ErrorReturns("some error")
parserErr = err
})
It("should return an error", func() {
Expect(err).To(MatchError("failed to parse the input: some error at testpos"))
Context("if the static check succeeds", func() {
It("should return no error", func() {
Expect(err).ToNot(HaveOccurred())
})
})

Expand Down

0 comments on commit 3e2b83c

Please sign in to comment.