Skip to content

Commit

Permalink
Use Eq instance in Repl tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcadman committed Feb 26, 2024
1 parent e2522a9 commit 6d7ecb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 50 deletions.
35 changes: 4 additions & 31 deletions test/Repl/Assertions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,14 @@ import Juvix.Compiler.Core.Pretty qualified as Core
assertNoJuvixError :: Either JuvixError a -> IO a
assertNoJuvixError = either (assertFailure . ("JuvixError: " <>) . unpack . renderTextDefault) return

assertPrettyCodeEqual :: (Core.PrettyCode a) => (a -> a -> Bool) -> a -> a -> Assertion
assertPrettyCodeEqual eq expected actual = unless (eq expected actual) (assertFailure (unpack msg))
assertPrettyCodeEqual :: (Core.PrettyCode a, Eq a) => a -> a -> Assertion
assertPrettyCodeEqual expected actual = unless (expected == actual) (assertFailure (unpack msg))
where
msg :: Text
msg = "expected: " <> Core.ppTrace expected <> "\n but got: " <> Core.ppTrace actual

assertNodeEqual :: Core.Node -> Core.Node -> Assertion
assertNodeEqual = assertPrettyCodeEqual (==)
assertNodeEqual = assertPrettyCodeEqual

assertValueEqual :: Core.Value -> Core.Value -> Assertion
assertValueEqual = assertPrettyCodeEqual valueEq

valueEqList :: [Core.Value] -> [Core.Value] -> Bool
valueEqList vs1 vs2 = case (vs1, vs2) of
([], []) -> True
([], _) -> False
(_, []) -> False
(x : xs, y : ys) -> valueEq x y && valueEqList xs ys

valueEq :: Core.Value -> Core.Value -> Bool
valueEq n = \case
Core.ValueConstrApp app1 -> case n of
Core.ValueConstrApp app2 ->
app1 ^. Core.constrAppName == app2 ^. Core.constrAppName
&& valueEqList (app1 ^. Core.constrAppArgs) (app2 ^. Core.constrAppArgs)
_ -> False
Core.ValueConstant v1 -> case n of
Core.ValueConstant v2 -> v1 == v2
_ -> False
Core.ValueWildcard -> case n of
Core.ValueWildcard -> True
_ -> False
Core.ValueFun -> case n of
Core.ValueFun -> True
_ -> False
Core.ValueType -> case n of
Core.ValueType -> True
_ -> False
assertValueEqual = assertPrettyCodeEqual
20 changes: 1 addition & 19 deletions test/Repl/Positive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Juvix.Compiler.Pipeline.Root
import Juvix.Data.Effect.TaggedLock
import Juvix.Extra.Paths qualified as P
import Juvix.Extra.Stdlib
import Juvix.Extra.Strings qualified as Str
import Repl.Assertions
import Repl.Value

runTaggedLockIO' :: Sem '[Files, TaggedLock, Embed IO] a -> IO a
runTaggedLockIO' =
Expand Down Expand Up @@ -77,24 +77,6 @@ replTest input' expectedNode getTestCtx = do
n' <- evalRepl artif ep n
assertValueEqual expectedNode n'

mkInteger :: Integer -> Core.Value
mkInteger = Core.ValueConstant . Core.ConstInteger

mkBool :: Bool -> Core.Value
mkBool b =
Core.ValueConstrApp
( Core.ConstrApp
{ _constrAppName = name,
_constrAppFixity = Nothing,
_constrAppArgs = []
}
)
where
name :: Text
name = case b of
True -> Str.true
False -> Str.false

allTests :: TestTree
allTests =
testGroup
Expand Down
24 changes: 24 additions & 0 deletions test/Repl/Value.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Repl.Value where

import Base
import Juvix.Compiler.Core qualified as Core
import Juvix.Compiler.Core.Language.Value qualified as Core
import Juvix.Extra.Strings qualified as Str

mkInteger :: Integer -> Core.Value
mkInteger = Core.ValueConstant . Core.ConstInteger

mkBool :: Bool -> Core.Value
mkBool b =
Core.ValueConstrApp
( Core.ConstrApp
{ _constrAppName = name,
_constrAppFixity = Irrelevant Nothing,
_constrAppArgs = []
}
)
where
name :: Text
name = case b of
True -> Str.true
False -> Str.false

0 comments on commit 6d7ecb4

Please sign in to comment.