Skip to content

Commit

Permalink
Added supporting tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
quephird committed Mar 24, 2024
1 parent 4de1578 commit c1f799e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions sloxTests/InterpreterTests.swift
Expand Up @@ -579,6 +579,52 @@ foo.count
XCTAssertEqual(actual, expected)
}

func testInterpretMappingOverAList() throws {
let input = """
var foo = [1, 2, 3, 4, 5];
foo.map(fun(n) { return n*n; })
"""

let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)
let expected = try interpreter.makeList(elements: [
.int(1),
.int(4),
.int(9),
.int(16),
.int(25),
])
XCTAssertEqual(actual, expected)
}

func testInterpretFilteringAList() throws {
let input = """
var foo = [1, 2, 3, 4, 5];
foo.filter(fun(n) { return n<=3; })
"""

let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)
let expected = try interpreter.makeList(elements: [
.int(1),
.int(2),
.int(3),
])
XCTAssertEqual(actual, expected)
}

func testInterpretReducingOverAList() throws {
let input = """
var foo = [1, 2, 3, 4, 5];
foo.reduce(0, fun(acc, n) { return acc+n; })
"""

let interpreter = Interpreter()
let actual = try interpreter.interpretRepl(source: input)
let expected: LoxValue = .int(15)
XCTAssertEqual(actual, expected)
}

func testInterpretForLoopWithBreakStatement() throws {
let input = """
var sum = 0;
Expand Down

0 comments on commit c1f799e

Please sign in to comment.