module Main (main) where

import Control.Monad (void)
import Data.List (isSuffixOf)
import Test.Tasty (defaultMain, TestTree, testGroup)
import Test.Tasty.Golden (goldenVsFile)
import System.Directory (getDirectoryContents, removeFile)

import Debug.Trace.File

main :: IO ()
main = do
  -- remove output files
  outputs <- getDirectoryContents "test/Golden/"
  mapM_ removeFile (filter (isSuffixOf ".output") $ map ("test/Golden/" <>) outputs)

  defaultMain goldenTests

goldenTests :: TestTree
goldenTests = testGroup "Debug.Trace.File golden tests"
  [ let fp = "test/Golden/traceFile.output" in
    goldenVsFile "traceFile" "test/Golden/traceFile" fp (pure $! traceFile fp "tracing to file" ())

  , let fp = "test/Golden/traceFile.output" in
    goldenVsFile "traceFileW" "test/Golden/traceFile" fp (pure $! traceFileW fp "tracing to file" ())

  , let fp = "test/Golden/traceFileId.output" in
    goldenVsFile "traceFileId" "test/Golden/traceFileId" fp ((pure $! traceFileId fp "tracing to file") >> pure ())

  , let fp = "test/Golden/traceFileId.output" in
    goldenVsFile "traceFileIdW" "test/Golden/traceFileId" fp ((pure $! traceFileIdW fp "tracing to file") >> pure ())

  , let fp = "test/Golden/traceFileShow.output" in
    goldenVsFile "traceFileShow" "test/Golden/traceFileShow" fp ((pure $! traceFileShow fp (2 :: Int) (3 :: Int)) >> pure ())

  , let fp = "test/Golden/traceFileShow.output" in
    goldenVsFile "traceFileShowW" "test/Golden/traceFileShow" fp ((pure $! traceFileShowW fp (2 :: Int) (3 :: Int)) >> pure ())

  , let fp = "test/Golden/traceFileShowId.output" in
    goldenVsFile "traceFileShowId" "test/Golden/traceFileShowId" fp ((pure $! traceFileShowId fp (3 :: Int)) >> pure ())

  , let fp = "test/Golden/traceFileShowId.output" in
    goldenVsFile "traceFileShowIdW" "test/Golden/traceFileShowId" fp ((pure $! traceFileShowIdW fp (3 :: Int)) >> pure ())

  , let fp = "test/Golden/traceFileWith.output" in
    goldenVsFile "traceFileWith" "test/Golden/traceFileWith" fp ((pure $! traceFileWith fp fst ("hello","world")) >> pure ())

  , let fp = "test/Golden/traceFileWith.output" in
    goldenVsFile "traceFileWithW" "test/Golden/traceFileWith" fp ((pure $! traceFileWithW fp fst ("hello","world")) >> pure ())

  , let fp = "test/Golden/traceFileShowWith.output" in
    goldenVsFile "traceFileShowWith" "test/Golden/traceFileShowWith" fp ((pure $! traceFileShowWith fp length [1 :: Int,2,3]) >> pure ())

  , let fp = "test/Golden/traceFileShowWith.output" in
    goldenVsFile "traceFileShowWithW" "test/Golden/traceFileShowWith" fp ((pure $! traceFileShowWithW fp length [1 :: Int,2,3]) >> pure ())

  , let fp = "test/Golden/traceFileM.output" in
    goldenVsFile "traceFileM" "test/Golden/traceFileM" fp $
      void $ pure $! do
        x <- Just (3 :: Int)
        traceFileM fp ("x: " ++ show x)
        y <- pure 12
        traceFileM fp ("y: " ++ show y)
        pure (x*2 + y)

  , let fp = "test/Golden/traceFileMW.output" in
    goldenVsFile "traceFileMW" "test/Golden/traceFileMW" fp $
      void $ pure $! do
        x <- Just (3 :: Int)
        traceFileMW fp ("x: " ++ show x)
        y <- pure 12
        traceFileMW fp ("y: " ++ show y)
        pure (x*2 + y)

  , let fp = "test/Golden/traceFileShowM.output" in
    goldenVsFile "traceFileShowM" "test/Golden/traceFileShowM" fp $
      void $ pure $! do
        x <- Just (3 :: Int)
        traceFileShowM fp x
        y <- pure 12
        traceFileShowM fp y
        pure (x*2 + y)

  , let fp = "test/Golden/traceFileShowMW.output" in
    goldenVsFile "traceFileShowMW" "test/Golden/traceFileShowMW" fp $
      void $ pure $! do
        x <- Just (3 :: Int)
        traceFileShowMW fp x
        y <- pure 12
        traceFileShowMW fp y
        pure (x*2 + y)

  ]