This repository was archived by the owner on Apr 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 457
Expand file tree
/
Copy pathStackTest.hs
More file actions
80 lines (71 loc) · 3.13 KB
/
StackTest.hs
File metadata and controls
80 lines (71 loc) · 3.13 KB
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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Main (main) where
import Analysis.Name (Name)
import qualified Analysis.Name as Name
import qualified AST.Unmarshal as TS
import Control.Algebra
import Control.Carrier.Lift
import Control.Carrier.Sketch.ScopeGraph
import qualified Control.Effect.ScopeGraph.Properties.Declaration as Props
import qualified Control.Effect.ScopeGraph.Properties.Function as Props
import qualified Control.Effect.ScopeGraph.Properties.Reference as Props
import Control.Monad
import qualified Data.ByteString as ByteString
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map.Strict as Map
import qualified Data.ScopeGraph as ScopeGraph
import Data.Semilattice.Lower
import Debug.Trace
import qualified Language.Python ()
import qualified Language.Python as Py (Term)
import qualified Language.Python.Grammar as TSP
import Scope.Graph.Convert
import Source.Loc
import qualified Source.Source as Source
import Source.Span
import qualified Stack.Graph as Stack
import System.Exit (die)
import System.Path ((</>))
import qualified System.Path as Path
import qualified System.Path.Directory as Path
import qualified Test.Tasty as Tasty
import qualified Test.Tasty.HUnit as HUnit
runStackGraph :: ToScopeGraph t => Path.AbsRelFile -> Source.Source -> t Loc -> (Stack.Graph Stack.Node, Result)
runStackGraph p _src item = (\(stack, (scopeGraph, result)) -> (stack, result)) . run . runSketch minfo $ scopeGraph item
where minfo = lowerBound
runStackGraphTest :: Monad m => SketchC Name m Result -> m (Stack.Graph Stack.Node, Result)
runStackGraphTest val = do
result <- runSketch lowerBound $ val
pure ((\(stack, (scopeGraph, result)) -> (stack, result)) result)
stackGraphFile :: FilePath -> IO (Stack.Graph Stack.Node, Result)
stackGraphFile fp = do
file <- ByteString.readFile fp
tree <- TS.parseByteString @Py.Term @Loc TSP.tree_sitter_python file
pyModule <- either die pure tree
pure $ runStackGraph (Path.absRel fp) (Source.fromUTF8 file) pyModule
expectedQualifiedImport :: ScopeGraphEff sig m => m Result
expectedQualifiedImport = do
ScopeGraph.CurrentScope currentName <- currentScope
name <- newScope (Map.singleton ScopeGraph.Lexical [ currentName ])
putCurrentScope name
pure Complete
assertQualifiedImport :: HUnit.Assertion
assertQualifiedImport = do
let path = "semantic-python/test/fixtures/cheese/6-02-qualified-imports.py"
(graph, _) <- stackGraphFile path
(expecto, Complete) <- runStackGraphTest expectedQualifiedImport
traceShowM expecto
HUnit.assertEqual "Should work for simple case" expecto graph
main :: IO ()
main = do
-- make sure we're in the root directory so the paths resolve properly
cwd <- Path.getCurrentDirectory
when (Path.takeDirName cwd == Just (Path.relDir "semantic-python"))
(Path.setCurrentDirectory (cwd </> Path.relDir ".."))
Tasty.defaultMain $
Tasty.testGroup "stack graph" [
HUnit.testCase "qualified import" assertQualifiedImport
]