Skip to content

Instantly share code, notes, and snippets.

@brentdax brentdax/quotetest.swift
Last active May 3, 2016

Embed
What would you like to do?
Tests for new quoting features.
#!/Library/Developer/Toolchains/swift-LOCAL-2016-05-02-a.xctoolchain/usr/bin/swift
// Change the line above if your prototype Swift interpreter is installed somewhere else.
// The easiest way to run this on El Capitan is:
// $ perl /usr/bin/prove5.18 quotetest.swift --verbose
/// Test output generator. Skip down to the comment that says "BEGIN TESTS" for the good stuff.
class Plan {
enum Verbosity {
case Always, Unexpected, Never
func beVerbose(passed: Bool, expected: Bool) -> Bool {
switch self {
case .Always:
return true
case .Unexpected:
return passed != expected
case .Never:
return false
}
}
}
var currentTest = 1
var expectedTests: Int
var verbosity: Verbosity = .Unexpected
init(_ tests: Range<Int>) {
currentTest = tests.startIndex
expectedTests = tests.endIndex - 1
}
func eq(_ experimental: @autoclosure () -> String, _ control: String, _ message: String, todo: String? = nil, file: String = #file, line: Int = #line) {
ok((experimental() == control), message, todo: todo, file: file, line: line) { _ in
print("# Expected:")
for line in control.lines {
print("# \(line)")
}
print("# Got:")
for line in experimental().lines {
print("# \(line)")
}
}
}
func ok(_ passed: @autoclosure () -> Bool, _ message: String, todo: String? = nil, file: String = #file, line: Int = #line, verboseHandler: @noescape (Bool) -> Void = { _ in }) {
if currentTest == 1 {
print("\(currentTest)..\(expectedTests)")
}
let directive: String
let expect: Bool
if let todo = todo {
directive = " # TODO \(todo)"
expect = false
}
else {
directive = ""
expect = true
}
let pass = passed()
if pass {
print("ok \(currentTest) - \(message)\(directive)")
}
else {
print("not ok \(currentTest) - \(message)\(directive)")
}
if verbosity.beVerbose(passed: pass, expected: expect) {
print("# Location: \(file):\(line)")
verboseHandler(pass)
}
currentTest += 1
}
}
extension String {
func split(separator: Character) -> [String] {
return characters.split(separator: separator).map(String.init)
}
var lines: [String] {
return split(separator: "\n")
}
}
///////////////
//
// BEGIN TESTS
//
///////////////
let plan = Plan(1...15)
// Variables used by various interpolation tests.
let author = "J.K. Rowling"
let url = "http://www.swift.org"
// The `this` variable shouldn't be used unless there's a bug.
let this = "incorrect"
plan.eq(
"I know my sister like I know my own mind
"You will never find anyone as trusting or as kind
"If I tell her that I love him she'd be silently resigned
"He'd be mine
"She would say, \"I'm fine\"
"She'd be lying
"",
"I know my sister like I know my own mind\nYou will never find anyone as trusting or as kind\nIf I tell her that I love him she'd be silently resigned\nHe'd be mine\nShe would say, \"I'm fine\"\nShe'd be lying\n",
"Multiline English text, escaped quotes, trailing newline through blank line"
)
plan.eq(
"<?xml version=\"1.0\"?>
"<catalog>
" <book id=\"bk101\" empty=\"\">
" <author>\(author)</author>
" </book>
"</catalog>",
"<?xml version=\"1.0\"?>\n<catalog>\n <book id=\"bk101\" empty=\"\">\n <author>J.K. Rowling</author>\n </book>\n</catalog>",
"Multiline markup generation, escaped quotes, interpolated text, no trailing newline"
)
plan.eq(
e"C:\Program Files\Microsoft Word",
"C:\\Program Files\\Microsoft Word",
"Example of unescaped backslashes"
)
plan.eq(
e"\\",
"\\\\",
"Example of unescaped multiple backslashes",
todo: "`e` should change handling of escapes with known meanings"
)
plan.eq(
e"You should see a backslash: \", // this should be a comment, not part of the literal",
"You should see a backslash: \\",
"Example of unescaped single backslashes",
todo: "`e` should change handling of escapes with known meanings"
)
plan.eq(
e"\w+",
"\\w+",
"Example of unescaped regex"
)
plan.eq(
e"Interpolation looks like
"\(this)",
"Interpolation looks like \n\\(this)",
"Unescaped interpolation",
todo: "`e` should change handling of escapes with known meanings"
)
plan.eq(
_"<a href="\(url)">"_,
"<a href=\"http://www.swift.org\">",
"Alternate delimiter string with interpolation"
)
plan.eq(
_"print("Hello, world!\n")"_,
"print(\"Hello, world!\n\")",
"Alternate delimiter string with escape"
)
plan.eq(
_""[^"\\\\]*(\\.[^"\\\\]*)*+""_,
"\"[^\"\\\\]*(\\.[^\"\\\\]*)*+\"",
"Regex with alternate delimiter"
)
plan.eq(
_"I know my sister like I know my own mind
"You will never find anyone as trusting or as kind
"If I tell her that I love him she'd be silently resigned
"He'd be mine
"She would say, "I'm fine"
"She'd be lying
""_,
"I know my sister like I know my own mind\nYou will never find anyone as trusting or as kind\nIf I tell her that I love him she'd be silently resigned\nHe'd be mine\nShe would say, \"I'm fine\"\nShe'd be lying\n",
"Multiline English text, alternate delimiters, trailing newline through blank line"
)
plan.eq(
_"<?xml version="1.0"?>
"<catalog>
" <book id="bk101" empty="">
" <author>\(author)</author>
" </book>
"</catalog>"_,
"<?xml version=\"1.0\"?>\n<catalog>\n <book id=\"bk101\" empty=\"\">\n <author>J.K. Rowling</author>\n </book>\n</catalog>",
"Multiline markup generation, alternate delimiters, interpolated text, no trailing newline"
)
plan.eq(
e_""\w+""_,
"\"\\w+\"",
"Simple regex with alternate delimiter and no escapes"
)
plan.eq(
e_"print("Hello, world!\n")"_,
"print(\"Hello, world!\\n\")",
"Alternate delimiter string without escapes",
todo: "`e` should change handling of escapes with known meanings"
)
plan.eq(
e_""[^"\\]*(\\.[^"\\]*)*+""_,
"\"[^\"\\\\]*(\\.[^\"\\\\]*)*+\"",
"Complex regex with alternate delimiter and no escapes",
todo: "`e` should change handling of escapes with known meanings"
)
/// Disabled from here down: These test error cases, which I don't
/// necessarily expect to be diagnosed very well in the prototype.
/// Forgot a continuation quote
// print("Hello,
// world!")
/// Stray character before continuation quote
// print("Hello,
// , "world!")
/// Forgot end quote
// let xml = "<?xml version=\"1.0\"?>
// "<catalog>
// " <book id=\"bk101\" empty=\"\">
// " <author>\(author)</author>
// " </book>
// "</catalog>
// print(xml)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.