@@ -54,11 +54,27 @@ fileprivate func expectEqual<T, U: Equatable>(
54
54
fileprivate func assertParse(
55
55
_ str: String ,
56
56
bindings: [ String : String ] = [ : ] ,
57
+ rules: [ String : NinjaBuildFile . Rule ] = [ : ] ,
58
+ edges: [ NinjaBuildFile . BuildEdge ] ,
59
+ file: StaticString = #file, line: UInt = #line
60
+ ) {
61
+ let filePath : AbsolutePath = " /tmp/build.ninja "
62
+ let files : [ AbsolutePath : String ] = [
63
+ filePath: str
64
+ ]
65
+ assertParse ( filePath, in: files, bindings: bindings, rules: rules, edges: edges, file: file, line: line)
66
+ }
67
+
68
+ fileprivate func assertParse(
69
+ _ filePath: AbsolutePath ,
70
+ in fileSystem: [ AbsolutePath : String ] ,
71
+ bindings: [ String : String ] = [ : ] ,
72
+ rules: [ String : NinjaBuildFile . Rule ] = [ : ] ,
57
73
edges: [ NinjaBuildFile . BuildEdge ] ,
58
74
file: StaticString = #file, line: UInt = #line
59
75
) {
60
76
do {
61
- let buildFile = try NinjaParser . parse ( filePath: " /tmp/build.ninja " , input : Data ( str . utf8) )
77
+ let buildFile = try NinjaParser . parse ( filePath: " /tmp/build.ninja " , fileReader : { Data ( fileSystem [ $0 ] ! . utf8) } )
62
78
guard edges. count == buildFile. buildEdges. count else {
63
79
XCTFail (
64
80
" Expected \( edges. count) rules, got \( buildFile. buildEdges. count) " ,
@@ -71,6 +87,10 @@ fileprivate func assertParse(
71
87
buildFile. bindings. values,
72
88
file: file, line: line
73
89
)
90
+ XCTAssertEqual (
91
+ rules, buildFile. rules,
92
+ file: file, line: line
93
+ )
74
94
for (expected, actual) in zip ( edges, buildFile. buildEdges) {
75
95
expectEqual ( expected, actual, \. ruleName, file: file, line: line)
76
96
expectEqual ( expected, actual, \. inputs, file: file, line: line)
@@ -87,12 +107,14 @@ fileprivate func assertParse(
87
107
88
108
class NinjaParserTests : XCTestCase {
89
109
func testBuildEdge( ) throws {
90
- assertParse ( """
110
+ assertParse (
111
+ """
91
112
# ignore comment, build foo.o: a.swift | dep || orderdep
92
113
#another build comment
93
114
build foo.o foo.swiftmodule: SWIFTC a.swift | dep || orderdep
94
115
notpartofthebuildrule
95
- """ , edges: [
116
+ """ ,
117
+ edges: [
96
118
. init(
97
119
ruleName: " SWIFTC " ,
98
120
inputs: [ " a.swift " ] ,
@@ -104,10 +126,66 @@ class NinjaParserTests: XCTestCase {
104
126
)
105
127
}
106
128
129
+ func testRule( ) throws {
130
+ assertParse (
131
+ """
132
+ rule SWIFTC
133
+ command = /bin/switfc -wmo -target unknown
134
+ other = whatever
135
+ notpartoftherule
136
+ """ ,
137
+ rules: [
138
+ " SWIFTC " : . init(
139
+ name: " SWIFTC " ,
140
+ bindings: [
141
+ " command " : " /bin/switfc -wmo -target unknown " ,
142
+ " other " : " whatever " ,
143
+ ] )
144
+ ] ,
145
+ edges: [ ]
146
+ )
147
+ }
148
+
149
+ func testInclude( ) throws {
150
+ let files : [ AbsolutePath : String ] = [
151
+ " /tmp/build.ninja " : """
152
+ include path/to/sub.ninja
153
+
154
+ build foo.swiftmodule : SWIFTC foo.swift
155
+ """ ,
156
+ " /tmp/path/to/sub.ninja " : """
157
+ rule SWIFTC
158
+ command = /bin/swiftc $in -o $out
159
+ """
160
+ ]
161
+ assertParse (
162
+ " /tmp/build.ninja " ,
163
+ in: files,
164
+ rules: [
165
+ " SWIFTC " : . init(
166
+ name: " SWIFTC " ,
167
+ bindings: [
168
+ " command " : " /bin/swiftc $in -o $out " ,
169
+ ] )
170
+ ] ,
171
+ edges: [
172
+ . init(
173
+ ruleName: " SWIFTC " ,
174
+ inputs: [ " foo.swift " ] ,
175
+ outputs: [ " foo.swiftmodule " ] ,
176
+ dependencies: [ ] ,
177
+ bindings: [ : ]
178
+ )
179
+ ]
180
+ )
181
+ }
182
+
107
183
func testPhonyRule( ) throws {
108
- assertParse ( """
184
+ assertParse (
185
+ """
109
186
build foo.swiftmodule : phony bar.swiftmodule
110
- """ , edges: [
187
+ """ ,
188
+ edges: [
111
189
. phony(
112
190
for: [ " foo.swiftmodule " ] ,
113
191
inputs: [ " bar.swiftmodule " ]
@@ -116,8 +194,9 @@ class NinjaParserTests: XCTestCase {
116
194
)
117
195
}
118
196
119
- func testAttributes( ) throws {
120
- assertParse ( """
197
+ func testBindings( ) throws {
198
+ assertParse (
199
+ """
121
200
x = y
122
201
123
202
CONFIGURATION = Debug
@@ -175,12 +254,14 @@ class NinjaParserTests: XCTestCase {
175
254
176
255
func testEscape( ) throws {
177
256
for newline in [ " \n " , " \r " , " \r \n " ] {
178
- assertParse ( """
257
+ assertParse (
258
+ """
179
259
build foo.o$:: SWIFTC xyz$ foo$$.swift | baz$ bar.o
180
260
FLAGS = -I /a$ \( newline) \
181
261
/b -wmo
182
262
COMMAND = swiftc$$
183
- """ , edges: [
263
+ """ ,
264
+ edges: [
184
265
. init(
185
266
ruleName: " SWIFTC " ,
186
267
inputs: [ " xyz foo$.swift " ] ,
0 commit comments