-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreturn_error_test.go
107 lines (85 loc) · 2.66 KB
/
return_error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package parser_test
import (
"errors"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/conflowio/parsley/data"
"github.com/conflowio/parsley/parser"
"github.com/conflowio/parsley/parsley"
"github.com/conflowio/parsley/parsley/parsleyfakes"
)
var _ = Describe("ReturnError", func() {
var q *parsleyfakes.FakeParser
var ctx *parsley.Context
var leftRecCtx data.IntMap
var pos parsley.Pos
var res, qres parsley.Node
var err, qerr parsley.Error
var cp, qcp data.IntSet
JustBeforeEach(func() {
q = &parsleyfakes.FakeParser{}
q.ParseReturnsOnCall(0, qres, qcp, qerr)
p := parser.ReturnError(q, errors.New("custom error"))
res, cp, err = p.Parse(ctx, leftRecCtx, pos)
})
BeforeEach(func() {
ctx = parsley.NewContext(parsley.NewFileSet(), &parsleyfakes.FakeReader{})
leftRecCtx = data.NewIntMap(map[int]int{1: 2})
pos = parsley.Pos(1)
qres = nil
qcp = data.NewIntSet(1, 2)
qerr = nil
})
Context("when q returns no error", func() {
BeforeEach(func() {
qres = &parsleyfakes.FakeNode{}
})
It("should return the original result", func() {
Expect(res).To(BeEquivalentTo(qres))
Expect(cp).To(BeEquivalentTo(qcp))
Expect(err).ToNot(HaveOccurred())
})
})
Context("when q returns a not found error with the reader's position", func() {
BeforeEach(func() {
qerr = parsley.NewError(pos, parsley.NotFoundError("some error"))
})
It("should return the custom error", func() {
Expect(res).To(BeNil())
Expect(cp).To(BeEquivalentTo(qcp))
Expect(err).To(MatchError("custom error"))
})
})
Context("when q returns a parse error with the reader's position", func() {
BeforeEach(func() {
qerr = parsley.NewErrorf(pos, "some error")
})
It("should return the original error", func() {
Expect(res).To(BeNil())
Expect(cp).To(BeEquivalentTo(qcp))
Expect(err).To(MatchError("some error"))
})
})
Context("when q returns an error with higher position than the reader's position", func() {
BeforeEach(func() {
qerr = parsley.NewErrorf(parsley.Pos(2), "some error")
})
It("should return the original error", func() {
Expect(res).To(BeNil())
Expect(cp).To(BeEquivalentTo(qcp))
Expect(err).To(MatchError("some error"))
})
})
Context("when q returns no result and no error", func() {
It("should return the custom error", func() {
Expect(res).To(BeNil())
Expect(cp).To(BeEquivalentTo(qcp))
Expect(err).To(MatchError("custom error"))
})
})
})