Skip to content

Commit 053bf9a

Browse files
committed
Swift: Test the library.
1 parent e04f6bf commit 053bf9a

3 files changed

Lines changed: 50 additions & 18 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
failures
2+
testFailures
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import swift
2+
import codeql.swift.regex.Regex
3+
import TestUtilities.InlineExpectationsTest
4+
5+
bindingset[s]
6+
string quote(string s) { if s.matches("% %") then result = "\"" + s + "\"" else result = s }
7+
8+
module RegexTest implements TestSig {
9+
string getARelevantTag() { result = ["regex", "input"] }
10+
11+
predicate hasActualResult(Location location, string element, string tag, string value) {
12+
exists(RegexEval eval, Expr regex |
13+
eval.getRegex() = regex and
14+
location = regex.getLocation() and
15+
element = regex.toString() and
16+
tag = "regex" and
17+
value = quote(regex.toString())
18+
)
19+
or
20+
exists(RegexEval eval, Expr input |
21+
eval.getInput() = input and
22+
location = input.getLocation() and
23+
element = input.toString() and
24+
tag = "input" and
25+
value = quote(input.toString())
26+
)
27+
}
28+
}
29+
30+
import MakeTest<RegexTest>

swift/ql/test/library-tests/regex/regex.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,36 +102,36 @@ func myRegexpMethodsTests() throws {
102102

103103
// --- Regex ---
104104

105-
_ = try regex.firstMatch(in: input)
106-
_ = try regex.prefixMatch(in: input)
107-
_ = try regex.wholeMatch(in: input)
105+
_ = try regex.firstMatch(in: input) // $ regex=regex input=input
106+
_ = try regex.prefixMatch(in: input) // $ regex=regex input=input
107+
_ = try regex.wholeMatch(in: input) // $ regex=regex input=input
108108

109-
// --- RangeReeplaceableCollection ---
109+
// --- RangeReplaceableCollection ---
110110

111111
var inputVar = input
112-
inputVar.replace(regex, with: "")Var
113-
_ = input.replacing(regex, with: "")
114-
inputVar.trimPrefix(regex)Var
112+
inputVar.replace(regex, with: "") // $ regex=regex input=&...
113+
_ = input.replacing(regex, with: "") // $ regex=regex input=input
114+
inputVar.trimPrefix(regex) // $ regex=regex input=&...
115115

116116
// --- StringProtocol ---
117117

118-
_ = input.range(of: ".*", options: .regularExpression, range: nil, locale: nil)
119-
_ = input.replacingOccurrences(of: ".*", with: "", options: .regularExpression)
118+
_ = input.range(of: ".*", options: .regularExpression, range: nil, locale: nil) // $ MISSING: regex=regex input=input
119+
_ = input.replacingOccurrences(of: ".*", with: "", options: .regularExpression) // $ MISSING: regex=regex input=input
120120

121121
// --- NSRegularExpression ---
122122

123123
let nsregex = try NSRegularExpression(pattern: ".*")
124-
_ = nsregex.numberOfMatches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
125-
nsregex.enumerateMatches(in: input, range: NSMakeRange(0, input.utf16.count), using: {a, b, c in } )
126-
_ = nsregex.matches(in: input, range: NSMakeRange(0, input.utf16.count))
127-
_ = nsregex.firstMatch(in: input, range: NSMakeRange(0, input.utf16.count))
128-
_ = nsregex.rangeOfFirstMatch(in: input, range: NSMakeRange(0, input.utf16.count))
129-
_ = nsregex.replaceMatches(in: NSMutableString(string: input), range: NSMakeRange(0, input.utf16.count), withTemplate: "")
130-
_ = nsregex.stringByReplacingMatches(in: input, range: NSMakeRange(0, input.utf16.count), withTemplate: "")
124+
_ = nsregex.numberOfMatches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count)) // $ regex=nsregex input=input
125+
nsregex.enumerateMatches(in: input, range: NSMakeRange(0, input.utf16.count), using: {a, b, c in } ) // $ regex=nsregex input=input
126+
_ = nsregex.matches(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=nsregex input=input
127+
_ = nsregex.firstMatch(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=nsregex input=input
128+
_ = nsregex.rangeOfFirstMatch(in: input, range: NSMakeRange(0, input.utf16.count)) // $ regex=nsregex input=input
129+
_ = nsregex.replaceMatches(in: NSMutableString(string: input), range: NSMakeRange(0, input.utf16.count), withTemplate: "") // $ regex=nsregex input="call to NSString.init(string:)"
130+
_ = nsregex.stringByReplacingMatches(in: input, range: NSMakeRange(0, input.utf16.count), withTemplate: "") // $ regex=nsregex input=input
131131

132132
// --- NSString ---
133133

134134
let inputNS = NSString(string: "abcdef")
135-
_ = inputNS.range(of: "*", options: .regularExpression)
136-
_ = inputNS.replacingOccurrences(of: ".*", with: "", options: .regularExpression, range: NSMakeRange(0, inputNS.length))
135+
_ = inputNS.range(of: "*", options: .regularExpression) // $ MISSING: regex=nsregex input=inputNS
136+
_ = inputNS.replacingOccurrences(of: ".*", with: "", options: .regularExpression, range: NSMakeRange(0, inputNS.length)) // $ MISSING: regex=nsregex input=inputNS
137137
}

0 commit comments

Comments
 (0)