-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Rewrote: Expression parser, function, lexer, CSV reader/writer
- Loading branch information
Showing
6 changed files
with
306 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 63 additions & 94 deletions
157
Tests/PoieticCoreTests/Expression/ExpressionParserTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,176 +1,145 @@ | ||
// | ||
// File.swift | ||
// | ||
// ExpressionParserTests.swift | ||
// | ||
// | ||
// Created by Stefan Urbanek on 28/05/2022. | ||
// | ||
|
||
import XCTest | ||
import Testing | ||
@testable import PoieticCore | ||
|
||
|
||
final class ExpressionParserTests: XCTestCase { | ||
func testEmpty() { | ||
@Suite struct ExpressionParserTests { | ||
@Test func empty() { | ||
let parser = ExpressionParser(string: "") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
} | ||
|
||
func testUnexpected() throws { | ||
@Test func unexpectedToken() throws { | ||
let parser = ExpressionParser(string: "$") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
|
||
let parser2 = ExpressionParser(string: "1 1") | ||
#expect(throws: ExpressionSyntaxError.unexpectedToken) { | ||
try parser2.parse() | ||
} | ||
} | ||
|
||
func testBinary() { | ||
let expr = UnboundExpression.binary( | ||
"+", | ||
.variable("a"), | ||
.value(1) | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "a + 1").parse(), expr) | ||
XCTAssertEqual(try ExpressionParser(string: "a+1").parse(), expr) | ||
@Test func parseBinary() throws { | ||
let expr = UnboundExpression.binary( "+", .variable("a"), .value(1) ) | ||
#expect(try ExpressionParser(string: "a + 1").parse() == expr) | ||
#expect(try ExpressionParser(string: "a+1").parse() == expr) | ||
} | ||
|
||
func testBinaryComparison() { | ||
let expr = UnboundExpression.binary( | ||
"<=", | ||
.value(1), | ||
.value(2) | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "1 <= 2").parse(), expr) | ||
XCTAssertEqual(try ExpressionParser(string: "1<=2").parse(), expr) | ||
@Test func binaryComparison() throws { | ||
let expr = UnboundExpression.binary( "<=", .value(1), .value(2) ) | ||
#expect(try ExpressionParser(string: "1 <= 2").parse() == expr) | ||
#expect(try ExpressionParser(string: "1<=2").parse() == expr) | ||
} | ||
|
||
func testFactorAndTermRepetition() { | ||
@Test func factorAndTermRepetition() throws { | ||
let expr = UnboundExpression.binary( | ||
"*", | ||
.binary( | ||
"*", | ||
.variable("a"), | ||
.variable("b") | ||
), | ||
.binary( "*", .variable("a"), .variable("b") ), | ||
.variable("c") | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "a * b * c").parse(), expr) | ||
#expect(try ExpressionParser(string: "a * b * c").parse() == expr) | ||
|
||
let expr2 = UnboundExpression.binary( | ||
"+", | ||
.binary( | ||
"+", | ||
.variable("a"), | ||
.variable("b") | ||
), | ||
.binary( "+", .variable("a"), .variable("b") ), | ||
.variable("c") | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "a + b + c").parse(), expr2) | ||
#expect(try ExpressionParser(string: "a + b + c").parse() == expr2) | ||
} | ||
|
||
func testPrecedence() { | ||
@Test func precedence() throws { | ||
let expr = UnboundExpression.binary( | ||
"+", | ||
.variable("a"), | ||
.binary( | ||
"*", | ||
.variable("b"), | ||
.variable("c") | ||
) | ||
.binary( "*", .variable("b"), .variable("c") ) | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "a + b * c").parse(), expr) | ||
XCTAssertEqual(try ExpressionParser(string: "a + (b * c)").parse(), expr) | ||
#expect(try ExpressionParser(string: "a + b * c").parse() == expr) | ||
#expect(try ExpressionParser(string: "a + (b * c)").parse() == expr) | ||
|
||
let expr2 = UnboundExpression.binary( | ||
"+", | ||
.binary( | ||
"*", | ||
.variable("a"), | ||
.variable("b") | ||
), | ||
.binary( "*", .variable("a"), .variable("b") ), | ||
.variable("c") | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "a * b + c").parse(), expr2) | ||
XCTAssertEqual(try ExpressionParser(string: "(a * b) + c").parse(), expr2) | ||
#expect(try ExpressionParser(string: "a * b + c").parse() == expr2) | ||
#expect(try ExpressionParser(string: "(a * b) + c").parse() == expr2) | ||
} | ||
|
||
func testUnary() { | ||
@Test func unaryExpression() throws { | ||
let expr = UnboundExpression.unary("-", .variable("x")) | ||
XCTAssertEqual(try ExpressionParser(string: "-x").parse(), expr) | ||
#expect(try ExpressionParser(string: "-x").parse() == expr) | ||
|
||
let expr2 = UnboundExpression.binary( | ||
"-", | ||
.variable("x"), | ||
.unary( | ||
"-", | ||
.variable("y") | ||
) | ||
.unary( "-", .variable("y") ) | ||
) | ||
XCTAssertEqual(try ExpressionParser(string: "x - -y").parse(), expr2) | ||
#expect(try ExpressionParser(string: "x - -y").parse() == expr2) | ||
} | ||
func testFunction() { | ||
@Test func functionCall() throws { | ||
let expr = UnboundExpression.function("fun", [.variable("x")]) | ||
XCTAssertEqual(try ExpressionParser(string: "fun(x)").parse(), expr) | ||
#expect(try ExpressionParser(string: "fun(x)").parse() == expr) | ||
|
||
let expr2 = UnboundExpression.function("fun", [.variable("x"), .variable("y")]) | ||
XCTAssertEqual(try ExpressionParser(string: "fun(x,y)").parse(), expr2) | ||
#expect(try ExpressionParser(string: "fun(x,y)").parse() == expr2) | ||
|
||
} | ||
|
||
func testErrorMissingParenthesis() throws { | ||
@Test func errorMissingParenthesis() throws { | ||
let parser = ExpressionParser(string: "(") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
} | ||
func testErrorMissingParenthesisFunctionCall() throws { | ||
@Test func errorMissingParenthesisFunctionCall() throws { | ||
let parser = ExpressionParser(string: "func(1,2,3") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.missingRightParenthesis) | ||
#expect(throws: ExpressionSyntaxError.missingRightParenthesis) { | ||
try parser.parse() | ||
} | ||
} | ||
|
||
func testUnaryExpressionExpected() throws { | ||
@Test func unaryExpressionExpected() throws { | ||
let parser = ExpressionParser(string: "1 + -") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
|
||
let parser2 = ExpressionParser(string: "-") | ||
XCTAssertThrowsError(try parser2.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser2.parse() | ||
} | ||
} | ||
|
||
func testFactorUnaryExpressionExpected() throws { | ||
@Test func factorUnaryExpressionExpected() throws { | ||
let parser = ExpressionParser(string: "1 *") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
} | ||
|
||
func testTermExpressionExpected() throws { | ||
@Test func termExpressionExpected() throws { | ||
let parser = ExpressionParser(string: "1 +") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.expressionExpected) | ||
#expect(throws: ExpressionSyntaxError.expressionExpected) { | ||
try parser.parse() | ||
} | ||
} | ||
|
||
func testUnexpectedToken() throws { | ||
let parser = ExpressionParser(string: "1 1") | ||
XCTAssertThrowsError(try parser.parse()) { | ||
XCTAssertEqual($0 as! ExpressionSyntaxError, ExpressionSyntaxError.unexpectedToken) | ||
} | ||
} | ||
|
||
func testFullText() throws { | ||
@Test func fullText() throws { | ||
// All-in-one, but works. Split this when nodes start mis-behaving. | ||
let text = " - ( a + b ) * f( c, d, 100_000\n)" | ||
let parser = ExpressionParser(string: text) | ||
guard let result = try parser.expression() else { | ||
XCTFail("Expected valid expression to be parsed") | ||
return | ||
} | ||
XCTAssertEqual(text, result.fullText) | ||
let result = try #require(try parser.expression(), | ||
"Expected valid expression to be parsed") | ||
#expect(text == result.fullText) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.