Skip to content

Commit

Permalink
use prettyString instead of (show . pretty)
Browse files Browse the repository at this point in the history
  • Loading branch information
janmasrovira committed Apr 11, 2024
1 parent b472e8c commit 9ca7cb4
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 47 deletions.
4 changes: 4 additions & 0 deletions src/Juvix/Prelude/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.Text qualified as Text
import Juvix.Prelude.Base
import Prettyprinter hiding (concatWith, defaultLayoutOptions, hsep, sep, vsep)
import Prettyprinter qualified as PP
import Prettyprinter.Render.String (renderString)
import Prettyprinter.Render.Terminal (AnsiStyle)
import Prettyprinter.Render.Terminal qualified as Ansi
import Prettyprinter.Render.Text (renderStrict)
Expand Down Expand Up @@ -139,6 +140,9 @@ trimText =
toPlainTextTrim :: (HasTextBackend a) => a -> Text
toPlainTextTrim = trimText . toPlainText

prettyString :: (Pretty a) => a -> String
prettyString = renderString . layoutPretty defaultLayoutOptions . pretty

prettyText :: (Pretty a) => a -> Text
prettyText = Text.renderStrict . layoutPretty defaultLayoutOptions . pretty

Expand Down
8 changes: 4 additions & 4 deletions test/Asm/Run/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ runAssertion hout sym tab = do
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value' -> do
case value' of
ValVoid -> return ()
Expand All @@ -28,7 +28,7 @@ asmRunAssertionParam' :: (Handle -> Symbol -> InfoTable -> IO ()) -> InfoTable -
asmRunAssertionParam' interpretFun tab expectedFile step = do
step "Validate"
case validate' tab of
Just err -> assertFailure (show (pretty err))
Just err -> assertFailure (prettyString err)
Nothing ->
case tab ^. infoMainFunction of
Just sym -> do
Expand All @@ -54,10 +54,10 @@ asmRunAssertionParam interpretFun mainFile expectedFile trans testTrans step = d
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab0 -> do
case trans tab0 of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab -> do
testTrans tab
asmRunAssertionParam' interpretFun tab expectedFile step
Expand Down
4 changes: 2 additions & 2 deletions test/BackendGeb/Eval/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gebEvalAssertion mainFile expectedFile step = do
step "Parse"
input_ <- readFile mainFile
case Geb.runParser mainFile input_ of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (Geb.ExpressionObject _) -> do
step "No evaluation for objects"
assertFailure (unpack Geb.objNoEvalMsg)
Expand Down Expand Up @@ -47,7 +47,7 @@ gebEvalAssertion' _mainFile expectedFile step gebMorphism = do
case result of
Left err -> do
hClose hout
assertFailure (show (pretty (fromJuvixError @GenericError err)))
assertFailure (prettyString (fromJuvixError @GenericError err))
Right value -> do
hPutStrLn hout (Geb.ppPrint value)
hClose hout
Expand Down
12 changes: 6 additions & 6 deletions test/BackendGeb/FromCore/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ coreToGebTranslationAssertion root mainFile expectedFile step = do
input_ <- readFile mainFile
entryPoint <- set entryPointTarget (Just TargetGeb) <$> testDefaultEntryPointIO root mainFile
case Core.runParserMain mainFile defaultModuleId mempty input_ of
Left err -> assertFailure . show . pretty $ err
Left err -> assertFailure . prettyString $ err
Right coreInfoTable -> coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step

