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

Fix crash with missing parameter #130

Merged
merged 5 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions Sources/LeafKit/LeafError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public struct LeafError: Error {
/// Attempt to render an AST with cyclical external references
/// - Provide template name & ordered array of template names that causes the cycle path
case cyclicalReference(String, [String])
/// Parameter missing
case missingParameter
b-nassler marked this conversation as resolved.
Show resolved Hide resolved

// MARK: Wrapped Errors related to Lexing or Parsing
/// Errors due to malformed template syntax or grammar
Expand Down Expand Up @@ -84,6 +86,8 @@ public struct LeafError: Error {
return "\(src) - \(key) cyclically referenced in [\(chain.joined(separator: " -> "))]"
case .lexerError(let e):
return "Lexing error - \(e.localizedDescription)"
case .missingParameter:
return "Expected a parameter but found none"
}
}

Expand Down
32 changes: 22 additions & 10 deletions Sources/LeafKit/LeafParser/LeafParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,20 @@ internal struct LeafParser {

var group = [ParameterDeclaration]()
var paramsList = [ParameterDeclaration]()

func dump() {
defer { group = [] }
if group.isEmpty { return }
group.evaluate()
if group.count > 1 { paramsList.append(.expression(group)) }
else { paramsList.append(group.first!) }

func dump() throws {
defer { group = [] }
if group.isEmpty { return }
group.evaluate()
if group.count > 1 { paramsList.append(.expression(group)) }
else {
guard let first = group.first else {
// It's better to handle this case as well, even though logically it might never happen
// since you're checking if group.isEmpty before.
throw LeafError(.missingParameter, file: #file, function: #function, line: #line, column: #column)
}
paramsList.append(first)
}
}

outer: while let next = peek() {
Expand All @@ -200,7 +207,12 @@ internal struct LeafParser {
let params = try readParameters()
// parameter tags not permitted to have bodies
if params.count > 1 { group.append(.expression(params)) }
else { group.append(params.first!) }
else {
guard let firstParam = params.first else {
b-nassler marked this conversation as resolved.
Show resolved Hide resolved
throw LeafError(.missingParameter)
}
group.append(firstParam)
}
case .parameter(let p):
pop()
switch p {
Expand All @@ -214,11 +226,11 @@ internal struct LeafParser {
}
case .parametersEnd:
pop()
dump()
try dump()
break outer
case .parameterDelimiter:
pop()
dump()
try dump()
case .whitespace:
pop()
continue
Expand Down
16 changes: 16 additions & 0 deletions Tests/LeafKitTests/LeafErrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,20 @@ final class LeafErrorTests: XCTestCase {
XCTFail("Wrong error: \(error.localizedDescription)")
}
}

/// Verify that rendering a template with a missing required parameter will throw `LeafError.missingParameter`
func testMissingParameterError() {
var test = TestFiles()
// Assuming "/missingParam.leaf" is a template that requires a parameter we intentionally don't provide
test.files["/missingParam.leaf"] = """
#(foo.bar.trim())
"""
XCTAssertThrowsError(try TestRenderer(sources: .singleSource(test))
.render(path: "missingParam", context: [:])
.wait()
) {
guard case .missingParameter = ($0 as? LeafError)?.reason else {
return XCTFail("Expected LeafError.missingParameter, got \(String(reflecting: $0))")
}
}
}
Loading