File size: 7,766 Bytes
a10b189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
{-|
Module      : Test.Surface.Parser
Description : Unit tests for the parser
Copyright   : (c) 2026 Ahmad Ali Parr
License     : BSL-1.0
-}

module Test.Surface.Parser (tests) where

import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase, (@?=), Assertion)
import Assertica.Surface.Parser
import Assertica.Surface.AST
import Assertica.Surface.Lexer (lexer)
import Data.Text (pack)

-- ============================================================================
-- PARSER TESTS
-- ============================================================================

tests :: TestTree
tests = testGroup "Assertica.Surface.Parser"
  [ testGroup "Terms"
      [ testCase "parse variable" $
          parseTermTest "x" (STVar (pack "x"))
      , testCase "parse integer literal" $
          parseTermTest "42" (STConst (IntLit 42))
      , testCase "parse boolean true" $
          parseTermTest "True" (STConst (BoolLit True))
      , testCase "parse boolean false" $
          parseTermTest "False" (STConst (BoolLit False))
      , testCase "parse string literal" $
          parseTermTest "\"hello\"" (STConst (StringLit "hello"))
      , testCase "parse application" $
          parseTermTest "f x" (STApp (Located (STVar (pack "f")) dummyLoc) [Located (STVar (pack "x")) dummyLoc])
      , testCase "parse multi-arg application" $
          parseTermTest "f x y z"
            (STApp (Located (STVar (pack "f")) dummyLoc)
              [Located (STVar (pack "x")) dummyLoc, Located (STVar (pack "y")) dummyLoc, Located (STVar (pack "z")) dummyLoc])
      , testCase "parse lambda" $
          parseTermLamTest "\\x -> x"
      , testCase "parse let binding" $
          parseTermLetTest "let x = 42 in x"
      , testCase "parse infix expression" $
          parseTermInfixTest "x + y"
      ]
  , testGroup "Types"
      [ testCase "parse type variable" $
          parseTypeTest "a" (STyVar (pack "a"))
      , testCase "parse type constant" $
          parseTypeTest "Int" (STyConst [pack "Int"] [])
      , testCase "parse function type" $
          parseTypeTest "Int -> String"
            (STyFun (STyConst [pack "Int"] []) (STyConst [pack "String"] []))
      , testCase "parse forall type" $
          parseTypeForallTest "forall a. a -> a"
      ]
  , testGroup "Propositions"
      [ testCase "parse equality" $
          parsePropositionTest "x == y" SPEq
      , testCase "parse forall proposition" $
          parsePropositionForallTest "forall x. x == x"
      , testCase "parse exists proposition" $
          parsePropositionExistsTest "exists x. x == x"
      ]
  , testGroup "Proofs"
      [ testCase "parse refl proof" $
          parseProofTest "refl" SPRefl
      , testCase "parse by_refl proof" $
          parseProofTest "by_refl" (SPByName (pack "by_refl"))
      ]
  , testGroup "Complete programs"
      [ testCase "parse assertion" $
          parseAssertionCompleteTest
      , testCase "parse proof" $
          parseProofCompleteTest
      ]
  ]

-- ============================================================================
-- HELPER FUNCTIONS
-- ============================================================================

dummyLoc :: SourceLoc
dummyLoc = SourceLoc "test.as" 1 1

{-| Parse a term from string and extract the core term.
-}
parseTermTest :: String -> SurfaceTerm -> Assertion
parseTermTest input expected = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseTerm tokens of
        Left err -> fail (show err)
        Right (Located term _) -> term @?= expected

{-| Test parsing a lambda expression (just check it succeeds).
-}
parseTermLamTest :: String -> Assertion
parseTermLamTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseTerm tokens of
        Left err -> fail (show err)
        Right (Located STLam{} _) -> return ()
        Right (Located term _) -> fail ("Expected lambda, got: " ++ show term)

{-| Test parsing a let binding (just check it succeeds).
-}
parseTermLetTest :: String -> Assertion
parseTermLetTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseTerm tokens of
        Left err -> fail (show err)
        Right (Located STLet{} _) -> return ()
        Right (Located term _) -> fail ("Expected let, got: " ++ show term)

{-| Test parsing an infix expression.
-}
parseTermInfixTest :: String -> Assertion
parseTermInfixTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseTerm tokens of
        Left err -> fail (show err)
        Right (Located STInfix{} _) -> return ()
        Right (Located term _) -> fail ("Expected infix, got: " ++ show term)

{-| Parse a type from string.
-}
parseTypeTest :: String -> SurfaceType -> Assertion
parseTypeTest input expected = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseType tokens of
        Left err -> fail (show err)
        Right ty -> ty @?= expected

{-| Test parsing a forall type.
-}
parseTypeForallTest :: String -> Assertion
parseTypeForallTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseType tokens of
        Left err -> fail (show err)
        Right STyForall{} -> return ()
        Right ty -> fail ("Expected forall type, got: " ++ show ty)

{-| Parse a proposition from string.
-}
parsePropositionTest :: String -> (SurfaceProposition -> Bool) -> Assertion
parsePropositionTest input predicate = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseProposition tokens of
        Left err -> fail (show err)
        Right (Located prop _) ->
          if predicate prop
          then return ()
          else fail ("Proposition does not match predicate: " ++ show prop)

{-| Test parsing a forall proposition.
-}
parsePropositionForallTest :: String -> Assertion
parsePropositionForallTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseProposition tokens of
        Left err -> fail (show err)
        Right (Located SPForall{} _) -> return ()
        Right (Located prop _) -> fail ("Expected forall, got: " ++ show prop)

{-| Test parsing an exists proposition.
-}
parsePropositionExistsTest :: String -> Assertion
parsePropositionExistsTest input = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseProposition tokens of
        Left err -> fail (show err)
        Right (Located SPExists{} _) -> return ()
        Right (Located prop _) -> fail ("Expected exists, got: " ++ show prop)

{-| Parse a proof from string.
-}
parseProofTest :: String -> SurfaceProof -> Assertion
parseProofTest input expected = do
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseProof tokens of
        Left err -> fail (show err)
        Right (Located proof _) -> proof @?= expected

{-| Test parsing a complete assertion.
-}
parseAssertionCompleteTest :: Assertion
parseAssertionCompleteTest = do
  let input = "assert commutativity: forall x y. x == y"
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseAssertion tokens of
        Left err -> fail (show err)
        Right SurfaceAssertion{} -> return ()

{-| Test parsing a complete proof.
-}
parseProofCompleteTest :: Assertion
parseProofCompleteTest = do
  let input = "proof my_proof : forall x y. x == y := refl"
  case lexer input of
    Left err -> fail err
    Right tokens -> do
      case parseProof tokens of
        Left err -> fail (show err)
        Right (Located proof _) ->
          case proof of
            SPRefl -> return ()
            _ -> fail "Expected refl proof"