diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-07-31 00:49:16 +0300 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-07-31 00:49:16 +0300 |
| commit | d4471ca2fa765c8c4c4f1e8bec59fc0c441eb824 (patch) | |
| tree | 1490547cc1662d66fbf6d27f5da3b6fdb352c905 /tests | |
| parent | 92990e52f017c3fa0b9ff99f517171051d8c7c18 (diff) | |
| download | elna-d4471ca2fa765c8c4c4f1e8bec59fc0c441eb824.tar.gz | |
Collect types into the global symbol table
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Language/Elna/NameAnalysisSpec.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/Language/Elna/NameAnalysisSpec.hs b/tests/Language/Elna/NameAnalysisSpec.hs new file mode 100644 index 0000000..c94e6c7 --- /dev/null +++ b/tests/Language/Elna/NameAnalysisSpec.hs @@ -0,0 +1,45 @@ +module Language.Elna.NameAnalysisSpec + ( spec + ) where + +import Data.Text (Text) +import Text.Megaparsec (runParser) +import Test.Hspec (Spec, describe, it, shouldBe, shouldSatisfy, pendingWith) +import Language.Elna.NameAnalysis (Error(..), nameAnalysis) +import Language.Elna.SymbolTable (Info(..), SymbolTable) +import qualified Language.Elna.SymbolTable as SymbolTable +import qualified Language.Elna.Parser as AST +import Language.Elna.Types (intType) +import Control.Exception (throwIO) + +nameAnalysisOnText :: Text -> IO (Either Error SymbolTable) +nameAnalysisOnText sourceText = nameAnalysis + <$> either throwIO pure (runParser AST.programP "" sourceText) + +spec :: Spec +spec = describe "nameAnalysis" $ do + it "adds type to the symbol table" $ do + let given = "type A = int" + expected = Right $ Just $ TypeInfo intType + actual <- nameAnalysisOnText given + + actual `shouldSatisfy` (expected ==) . fmap (SymbolTable.lookup "A") + + it "errors when the aliased type is not defined" $ do + let given = "type A = B" + expected = Left $ UndefinedTypeError "B" + actual <- nameAnalysisOnText given + + actual `shouldBe` expected + + it "errors if the aliased identifier is not a type" $ do + let given = "proc main() {} type A = main" + expected = Left + $ UnexpectedTypeInfoError + $ ProcedureInfo mempty mempty + actual <- nameAnalysisOnText given + + actual `shouldBe` expected + + it "replaces the alias with an equivalent base type" $ + pendingWith "Not implemented" |
