]> 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 lam
33 .|. Expr_Maybe lam
34 .|. Expr_List lam
35 .|. Expr_Functor lam
36 .|. Expr_Int
37 .|. Expr_Bool
38 )
39 ex_from = root_expr_from (Proxy::Proxy (Ex lam)) (Proxy::Proxy lam)
40
41 (==>) ast expected =
42 testCase (show ast) $
43 case ex_from ast of
44 Left err -> Left err @?= snd `left` expected
45 Right (Exists_Type_and_Repr ty (Forall_Repr r)) ->
46 case expected of
47 Left (_, err) -> Right ("…"::String) @?= Left err
48 Right (ty_expected::Type_Root_of_Expr (Ex IO) h, _::h, _::Text) ->
49 (>>= (@?= (\(_::Proxy h, err) -> err) `left` expected)) $
50 case ty `eq_type` ty_expected of
51 Nothing -> return $ Left $
52 error_expr (Proxy::Proxy (Ex IO)) $
53 Error_Expr_Type_mismatch ast
54 (Exists_Type ty)
55 (Exists_Type ty_expected)
56 Just Refl -> do
57 h <- host_from_expr r
58 return $
59 Right
60 ( ty
61 , h
62 , text_from_expr r
63 -- , (text_from_expr :: Repr_String IO h -> Text) r
64 )
65
66 tests :: TestTree
67 tests = testGroup "Functor"
68 [ AST "fmap"
69 [ AST "val"
70 [ AST "x" []
71 , AST "Int" []
72 , AST "+" [ AST "var" [AST "x" []]
73 , AST "int" [AST "1" []] ]
74 ]
75 , AST "list"
76 [ AST "Int" []
77 , AST "int" [AST "1" []]
78 , AST "int" [AST "2" []]
79 , AST "int" [AST "3" []]
80 ]
81 ] ==> Right
82 ( type_list type_int
83 , [2,3,4]
84 , "fmap (\\x0 -> x0 + 1) [1, 2, 3]" )
85 ]