packages feed

papa-0.4.0: test/Main.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

module Main where

import Papa
import System.Exit (exitFailure, exitSuccess)

assert :: Bool -> String -> IO ()
assert True _ = pure ()
assert False msg = putStrLn ("FAIL: " ++ msg) *> exitFailure

main :: IO ()
main = do
  -- Total (!!) using lens-based safe indexing
  assert (is _Nothing (([] :: [Int]) !! (1 :: Int))) "[] !! 1 == Nothing"
  assert ([10 :: Int, 20, 30] !! (1 :: Int) == Just 20) "[10,20,30] !! 1 == Just 20"

  -- Generalized map (Functor)
  assert (map (+ (1 :: Int)) [1, 2, 3] == [2, 3, 4]) "map (+1) [1,2,3] == [2,3,4]"

  -- Generalized (++) (Semigroup)
  assert ("abc" ++ "def" == "abcdef") "abc ++ def == abcdef"

  -- Generalized lookup (Foldable)
  assert (lookup 'b' [('a', 1 :: Int), ('b', 2)] == Just 2) "lookup 'b' == Just 2"
  assert (is _Nothing (lookup 'z' [('a', 1 :: Int)])) "lookup 'z' == Nothing"

  -- fst/snd via lens (Field1/Field2)
  assert (fst (1 :: Int, 'a') == 1) "fst (1, 'a') == 1"
  assert (snd (1 :: Int, 'a') == 'a') "snd (1, 'a') == 'a'"

  -- mconcat (Foldable Monoid)
  assert (mconcat ["a", "b", "c"] == "abc") "mconcat [a,b,c] == abc"

  -- maximum/minimum (Foldable1)
  assert (maximum (1 :| [2, 3 :: Int]) == 3) "maximum (1 :| [2,3]) == 3"
  assert (minimum (1 :| [2, 3 :: Int]) == 1) "minimum (1 :| [2,3]) == 1"

  -- const (Applicative pure)
  assert ((const 42 :: Maybe Int) == Just 42) "const 42 == Just 42"

  -- Const constructor accessible (issue 6)
  assert (getConst (Const "hello" :: Const String Int) == "hello") "Const constructor"

  putStrLn "All tests passed."
  exitSuccess