|
5 | 5 | // Created by 박승찬 on 1/8/25. |
6 | 6 | // |
7 | 7 |
|
| 8 | +import Domain |
8 | 9 | import XCTest |
9 | 10 |
|
10 | 11 | final class LWWRegisterTests: XCTestCase { |
| 12 | + private var register: LWWRegister! |
| 13 | + private var defaultTimestamp: Timestamp! |
| 14 | + private var defaultDate: Date! |
| 15 | + private var defaultObject: WhiteboardObject! |
11 | 16 |
|
12 | | - override func setUpWithError() throws { |
13 | | - // Put setup code here. This method is called before the invocation of each test method in the class. |
| 17 | + override func setUp() { |
| 18 | + super.setUp() |
| 19 | + defaultObject = TextObject( |
| 20 | + id: UUID(), |
| 21 | + centerPosition: CGPoint(x: 0, y: 0), |
| 22 | + size: CGSize(width: 100, height: 100), |
| 23 | + text: "default") |
| 24 | + defaultDate = Date() |
| 25 | + defaultTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: UUID()) |
| 26 | + register = LWWRegister(whiteboardObject: defaultObject, timestamp: defaultTimestamp) |
14 | 27 | } |
15 | 28 |
|
16 | | - override func tearDownWithError() throws { |
17 | | - // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 29 | + override func tearDown() { |
| 30 | + register = nil |
| 31 | + defaultTimestamp = nil |
| 32 | + defaultDate = nil |
| 33 | + defaultObject = nil |
18 | 34 | } |
19 | 35 |
|
20 | | - func testExample() throws { |
21 | | - // This is an example of a functional test case. |
22 | | - // Use XCTAssert and related functions to verify your tests produce the correct results. |
23 | | - // Any test you write for XCTest can be annotated as throws and async. |
24 | | - // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. |
25 | | - // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. |
| 36 | + // Timestamp가 같을 때 |
| 37 | + func testMergeWhenEqualTimestmap() { |
| 38 | + // 준비 |
| 39 | + let textObject = TextObject( |
| 40 | + id: UUID(), |
| 41 | + centerPosition: CGPoint(x: 50, y: 50), |
| 42 | + size: CGSize(width: 200, height: 200), |
| 43 | + text: "equal") |
| 44 | + let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: defaultTimestamp) |
| 45 | + |
| 46 | + // 실행 |
| 47 | + let sut = register.merge(register: mockRegister) |
| 48 | + |
| 49 | + // 검증 |
| 50 | + XCTAssertEqual(sut, register) |
| 51 | + } |
| 52 | + |
| 53 | + // 새로 들어온 updatedAt이 더 빠를 때 |
| 54 | + func testMergeWhenIncomingTimestampIsEarlier() { |
| 55 | + // 준비 |
| 56 | + let earlierDate = defaultDate.addingTimeInterval(-10) |
| 57 | + let earlierTimestamp = Timestamp(updatedAt: earlierDate, updatedBy: UUID()) |
| 58 | + let textObject = TextObject( |
| 59 | + id: UUID(), |
| 60 | + centerPosition: CGPoint(x: 50, y: 50), |
| 61 | + size: CGSize(width: 200, height: 200), |
| 62 | + text: "incoming") |
| 63 | + let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: earlierTimestamp) |
| 64 | + |
| 65 | + // 실행 |
| 66 | + let sut = register.merge(register: mockRegister) |
| 67 | + |
| 68 | + // 검증 |
| 69 | + XCTAssertEqual(sut, register) |
26 | 70 | } |
27 | 71 |
|
28 | | - func testPerformanceExample() throws { |
29 | | - // This is an example of a performance test case. |
30 | | - self.measure { |
31 | | - // Put the code you want to measure the time of here. |
| 72 | + // updatedAt은 같지만 새로 들어온 UUID가 작을 때 |
| 73 | + func testMergeWhenIncomingTimestampHasSmallerUUID() { |
| 74 | + // 준비 |
| 75 | + guard let smallerUUID = UUID(uuidString: "00000000-0000-0000-0000-000000000000") else { |
| 76 | + XCTFail("Test UUID생성 실패") |
| 77 | + return |
32 | 78 | } |
| 79 | + let smallerTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: smallerUUID) |
| 80 | + let textObject = TextObject( |
| 81 | + id: UUID(), |
| 82 | + centerPosition: CGPoint(x: 50, y: 50), |
| 83 | + size: CGSize(width: 200, height: 200), |
| 84 | + text: "incoming") |
| 85 | + let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: smallerTimestamp) |
| 86 | + |
| 87 | + // 실행 |
| 88 | + let sut = register.merge(register: mockRegister) |
| 89 | + |
| 90 | + // 검증 |
| 91 | + XCTAssertEqual(sut, register) |
33 | 92 | } |
34 | 93 |
|
| 94 | + // 새로 들어온 updatedAt이 더 느릴 때 |
| 95 | + func testMergeWhenIncomingTimestampIsLater() { |
| 96 | + // 준비 |
| 97 | + let laterDate = defaultDate.addingTimeInterval(10) |
| 98 | + let laterTimestamp = Timestamp(updatedAt: laterDate, updatedBy: UUID()) |
| 99 | + let textObject = TextObject( |
| 100 | + id: UUID(), |
| 101 | + centerPosition: CGPoint(x: 50, y: 50), |
| 102 | + size: CGSize(width: 200, height: 200), |
| 103 | + text: "incoming") |
| 104 | + let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: laterTimestamp) |
| 105 | + |
| 106 | + // 실행 |
| 107 | + let sut = register.merge(register: mockRegister) |
| 108 | + |
| 109 | + // 검증 |
| 110 | + XCTAssertEqual(sut, mockRegister) |
| 111 | + } |
| 112 | + |
| 113 | + // updatedAt은 같지만 새로 들어온 UUID가 클 때 |
| 114 | + func testMergeWhenIncomingTimestampHasLargerUUID() { |
| 115 | + // 준비 |
| 116 | + guard let largerUUID = UUID(uuidString: "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF") else { |
| 117 | + XCTFail("Test UUID생성 실패") |
| 118 | + return |
| 119 | + } |
| 120 | + let largerTimestamp = Timestamp(updatedAt: defaultDate, updatedBy: largerUUID) |
| 121 | + let textObject = TextObject( |
| 122 | + id: UUID(), |
| 123 | + centerPosition: CGPoint(x: 50, y: 50), |
| 124 | + size: CGSize(width: 200, height: 200), |
| 125 | + text: "incoming") |
| 126 | + let mockRegister = LWWRegister(whiteboardObject: textObject, timestamp: largerTimestamp) |
| 127 | + |
| 128 | + // 실행 |
| 129 | + let sut = register.merge(register: mockRegister) |
| 130 | + |
| 131 | + // 검증 |
| 132 | + XCTAssertEqual(sut, mockRegister) |
| 133 | + } |
35 | 134 | } |
0 commit comments