Skip to content

Commit

Permalink
fix: improve validation logic for date of birth in CreateProfile screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrish92 committed Oct 13, 2024
1 parent ae0c64a commit 0729554
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -45,8 +47,8 @@ class CreateProfileTest {
composeTestRule.onNodeWithTag("save_button").performClick()
composeTestRule.waitForIdle()

// Add assertions to verify the behavior after clicking the save button with valid date
// For example, you can check if the date is correctly parsed and saved
assertTrue(validateDate("01/01/2000"))
assertTrue(validateDate("31/12/1999"))
}

@Test
Expand All @@ -59,5 +61,11 @@ class CreateProfileTest {
// Perform click on the save button
composeTestRule.onNodeWithTag("save_button").performClick()
composeTestRule.waitForIdle()

assertFalse(validateDate("32/01/2000")) // Invalid day
assertFalse(validateDate("01/13/2000")) // Invalid month
assertFalse(validateDate("01/01/abcd")) // Invalid year
assertFalse(validateDate("01-01-2000")) // Invalid format
assertFalse(validateDate("01/01")) // Incomplete date
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,12 @@ fun CreateProfile() {

Button(
onClick = {
val parts = age.split("/")
if (parts.size == 3) {
try {
GregorianCalendar(parts[2].toInt(), parts[1].toInt() - 1, parts[0].toInt())
return@Button
} catch (_: NumberFormatException) {
Toast.makeText(context, "Invalid date of birth", Toast.LENGTH_SHORT).show()
}
if (validateDate(age)) {
// Save the profile (future implementation)
Toast.makeText(context, "Profile saved", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context, "Invalid date", Toast.LENGTH_SHORT).show()
}
Toast.makeText(context, "Invalid date of birth", Toast.LENGTH_SHORT).show()
},
enabled = true,
modifier =
Expand All @@ -184,3 +180,19 @@ fun CreateProfile() {
}
})
}

fun validateDate(date: String): Boolean {
val parts = date.split("/")
val calendar = GregorianCalendar.getInstance()
calendar.isLenient = false
if (parts.size == 3) {
return try {
calendar.set(parts[2].toInt(), parts[1].toInt() - 1, parts[0].toInt())
calendar.time
true
} catch (e: Exception) {
false
}
}
return false
}

0 comments on commit 0729554

Please sign in to comment.