MyNixOS website logo
Description

Table-driven (by-example) HSpec tests.

hspec-tables

Hackage Stackage Lts Stackage Nightly MIT license Build Status

Test.Hspec.Tables allows you to define table-driven (or, by-example) HSpec tests. For example:

import Test.Hspec
import Test.Hspec.Tables

example1 :: Spec
example1 =
  describe "multiplication table" $
    byExample
      ("x", "y", "result")
      [ (0, 0, 0),
        (0, 1, 0),
        (0, 2, 0),
        (1, 0, 0),
        (1, 1, 1),
        (1, 2, 2),
        (2, 0, 0),
        (2, 1, 2),
        (2, 2, 4)
      ]
      (\a b expected -> a * b == expected)

or

example2 :: Spec
example2 =
  describe "reverse" $
    byExample
      ("list", "reversed")
      [("abc", "cba"), ("", ""), ("0123456", "6543210")]
      (shouldBe . reverse)

When you run these, you'll see that each row becomes a seperate test labelled with show row (so the requirement for rows is to consist of elements that have a Show instance):

Example specs
  multiplication table
    ("x","y","result")
      (0,0,0)
      (0,1,0)
      (0,2,0)
      (1,0,0)
      (1,1,1)
      (1,2,2)
      (2,0,0)
      (2,1,2)
      (2,2,4)
  reverse
    ("list","reversed")
      ("abc","cba")
      ("","")
      ("0123456","6543210")

Finished in 0.0008 seconds
12 examples, 0 failures

The byExample method is type-safe. The table header must be a 2- to 7- element tuple of String and you must provide the same number of "columns" or it doesn't compile. So if you attempted to write:

example1 :: Spec
example1 =
  describe "THIS EXAMPLE DOES NOT COMPILE" $
    byExample
      ("x", "y", "result") -- 3 columns
      [ (0, 0, 0, 0) ]     -- 4 columns
      (\a b c expected -> a * b * c == expected)

then you should get the following error, hopefully guiding to what the problem is

error:
    • Couldn't match type ‘([Char], [Char], [Char])’
                     with ‘(String, String, String, String)’
      Expected type: Test.Hspec.Tables.Header
                       (Integer, Integer, Integer, Integer)
        Actual type: ([Char], [Char], [Char])

The assertion function will match the table type, so if your table is of shape (a, b, c) then the assertion is assumed to be of type (Example e) => a -> b -> c -> e (ie. it's always curried). Example comes from HSpec

Caveats

  • You can define tables up-to 7 columns (adding columns beyond that requires providing an instance of the Table type-class)
Metadata

Version

0.0.1

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows