Repository navigation
Expand file tree
/
Copy pathcodec_regression_test.go
More file actions
177 lines (147 loc) · 7.06 KB
/
Copy pathcodec_regression_test.go
File metadata and controls
177 lines (147 loc) · 7.06 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package uir
import (
"encoding/json"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// roundTripStatement encodes stmt as a block child would be and decodes it back.
func roundTripStatement(stmt Statement) Statement {
GinkgoHelper()
data, err := MarshalStatement(stmt)
Expect(err).NotTo(HaveOccurred())
decoded, err := StatementMarshaler.UnmarshalByType(data)
Expect(err).NotTo(HaveOccurred(), "decoding %s", data)
return decoded
}
var _ = Describe("UIR JSON codec regressions", func() {
const (
sourcePath = "rules/Transaction.xml"
comment = "spawned by the plan"
)
startLine := 12
Describe("MethodCallStmt", func() {
It("keeps a constructor call a constructor, with its location and metadata", func() {
call := NewMethodCall("AddRider", Identifier{Type: "Transactions"}).AsConstructor().Build()
call.Path = sourcePath
call.StartLine = &startLine
call.Comments = []Comment{{Text: comment}}
decoded := roundTripStatement(BlockStmt{Children: []Statement{call}}).(*BlockStmt)
got := decoded.Children[0].(*MethodCallStmt)
Expect(got.IsConstructor).To(BeTrue(), "a constructor rendered as a plain call emits X() instead of new X()")
Expect(got.Path).To(Equal(sourcePath))
Expect(got.StartLine).To(HaveValue(Equal(startLine)))
Expect(got.Comments).To(ConsistOf(HaveField("Text", comment)))
Expect(got.Method).To(HaveValue(Equal(call.Method)), "a reference decodes as a *NodeRef")
})
It("keeps an absent method absent", func() {
decoded := roundTripStatement(MethodCallStmt{}).(*MethodCallStmt)
Expect(decoded.Method).To(BeNil())
})
It("reports a method it cannot decode instead of dropping it", func() {
_, err := StatementMarshaler.UnmarshalByType([]byte(`{"statement_type":"call","Method":{"node_kind":"no-such-kind"}}`))
Expect(err).To(MatchError(ContainSubstring("no-such-kind")))
})
})
Describe("record reads and writes", func() {
// The arch-unit proto converter and the correspondence parser both name the
// record by a bare reference that carries no node type of its own.
It("round-trips a record named by a reference without a node type", func() {
ref := NewRef(Identifier{Package: "dbo", Type: "AsPolicy"})
read := NewRecordRead(RecordTypeTable, ref).WithExpression(ExpressionTypeSQL, "SELECT 1").Build()
got := roundTripStatement(read).(*RecordReadStmt)
Expect(got.Record).To(HaveValue(Equal(ref)))
Expect(got.RecordType).To(Equal(RecordTypeTable), "NewRecordRead's record type must not be discarded")
})
It("keeps the statement location apart from the expression's", func() {
write := NewRecordWrite(RecordTypeTable, NewRef(Identifier{Type: "AsPolicy"})).
WithExpression(ExpressionTypeSQL, "UPDATE AsPolicy SET x = 1").Build()
write.statementBase.Path = sourcePath
write.Path = "inline.sql" // the Expression's, which is the shallower Path
got := roundTripStatement(write).(*RecordWriteStmt)
Expect(got.recordBase.statementBase.Path).To(Equal(sourcePath))
Expect(got.Expression.Path).To(Equal("inline.sql"))
Expect(got.RecordType).To(Equal(RecordTypeTable))
})
})
It("decodes an endpoint call's endpoint", func() {
endpoint := NewEndpoint("getPolicy").Build()
call := NewEndpointCall(EndpointTypeHTTP, endpoint).Build()
got := roundTripStatement(call).(*EndpointCallStmt)
Expect(got.Endpoint).To(Equal(&endpoint))
})
It("keeps a function declaration's statement fields and its method's metadata", func() {
method := NewMethod("compute").Build()
method.Comments = []Comment{{Text: comment}}
decl := FunctionDeclStmt{MethodNode: method}
decl.Path = sourcePath
got := roundTripStatement(decl).(*FunctionDeclStmt)
Expect(got.statementBase.Path).To(Equal(sourcePath))
Expect(got.MethodNode.Comments).To(ConsistOf(HaveField("Text", comment)))
Expect(got.MethodNode.Method).To(Equal("compute"))
})
It("decodes a scoped variable reference held as a block child", func() {
ref := &ScopedVariableRef{VariableRef: VariableRef{Name: "PolicyNumber"}}
got := roundTripStatement(BlockStmt{Children: []Statement{ref}}).(*BlockStmt)
Expect(got.Children).To(ConsistOf(ref))
})
It("keeps an endpoint's name apart from its HTTP method", func() {
endpoint := NewEndpoint("getPolicy").Build()
endpoint.Method = EndpointMethodGet
data, err := MarshalNode(endpoint)
Expect(err).NotTo(HaveOccurred())
got, err := UnmarshalNode(data)
Expect(err).NotTo(HaveOccurred())
Expect(got.GetIdentifier()).To(Equal(endpoint.GetIdentifier()), "the endpoint's identity must survive")
Expect(got.(*ASTEndpoint).Method).To(Equal(EndpointMethodGet))
})
It("keeps both ends of a relationship", func() {
from := NewRef(Identifier{Type: "Caller", Method: "run"})
to := NewRecord("AsPolicy").Build()
rel := NewRelationship(RelationshipTypeRead, from, to).Build()
data, err := json.Marshal(rel)
Expect(err).NotTo(HaveOccurred())
var got UIRRelationship
Expect(json.Unmarshal(data, &got)).To(Succeed(), "decoding %s", data)
Expect(got.GetFrom()).To(HaveValue(Equal(from)))
Expect(got.GetTo()).To(Equal(&to))
})
It("keeps a relationship's reference kind and the construct it was written in under kind and via", func() {
rel := NewRelationship(RelationshipTypeIncludes, NewRef(Identifier{Type: "Caller"}), NewRef(Identifier{Type: "CopyBook-Rates"})).
Kind("copybook").Via("CopyBook").Text("SELECT 1").Build()
data, err := json.Marshal(rel)
Expect(err).NotTo(HaveOccurred())
var keys map[string]json.RawMessage
Expect(json.Unmarshal(data, &keys)).To(Succeed())
Expect(keys).To(HaveKeyWithValue("kind", json.RawMessage(`"copybook"`)))
Expect(keys).To(HaveKeyWithValue("via", json.RawMessage(`"CopyBook"`)))
Expect(keys).To(HaveKeyWithValue("content", json.RawMessage(`"SELECT 1"`)))
var got UIRRelationship
Expect(json.Unmarshal(data, &got)).To(Succeed(), "decoding %s", data)
Expect([]string{got.Kind, got.Via, *got.Content}).To(Equal([]string{"copybook", "CopyBook", "SELECT 1"}))
})
It("omits an unset reference kind and construct", func() {
data, err := json.Marshal(NewRelationship(RelationshipTypeCall, nil, NewRef(Identifier{Type: "Callee"})).Build())
Expect(err).NotTo(HaveOccurred())
var keys map[string]json.RawMessage
Expect(json.Unmarshal(data, &keys)).To(Succeed())
Expect(keys).NotTo(HaveKey("kind"))
Expect(keys).NotTo(HaveKey("via"))
})
It("refuses a node slot holding a typed nil", func() {
var record *ASTRecord
_, err := MarshalStatement(NewRecordRead(RecordTypeTable, record).Build())
Expect(err).To(MatchError(ContainSubstring("nil")))
})
It("treats a null method body and a null branch as absent", func() {
var method MethodNode
Expect(json.Unmarshal([]byte(`{"method":"compute","body":null}`), &method)).To(Succeed())
Expect(method.Body).To(BeNil())
stmt, err := StatementMarshaler.UnmarshalByType([]byte(`{"statement_type":"control:if","then":null}`))
Expect(err).NotTo(HaveOccurred())
Expect(stmt.(*IfStmt).Then.Children).To(BeEmpty())
})
It("refuses a node without a node_kind", func() {
_, err := UnmarshalNode([]byte(`{"type":"AsPolicy","node_type":"record"}`))
Expect(err).To(MatchError(ContainSubstring("node_kind")))
})
})