Skip to content

Commit b37bbb5

Browse files
committed
Allow user-defined global constants in capture patterns
1 parent 1c96658 commit b37bbb5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

Sources/FileCheck/FileCheck.swift

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ extension FileCheckSource: ExpressibleByStringLiteral {
101101
/// - parameter FD: The file descriptor to override and read from.
102102
/// - parameter prefixes: Specifies one or more prefixes to match. By default
103103
/// these patterns are prefixed with "CHECK".
104+
/// - parameter globals: Specifies a dictionary of global variables whose
105+
/// names may be used in capture patterns.
104106
/// - parameter checkNot: Specifies zero or more prefixes to implicitly reject
105107
/// in the output stream. This can be used to implement LLVM-verifier-like
106108
/// checks.
@@ -111,7 +113,15 @@ extension FileCheckSource: ExpressibleByStringLiteral {
111113
/// file descriptor.
112114
///
113115
/// - returns: Whether or not FileCheck succeeded in verifying the file.
114-
public func fileCheckOutput(of FD : FileCheckFD = .stdout, withPrefixes prefixes : [String] = ["CHECK"], checkNot : [String] = [], against source : FileCheckSource = #file, options: FileCheckOptions = [], block : () -> ()) -> Bool {
116+
public func fileCheckOutput(
117+
of FD : FileCheckFD = .stdout,
118+
withPrefixes prefixes : [String] = ["CHECK"],
119+
withGlobals globals: [String:String] = [:],
120+
checkNot : [String] = [],
121+
against source : FileCheckSource = #file,
122+
options: FileCheckOptions = [],
123+
block : () -> ()
124+
) -> Bool {
115125
guard let validPrefixes = validateCheckPrefixes(prefixes) else {
116126
print("Supplied check-prefix is invalid! Prefixes must be unique and",
117127
"start with a letter and contain only alphanumeric characters,",
@@ -154,7 +164,7 @@ public func fileCheckOutput(of FD : FileCheckFD = .stdout, withPrefixes prefixes
154164
return false
155165
}
156166

157-
return check(input: input, against: checkStrings, options: options)
167+
return check(input: input, against: checkStrings, withGlobals: globals, options: options)
158168
}
159169

160170
private func overrideFDAndCollectOutput(file : FileCheckFD, of block : () -> ()) -> String {
@@ -511,12 +521,17 @@ private func readCheckStrings(in buf : UnsafeBufferPointer<CChar>, withPrefixes
511521
/// strings read from the check file.
512522
///
513523
/// Returns `false` if the input fails to satisfy the checks.
514-
private func check(input b : String, against checkStrings : [CheckString], options: FileCheckOptions) -> Bool {
524+
private func check(
525+
input b : String,
526+
against checkStrings : [CheckString],
527+
withGlobals globals: [String:String],
528+
options: FileCheckOptions
529+
) -> Bool {
515530
var buffer = Substring(b)
516531
var failedChecks = false
517532

518533
// This holds all the current filecheck variables.
519-
var variableTable = [String:String]()
534+
var variableTable = globals
520535

521536
var i = 0
522537
var j = 0
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import FileCheck
2+
import XCTest
3+
import Foundation
4+
5+
class DefinesSpec : XCTestCase {
6+
func testEnvironmentVariables() {
7+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["PASSDEF"], withGlobals: ["VALUE":"10"]) {
8+
// PASSDEF: Value = [[VALUE]]
9+
print("Value = 10")
10+
})
11+
12+
XCTAssert(fileCheckOutput(of: .stdout, withPrefixes: ["FAILDEF-ERRMSG"]) {
13+
// FAILDEF-ERRMSG: error: {{.*}}: could not find a match for regex 'Value = 20' in input
14+
// FAILDEF-ERRMSG: note: with variable 'VALUE' equal to '20'
15+
// FAILDEF-ERRMSG: note: possible intended match here
16+
XCTAssertFalse(fileCheckOutput(of: .stdout, withPrefixes: ["FAILDEF"], withGlobals: ["VALUE":"20"], options: [.disableColors]) {
17+
// FAILDEF: Value = [[VALUE]]
18+
print("Value = 10")
19+
})
20+
})
21+
}
22+
}
23+

0 commit comments

Comments
 (0)