Skip to content

Commit f398035

Browse files
author
Pedro David Rodríguez Sánchez
committed
Added test and support for test
1 parent 1d9945d commit f398035

6 files changed

Lines changed: 59 additions & 4 deletions

File tree

src/main/kotlin/oop/Flyweight/Admiral.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Admiral : Soldier {
77
val TYPE = 1
88
}
99
init {
10-
Flyweitght.objectInstances++
10+
Flyweight.objectInstances++
1111
}
1212

1313
val attack = 6

src/main/kotlin/oop/Flyweight/Captain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Captain : Soldier {
77
val TYPE = 2
88
}
99
init {
10-
Flyweitght.objectInstances++
10+
Flyweight.objectInstances++
1111
}
1212

1313
val attack = 10

src/main/kotlin/oop/Flyweight/Flyweitght.kt renamed to src/main/kotlin/oop/Flyweight/Flyweight.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package oop.Flyweight
22

33
import java.awt.Point
4-
class Flyweitght{
4+
class Flyweight{
55
companion object{
66
var objectInstances = 0
77
}
@@ -21,6 +21,6 @@ fun main(args: Array<String>) {
2121
soldiers[3].attack(Point(11,7))
2222
soldiers[4].attack(Point(21,3))
2323

24-
System.out.println(Flyweitght.objectInstances)
24+
System.out.println(Flyweight.objectInstances)
2525

2626
}

src/main/kotlin/oop/Flyweight/SoldierClient.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package oop.Flyweight
22

3+
import org.jetbrains.annotations.TestOnly
34
import java.awt.Point
45

56
class SoldierClient(private val type: Int) {
@@ -10,4 +11,7 @@ class SoldierClient(private val type: Int) {
1011
soldier.attack(currentPosition,attackPoint)
1112
}
1213

14+
@TestOnly
15+
fun getSoldier() = soldier
16+
1317
}

src/main/kotlin/oop/Flyweight/SoldierFactory.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package oop.Flyweight
22

3+
import org.jetbrains.annotations.TestOnly
4+
35
class SoldierFactory {
46
companion object {
57
var admiral: Admiral? = null
@@ -22,6 +24,12 @@ class SoldierFactory {
2224
}
2325
throw IllegalArgumentException()
2426
}
27+
28+
@TestOnly
29+
fun clearInstances(){
30+
admiral = null
31+
captain = null
32+
}
2533
}
2634

2735

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package oop.Flyweigth
2+
3+
import oop.Flyweight.*
4+
import org.hamcrest.CoreMatchers.`is`
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.Before
7+
import org.junit.Test
8+
import java.util.*
9+
10+
class FlyweightPatternShould {
11+
12+
@Before
13+
fun initTests(){
14+
Flyweight.objectInstances = 0
15+
SoldierFactory.clearInstances()
16+
}
17+
18+
@Test
19+
fun `Have one instance when only one type of soldier is created`() {
20+
SoldierClient(Admiral.TYPE)
21+
SoldierClient(Admiral.TYPE)
22+
SoldierClient(Admiral.TYPE)
23+
24+
assertThat(Flyweight.objectInstances, `is`(1))
25+
}
26+
@Test
27+
fun `Have two instances when both types are created, no matter the number of soldiers`(){
28+
(1..Random().nextInt(30)).forEach {
29+
SoldierClient(Admiral.TYPE)
30+
SoldierClient(Captain.TYPE)
31+
}
32+
33+
assertThat(Flyweight.objectInstances, `is`(2))
34+
}
35+
36+
@Test
37+
fun `Have the same main object in two instances of the same type`(){
38+
val soldierOne: SoldierClient = SoldierClient(Admiral.TYPE)
39+
val soldierTwo: SoldierClient = SoldierClient(Admiral.TYPE)
40+
41+
assertThat(soldierOne.getSoldier(), `is`(soldierTwo.getSoldier()))
42+
}
43+
}

0 commit comments

Comments
 (0)