calculator tests - #4
Conversation
WalkthroughA new Changes
Sequence Diagram(s)sequenceDiagram
participant Test as CalculatorTest
participant Calc as Calculator
Test->>Calc: divide(4, 2)
Calc-->>Test: 2
Test->>Test: assertEquals(2, result)
Estimated code review effort🎯 2 (Simple) | ⏱️ ~7 minutes Possibly related PRs
Poem
Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/src/main/java/com/coderabbit/app/calculator/Calculator.kt(1 hunks)app/src/test/java/com/coderabbit/app/calculator/CalculatorTest.kt(1 hunks)
| fun divide(a: Int, b: Int): Int { | ||
| return a/b | ||
| } |
There was a problem hiding this comment.
Critical: Missing division by zero validation.
The divide method will throw an ArithmeticException when b is zero, which could crash the application. Add proper validation to handle this edge case.
Apply this diff to add division by zero validation:
fun divide(a: Int, b: Int): Int {
+ if (b == 0) {
+ throw IllegalArgumentException("Division by zero is not allowed")
+ }
return a/b
}Alternatively, consider returning a nullable result or using a Result wrapper:
fun divide(a: Int, b: Int): Int? {
return if (b == 0) null else a / b
}🤖 Prompt for AI Agents
In app/src/main/java/com/coderabbit/app/calculator/Calculator.kt around lines 9
to 11, the divide function lacks validation for division by zero, which can
cause an ArithmeticException and crash the app. Add a check to verify if the
divisor b is zero before performing the division. If b is zero, either throw a
custom exception, return null, or use a Result wrapper to safely handle this
edge case and prevent runtime crashes.
| @Test | ||
| fun `test divide function equals`() { | ||
| val calculator = Calculator() | ||
| assertEquals(calculator.divide(4,2) ,2) | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Test structure looks good, but coverage is incomplete.
The basic divide test follows the established pattern and correctly validates the happy path. However, comprehensive testing for division requires additional edge cases.
Add these additional test cases for comprehensive coverage:
@Test
fun `test divide by zero throws exception`() {
val calculator = Calculator()
assertThrows(IllegalArgumentException::class.java) {
calculator.divide(4, 0)
}
}
@Test
fun `test divide negative numbers`() {
val calculator = Calculator()
assertEquals(calculator.divide(-4, 2), -2)
assertEquals(calculator.divide(4, -2), -2)
assertEquals(calculator.divide(-4, -2), 2)
}
@Test
fun `test integer division truncation`() {
val calculator = Calculator()
assertEquals(calculator.divide(5, 2), 2) // 5/2 = 2.5 → 2
assertEquals(calculator.divide(1, 2), 0) // 1/2 = 0.5 → 0
}Note: You'll also need to add this import for assertThrows:
import org.junit.Assert.assertThrows🤖 Prompt for AI Agents
In app/src/test/java/com/coderabbit/app/calculator/CalculatorTest.kt around
lines 21 to 25, the existing divide function test only covers the basic case. To
improve coverage, add tests for division by zero that assert an
IllegalArgumentException is thrown, tests for dividing negative numbers with
expected signed results, and tests for integer division truncation verifying
correct floor behavior. Also, add the import statement for assertThrows from
org.junit.Assert to support exception testing.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Summary by CodeRabbit
New Features
Tests