diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6199bb5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +# Android Studio +.idea/ +*.iml +.gradle/ +build/ +captures/ + +# Kotlin +*.class +*.kotlin_module + +# Gradle +local.properties +/out/ + +# Logs +*.log + +# Virtual Machine +.DS_Store + +# Dependency directories +/node_modules/ + +# Compiled class files +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs +hs_err_pid* \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..919b394 --- /dev/null +++ b/build.gradle @@ -0,0 +1,16 @@ +plugins { + id 'org.jetbrains.kotlin.jvm' version '1.9.0' +} + +dependencies { + testImplementation 'junit:junit:4.13.2' + testImplementation "org.jetbrains.kotlin:kotlin-test-junit" +} + +repositories { + mavenCentral() +} + +test { + useJUnit() +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..f809b70 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,16 @@ +plugins { + kotlin("jvm") version "1.9.0" +} + +dependencies { + testImplementation("junit:junit:4.13.2") + testImplementation(kotlin("test")) +} + +repositories { + mavenCentral() +} + +tasks.test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/src/main/kotlin/com/todoapp/validation/TodoValidation.kt b/src/main/kotlin/com/todoapp/validation/TodoValidation.kt new file mode 100644 index 0000000..bbcd126 --- /dev/null +++ b/src/main/kotlin/com/todoapp/validation/TodoValidation.kt @@ -0,0 +1,27 @@ +package com.todoapp.validation + +/** + * Provides validation methods for Todo items + */ +object TodoValidation { + /** + * Validates the title of a todo item + * + * @param title The title to validate + * @return Boolean indicating if the title is valid + */ + fun isValidTitle(title: String?): Boolean { + // Check if title is null or empty after trimming whitespace + return !title.isNullOrBlank() + } + + /** + * Throws an exception if the title is invalid + * + * @param title The title to validate + * @throws IllegalArgumentException if title is invalid + */ + fun validateTitle(title: String?) { + require(isValidTitle(title)) { "Todo item title cannot be empty" } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/todoapp/validation/TodoValidationTest.kt b/src/test/kotlin/com/todoapp/validation/TodoValidationTest.kt new file mode 100644 index 0000000..698061d --- /dev/null +++ b/src/test/kotlin/com/todoapp/validation/TodoValidationTest.kt @@ -0,0 +1,67 @@ +package com.todoapp.validation + +import org.junit.Assert.* +import org.junit.Test + +class TodoValidationTest { + @Test + fun `test valid title returns true`() { + // Arrange + val validTitle = "Buy groceries" + + // Act + val result = TodoValidation.isValidTitle(validTitle) + + // Assert + assertTrue("Valid title should return true", result) + } + + @Test + fun `test empty title returns false`() { + // Arrange + val emptyTitle = "" + + // Act + val result = TodoValidation.isValidTitle(emptyTitle) + + // Assert + assertFalse("Empty title should return false", result) + } + + @Test + fun `test null title returns false`() { + // Arrange + val nullTitle: String? = null + + // Act + val result = TodoValidation.isValidTitle(nullTitle) + + // Assert + assertFalse("Null title should return false", result) + } + + @Test + fun `test whitespace-only title returns false`() { + // Arrange + val whitespaceTitle = " " + + // Act + val result = TodoValidation.isValidTitle(whitespaceTitle) + + // Assert + assertFalse("Whitespace-only title should return false", result) + } + + @Test(expected = IllegalArgumentException::class) + fun `test validateTitle throws exception for invalid title`() { + // Act + TodoValidation.validateTitle("") + } + + @Test + fun `test validateTitle does not throw for valid title`() { + // Act + TodoValidation.validateTitle("Valid Title") + // Assert (no exception thrown) + } +} \ No newline at end of file