|
| 1 | +// RUN: %target-run-simple-swift |
| 2 | +// REQUIRES: executable_test |
| 3 | +// REQUIRES: OS=macosx |
| 4 | +// REQUIRES: objc_interop |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import StdlibUnittest |
| 8 | +import SwiftSyntax |
| 9 | + |
| 10 | +func integerLiteralElement(_ int: Int) -> ArrayElementSyntax { |
| 11 | + let literal = SyntaxFactory.makeIntegerLiteral("\(int)") |
| 12 | + return SyntaxFactory.makeArrayElement( |
| 13 | + expression: SyntaxFactory.makeIntegerLiteralExpr(digits: literal), |
| 14 | + trailingComma: nil) |
| 15 | +} |
| 16 | + |
| 17 | +var SyntaxCollectionsAPI = TestSuite("SyntaxCollectionsAPI") |
| 18 | + |
| 19 | +SyntaxCollectionsAPI.test("AppendingElement") { |
| 20 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 21 | + integerLiteralElement(0) |
| 22 | + ]) |
| 23 | + |
| 24 | + let newArrayElementList = arrayElementList.appending(integerLiteralElement(1)) |
| 25 | + |
| 26 | + expectEqual(newArrayElementList.count, 2) |
| 27 | + expectNotNil(newArrayElementList.child(at: 1)) |
| 28 | + expectEqual("\(newArrayElementList.child(at: 1)!)", "1") |
| 29 | +} |
| 30 | + |
| 31 | +SyntaxCollectionsAPI.test("InsertingElement") { |
| 32 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 33 | + integerLiteralElement(1) |
| 34 | + ]) |
| 35 | + |
| 36 | + var newArrayElementList = arrayElementList.inserting(integerLiteralElement(0), at: 0) |
| 37 | + |
| 38 | + expectEqual(newArrayElementList.count, 2) |
| 39 | + expectNotNil(newArrayElementList.child(at: 0)) |
| 40 | + expectEqual("\(newArrayElementList.child(at: 0)!)", "0") |
| 41 | + |
| 42 | + newArrayElementList = newArrayElementList.inserting(integerLiteralElement(2), at: 2) |
| 43 | + |
| 44 | + expectEqual(newArrayElementList.count, 3) |
| 45 | + expectNotNil(newArrayElementList.child(at: 2)) |
| 46 | + expectEqual("\(newArrayElementList.child(at: 2)!)", "2") |
| 47 | +} |
| 48 | + |
| 49 | +SyntaxCollectionsAPI.test("PrependingElement") { |
| 50 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 51 | + integerLiteralElement(1) |
| 52 | + ]) |
| 53 | + |
| 54 | + let newArrayElementList = arrayElementList.prepending(integerLiteralElement(0)) |
| 55 | + |
| 56 | + expectEqual(newArrayElementList.count, 2) |
| 57 | + expectNotNil(newArrayElementList.child(at: 0)) |
| 58 | + expectEqual("\(newArrayElementList.child(at: 0)!)", "0") |
| 59 | +} |
| 60 | + |
| 61 | +SyntaxCollectionsAPI.test("RemovingFirstElement") { |
| 62 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 63 | + integerLiteralElement(0), |
| 64 | + integerLiteralElement(1) |
| 65 | + ]) |
| 66 | + |
| 67 | + let newArrayElementList = arrayElementList.removingFirst() |
| 68 | + |
| 69 | + expectEqual(newArrayElementList.count, 1) |
| 70 | + expectNotNil(newArrayElementList.child(at: 0)) |
| 71 | + expectEqual("\(newArrayElementList.child(at: 0)!)", "1") |
| 72 | +} |
| 73 | + |
| 74 | +SyntaxCollectionsAPI.test("RemovingLastElement") { |
| 75 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 76 | + integerLiteralElement(0), |
| 77 | + integerLiteralElement(1) |
| 78 | + ]) |
| 79 | + |
| 80 | + let newArrayElementList = arrayElementList.removingLast() |
| 81 | + |
| 82 | + expectEqual(newArrayElementList.count, 1) |
| 83 | + expectNotNil(newArrayElementList.child(at: 0)) |
| 84 | + expectEqual("\(newArrayElementList.child(at: 0)!)", "0") |
| 85 | +} |
| 86 | + |
| 87 | +SyntaxCollectionsAPI.test("RemovingElement") { |
| 88 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 89 | + integerLiteralElement(0) |
| 90 | + ]) |
| 91 | + |
| 92 | + let newArrayElementList = arrayElementList.removing(childAt: 0) |
| 93 | + |
| 94 | + expectEqual(newArrayElementList.count, 0) |
| 95 | + expectNil(newArrayElementList.child(at: 0)) |
| 96 | +} |
| 97 | + |
| 98 | +SyntaxCollectionsAPI.test("ReplacingElement") { |
| 99 | + let arrayElementList = SyntaxFactory.makeArrayElementList([ |
| 100 | + integerLiteralElement(0), |
| 101 | + integerLiteralElement(1), |
| 102 | + integerLiteralElement(2) |
| 103 | + ]) |
| 104 | + |
| 105 | + let newArrayElementList = arrayElementList.replacing(childAt: 2, |
| 106 | + with: integerLiteralElement(3)) |
| 107 | + |
| 108 | + expectNotNil(newArrayElementList.child(at: 2)) |
| 109 | + expectEqual("\(newArrayElementList.child(at: 2)!)", "3") |
| 110 | +} |
| 111 | + |
| 112 | +runAllTests() |
0 commit comments