]> Git — Sourcephile - haskell/symantic.git/blob - Language/Symantic/Expr/List/Test.hs
Map
[haskell/symantic.git] / Language / Symantic / Expr / List / Test.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE NoMonomorphismRestriction #-}
4 {-# LANGUAGE Rank2Types #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE TypeOperators #-}
7 {-# OPTIONS_GHC -fno-warn-missing-signatures #-}
8 module Expr.List.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 (mod, (==), return)
19
20 import Language.Symantic.Repr
21 import Language.Symantic.Expr
22 import Language.Symantic.Type
23
24 import AST.Test
25
26 -- * Expressions
27 t = bool True
28 f = bool False
29 e1 = list_filter (lam $ \x -> if_ (x `mod` int 2 == int 0) t f)
30 (list $ int Prelude.<$> [1..5])
31
32 -- * Tests
33 type Ex = Expr_Root
34 ( Expr_Lambda
35 .|. Expr_List
36 .|. Expr_Int
37 .|. Expr_Bool
38 )
39 ex_from = root_expr_from (Proxy::Proxy Ex)
40
41 (==>) ast expected =
42 testCase (show ast) $
43 case ex_from ast of
44 Left err -> Left err @?= Prelude.snd `Arrow.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 h, _::h, _::Text) ->
49 (Monad.>>= (@?= (\(_::Proxy h, err) -> err) `Arrow.left` expected)) $
50 case ty `eq_type` ty_expected of
51 Nothing -> Monad.return $ Left $
52 error_expr (Proxy::Proxy Ex) $
53 Error_Expr_Type_mismatch ast
54 (Exists_Type ty)
55 (Exists_Type ty_expected)
56 Just Refl -> do
57 let h = host_from_expr r
58 Monad.return $
59 Right
60 ( ty
61 , h
62 , text_from_expr r
63 -- , (text_from_expr :: Repr_Text h -> Text) r
64 )
65
66 tests :: TestTree
67 tests = testGroup "List"
68 [ AST "list_reverse"
69 [ AST "list"
70 [ AST "Int" []
71 , AST "int" [AST "1" []]
72 , AST "int" [AST "2" []]
73 , AST "int" [AST "3" []]
74 ]
75 ] ==> Right
76 ( type_list type_int
77 , [3,2,1]
78 , "list_reverse [1, 2, 3]" )
79 ]