NXT / tests / Main.hs

module Main where

import Control.Exception
import Control.Monad
import Data.Maybe
import Data.List
import Data.IORef
import System.Console.GetOpt
import System.Environment
import System.Exit
import System.IO

import Test.Framework
import Test.Framework.Providers.HUnit

import Robotics.NXT
import Robotics.NXT.Basic

data Option = Help | Device FilePath deriving (Eq, Show)

isDevice :: Option -> Bool
isDevice (Device _) = True
isDevice _          = False

options :: [OptDescr Option]
options = [
    Option "h" ["help"] (NoArg Help) "show this help",
    Option "d" ["device"] (ReqArg Device "filename") "serial port device"

main :: IO ()
main = do
  programName <- getProgName
  let header = programName ++ " [option ...]" ++ "\n\nOptions:"
      usage  = "Usage:\n" ++ usageInfo header options

  args <- getArgs
  (opts, otherArgs) <- case getOpt Permute options args of
                         (o, otherArgs, []) -> return (o, otherArgs)
                         (_, _, errs)       -> do
                           hPutStrLn stderr $ "Error(s):\n" ++ concat errs ++ "\n" ++ usage
                           exitWith $ ExitFailure 1

  when (Help `elem` opts) $ do
    putStrLn "Runs the NXT package tests.\n"

    putStrLn usage
    exitWith ExitSuccess

  let Device device = fromMaybe (Device defaultDevice) . find isDevice $ opts

  putStrLn "Please connect a motor to port A, a swich sensor to port 1, an ultrasonic sensor to port 2, and press enter key to continue."

  _ <- try getLine :: IO (Either IOException String)

    (initialize device >>= newIORef)
    (\ref -> do
       nxt <- readIORef ref
       terminate nxt
    (\ref -> defaultMainWithArgs (tests ref) otherArgs)
tests :: IORef NXTInternals -> [Test]
tests ref = [
    testGroup "Basic Tests" (concatMap hUnitTestToTests (basicTests ref))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.