summaryrefslogtreecommitdiff
path: root/test/Checks.hs
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-06-10 23:03:04 -0400
committerJoe Crayne <joe@jerkface.net>2019-06-10 23:03:04 -0400
commit38b7bcf654e5e804a13518b060ebdba59bf232bb (patch)
tree2fa3c4ccf3496750f0ce388a9ea0998fdd93bf69 /test/Checks.hs
Initial commit.
Diffstat (limited to 'test/Checks.hs')
-rw-r--r--test/Checks.hs125
1 files changed, 125 insertions, 0 deletions
diff --git a/test/Checks.hs b/test/Checks.hs
new file mode 100644
index 0000000..67cc586
--- /dev/null
+++ b/test/Checks.hs
@@ -0,0 +1,125 @@
1--
2-- Graphics.Wavefront.Checks
3-- Executable containing checks and tests for the modules in this package
4--
5-- Jonatan H Sundqvist
6-- February 24 2015
7--
8
9-- TODO | - Use QuickCheck (?)
10-- - Full coverage
11-- - Benchmarking
12
13-- SPEC | -
14-- -
15
16
17
18module Graphics.WaveFront.Checks where
19
20
21
22--------------------------------------------------------------------------------------------------------------------------------------------
23-- We'll need these
24--------------------------------------------------------------------------------------------------------------------------------------------
25import Text.Printf (printf)
26import Data.Either (lefts)
27import Data.Char (toLower)
28import System.IO (hFlush, stdout)
29
30import Control.Monad (forM_, when)
31
32import Graphics.WaveFront.Parsers (MTL, OBJ, OBJNoParse(..), MTLNoParse(..), MTLToken(..))
33import Graphics.WaveFront.Load (loadOBJ, loadMTL)
34
35
36
37--------------------------------------------------------------------------------------------------------------------------------------------
38-- Functions (IO)
39--------------------------------------------------------------------------------------------------------------------------------------------
40
41-- IO utilities ----------------------------------------------------------------------------------------------------------------------------
42
43-- |
44promptContinue :: String -> IO ()
45promptContinue prompt = do
46 putStr prompt
47 hFlush stdout
48 getChar
49 putChar '\n'
50
51
52
53-- |
54--
55-- TODO: Refactor (cf. untilM)
56-- TODO: Allow flexible feedback
57-- TODO: Default return value for invalid replies (?)
58-- TODO: Customisable validation (eg. for other languages than English)
59--
60askYesNo :: String -> IO Bool
61askYesNo q = do
62 putStr q
63 hFlush stdout
64 answer <- getLine
65 affirmed $ map toLower answer
66 where affirmed answer | answer `elem` ["yes", "y", "yeah"] = return True
67 | answer `elem` ["no", "n", "nah"] = return False
68 | otherwise = askYesNo "I don't understand. Answer 'yes' or 'no': "
69 -- return [(`elem` ["yes", "y", "yeah"]), (`elem` "no", "n", "nah")]
70
71
72-- |
73askPerformAction :: String -> IO () -> IO ()
74askPerformAction q action = do
75 affirmed <- askYesNo q
76 when affirmed action
77
78
79-- |
80showTokens :: Show a => [(Int, Either MTLNoParse a, String)] -> IO ()
81showTokens materials = mapM_ (uncurry $ printf "[%d] %s\n") [ (n, show token) | (n, Right token, comment) <- materials ] -- TODO: cf. line 65
82
83
84
85--------------------------------------------------------------------------------------------------------------------------------------------
86-- Entry point
87--------------------------------------------------------------------------------------------------------------------------------------------
88-- |
89--
90-- TODO: Print culprit lines (✓)
91--
92main :: IO ()
93main = do
94 putStrLn "This is where the checks should be."
95
96 let path = "C:/Users/Jonatan/Desktop/Python/experiments/WaveFront/"
97
98 forM_ ["queen", "cube"] $ \ fn -> do
99 printf "\nParsing OBJ file: %s.obj\n" fn
100 model <- loadOBJ $ printf (path ++ "data/%s.obj") fn
101 -- TODO: Utility for partioning a list based on several predicates ([a] -> [a -> Bool] -> [[a]])
102 -- TODO: Utilities for displaying output and asking for input
103 -- TODO: Oh, the efficiency!
104 -- TODO: Less ugly naming convention for monadic functions which ignore the output (cf. mapM_, forM_, etc.)
105 let unparsed = lefts $ map second model
106 let comments = [ c | c@(OBJComment _) <- unparsed ]
107 let blanks = [ c | c@(OBJEmpty) <- unparsed ]
108 let errors = length unparsed - (length comments + length blanks)
109 printf "Found %d invalid rows in OBJ file (%d comments, %d blanks, %d errors).\n" (length unparsed) (length comments) (length blanks) errors
110 when (length unparsed > 0) . askPerformAction "Would you like to see view them (yes/no)? " $ putStrLn "Ok, here they are:" >> mapM_ print unparsed
111
112 promptContinue "Press any key to continue..."
113
114 mapM (uncurry $ printf "[%d] %s\n") [ (n, show token) | (n, Right token, comment) <- model ]
115 -- TODO: Print culprit lines (✓)
116
117 promptContinue "Press any key to continue..."
118
119 printf "\nParsing MTL file: %s.mtl\n" fn
120 materials <- loadMTL $ printf "%sdata/%s.mtl" path fn
121 printf "Found %d invalid rows in MTL file (n comments, m blanks, o errors).\n" . length . lefts $ map second materials
122 showTokens materials
123
124 promptContinue "Press any key to continue..."
125 where second (_, b, _) = b \ No newline at end of file