Browse Source

Added test for deleteUser function

master
BinHong Lee 7 years ago
parent
commit
82d80e43f9
2 changed files with 19 additions and 6 deletions
  1. +12
    -5
      src/main/kotlin/libsys/UserFactory.kt
  2. +7
    -1
      src/test/kotlin/libsys/UserFactoryTest.kt

+ 12
- 5
src/main/kotlin/libsys/UserFactory.kt View File

@@ -147,7 +147,14 @@ class UserFactory
*/ */
fun getUser(index: Int): User fun getUser(index: Int): User
{ {
return search(index, 0, users.size - 1)
try
{
return search(index, 0, users.size - 1)
}
catch (e: Exception)
{
throw NullPointerException()
}
} }


/** /**
@@ -186,11 +193,11 @@ class UserFactory
} }
} }


fun deleteUser(id: Int): Boolean
fun deleteUser(user: User): Boolean
{ {
try try
{ {
return users.remove(getUser(id))
return users.remove(user)
} }
catch (e: Exception) catch (e: Exception)
{ {
@@ -198,11 +205,11 @@ class UserFactory
} }
} }


fun deleteUser(user: User): Boolean
fun deleteUser(i: Int): Boolean
{ {
try try
{ {
return users.remove(user)
return users.remove(getUser(i))
} }
catch (e: Exception) catch (e: Exception)
{ {


+ 7
- 1
src/test/kotlin/libsys/UserFactoryTest.kt View File

@@ -6,7 +6,8 @@ import java.io.File
/** /**
* Test Book related operations * Test Book related operations
*/ */
class UserFactoryTest {
class UserFactoryTest
{
private var userFactory: UserFactory? = null private var userFactory: UserFactory? = null


init init
@@ -85,6 +86,11 @@ class UserFactoryTest {
val anotherUserFactory = UserFactory("noSuchFile.json") val anotherUserFactory = UserFactory("noSuchFile.json")
Assert.assertEquals("Check ID of new User of non-existent import file", anotherUserFactory.newUser("Some person", 1).id, 0) Assert.assertEquals("Check ID of new User of non-existent import file", anotherUserFactory.newUser("Some person", 1).id, 0)


Assert.assertTrue(userFactory!!.deleteUser(0))
Assert.assertFalse(userFactory!!.deleteUser(0))
Assert.assertTrue(userFactory!!.deleteUser(user2))
Assert.assertFalse(userFactory!!.deleteUser(user2))

try try
{ {
anotherUserFactory.getUser(1) anotherUserFactory.getUser(1)


Loading…
Cancel
Save