]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/MonoFunctor/Test.hs
MonoFunctor
[haskell/symantic.git] / Language / Symantic / Expr / MonoFunctor / Test.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeOperators #-}
6 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
7
8 module Expr.MonoFunctor.Test where
9
10 import Test.Tasty
11 import Test.Tasty.HUnit
12
13 import qualified Control.Arrow as Arrow
14 import qualified Control.Monad as Monad
15 import Data.Proxy (Proxy(..))
16 import Data.Text (Text)
17 import Data.Type.Equality ((:~:)(Refl))
18 import Prelude hiding ((&&), not, (||), (==), fmap, (+))
19
20 import Language.Symantic.Type
21 import Language.Symantic.Expr as Expr
22 import Language.Symantic.Repr
23
24 import AST.Test
25
26 -- * Expressions
27 t = bool True
28 f = bool False
29 e1 = omap (lam $ \x -> x + int 1) (list $ int Prelude.<$> [1..3])
30 e2 = omap (lam char_toUpper) (text "abcde")
31
32 -- * Tests
33 type Ex = Expr_Root
34 ( Expr_Lambda
35 .|. Expr_Maybe
36 .|. Expr_List
37 .|. Expr_MonoFunctor
38 .|. Expr_Int
39 .|. Expr_Num
40 .|. Expr_Bool
41 .|. Expr_Char
42 .|. Expr_Text
43 )
44 ex_from = root_expr_from (Proxy::Proxy Ex)
45
46 (==>) ast expected =
47 testCase (show ast) $
48 case ex_from ast of
49 Left err -> Left err @?= Prelude.snd `Arrow.left` expected
50 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
51 case expected of
52 Left (_, err) -> Right ("…"::String) @?= Left err
53 Right (ty_expected::Type_Root_of_Expr Ex h, _::h, _::Text) ->
54 (Monad.>>= (@?= (\(_::Proxy h, err) -> err) `Arrow.left` expected)) $
55 case ty `eq_type` ty_expected of
56 Nothing -> Monad.return $ Left $
57 error_expr (Proxy::Proxy Ex) $
58 Error_Expr_Type_mismatch ast
59 (Exists_Type ty)
60 (Exists_Type ty_expected)
61 Just Refl -> do
62 let h = host_from_expr r
63 Monad.return $
64 Right
65 ( ty
66 , h
67 , text_from_expr r
68 -- , (text_from_expr :: Repr_Text h -> Text) r
69 )
70
71 tests :: TestTree
72 tests = testGroup "MonoFunctor"
73 [ AST "omap"
74 [ AST "\\"
75 [ AST "x" []
76 , AST "Int" []
77 , AST "+" [ AST "var" [AST "x" []]
78 , AST "int" [AST "1" []] ]
79 ]
80 , AST "list"
81 [ AST "Int" []
82 , AST "int" [AST "1" []]
83 , AST "int" [AST "2" []]
84 , AST "int" [AST "3" []]
85 ]
86 ] ==> Right
87 ( type_list type_int
88 , [2,3,4]
89 , "omap (\\x0 -> x0 + 1) [1, 2, 3]" )
90 , AST "omap"
91 [ AST "\\"
92 [ AST "x" []
93 , AST "Char" []
94 , AST "char_toUpper" [ AST "var" [AST "x" []] ]
95 ]
96 , AST "text" [ AST "abcde" [] ]
97 ] ==> Right
98 ( type_text
99 , "ABCDE"
100 , "omap (\\x0 -> char_toUpper x0) \"abcde\"" )
101 ]