Fix for crashing range indexes when variable length is 1
This commit is contained in:
@@ -50,6 +50,7 @@ public struct Variable: Equatable, Resolvable {
|
||||
public func resolve(_ context: Context) throws -> Any? {
|
||||
if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) {
|
||||
// String literal
|
||||
guard variable.count > 1 else { return "" }
|
||||
return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)])
|
||||
}
|
||||
|
||||
|
||||
@@ -64,12 +64,24 @@ final class VariableTests: XCTestCase {
|
||||
try expect(result) == "name"
|
||||
}
|
||||
|
||||
it("can resolve a string literal with one double quote") {
|
||||
let variable = Variable("\"")
|
||||
let result = try variable.resolve(self.context) as? String
|
||||
try expect(result) == ""
|
||||
}
|
||||
|
||||
it("can resolve a string literal with single quotes") {
|
||||
let variable = Variable("'name'")
|
||||
let result = try variable.resolve(self.context) as? String
|
||||
try expect(result) == "name"
|
||||
}
|
||||
|
||||
it("can resolve a string literal with one single quote") {
|
||||
let variable = Variable("'")
|
||||
let result = try variable.resolve(self.context) as? String
|
||||
try expect(result) == ""
|
||||
}
|
||||
|
||||
it("can resolve an integer literal") {
|
||||
let variable = Variable("5")
|
||||
let result = try variable.resolve(self.context) as? Int
|
||||
|
||||
Reference in New Issue
Block a user