]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/Functor/Test.hs
init
[haskell/symantic.git] / Language / Symantic / Expr / Functor / 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.Functor.Test where
9
10 import Test.Tasty
11 import Test.Tasty.HUnit
12
13 import Control.Arrow (left)
14 import Data.Proxy (Proxy(..))
15 import Data.Text (Text)
16 import Data.Type.Equality ((:~:)(Refl))
17 import Prelude hiding ((&&), not, (||), (==), fmap, (+))
18
19 import Language.Symantic.Type
20 import Language.Symantic.Expr as Expr
21 import Language.Symantic.Repr
22
23 import AST.Test
24
25 -- * Expressions
26 t = bool True
27 f = bool False
28 e1 = fmap (val $ \x -> x + int 1) (list $ int Prelude.<$> [1..3])
29
30 -- * Tests
31 type Ex lam = Expr_Root
32 ( Expr_Lambda_App lam
33 .|. Expr_Lambda_Val lam
34 .|. Expr_Maybe lam
35 .|. Expr_List lam
36 .|. Expr_Functor lam
37 .|. Expr_Int
38 .|. Expr_Bool
39 )
40 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
41
42 (==>) ast expected =
43 testCase (show ast) $
44 case ex_from ast of
45 Left err -> Left err @?= snd `left` expected
46 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
47 case expected of
48 Left (_, err) -> Right ("…"::String) @?= Left err
49 Right (ty_expected::Type_Root_of_Expr (Ex IO) h, _::h, _::Text) ->
50 (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $
51 case ty `eq_type` ty_expected of
52 Nothing -> return $ Left $
53 error_expr (Proxy::Proxy (Ex IO)) $
54 Error_Expr_Type_mismatch ast
55 (Exists_Type ty)
56 (Exists_Type ty_expected)
57 Just Refl -> do
58 h <- host_from_expr r
59 return $
60 Right
61 ( ty
62 , h
63 , text_from_expr r
64 -- , (text_from_expr :: Repr_String IO h -> Text) r
65 )
66
67 tests :: TestTree
68 tests = testGroup "Functor"
69 [ AST "fmap"
70 [ AST "val"
71 [ AST "x" []
72 , AST "Int" []
73 , AST "+" [ AST "var" [AST "x" []]
74 , AST "int" [AST "1" []] ]
75 ]
76 , AST "list"
77 [ AST "Int" []
78 , AST "int" [AST "1" []]
79 , AST "int" [AST "2" []]
80 , AST "int" [AST "3" []]
81 ]
82 ] ==> Right
83 ( type_list type_int
84 , [2,3,4]
85 , "fmap (\\x0 -> x0 + 1) [1, 2, 3]" )
86 ]