Open Hikapasya05 opened 1 year ago
2
this unit testing i'm doing is from example 2. the code there works to make a sequence of battle between mega charizard and mewtwo, where mega charizard attacks mewtwo for 25 damage and mewtwo attacks back after that dealing 20 damage. But in a case where mewtwo's health is >=25, it doesnt get to attack charizard back because it fainted.
fun megacharizardTurnAttackMewtwo(
mewtwoHealth : Int,
megacharizardDamage : Int,
mewtwoState : Boolean
): Boolean {
return if (mewtwoHealth <= megacharizardDamage && !mewtwoState) {
true
} else if (mewtwoHealth > megacharizardDamage && mewtwoState) {
true
} else {
false
}
}
this test unit code is an extracted method from example 2 where it takes 3 parameters, the health of mewtwo, the damage from mega charizard, and whether mewtwo is still alive or not (mewtwo state). if mewtwo's health is lower than mega charizard's damage, mewtwo state should be false. and if mewtwo's health is above charizard's health, mewtwo's state should be true.
@Test
fun megacharizardTurn_mewtwoEqualDamageAlive_returnFalse() {
val result = Lab2Unit.megacharizardTurnAttackMewtwo(
25,
25,
true
)
assertFalse(result)
}
so in this test, the turn starts with mewtwo's health being equal to charizard's damage, thus after testing the code it will return false because mewtwo will not be alive after taking the amount of damage with that health remaining.
@Test
fun megacharizardTurn_mewtwoEqualDamageAlive_returnTrue() {
val result = Lab2Unit.megacharizardTurnAttackMewtwo(
25,
25,
false
)
assertTrue(result)
}
so in this test, the turn starts with mewtwo's health being equal to charizard's damage, thus after testing the code it will return true because mewtwo will not be alive after taking the amount of damage with that health remaining.
@Test
fun megacharizardTurn_mewtwoAboveDamageDead_returnFalse() {
val result = Lab2Unit.megacharizardTurnAttackMewtwo(
50,
25,
false
)
assertFalse(result)
}
so in this test, the turn starts with mewtwo's health being more than charizard's damage, thus after testing the code it will return false because mewtwo will still be alive after taking the amount of damage with that health remaining.
@Test
fun megacharizardTurn_mewtwoAboveDamageAlive_returnTrue() {
val result = Lab2Unit.megacharizardTurnAttackMewtwo(
50,
25,
true
)
assertTrue(result)
}
so in this test, the turn starts with mewtwo's health being more than charizard's damage, thus after testing the code it will return true because mewtwo will still be alive after taking the amount of damage with that health remaining.
Example 1
This codes functions to show the health bar of each pokemon subjected to it, by implementing a progress bar for each pokemon.
Duplicate code is considered bad for several reasons. Firstly, it violates the DRY (Don't Repeat Yourself) principle, which emphasizes the importance of code reuse and maintaining a single source of truth.
Example 2
This code is to activate the attack button on a pokemon which then when clicked it will decrease the value of the progress bar or health of the opposite pokemon.
Having duplicate code can lead to various issues. It makes the codebase harder to maintain since any changes or bug fixes need to be applied to multiple places, increasing the chances of inconsistencies.
Example 3
lucario_battle.kt
charizard_battle.kt
This code is for the transition of a battle for when the opposite pokemon's progress bar or health has reached 0 , it will then continue to another activity.
Duplicated code can lead to a proliferation of bugs, as fixing an issue in one instance may be overlooked in others. It can also hinder code evolution and scalability.
Example 4
activity_scene_1.xml
activity_scene_2.xml
</androidx.constraintlayout.widget.ConstraintLayout>