Skip to content

Commit 62c2019

Browse files
committed
Progress
1 parent c12fece commit 62c2019

File tree

5 files changed

+104
-16
lines changed

5 files changed

+104
-16
lines changed

Sources/App/TestRunner.swift

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import Foundation
2+
3+
func RunTest(_ testInput: TestInput) -> TestOutput {
4+
5+
if testInput.regex == nil || testInput.regex == "" {
6+
return TestOutput(success: false, html: nil, message: "No regular expression to test!")
7+
}
8+
9+
let regexStr = testInput.regex ?? ""
10+
let replacement = testInput.replacement ?? ""
11+
12+
var html = String()
13+
html += "<table class=\"table table-bordered table-striped\" style=\"width:auto;\">"
14+
html += "<tbody>"
15+
html += "<tr>"
16+
html += "<td>Regular Expression</td>"
17+
html += "<td>\(escapeHtml(regexStr))</td>"
18+
html += "</tr>"
19+
if replacement != "" {
20+
html += "<tr>"
21+
html += "<td>Replacement</td>"
22+
html += "<td>\(escapeHtml(replacement))</td>"
23+
html += "</tr>"
24+
}
25+
html += "</tbody>"
26+
html += "</table>"
27+
28+
do {
29+
_ = try Regex(regexStr)
30+
} catch {
31+
return TestOutput(success: false, html: html, message: "Invalid regular expression! Error: \(error.localizedDescription)")
32+
}
33+
34+
let regex = try! Regex(regexStr)
35+
36+
if testInput.input == nil || testInput.input!.count == 0 {
37+
return TestOutput(success: false, html: html, message: nil)
38+
}
39+
40+
html += "<table class=\"table table-bordered table-striped\" style=\"width:auto;\">"
41+
html += "<thead>"
42+
html += "<tr>"
43+
html += "<th>Test</th>"
44+
html += "<th>Target String</th>"
45+
html += "<th>contains</th>"
46+
html += "<th>replacingOccurrences</th>"
47+
html += "<th>wholeMatch</th>"
48+
html += "</tr>"
49+
html += "</thead>"
50+
html += "<tbody>"
51+
var testIndex = 0;
52+
for input in testInput.input! {
53+
testIndex += 1
54+
if input == "" {
55+
continue
56+
}
57+
let contains = input.contains(regexStr)
58+
let replace = input.replacingOccurrences(of: regexStr, with: replacement, options: testInput.option?.contains("i") == true ? .caseInsensitive : [])
59+
let wholeMatch = input.wholeMatch(of: regex);
60+
let wholeMatchStr = wholeMatch == nil ? "<i>nil</i>" : escapeHtml("\(wholeMatch)");
61+
html += "<tr>"
62+
html += "<td class=\"text-center\">\(testIndex)</td>"
63+
html += "<td>\(escapeHtml(input))</td>"
64+
html += "<td>\(contains ? "yes" : "no")</td>"
65+
html += "<td>\(escapeHtml(replace))</td>"
66+
html += "<td>\(wholeMatchStr)</td>"
67+
html += "</tr>"
68+
}
69+
html += "</tbody>"
70+
html += "</table>"
71+
72+
return TestOutput(success: true, html: html, message: nil)
73+
}
74+
75+
import Foundation
76+
77+
func escapeHtml(_ input: String) -> String {
78+
var escapedString = input
79+
escapedString = escapedString.replacingOccurrences(of: "&", with: "&amp;")
80+
escapedString = escapedString.replacingOccurrences(of: "<", with: "&lt;")
81+
escapedString = escapedString.replacingOccurrences(of: ">", with: "&gt;")
82+
return escapedString
83+
}

Sources/App/configure.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Vapor
44
public func configure(_ app: Application) async throws {
55
app.http.server.configuration.hostname = Environment.get("HOSTNAME") ?? "0.0.0.0"
66
app.http.server.configuration.port = Environment.get("PORT").flatMap(Int.init) ?? 4000
7-
app.http.server.configuration.serverName = "vapor"
7+
app.http.server.configuration.serverName = "regexplanet-swift"
88

99
// uncomment to serve files from /Public folder
1010
app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

Sources/App/routes.swift

+16-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@ func routes(_ app: Application) throws {
66
//LATER: req.redirect(to: "https://www.regexplanet.com/advanced/swift/index.html", redirectType: .normal)
77
}
88

9+
app.get("test.json") { req -> Response in
10+
let testInput = try req.query.decode(TestInput.self)
11+
12+
let retVal = RunTest(testInput);
13+
14+
return try handleJsonp(req: req, from: retVal)
15+
}
16+
917
app.post("test.json") { req -> Response in
1018
let testInput = try req.content.decode(TestInput.self)
11-
let retVal = TestOutput(
12-
success: true,
13-
html: "\(testInput.regex): \(testInput.replacement)"
14-
)
19+
20+
let retVal = RunTest(testInput);
1521

1622
return try handleJsonp(req: req, from: retVal)
1723
}
@@ -36,15 +42,16 @@ struct JsonpParams: Content {
3642
}
3743

3844
struct TestInput: Content {
39-
let regex: String
40-
let replacement: String
41-
let options: [String]
42-
let input: [String]
45+
let regex: String?
46+
let replacement: String?
47+
let option: [String]?
48+
let input: [String]?
4349
}
4450

4551
struct TestOutput: Content {
4652
let success: Bool
47-
let html: String
53+
let html: String?
54+
let message: String?
4855
}
4956

5057
struct StatusResponse: Content {

TODO.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,5 @@
22

33
- [ ] LICENSE.txt
44
- [ ] README
5-
- [ ] CloudRun service
6-
- [ ] Github Actions
7-
- [ ] status.json: https://github.com/vapor/university/blob/main/api/Sources/App/Controllers/Tutorials.swift
8-
- [ ] redirect for '/': req.redirect(to: "/some/new/path")
9-
- [ ] error handling, logging, cors: https://docs.vapor.codes/4.0/middleware/
5+
- [ ] test functionality
106
- [ ] regex timeout

run.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ set -o errexit
77
set -o pipefail
88
set -o nounset
99

10+
clear
11+
1012
#
1113
# load an .env file if it exists
1214
#
@@ -16,4 +18,4 @@ if [ -f "${ENV_FILE}" ]; then
1618
export $(cat "${ENV_FILE}")
1719
fi
1820

19-
swift run
21+
swift run

0 commit comments

Comments
 (0)