|
| 1 | +package org.jetbrains.kotlin.junit; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.DisplayName; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | +import org.junit.jupiter.params.ParameterizedTest; |
| 6 | +import org.junit.jupiter.params.provider.ValueSource; |
| 7 | + |
| 8 | +import java.time.LocalDateTime; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +class TodoItemTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + @DisplayName("Should create a new todo item with title and description") |
| 16 | + void shouldCreateTodoItem() { |
| 17 | + var item = new TodoItem("Buy groceries", "Milk, eggs, bread"); |
| 18 | + |
| 19 | + assertNotNull(item.id()); |
| 20 | + assertEquals("Buy groceries", item.title()); |
| 21 | + assertEquals("Milk, eggs, bread", item.description()); |
| 22 | + assertFalse(item.completed()); |
| 23 | + assertNotNull(item.createdAt()); |
| 24 | + assertNotNull(item.updatedAt()); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + @DisplayName("Should generate unique IDs for different items") |
| 29 | + void shouldGenerateUniqueIds() { |
| 30 | + var item1 = new TodoItem("Task 1", "Description 1"); |
| 31 | + var item2 = new TodoItem("Task 2", "Description 2"); |
| 32 | + |
| 33 | + assertNotEquals(item1.id(), item2.id()); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + @DisplayName("Should update title and updatedAt timestamp") |
| 38 | + void shouldUpdateTitle() { |
| 39 | + var item = new TodoItem("Old title", "Description"); |
| 40 | + var originalUpdatedAt = item.updatedAt(); |
| 41 | + |
| 42 | + try { |
| 43 | + Thread.sleep(10); |
| 44 | + } catch (InterruptedException e) { |
| 45 | + Thread.currentThread().interrupt(); |
| 46 | + } |
| 47 | + |
| 48 | + item.setTitle("New title"); |
| 49 | + |
| 50 | + assertEquals("New title", item.title()); |
| 51 | + assertTrue(item.updatedAt().isAfter(originalUpdatedAt)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @DisplayName("Should update description and updatedAt timestamp") |
| 56 | + void shouldUpdateDescription() { |
| 57 | + var item = new TodoItem("Title", "Old description"); |
| 58 | + var originalUpdatedAt = item.updatedAt(); |
| 59 | + |
| 60 | + try { |
| 61 | + Thread.sleep(10); |
| 62 | + } catch (InterruptedException e) { |
| 63 | + Thread.currentThread().interrupt(); |
| 64 | + } |
| 65 | + |
| 66 | + item.setDescription("New description"); |
| 67 | + |
| 68 | + assertEquals("New description", item.description()); |
| 69 | + assertTrue(item.updatedAt().isAfter(originalUpdatedAt)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @DisplayName("Should toggle completion status") |
| 74 | + void shouldToggleCompletion() { |
| 75 | + var item = new TodoItem("Task", "Description"); |
| 76 | + |
| 77 | + assertFalse(item.completed()); |
| 78 | + |
| 79 | + item.setCompleted(true); |
| 80 | + assertTrue(item.completed()); |
| 81 | + |
| 82 | + item.setCompleted(false); |
| 83 | + assertFalse(item.completed()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("Should update updatedAt when setting completion status") |
| 88 | + void shouldUpdateTimestampOnCompletion() { |
| 89 | + var item = new TodoItem("Task", "Description"); |
| 90 | + var originalUpdatedAt = item.updatedAt(); |
| 91 | + |
| 92 | + try { |
| 93 | + Thread.sleep(10); |
| 94 | + } catch (InterruptedException e) { |
| 95 | + Thread.currentThread().interrupt(); |
| 96 | + } |
| 97 | + |
| 98 | + item.setCompleted(true); |
| 99 | + |
| 100 | + assertTrue(item.updatedAt().isAfter(originalUpdatedAt)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("Should format toString correctly for incomplete task") |
| 105 | + void shouldFormatToStringForIncompleteTask() { |
| 106 | + var item = new TodoItem("Buy milk", "From store"); |
| 107 | + |
| 108 | + var result = item.toString(); |
| 109 | + |
| 110 | + assertTrue(result.contains("[ ]")); |
| 111 | + assertTrue(result.contains("Buy milk")); |
| 112 | + assertTrue(result.contains("From store")); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + @DisplayName("Should format toString correctly for completed task") |
| 117 | + void shouldFormatToStringForCompletedTask() { |
| 118 | + var item = new TodoItem("Buy milk", "From store"); |
| 119 | + item.setCompleted(true); |
| 120 | + |
| 121 | + var result = item.toString(); |
| 122 | + |
| 123 | + assertTrue(result.contains("[X]")); |
| 124 | + assertTrue(result.contains("Buy milk")); |
| 125 | + assertTrue(result.contains("From store")); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + @DisplayName("Should compare items by ID for equality") |
| 130 | + void shouldCompareById() { |
| 131 | + var item1 = new TodoItem("Task", "Description"); |
| 132 | + var item2 = new TodoItem("id", "Task", "Description", false, |
| 133 | + LocalDateTime.now(), LocalDateTime.now()); |
| 134 | + |
| 135 | + assertNotEquals(item1, item2); |
| 136 | + |
| 137 | + var item3 = new TodoItem(item1.id(), "Different", "Different", true, |
| 138 | + LocalDateTime.now(), LocalDateTime.now()); |
| 139 | + |
| 140 | + assertEquals(item1, item3); |
| 141 | + assertEquals(item1.hashCode(), item3.hashCode()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + @DisplayName("Should not be equal to null or different class") |
| 146 | + void shouldHandleNullAndDifferentClass() { |
| 147 | + var item = new TodoItem("Task", "Description"); |
| 148 | + |
| 149 | + assertNotEquals(null, item); |
| 150 | + assertNotEquals("String", item); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + @DisplayName("Should maintain createdAt timestamp") |
| 155 | + void shouldMaintainCreatedAt() { |
| 156 | + var item = new TodoItem("Task", "Description"); |
| 157 | + var createdAt = item.createdAt(); |
| 158 | + |
| 159 | + item.setTitle("New title"); |
| 160 | + item.setDescription("New description"); |
| 161 | + item.setCompleted(true); |
| 162 | + |
| 163 | + assertEquals(createdAt, item.createdAt()); |
| 164 | + } |
| 165 | + |
| 166 | + @ParameterizedTest |
| 167 | + @ValueSource(strings = {"", " ", "A", "This is a very long title for a todo item"}) |
| 168 | + @DisplayName("Should handle various title lengths") |
| 169 | + void shouldHandleVariousTitles(String title) { |
| 170 | + var item = new TodoItem(title, "Description"); |
| 171 | + |
| 172 | + assertEquals(title, item.title()); |
| 173 | + } |
| 174 | +} |
0 commit comments