Skip to content

Commit

Permalink
Added test to exercise introduction of separate double and int types.
Browse files Browse the repository at this point in the history
  • Loading branch information
quephird committed Mar 22, 2024
1 parent 935a087 commit c2fb58c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion sloxTests/InterpreterTests.swift
Expand Up @@ -50,14 +50,30 @@ final class InterpreterTests: XCTestCase {
}
}

func testInterpretNumericBinaryExpression() throws {
func testInterpretBinaryExpressionInvolvingIntegers() throws {
let input = "21 * 2"
let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)!
let expected: LoxValue = .int(42)
XCTAssertEqual(actual, expected)
}

func testInterpretBinaryExpressionInvolvingDoubles() throws {
let input = "21.0 * 2.0"
let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)!
let expected: LoxValue = .double(42.0)
XCTAssertEqual(actual, expected)
}

func testInterpretBinaryExpressionInvolvingAnIntAndADouble() throws {
let input = "21.0 * 2"
let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)!
let expected: LoxValue = .double(42.0)
XCTAssertEqual(actual, expected)
}

func testInterpretStringlyBinaryExpression() throws {
let input = "\"forty\" + \"-two\""
let interpreter = Interpreter()
Expand Down Expand Up @@ -416,6 +432,19 @@ foo[2]
XCTAssertEqual(actual, expected)
}

func testInterpretAccessingElementOfListWithDouble() throws {
let input = """
var foo = [1, 2, 3, 4, 5];
foo[2.0]
"""

let interpreter = Interpreter()
let expectedError = RuntimeError.indexMustBeAnInteger
XCTAssertThrowsError(try interpreter.interpretRepl(source: input)!) { actualError in
XCTAssertEqual(actualError as! RuntimeError, expectedError)
}
}

func testInterpretMutationOfList() throws {
let input = """
var foo = [1, 2, 3, 4, 5];
Expand Down

0 comments on commit c2fb58c

Please sign in to comment.