Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: put all unit tests in sources #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ let package = Package(
.target(
name: "Tokenizer",
dependencies: [
"TokenizerMacros"
"TokenizerMacros",
.product(name: "Testing", package: "swift-testing"),
],
swiftSettings: [
.unsafeFlags(["-Xfrontend", "-warn-long-function-bodies=100"], .when(configuration: .debug)),
Expand Down
65 changes: 65 additions & 0 deletions Sources/Tokenizer/Tokenizer.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Testing

@freestanding(codeItem) macro go(emit token: Character) = #externalMacro(module: "TokenizerMacros", type: "GoMacro")
@freestanding(codeItem) macro go(error: ParseError..., emit token: Token...) = #externalMacro(module: "TokenizerMacros", type: "GoMacro")
@freestanding(codeItem) macro go(error: ParseError..., emit token: Token..., to state: State) = #externalMacro(module: "TokenizerMacros", type: "GoMacro")
Expand Down Expand Up @@ -837,3 +839,66 @@ public struct Tokenizer<Sink: TokenSink>: ~Copyable {
self.sink.process(.doctype(self.currentDOCTYPE))
}
}

private struct TestSink {
var tokens = [Token]()
}

extension TestSink: TokenSink {
mutating func process(_ token: consuming Token) {
self.tokens.append(consume token)
}
}

struct TokenizerTests {
@Test func basicHTML() {
let html = #"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>title</title>
</head>
<body>
hi
</body>
</html>
"""#

var tokenizer = Tokenizer(sink: TestSink())
var iter = html.makeIterator()
tokenizer.tokenize(&iter)

let tokens: [Token] = [
.doctype(.init(name: "html")),
"\n",
.tag(Tag(name: "html", kind: .start, attrs: [.init(name: "lang", value: "en")])),
"\n",
.tag(Tag(name: "head", kind: .start)),
"\n",
.tag(Tag(name: "meta", kind: .start, attrs: [.init(name: "charset", value: "UTF-8")], selfClosing: true)),
"\n",
.tag(Tag(name: "title", kind: .start)),
"t",
"i",
"t",
"l",
"e",
.tag(Tag(name: "title", kind: .end)),
"\n",
.tag(Tag(name: "head", kind: .end)),
"\n",
.tag(Tag(name: "body", kind: .start)),
"\n",
"h",
"i",
"\n",
.tag(Tag(name: "body", kind: .end)),
"\n",
.tag(Tag(name: "html", kind: .end)),
.eof,
]
let result = tokenizer.sink.tokens
#expect(result == tokens)
}
}
64 changes: 0 additions & 64 deletions Tests/TokenizerTests/TokenizerTests.swift

This file was deleted.