1+ import com.lambda.context.ClientContext
2+ import com.lambda.context.SafeContext
3+ import com.lambda.task.RootTask
4+ import com.lambda.task.RootTask.run
5+ import com.lambda.task.Task
6+ import org.junit.jupiter.api.AfterEach
7+ import org.junit.jupiter.api.BeforeEach
8+ import org.junit.jupiter.api.Test
9+ import org.mockito.Mock
10+ import org.mockito.MockedConstruction
11+ import org.mockito.Mockito
12+ import org.mockito.Mockito.`when`
13+ import org.mockito.MockitoAnnotations
14+ import kotlin.test.assertEquals
15+ import kotlin.test.assertTrue
16+
17+ /*
18+ * Copyright 2025 Lambda
19+ *
20+ * This program is free software: you can redistribute it and/or modify
21+ * it under the terms of the GNU General Public License as published by
22+ * the Free Software Foundation, either version 3 of the License, or
23+ * (at your option) any later version.
24+ *
25+ * This program is distributed in the hope that it will be useful,
26+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+ * GNU General Public License for more details.
29+ *
30+ * You should have received a copy of the GNU General Public License
31+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
32+ */
33+
34+
35+ class TaskTest {
36+
37+ @Mock
38+ private lateinit var mockSafeContext: SafeContext
39+
40+ private lateinit var clientContextMock: MockedConstruction <ClientContext >
41+
42+ class TestTask (private val i : Int = 0 ) : Task<Int>() {
43+ override val name get() = " TestTask of $i "
44+
45+ override fun SafeContext.onStart () {
46+ success(i + 1 )
47+ }
48+
49+ override fun SafeContext.onCancel () {
50+ success(i)
51+ }
52+ }
53+
54+ @BeforeEach
55+ fun setUp () {
56+ MockitoAnnotations .openMocks(this )
57+ clientContextMock = Mockito .mockConstruction(ClientContext ::class .java) { mock, _ ->
58+ `when `(mock.toSafe()).thenReturn(mockSafeContext)
59+ }
60+ }
61+
62+ @AfterEach
63+ fun tearDown () {
64+ clientContextMock.close()
65+ RootTask .clear()
66+ }
67+
68+ @Test
69+ fun `test task` () {
70+ val task = TestTask (5 )
71+
72+ assertEquals(task.name, " TestTask of 5" )
73+
74+ task.finally { result ->
75+ assertEquals(result, 6 )
76+ assertEquals(task.state, Task .State .COMPLETED )
77+ assertTrue(task.isCompleted)
78+ }.run ()
79+ }
80+ }
0 commit comments