coreToGebTranslationAssertion' ::
Expand All @@ -32,7 +32,7 @@ coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step = do
step "Prepare the Juvix Core node for translation to Geb"
case run . runReader entryPoint . runError @Geb.JuvixError $ Core.toGeb (Core.moduleFromInfoTable coreInfoTable) of
Left err ->
assertFailure . show . pretty $
assertFailure . prettyString $
fromJuvixError @GenericError err
Right readyCoreModule ->
let readyCoreInfoTable = Core.computeCombinedInfoTable readyCoreModule
Expand All @@ -47,7 +47,7 @@ coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step = do
}
case run . runError @Geb.CheckingError $ Geb.check' typeMorph of
Left err ->
assertFailure . show . pretty $
assertFailure . prettyString $
fromJuvixError @GenericError (JuvixError err)
Right _ -> do
step "Try evaluating the JuvixCore node"
Expand All @@ -59,11 +59,11 @@ coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step = do
) of
(Left err, _) -> do
step "The evaluation of the translated Geb node failed"
assertFailure . show . pretty $
assertFailure . prettyString $
fromJuvixError @GenericError (JuvixError err)
(_, Left err) -> do
step "The evaluation of gebCoreEvalResult failed"
assertFailure . show . pretty $ fromJuvixError @GenericError (JuvixError err)
assertFailure . prettyString $ fromJuvixError @GenericError (JuvixError err)
( Right resEvalTranslatedMorph,
Right resEvalGebCoreEvalResult
) -> do
Expand All @@ -82,7 +82,7 @@ coreToGebTranslationAssertion' coreInfoTable entryPoint expectedFile step = do
<> "node is not equal to the expected output"
| otherwise -> assertBool "" True
case Geb.runParser expectedFile expectedInput of
Left parseErr -> assertFailure . show . pretty $ parseErr
Left parseErr -> assertFailure . prettyString $ parseErr
Right (Geb.ExpressionMorphism m) -> compareEvalOutput m
Right (Geb.ExpressionTypedMorphism m) -> compareEvalOutput (m ^. Geb.typedMorphism)
Right (Geb.ExpressionObject _) ->
Expand Down
2 changes: 1 addition & 1 deletion test/Casm/Compilation/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ compileAssertionEntry adjustEntry root' bRunVM optLevel mainFile expectedFile st
step "Translate to CASM"
let entryPoint' = entryPoint {_entryPointOptimizationLevel = optLevel}
case run $ runError @JuvixError $ runReader entryPoint' $ storedCoreToCasm (_pipelineResult ^. Core.coreResultModule) of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right Result {..} -> do
withTempDir'
( \dirPath -> do
Expand Down
4 changes: 2 additions & 2 deletions test/Casm/Reg/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ compileAssertion' :: Maybe (Path Abs File) -> Path Abs Dir -> Path Abs File -> S
compileAssertion' inputFile _ outputFile _ tab step = do
step "Translate to CASM"
case run $ runError @JuvixError $ regToCasm tab of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right Result {..} -> do
step "Interpret"
hout <- openFile (toFilePath outputFile) WriteMode
Expand All @@ -31,7 +31,7 @@ cairoAssertion' :: Maybe (Path Abs File) -> Path Abs Dir -> Path Abs File -> Sym
cairoAssertion' inputFile dirPath outputFile _ tab step = do
step "Translate to Cairo"
case run $ runError @JuvixError $ regToCairo tab of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right res -> do
step "Serialize to Cairo bytecode"
encodeFile (toFilePath outputFile) res
Expand Down
6 changes: 3 additions & 3 deletions test/Casm/Run/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ casmRunAssertion' :: Bool -> LabelInfo -> Code -> Maybe (Path Abs File) -> Path
casmRunAssertion' bRunVM labi instrs inputFile expectedFile step =
case validate labi instrs of
Left err -> do
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right () -> do
withTempDir'
( \dirPath -> do
Expand All @@ -50,7 +50,7 @@ casmRunAssertion' bRunVM labi instrs inputFile expectedFile step =
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value' -> do
hPrint hout value'
hClose hout
Expand All @@ -67,7 +67,7 @@ casmRunAssertion bRunVM mainFile inputFile expectedFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (labi, instrs) -> casmRunAssertion' bRunVM labi instrs inputFile expectedFile step

casmRunErrorAssertion :: Path Abs File -> (String -> IO ()) -> Assertion
Expand Down
4 changes: 2 additions & 2 deletions test/Core/Asm/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ coreAsmAssertion mainFile expectedFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> do
step "Empty program: compare expected and actual program output"
expected <- readFile expectedFile
assertEqDiffText ("Check: EVAL output = " <> toFilePath expectedFile) "" expected
Right (tabIni, Just node) -> do
step "Translate"
case run $ runReader defaultCoreOptions $ runError $ toStored' >=> toStripped' Identity $ moduleFromInfoTable $ setupMainFunction defaultModuleId tabIni node of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right m -> do
let tab = Asm.fromTree $ Tree.fromCore $ Stripped.fromCore (maximum allowedFieldSizes) $ computeCombinedInfoTable m
Asm.asmRunAssertion' tab expectedFile step
4 changes: 2 additions & 2 deletions test/Core/Compile/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ coreCompileAssertion' ::
coreCompileAssertion' optLevel tab mainFile expectedFile stdinText step = do
step "Translate to JuvixAsm"
case run . runReader opts . runError $ toStored' (moduleFromInfoTable tab) >>= toStripped' CheckExec of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right m -> do
let tab0 = computeCombinedInfoTable m
assertBool "Check info table" (checkInfoTable tab0)
Expand All @@ -71,7 +71,7 @@ coreCompileAssertion mainFile expectedFile stdinText step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> do
step "Empty program: compare expected and actual program output"
expected <- readFile expectedFile
Expand Down
6 changes: 3 additions & 3 deletions test/Core/Eval/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ coreEvalAssertion' mode tab mainFile expectedFile step =
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value -> do
unless
(Info.member kNoDisplayInfo (getInfo value))
Expand Down Expand Up @@ -143,14 +143,14 @@ coreEvalAssertion mainFile expectedFile trans testTrans step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> do
step "Compare expected and actual program output"
expected <- readFile expectedFile
assertEqDiffText ("Check: EVAL output = " <> toFilePath expectedFile) "" expected
Right (tabIni, Just node) ->
case run $ runReader defaultCoreOptions $ runError $ applyTransformations trans $ moduleFromInfoTable $ setupMainFunction defaultModuleId tabIni node of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right m -> do
let tab = computeCombinedInfoTable m
assertBool "Check info table" (checkInfoTable tab)
Expand Down
4 changes: 2 additions & 2 deletions test/Core/Normalize/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ coreNormalizeAssertion mainFile expectedFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> assertFailure "Empty program"
Right (tabIni, Just node) -> do
step "Transform"
let tab = setupMainFunction defaultModuleId tabIni node
transforms = toStoredTransformations ++ toNormalizeTransformations
case run $ runReader defaultCoreOptions $ runError @JuvixError $ applyTransformations transforms (moduleFromInfoTable tab) of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right m -> do
step "Normalize"
let tab' = computeCombinedInfoTable m
Expand Down
4 changes: 2 additions & 2 deletions test/Core/Print/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ corePrintAssertion mainFile expectedFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> do
step "Empty program: compare expected and actual program output"
expected <- readFile expectedFile
Expand All @@ -50,5 +50,5 @@ corePrintAssertion mainFile expectedFile step = do
step "Print and parse back"
let r' = runParserMain mainFile defaultModuleId mempty (ppPrint tab)
case r' of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab' -> coreEvalAssertion' EvalModePlain tab' mainFile expectedFile step
4 changes: 2 additions & 2 deletions test/Core/VampIR/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ coreVampIRAssertion transforms mainFile expectedFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right (_, Nothing) -> assertFailure "Empty program"
Right (tabIni, Just node) -> do
coreVampIRAssertion' (setupMainFunction defaultModuleId tabIni node) transforms mainFile expectedFile step
Expand All @@ -34,7 +34,7 @@ coreVampIRAssertion' tab transforms mainFile expectedFile step = do
step "Transform and normalize"
case run . runReader defaultCoreOptions . runError @JuvixError $
applyTransformations transforms (moduleFromInfoTable tab) of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right m -> do
let tab' = computeCombinedInfoTable m
step "Check let-hoisted"
Expand Down
2 changes: 1 addition & 1 deletion test/Internal/Eval/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internalCoreAssertion root' mainFile expectedFile step = do
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value -> do
unless
(Info.member kNoDisplayInfo (getInfo value))
Expand Down
2 changes: 1 addition & 1 deletion test/Nockma/Parse/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ testDescr PosTest {..} =

assertParse :: Text -> IO (Term Natural)
assertParse txt = case parseText txt of
Left (MegaparsecError b) -> assertFailure ("Nockma parsing failed " <> unpack (prettyText (errorBundlePretty b)))
Left (MegaparsecError b) -> assertFailure ("Nockma parsing failed " <> prettyString (errorBundlePretty b))
Right t -> return t

allTests :: TestTree
Expand Down
4 changes: 2 additions & 2 deletions test/Reg/Parse/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ regParseAssertion mainFile step = do
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab -> do
withTempDir'
( \dirPath -> do
Expand All @@ -21,7 +21,7 @@ regParseAssertion mainFile step = do
step "Parse printed"
r' <- parseFile outputFile
case r' of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab' -> do
assertBool ("Check: print . parse = print . parse . print . parse") (ppPrint tab tab == ppPrint tab' tab')
)
Expand Down
6 changes: 3 additions & 3 deletions test/Reg/Run/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ runAssertion _ outputFile sym tab step = do
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value' -> do
case value' of
ValVoid ->
Expand Down Expand Up @@ -52,12 +52,12 @@ regRunAssertionParam interpretFun mainFile expectedFile trans testTrans step = d
step "Parse"
r <- parseFile mainFile
case r of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab0 -> do
unless (null trans) $
step "Transform"
case run $ runError @JuvixError $ applyTransformations trans tab0 of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right tab -> do
testTrans tab
regRunAssertionParam' interpretFun tab expectedFile step
Expand Down
2 changes: 1 addition & 1 deletion test/Tree/Asm/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ treeAsmAssertion mainFile expectedFile step = do
step "Parse"
s <- readFile mainFile
case runParser mainFile s of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tabIni -> do
step "Translate"
let tab = Asm.fromTree tabIni
Expand Down
10 changes: 5 additions & 5 deletions test/Tree/Eval/Base.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ treeEvalAssertionParam evalParam mainFile expectedFile trans testTrans step = do
step "Parse"
s <- readFile mainFile
case runParser mainFile s of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab0 -> do
step "Validate"
case run $ runError @JuvixError $ applyTransformations [Validate] tab0 of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right tab1 -> do
unless (null trans) $
step "Transform"
case run $ runError @JuvixError $ applyTransformations trans tab1 of
Left err -> assertFailure (show (pretty (fromJuvixError @GenericError err)))
Left err -> assertFailure (prettyString (fromJuvixError @GenericError err))
Right tab -> do
testTrans tab
case tab ^. infoMainFunction of
Expand All @@ -67,7 +67,7 @@ evalAssertion hout sym tab = do
case r' of
Left err -> do
hClose hout
assertFailure (show (pretty err))
assertFailure (prettyString err)
Right value' -> do
case value' of
ValVoid -> return ()
Expand All @@ -85,7 +85,7 @@ treeEvalErrorAssertion mainFile step = do
step "Parse"
s <- readFile mainFile
case runParser mainFile s of
Left err -> assertFailure (show (pretty err))
Left err -> assertFailure (prettyString err)
Right tab ->
case tab ^. infoMainFunction of
Just sym -> do
Expand Down
Loading

0 comments on commit 9ca7cb4

Please sign in to comment.