{-# OPTIONS -fglasgow-exts #-}
-- module BasicTests where

import EmacsMonad

import Control.Applicative
import Control.Monad
import Data.Maybe
import Data.List
import Data.Generics
import Data.Typeable
import System.Environment
import Test.HUnit 

deriving instance Typeable Test.HUnit.Test
deriving instance Data     Test.HUnit.Test

main :: IO ()
main = do 
  args <- getArgs
  let accept = "accept" `elem` args
      tests  = maybe all_tests                      
                     (\names -> [ t | t@(EmacsMonad.Test name _) <- all_tests, name `elem` words names])
                     (drop 6 <$> find ("TESTS=" `isPrefixOf`) args)
  if accept
     then forM_ tests $ \t -> putStrLn ("Accepting " ++ testName t ++ "...") >> acceptTest t
     else runTestTT (TestList (map hunit tests)) >> return ()

all_tests = [ test1
            , test2
            , test3
            , test4
            , test5 ]

instance Show a => Assertable (Either a b) where
  assert (Left err) = error (show err)
  assert (Right _ ) = return ()

mkTest = EmacsMonad.Test
hunit t@(EmacsMonad.Test name _) = TestLabel name $ test (runTest t)

test1 = mkTest "test1" $ do 
  load "project/Foo/Bar.hs"
  simpleCompleteId "project/Foo/Bar.hs" "countAtE" 

test2 = mkTest "test2" $ do 
  load "project/Foo/Bar.hs"
  fuzzyCompleteId "project/Foo/Bar.hs" "countAtE" 

test3 = mkTest "test3" $ do 
  load "project/Foo/Bar.hs"
  lookupType "project/Foo/Bar.hs" 7 27

test4 = mkTest "test4" $ do 
  load "project/Foo/Bar.hs"
  findDefinition "project/Foo/Bar.hs" 9 26

test5 = mkTest "test5" $ do 
  load "project/Foo/Bar.hs"
  findDefinition "project/Foo/Bar.hs" 7 27

