samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 03 Apr 2020 16:32:36 +0200
changeset 416 9ed8788a1a4e
parent 402 e25dbfce40e9
permissions -rw-r--r--
Using HTTPS to download the libraries
     1 package org.apidesign.effectivelist
     2 
     3 import org.junit._
     4 import Assert._
     5 
     6 class ListTest {
     7   // BEGIN: effectivelist.person
     8   case class Person(name : String, age : Int) extends Listable[Person]
     9   
    10   var list : List[Person] = _
    11   var p1, p2, p3, p4 : Person = _
    12   
    13   @Before def initializeListAndValues: Unit = {
    14     list = new List[Person]
    15     p1 = new Person("Jarda", 39)
    16     p2 = new Person("Sona", 37)
    17     p3 = new Person("Anna", 7)
    18     p4 = new Person("Ondra", 6)
    19   }
    20 
    21   @Test def tryFewAdditions = {
    22     assertTrue(list.add(p1))
    23     assertTrue(list.add(p2))
    24     assertTrue(list.add(p3))
    25     assertTrue(list.add(p4))
    26     
    27     assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    28     assertEquals("Sona was inserted 2nd", 37, list.get(1).age)
    29     assertEquals("Anna was inserted 3rd", "Anna", list.get(2).name)
    30     assertEquals("Anna was inserted 4th", 6, list.get(3).age)
    31   }
    32   // END: effectivelist.person
    33 
    34   @Test def addFewAndThenTryToRemoveHead() = {
    35     assertTrue(list.add(p1))
    36     assertTrue(list.add(p2))
    37     assertTrue(list.add(p3))
    38     
    39     assertEquals("Size it three", 3, list.size())
    40     assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    41 
    42     assertFalse("Can't remove not added element", list.remove(p4))
    43     assertTrue("OK to remove head", list.remove(p1))
    44     
    45     assertEquals("Sona is now first", "Sona", list.get(0).name)
    46     assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
    47     
    48     try {
    49       list.get(2)
    50       fail("Previous call should throw exception")
    51     } catch {
    52       case e : IndexOutOfBoundsException => ; // OK
    53     }
    54   }
    55   
    56   @Test def addFewAndThenTryToRemoveSecond() = {
    57     assertEquals(0, list.size())
    58     assertTrue(list.add(p1))
    59     assertEquals(1, list.size())
    60     assertTrue(list.add(p2))
    61     assertEquals(2, list.size())
    62     assertTrue(list.add(p3))
    63     assertEquals(3, list.size())
    64     
    65     assertEquals("Size it three", 3, list.size())
    66     assertEquals("Jarda was inserted first", "Jarda", list.get(0).name)
    67 
    68     assertFalse("Can't remove not added element", list.remove(p4))
    69     assertTrue("OK to remove head", list.remove(p2))
    70     
    71     assertEquals("Jarda is still first", "Jarda", list.get(0).name)
    72     assertEquals("Anna is now 2nd", "Anna", list.get(1).name)
    73     
    74     try {
    75       list.get(2);
    76       fail("Previous call should throw exception")
    77     } catch {
    78       case e : IndexOutOfBoundsException => ; // OK
    79     }
    80     assertEquals(2, list.size());
    81     
    82     list.remove(p1)
    83     list.remove(p3)
    84     
    85     assertEquals("Not it is empty", 0, list.size())
    86   }
    87 }