I was trying to use DataTables in specs2 to define both input and how the result should look like and could not get it to work. I was thinking something similar to the below code:
class MySpec extends Specification with DataTables {
"A Container" should {
"after data is added container should have the following data" in new TestContainer {
"a" | "flag" | "d" |
100 ! 1 ! "abc" |
300 ! 1 ! "abc" |
200 ! 0 ! "xyz" |>
{ (a, flag, d) =>
container.add(Data(a, flag, d)) must not(throwA[Exception])
} and
"a" | "flag" | "d" |
300 ! 1 ! "abc" |
100 ! 1 ! "abc" |>
{ (a, flag, d) => ????
}
}
}
Disclaimer: I am new to scala and specs. Some of the code was omitted for brevity.
After gaining better understanding of specs2 here is a solution I came up with:
class MySpec extends Specification with DataTables {
"A Container" should {
"after data is added container should have the following data" in new TestContainer {
"a" | "flag" | "d" |
100 ! 1 ! "abc" |
300 ! 1 ! "abc" |
200 ! 0 ! "xyz" |>
{ (a, flag, d) =>
container.add(Data(a, flag, d)) must not(throwAn[Exception])
}
val state = container.list
"a" | "flag" | "d" |
300 ! 1 ! "abc" |
100 ! 1 ! "abc" |>
{ (a, flag, d) => state must contain((a, flag, d))
}
}
}
If order is important then second table can add tuples to some list and then compare 2 lists after second table is processed. Please, note this behavior is broken in 2.3, but works in 2.4-SNAPSHOT.