A table with 1 column.
For an overview of using tables, see the documentation for trait TableDrivenPropertyChecks.
This table is a sequence of objects, where each object represents one row of the (one-column) table. This table also carries with it a heading tuple that gives a string name to the lone column of the table.
A handy way to create a TableFor1
is via an apply
factory method in the Table
singleton object provided by the Tables
trait. Here's an example:
val examples = Table( "a", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
Because you supplied a list of non-tuple objects, the type you'll get back will be a TableFor1
.
The table provides an apply
method that takes a function with a parameter list that matches
the type of the objects contained in this table. The apply
method will invoke the
function with the object in each row passed as the lone argument, in ascending order by index. (I.e.,
the zeroth object is checked first, then the object with index 1, then index 2, and so on until all the rows
have been checked (or until a failure occurs). The function represents a property of the code under test
that should succeed for every row of the table. If the function returns normally, that indicates the property
check succeeded for that row. If the function completes abruptly with an exception, that indicates the
property check failed and the apply
method will complete abruptly with a
TableDrivenPropertyCheckFailedException
that wraps the exception thrown by the supplied property function.
The usual way you'd invoke the apply
method that checks a property is via a forAll
method
provided by trait TableDrivenPropertyChecks
. The forAll
method takes a TableFor1
as its
first argument, then in a curried argument list takes the property check function. It invokes apply
on
the TableFor1
, passing in the property check function. Here's an example:
forAll (examples) { (a) => a should equal (a * 1) }
Because TableFor1
is a Seq[(A)]
, you can use it as a Seq
. For example, here's how
you could get a sequence of Outcome
s for each row of the table, indicating whether a property check succeeded or failed
on each row of the table:
for (row <- examples) yield { outcomeOf { row._1 should not equal (7) } }
Note: the outcomeOf
method, contained in the OutcomeOf
trait, will execute the supplied code (a by-name parameter) and
transform it to an Outcome
. If no exception is thrown by the code, outcomeOf
will result in a
Succeeded
, indicating the "property check"
succeeded. If the supplied code completes abruptly in an exception that would normally cause a test to fail, outcomeOf
will result in
in a Failed
instance containing that exception. For example, the previous for expression would give you:
Vector(Succeeded, Succeeded, Succeeded, Succeeded, Succeeded, Succeeded, Succeeded, Failed(org.scalatest.TestFailedException: 7 equaled 7), Succeeded, Succeeded)
This shows that all the property checks succeeded, except for the one at index 7.
One other way to use a TableFor1
is to test subsequent return values
of a stateful function. Imagine, for example, you had an object named FiboGen
whose next
method returned the next fibonacci number, where next
means the next number in the series following the number previously returned by next
.
So the first time next
was called, it would return 0. The next time it was called
it would return 1. Then 1. Then 2. Then 3, and so on. FiboGen
would need to
be stateful, because it has to remember where it is in the series. In such a situation,
you could create a TableFor1
(a table with one column, which you could alternatively
think of as one row), in which each row represents
the next value you expect.
val first14FiboNums = Table("n", 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233)
Then in your forAll
simply call the function and compare it with the
expected return value, like this:
forAll (first14FiboNums) { n => FiboGen.next should equal (n) }
- Value parameters:
- heading
a string name for the lone column of this table
- rows
a variable length parameter list of objects containing the data of this table
- Companion:
- object
Value members
Concrete methods
Applies the passed property check function to each row of this TableFor1
.
Applies the passed property check function to each row of this TableFor1
.
If the property checks for all rows succeed (the property check function returns normally when passed
the data for each row), this apply
method returns normally. If the property check function
completes abruptly with an exception for any row, this apply
method wraps that exception
in a TableDrivenPropertyCheckFailedException
and completes abruptly with that exception. Once
the property check function throws an exception for a row, this apply
method will complete
abruptly immediately and subsequent rows will not be checked against the function.
- Value parameters:
- fun
the property check function to apply to each row of this
TableFor1
The number of rows of data in the table. (This does not include the heading
tuple)
The number of rows of data in the table. (This does not include the heading
tuple)
Inherited methods
- Inherited from:
- IterableOnceOps
- Inherited from:
- IterableOnceOps
- Inherited from:
- SeqOps
- Inherited from:
- SeqOps
- Inherited from:
- SeqOps
- Definition Classes
- IndexedSeq -> IndexedSeq -> Seq -> Seq -> Iterable -> Iterable -> IterableOps
- Inherited from:
- IndexedSeq
- Inherited from:
- SeqOps
- Inherited from:
- SeqOps
- Inherited from:
- IterableOnceOps
- Inherited from:
- IndexedSeq
- Definition Classes
- IndexedSeqOps -> IndexedSeqOps -> IterableOps -> IterableOnceOps
- Inherited from:
- IndexedSeqOps
- Inherited from:
- IterableOps
Deprecated and Inherited methods
- Deprecated
[Since version 2.13.0]
Use foldLeft instead of /:- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
Use foldRight instead of :\\- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
`aggregate` is not relevant for sequential collections. Use `foldLeft(z)(seqop)` instead.- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
Use iterableFactory instead- Inherited from:
- IterableOps
- Deprecated
[Since version 2.13.0]
Use `dest ++= coll` instead- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
Check .knownSize instead of .hasDefiniteSize for more actionable information (see scaladoc for details)- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
Use segmentLength instead of prefixLength- Inherited from:
- SeqOps
- Deprecated
[Since version 2.13.0]
Use coll instead of repr in a collection implementation, use the collection value itself from the outside- Inherited from:
- IterableOps
- Deprecated
[Since version 2.13.0]
Use .reverseIterator.map(f).to(...) instead of .reverseMap(f)- Inherited from:
- SeqOps
- Deprecated
[Since version 2.13.0]
Iterable.seq always returns the iterable itself- Inherited from:
- Iterable
- Deprecated
[Since version 2.13.7]
toIterable is internal and will be made protected; its name is similar to `toList` or `toSeq`, but it doesn\'t copy non-immutable collections- Inherited from:
- Iterable
- Deprecated
[Since version 2.13.0]
Use .iterator instead of .toIterator- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
Use .to(LazyList) instead of .toStream- Inherited from:
- IterableOnceOps
- Deprecated
[Since version 2.13.0]
toTraversable is internal and will be made protected; its name is similar to `toList` or `toSeq`, but it doesn\'t copy non-immutable collections- Inherited from:
- IterableOps
- Deprecated
[Since version 2.13.0]
Use `concat` instead- Inherited from:
- SeqOps