55// Created by Tommy Ludwig on 08.07.24.
66//
77
8- import XCTest
8+ import Testing
99@testable import CodeEdit
1010
11- final class CEActiveTaskTests : XCTestCase {
12- var task : CETask !
13- var activeTask : CEActiveTask !
14-
15- override func setUpWithError( ) throws {
16- try super. setUpWithError ( )
11+ @MainActor
12+ @Suite
13+ class CEActiveTaskTests {
14+ var task : CETask
15+ var activeTask : CEActiveTask
1716
17+ init ( ) {
1818 task = CETask (
1919 name: " Test Task " ,
2020 command: " echo $STATE " ,
@@ -23,76 +23,60 @@ final class CEActiveTaskTests: XCTestCase {
2323 activeTask = CEActiveTask ( task: task)
2424 }
2525
26- override func tearDownWithError( ) throws {
27- task = nil
28- activeTask = nil
29- try super. tearDownWithError ( )
30- }
31-
26+ @Test
3227 func testInitialization( ) throws {
33- XCTAssertEqual ( activeTask. task, task, " Active task should be initialized with the provided CETask. " )
28+ #expect ( activeTask. task == task, " Active task should be initialized with the provided CETask. " )
3429 }
3530
36- func testRunMethod( ) {
37- activeTask. run ( )
38- activeTask. process? . waitUntilExit ( )
39-
40- let testExpectation = XCTestExpectation ( )
31+ @Test ( . timeLimit( . minutes( 1 ) ) )
32+ func testRunMethod( ) async throws {
33+ activeTask. run ( workspaceURL: nil )
34+ activeTask. waitForExit ( )
4135
42- DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 0.2 ) {
43- XCTAssertEqual ( self . activeTask. status, . finished)
44- XCTAssertTrue ( self . activeTask . output . contains ( " Testing " ) )
45- testExpectation . fulfill ( )
36+ await waitForExpectation {
37+ activeTask. status == . finished
38+ } onTimeout : {
39+ Issue . record ( " Status never changed to finished. " )
4640 }
47- wait ( for: [ testExpectation] , timeout: 1 )
48- }
4941
50- // the renew method is needed because a Process can only be run once
51- func testRenewMethod( ) {
52- activeTask. run ( )
53- let testExpectation1 = XCTestExpectation ( )
54- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.3 ) {
55- testExpectation1. fulfill ( )
56- }
57- wait ( for: [ testExpectation1] , timeout: 1 )
42+ let output = try #require( activeTask. output)
43+ #expect( output. getBufferAsString ( ) . contains ( " Testing " ) )
44+ }
5845
59- activeTask. renew ( )
60- activeTask. run ( )
46+ @Test ( . timeLimit( . minutes( 1 ) ) )
47+ func testHandleProcessFinished( ) async throws {
48+ task. command = " aNon-existentCommand "
49+ activeTask. run ( workspaceURL: nil )
50+ activeTask. waitForExit ( )
6151
62- let testExpectation2 = XCTestExpectation ( )
63- DispatchQueue . main . asyncAfter ( deadline : . now ( ) + 0.3 ) {
64- XCTAssertEqual ( self . activeTask . status , . finished )
65- testExpectation2 . fulfill ( )
52+ await waitForExpectation {
53+ activeTask . status == . failed
54+ } onTimeout : {
55+ Issue . record ( " Status never changed to failed. " )
6656 }
67- wait ( for: [ testExpectation2] , timeout: 1 )
6857 }
6958
70- func testHandleProcessFinished( ) {
71- task. command = " aNon-existentCommand "
72- activeTask. run ( )
73- let testExpectation1 = XCTestExpectation ( )
74- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.3 ) {
75- XCTAssertEqual ( self . activeTask. status, . failed)
76- testExpectation1. fulfill ( )
59+ @Test
60+ func testClearOutput( ) async throws {
61+ activeTask. run ( workspaceURL: nil )
62+ activeTask. waitForExit ( )
63+
64+ await waitForExpectation {
65+ activeTask. status == . finished
66+ } onTimeout: {
67+ Issue . record ( " Status never changed to finished. " )
7768 }
78- wait ( for: [ testExpectation1] , timeout: 1 )
79- }
8069
81- func testClearOutput( ) {
82- activeTask. run ( )
83- let testExpectation1 = XCTestExpectation ( )
84- Task {
85- await activeTask. clearOutput ( )
86- testExpectation1. fulfill ( )
70+ #expect(
71+ activeTask. output? . getBufferAsString ( ) . isEmpty == false ,
72+ " Task output should not be empty after task completion. "
73+ )
74+ activeTask. clearOutput ( )
75+
76+ await waitForExpectation {
77+ activeTask. output? . getBufferAsString ( ) . isEmpty == true
78+ } onTimeout: {
79+ Issue . record ( " Task output should be empty after clearOutput is called. " )
8780 }
88- wait ( for: [ testExpectation1] , timeout: 1 )
89- XCTAssertTrue ( activeTask. output. isEmpty)
9081 }
91- // func testClearOutputMethod() async {
92- // // Assuming the task generates some output
93- // await activeTask.run()
94- // XCTAssertFalse(activeTask.output.isEmpty, "Task output should not be empty after task completion.")
95- // await activeTask.clearOutput()
96- // XCTAssertTrue(activeTask.output.isEmpty, "Task output should be empty after clearOutput is called.")
97- // }
9882}
0 commit comments