From 4f84627caa594a2982f19284fe03d42e1b39609d Mon Sep 17 00:00:00 2001 From: David Jennes Date: Sat, 22 Sep 2018 00:54:10 +0200 Subject: [PATCH] Add test for crashing --- Tests/StencilTests/LexerSpec.swift | 44 +++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Tests/StencilTests/LexerSpec.swift b/Tests/StencilTests/LexerSpec.swift index 6f49a4c..02f6d5e 100644 --- a/Tests/StencilTests/LexerSpec.swift +++ b/Tests/StencilTests/LexerSpec.swift @@ -61,36 +61,42 @@ class LexerTests: XCTestCase { $0.it("can tokenize an unclosed block") { let lexer = Lexer(templateString: "{%}") - let _ = lexer.tokenize() + _ = lexer.tokenize() + } + + $0.it("can tokenize incorrect syntax without crashing") { + let lexer = Lexer(templateString: "func some() {{% if %}") + _ = lexer.tokenize() } $0.it("can tokenize an empty variable") { let lexer = Lexer(templateString: "{{}}") - let _ = lexer.tokenize() + _ = lexer.tokenize() } $0.it("can tokenize with new lines") { let templateString = """ - My name is {% - if name - and - name - %}{{ - name - }}{% - endif %}. - """ + My name is {% + if name + and + name + %}{{ + name + }}{% + endif %}. + """ - let lexer = Lexer(templateString: templateString) + let lexer = Lexer(templateString: templateString) - let tokens = lexer.tokenize() + let tokens = lexer.tokenize() - try expect(tokens.count) == 5 - try expect(tokens[0]) == Token.text(value: "My name is ", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "My name is")!))) - try expect(tokens[1]) == Token.block(value: "if name and name", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "{%")!))) - try expect(tokens[2]) == Token.variable(value: "name", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "name", options: [.backwards])!))) - try expect(tokens[3]) == Token.block(value: "endif", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "endif")!))) - try expect(tokens[4]) == Token.text(value: ".", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: ".")!))) + try expect(tokens.count) == 5 + try expect(tokens[0]) == Token.text(value: "My name is ", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "My name is")!))) + try expect(tokens[1]) == Token.block(value: "if name and name", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "{%")!))) + try expect(tokens[2]) == Token.variable(value: "name", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "name", options: [.backwards])!))) + try expect(tokens[3]) == Token.block(value: "endif", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: "endif")!))) + try expect(tokens[4]) == Token.text(value: ".", at: SourceMap(location: lexer.rangeLocation(templateString.range(of: ".")!))) + } } } }