forked from nicklockwood/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParserTests.swift
More file actions
76 lines (72 loc) · 2.8 KB
/
ParserTests.swift
File metadata and controls
76 lines (72 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// ParserTests.swift
// ShapeScriptTests
//
// Created by Nick Lockwood on 07/09/2018.
// Copyright © 2018 Nick Lockwood. All rights reserved.
//
@testable import ShapeScript
import XCTest
class ParserTests: XCTestCase {
// MARK: operators
func testRightAssociativity() {
let input = "color 1 + 2 + 3"
let colorRange = input.range(of: "color")!
let range1 = input.range(of: "1")!
let range2 = input.range(of: "2")!
let range3 = input.range(of: "3")!
XCTAssertEqual(try parse(input), Program(source: input, statements: [
Statement(
type: .command(
Identifier(name: "color", range: colorRange),
Expression(
type: .infix(
Expression(type: .number(1), range: range1),
.plus,
Expression(
type: .infix(
Expression(type: .number(2), range: range2),
.plus,
Expression(type: .number(3), range: range3)
),
range: range2.lowerBound ..< range3.upperBound
)
),
range: range1.lowerBound ..< range3.upperBound
)
),
range: colorRange.lowerBound ..< range3.upperBound
),
]))
}
func testOperatorPrecedence() {
let input = "color 1 * 2 + 3"
let colorRange = input.range(of: "color")!
let range1 = input.range(of: "1")!
let range2 = input.range(of: "2")!
let range3 = input.range(of: "3")!
XCTAssertEqual(try parse(input), Program(source: input, statements: [
Statement(
type: .command(
Identifier(name: "color", range: colorRange),
Expression(
type: .infix(
Expression(
type: .infix(
Expression(type: .number(1), range: range1),
.times,
Expression(type: .number(2), range: range2)
),
range: range1.lowerBound ..< range2.upperBound
),
.plus,
Expression(type: .number(3), range: range3)
),
range: range1.lowerBound ..< range3.upperBound
)
),
range: colorRange.lowerBound ..< range3.upperBound
),
]))
}
}