|
| 1 | +{-# LANGUAGE OverloadedStrings, TypeApplications #-} |
| 2 | +{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 | + |
| 4 | +module Main (main) where |
| 5 | + |
| 6 | +import Data.String |
| 7 | +import qualified Text.Trifecta as Trifecta |
| 8 | + |
| 9 | +import Hedgehog hiding (Var) |
| 10 | +import Test.Tasty |
| 11 | +import Test.Tasty.Hedgehog |
| 12 | +import Test.Tasty.HUnit |
| 13 | + |
| 14 | +import Data.File |
| 15 | +import qualified Generators as Gen |
| 16 | +import qualified Analysis.Eval as Eval |
| 17 | +import Data.Core |
| 18 | +import Data.Core.Pretty |
| 19 | +import Data.Core.Parser as Parse |
| 20 | +import Data.Name |
| 21 | + |
| 22 | +-- * Helpers |
| 23 | + |
| 24 | +true, false :: Core |
| 25 | +true = Bool True |
| 26 | +false = Bool False |
| 27 | + |
| 28 | +instance IsString Name where fromString = User |
| 29 | + |
| 30 | +parseEither :: Trifecta.Parser a -> String -> Either String a |
| 31 | +parseEither p = Trifecta.foldResult (Left . show . Trifecta._errDoc) Right . Trifecta.parseString (p <* Trifecta.eof) mempty |
| 32 | + |
| 33 | +-- * Parser roundtripping properties. Note that parsing and prettyprinting is generally |
| 34 | +-- not a roundtrip, because the parser inserts 'Ann' nodes itself. |
| 35 | + |
| 36 | +prop_roundtrips :: Gen Core -> Property |
| 37 | +prop_roundtrips gen = property $ do |
| 38 | + input <- forAll gen |
| 39 | + tripping input showCore (parseEither (Parse.core <* Trifecta.eof)) |
| 40 | + |
| 41 | +parserProps :: TestTree |
| 42 | +parserProps = testGroup "Parsing: roundtripping" |
| 43 | + [ testProperty "literals" $ prop_roundtrips Gen.literal |
| 44 | + , testProperty "if/then/else" . prop_roundtrips . Gen.ifthenelse $ Gen.variable |
| 45 | + , testProperty "lambda" . prop_roundtrips $ Gen.lambda Gen.literal |
| 46 | + , testProperty "function application" . prop_roundtrips $ Gen.apply Gen.variable |
| 47 | + ] |
| 48 | + |
| 49 | +-- * Parser specs |
| 50 | + |
| 51 | +parsesInto :: String -> Core -> Assertion |
| 52 | +parsesInto str res = case parseEither Parse.core str of |
| 53 | + Right x -> x @?= res |
| 54 | + Left m -> assertFailure m |
| 55 | + |
| 56 | +assert_booleans_parse :: Assertion |
| 57 | +assert_booleans_parse = do |
| 58 | + parseEither Parse.core "#true" @?= Right true |
| 59 | + parseEither Parse.core "#false" @?= Right false |
| 60 | + |
| 61 | +a, f, g, h :: Core |
| 62 | +(a, f, g, h) = (Var "a", Var "f", Var "g", Var "h") |
| 63 | + |
| 64 | +assert_ifthen_parse :: Assertion |
| 65 | +assert_ifthen_parse = "if #true then #true else #false" `parsesInto` (If true true false) |
| 66 | + |
| 67 | +assert_application_parse :: Assertion |
| 68 | +assert_application_parse ="f g" `parsesInto` (f :$ g) |
| 69 | + |
| 70 | +assert_application_left_associative :: Assertion |
| 71 | +assert_application_left_associative = "f g h" `parsesInto` (f :$ g :$ h) |
| 72 | + |
| 73 | +assert_push_left_associative :: Assertion |
| 74 | +assert_push_left_associative = "f.g.h" `parsesInto` (f :. g :. h) |
| 75 | + |
| 76 | +assert_ascii_lambda_parse :: Assertion |
| 77 | +assert_ascii_lambda_parse = "\\a -> a" `parsesInto` Lam "a" a |
| 78 | + |
| 79 | +assert_unicode_lambda_parse :: Assertion |
| 80 | +assert_unicode_lambda_parse = "λa → a" `parsesInto` Lam "a" a |
| 81 | + |
| 82 | +assert_quoted_name_parse :: Assertion |
| 83 | +assert_quoted_name_parse = "#{(NilClass)}" `parsesInto` Var (User "(NilClass)") |
| 84 | + |
| 85 | +assert_let_dot_precedence :: Assertion |
| 86 | +assert_let_dot_precedence = "let a = f.g.h" `parsesInto` (Let "a" := (f :. g :. h)) |
| 87 | + |
| 88 | +assert_let_in_push_precedence :: Assertion |
| 89 | +assert_let_in_push_precedence = "f.let g = h" `parsesInto` (f :. (Let "g" := h)) |
| 90 | + |
| 91 | +parserSpecs :: TestTree |
| 92 | +parserSpecs = testGroup "Parsing: simple specs" |
| 93 | + [ testCase "true/false" assert_booleans_parse |
| 94 | + , testCase "if/then/else" assert_ifthen_parse |
| 95 | + , testCase "function application" assert_application_parse |
| 96 | + , testCase "application is left-associative" assert_application_left_associative |
| 97 | + , testCase "dotted push is left-associative" assert_push_left_associative |
| 98 | + , testCase "lambda with ASCII syntax" assert_ascii_lambda_parse |
| 99 | + , testCase "lambda with unicode syntax" assert_unicode_lambda_parse |
| 100 | + , testCase "quoted names" assert_quoted_name_parse |
| 101 | + , testCase "let + dot precedence" assert_let_dot_precedence |
| 102 | + , testCase "let in push" assert_let_in_push_precedence |
| 103 | + ] |
| 104 | + |
| 105 | +assert_roundtrips :: File Core -> Assertion |
| 106 | +assert_roundtrips (File _ core) = parseEither Parse.core (showCore core) @?= Right (stripAnnotations core) |
| 107 | + |
| 108 | +parserExamples :: TestTree |
| 109 | +parserExamples = testGroup "Parsing: Eval.hs examples" |
| 110 | + [ testCase "prog1" (assert_roundtrips Eval.prog1) |
| 111 | + , testCase "prog2" (assert_roundtrips Eval.prog2) |
| 112 | + , testCase "prog3" (assert_roundtrips Eval.prog3) |
| 113 | + , testCase "prog4" (assert_roundtrips Eval.prog4) |
| 114 | + , testCase "prog6.1" (assert_roundtrips (head Eval.prog6)) |
| 115 | + , testCase "prog6.2" (assert_roundtrips (last Eval.prog6)) |
| 116 | + , testCase "ruby" (assert_roundtrips Eval.ruby) |
| 117 | + ] |
| 118 | + |
| 119 | +tests :: TestTree |
| 120 | +tests = testGroup "semantic-core" |
| 121 | + [ parserSpecs |
| 122 | + , parserExamples |
| 123 | + , parserProps |
| 124 | + ] |
| 125 | + |
| 126 | +main :: IO () |
| 127 | +main = defaultMain tests |
0 commit comments