Skip to content

Commit 0e638d3

Browse files
committed
Incorporate feedback
1 parent b86531f commit 0e638d3

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

cardano-node/app/conformance-test-viewer.hs

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Options.Applicative
2525
import Ouroboros.Consensus.Byron.Ledger.Block
2626
import ShrinkIndex
2727
import System.Environment (getArgs)
28-
import System.IO (hPutStr, hPutStrLn, stderr)
28+
import System.IO (hPutStr, hPutStrLn, hSetEcho, hSetBuffering, stdin, stdout, stderr, BufferMode(..))
2929
import Test.Consensus.OrphanInstances ()
3030
import Test.Consensus.PointSchedule (GenesisTest, PointSchedule)
3131
import Test.QuickCheck (Arbitrary(..))
@@ -47,6 +47,7 @@ data Options = Options
4747

4848
data Mode
4949
= ShowDescendant
50+
| Interactive
5051
deriving (Eq, Show)
5152

5253
data TestCaseType
@@ -88,7 +89,12 @@ optionParser = Options
8889
, metavar "TYPE"
8990
, help "Which type of test case to parse"
9091
]))
91-
<*> pure ShowDescendant
92+
<*> (option (eitherReader parseMode)
93+
(long "mode" <> mconcat
94+
[ value ShowDescendant
95+
, metavar "STRING"
96+
, help "Viewer mode (--show (default), --interactive)"
97+
]))
9298

9399

94100

@@ -99,8 +105,10 @@ main = do
99105

100106
result <- runExceptT $ do
101107
testCase <- getInputTestCase (optTestCaseType opts) (optInputPath opts)
102-
shrinkResult <- analyzeShrinkTree (optMode opts) (optShrinkIndex opts) testCase
103-
writeOutputTestCase (optOutputPath opts) shrinkResult
108+
case optMode opts of
109+
ShowDescendant -> do
110+
shrinkResult <- analyzeShrinkTree (optMode opts) (optShrinkIndex opts) testCase
111+
writeOutputTestCase (optOutputPath opts) shrinkResult
104112

105113
case result of
106114
Right () -> pure ()
@@ -161,9 +169,15 @@ analyzeShrinkTree
161169
:: (Monad m)
162170
=> Mode -> ShrinkIndex -> ViewableTestCase -> ExceptT String m ViewableTestCase
163171
analyzeShrinkTree mode shrinkIndex (TestCase testCase) = fmap TestCase $
164-
case mode of
172+
let shrinkTree = arbitraryShrinkTree testCase
173+
in case mode of
165174
ShowDescendant -> failWith "Descendant does not exist. :(" $
166-
lookup shrinkIndex $ arbitraryShrinkTree testCase
175+
lookup shrinkIndex shrinkTree
176+
Interactive -> do
177+
hSetEcho stdout False
178+
hSetBuffering stdin NoBuffering
179+
displayTestCase shrinkIndex
180+
interactWithShrinks shrinkIndex shrinkTree
167181

168182
writeOutputTestCase
169183
:: (MonadIO m) => Maybe FilePath -> ViewableTestCase -> m ()
@@ -173,6 +187,50 @@ writeOutputTestCase outputPath (TestCase testCase) = do
173187
Nothing -> BS.putStr bytes >> putStrLn ""
174188
Just oPath -> BS.writeFile oPath bytes
175189

190+
interactWithShrinks
191+
:: ShrinkIndex -> ShrinkTree a -> ExceptT String m ()
192+
interactWithShrinks index allTrees = do
193+
mNextIndex <- fmap (getNextIndex index) getNextCommand
194+
case mNextIndex of
195+
Nothing -> pure ()
196+
Just nextIndex -> do
197+
displayTestCase nextIndex allTrees
198+
199+
-- Read an arrow key press or q from stdin.
200+
getNextCommand :: IO (Maybe Command)
201+
getNextCommand = do
202+
c1 <- getChar
203+
case c1 of
204+
'\ESC' -> do
205+
c2 <- getChar
206+
c3 <- getChar
207+
pure $ case (c2, c3) of
208+
('[', 'A') -> Just ToLeftSibling
209+
('[', 'B') -> Just ToRightSibling
210+
('[', 'C') -> Just ToFirstChild
211+
('[', 'D') -> Just ToParent
212+
_ -> Nothing
213+
'q' -> pure $ Just Quit
214+
_ -> pure Nothing
215+
216+
getNextIndex
217+
:: ShrinkIndex -> Maybe Command -> Maybe ShrinkIndex
218+
getNextIndex index mCmd = case mCmd of
219+
Just ToLeftSibling -> _1
220+
Just ToRightSibling -> _2
221+
Just ToFirstChild -> _3
222+
Just ToParent -> _4
223+
Just Quit -> Nothing
224+
Nothing -> Nothing
225+
226+
data Command
227+
= ToLeftSibling
228+
| ToRightSibling
229+
| ToFirstChild
230+
| ToParent
231+
| Quit
232+
deriving (Eq, Show)
233+
176234

177235

178236
-- Very permissive; given a string, interpret any maximal contiguous
@@ -192,3 +250,9 @@ parseTestCaseType symbol = case symbol of
192250
"string" -> Right StringTC
193251
"genesis" -> Right GenesisTestTC
194252
_ -> Left $ "Unrecognized test case type \"" <> symbol <> "\""
253+
254+
parseMode :: String -> Either String Mode
255+
parseMode symbol = case symbol of
256+
"show" -> Right ShowDescendant
257+
"interactive" -> Right Interactive
258+
_ -> Left $ "Unrecognized viewing mode \"" <> symbol <> "\""

0 commit comments

Comments
 (0)