3
3
{-# LANGUAGE ScopedTypeVariables #-}
4
4
{-# LANGUAGE TypeApplications #-}
5
5
{-# LANGUAGE TypeOperators #-}
6
+
6
7
module Main (main ) where
7
8
8
- import Analysis.Name (Name )
9
- import qualified Analysis.Name as Name
10
- import qualified AST.Unmarshal as TS
11
- import Control.Algebra
12
- import Control.Carrier.Lift
13
- import Control.Carrier.Sketch.ScopeGraph
14
- import qualified Control.Effect.ScopeGraph.Properties.Declaration as Props
15
- import qualified Control.Effect.ScopeGraph.Properties.Function as Props
16
- import qualified Control.Effect.ScopeGraph.Properties.Reference as Props
17
- import Control.Monad
18
- import qualified Data.ByteString as ByteString
19
- import qualified Data.List.NonEmpty as NonEmpty
20
- import Data.Module (ModuleInfo (.. ))
21
- import qualified Data.ScopeGraph as ScopeGraph
22
- import Data.Semilattice.Lower
9
+ import Control.Monad
23
10
import qualified Language.Python ()
24
- import qualified Language.Python as Py (Term )
25
- import qualified Language.Python.Grammar as TSP
26
- import Scope.Graph.Convert
27
- import Scope.Types
28
- import Source.Loc
29
- import qualified Source.Source as Source
30
- import Source.Span
31
- import qualified Stack.Graph as Stack
32
- import System.Exit (die )
33
- import System.Path ((</>) )
11
+ import System.Path ((</>) )
34
12
import qualified System.Path as Path
35
13
import qualified System.Path.Directory as Path
36
14
import qualified Test.Tasty as Tasty
37
- import qualified Test.Tasty.HUnit as HUnit
38
15
39
16
{-
40
17
@@ -56,200 +33,18 @@ The graph should be
56
33
57
34
-}
58
35
59
-
60
- runScopeGraph :: ToScopeGraph t => Path. AbsRelFile -> Source. Source -> t Loc -> (ScopeGraph. ScopeGraph Name , Result )
61
- runScopeGraph p _src item = snd . run . runSketch info $ scopeGraph item
62
- where
63
- info = ModuleInfo p " Python" mempty
64
-
65
- runScopeGraphTest :: Monad m => SketchC Name m Result -> m (ScopeGraph. ScopeGraph Name , Result )
66
- runScopeGraphTest val = do
67
- result <- runSketch lowerBound $ val
68
- pure (snd result)
69
-
70
- sampleGraphThing :: ScopeGraphEff sig m => m Result
71
- sampleGraphThing = do
72
- declare " hello" (Props. Declaration ScopeGraph. Assignment ScopeGraph. Default Nothing (Span (Pos 2 0 ) (Pos 2 10 )))
73
- declare " goodbye" (Props. Declaration ScopeGraph. Assignment ScopeGraph. Default Nothing (Span (Pos 3 0 ) (Pos 3 12 )))
74
- pure Complete
75
-
76
- graphFile :: Path. AbsRelFile -> IO (ScopeGraph. ScopeGraph Name , Result )
77
- graphFile fp = do
78
- file <- ByteString. readFile $ Path. toString fp
79
- tree <- TS. parseByteString @ Py. Term @ Loc TSP. tree_sitter_python file
80
- pyModule <- either die pure tree
81
- pure $ runScopeGraph fp (Source. fromUTF8 file) pyModule
82
-
83
- assertSimpleAssignment :: HUnit. Assertion
84
- assertSimpleAssignment = do
85
- let path = Path. absRel " semantic-python/test/fixtures/1-04-toplevel-assignment.py"
86
- (result, Complete ) <- graphFile path
87
- (expecto, Complete ) <- runScopeGraphTest sampleGraphThing
88
- HUnit. assertEqual " Should work for simple case" expecto result
89
-
90
- assertSimpleReference :: HUnit. Assertion
91
- assertSimpleReference = do
92
- let path = Path. absRel " semantic-python/test/fixtures/5-01-simple-reference.py"
93
- (result, Complete ) <- graphFile path
94
- (expecto, Complete ) <- runScopeGraphTest expectedReference
95
-
96
- HUnit. assertEqual " Should work for simple case" expecto result
97
-
98
- expectedReference :: ScopeGraphEff sig m => m Result
99
- expectedReference = do
100
- declare " x" (Props. Declaration ScopeGraph. Assignment ScopeGraph. Default Nothing (Span (Pos 0 0 ) (Pos 0 5 )))
101
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 1 0 ) (Pos 1 1 ))
102
- newReference " x" refProperties
103
- pure Complete
104
-
105
- expectedQualifiedImport :: ScopeGraphEff sig m => m Result
106
- expectedQualifiedImport = do
107
- newEdge ScopeGraph. Import (NonEmpty. fromList [" cheese" , " ints" ])
108
-
109
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 7 ) (Pos 0 13 ))
110
- newReference (Name. name " cheese" ) refProperties
111
-
112
- withScope (CurrentScope " cheese" ) $ do
113
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 14 ) (Pos 0 18 ))
114
- newReference (Name. name " ints" ) refProperties
115
- pure Complete
116
-
117
- expectedWildcardImport :: ScopeGraphEff sig m => m Result
118
- expectedWildcardImport = do
119
- newEdge ScopeGraph. Import (NonEmpty. fromList [" cheese" , " ints" ])
120
- pure Complete
121
-
122
- assertLexicalScope :: HUnit. Assertion
123
- assertLexicalScope = do
124
- let path = Path. absRel " semantic-python/test/fixtures/5-02-simple-function.py"
125
- let info = ModuleInfo path " Python" mempty
126
- (graph, _) <- graphFile path
127
- (expecto, Complete ) <- runScopeGraphTest expectedLexicalScope
128
- HUnit. assertEqual " Should work for simple case" expecto graph
129
-
130
- expectedLexicalScope :: ScopeGraphEff sig m => m Result
131
- expectedLexicalScope = do
132
- _ <- declareFunction (Just $ Name. name " foo" ) (Props. Function ScopeGraph. Function (Span (Pos 0 0 ) (Pos 1 24 )))
133
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 3 0 ) (Pos 3 3 ))
134
- newReference " foo" refProperties
135
- pure Complete
136
-
137
-
138
- assertFunctionArg :: HUnit. Assertion
139
- assertFunctionArg = do
140
- let path = Path. absRel " semantic-python/test/fixtures/5-03-function-argument.py"
141
- (graph, _) <- graphFile path
142
- (expecto, Complete ) <- runScopeGraphTest expectedFunctionArg
143
- HUnit. assertEqual " Should work for simple case" expecto graph
144
-
145
- expectedFunctionArg :: ScopeGraphEff sig m => m Result
146
- expectedFunctionArg = do
147
- (_, associatedScope) <- declareFunction (Just $ Name. name " foo" ) (Props. Function ScopeGraph. Function (Span (Pos 0 0 ) (Pos 1 12 )))
148
- withScope (CurrentScope associatedScope) $ do
149
- declare " x" (Props. Declaration ScopeGraph. Parameter ScopeGraph. Default Nothing (Span (Pos 0 8 ) (Pos 0 9 )))
150
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 1 11 ) (Pos 1 12 ))
151
- newReference " x" refProperties
152
- pure ()
153
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 3 0 ) (Pos 3 3 ))
154
- newReference " foo" refProperties
155
- pure Complete
156
-
157
-
158
- assertWildcardImport :: HUnit. Assertion
159
- assertWildcardImport = do
160
- let path = Path. absRel " semantic-python/test/fixtures/cheese/6-01-imports.py"
161
- (graph, _) <- graphFile path
162
- (expecto, Complete ) <- runScopeGraphTest expectedWildcardImport
<
10000
td data-grid-cell-id="diff-600b303c70dea69f6a74dcba2533dffaec9611690e916140c3a90c5816f552c8-163-35-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">163
- HUnit. assertEqual " Should work for simple case" expecto graph
164
-
165
- assertQualifiedImport :: HUnit. Assertion
166
- assertQualifiedImport = do
167
- let path = Path. absRel " semantic-python/test/fixtures/cheese/6-02-qualified-imports.py"
168
- (graph, _) <- graphFile path
169
- (expecto, Complete ) <- runScopeGraphTest expectedQualifiedImport
170
- HUnit. assertEqual " Should work for simple case" expecto graph
171
-
172
- assertImportFromSymbols :: HUnit. Assertion
173
- assertImportFromSymbols = do
174
- let path = " semantic-python/test/fixtures/cheese/6-03-import-from.py"
175
- (graph, _) <- graphFile path
176
- (expecto, Complete ) <- runScopeGraphTest expectedImportFromSymbols
177
- HUnit. assertEqual " Should work for simple case" expecto graph
178
-
179
- expectedImportFromSymbols :: ScopeGraphEff sig m => m Result
180
- expectedImportFromSymbols = do
181
- newEdge ScopeGraph. Import (NonEmpty. fromList [" cheese" , " ints" ])
182
-
183
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 5 ) (Pos 0 11 ))
184
- newReference (Name. name " cheese" ) refProperties
185
-
186
- withScope (CurrentScope " cheese" ) $ do
187
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 12 ) (Pos 0 16 ))
188
- newReference (Name. name " ints" ) refProperties
189
-
190
-
191
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 24 ) (Pos 0 27 ))
192
- newReference (Name. name " two" ) refProperties
193
-
194
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 2 0 ) (Pos 2 3 ))
195
- newReference " two" refProperties
196
-
197
- pure Complete
198
-
199
- assertImportFromSymbolAliases :: HUnit. Assertion
200
- assertImportFromSymbolAliases = do
201
- let path = " semantic-python/test/fixtures/cheese/6-04-import-from-alias.py"
202
- (graph, _) <- graphFile path
203
- (expecto, Complete ) <- runScopeGraphTest expectedImportFromSymbolAliases
204
- HUnit. assertEqual " Should work for simple case" expecto graph
205
-
206
- expectedImportFromSymbolAliases :: ScopeGraphEff sig m => m Result
207
- expectedImportFromSymbolAliases = do
208
- newEdge ScopeGraph. Import (NonEmpty. fromList [" cheese" , " ints" ])
209
-
210
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 5 ) (Pos 0 11 ))
211
- newReference (Name. name " cheese" ) refProperties
212
-
213
- withScope (CurrentScope " cheese" ) $ do
214
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 12 ) (Pos 0 16 ))
215
- newReference (Name. name " ints" ) refProperties
216
-
217
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 0 24 ) (Pos 0 27 ))
218
- newReference (Name. name " two" ) refProperties
219
-
220
- let declProperties = Props. Declaration ScopeGraph. UnqualifiedImport ScopeGraph. Default Nothing (Span (Pos 0 31 ) (Pos 0 36 ))
221
- declare (Name. name " three" ) declProperties
222
-
223
- let refProperties = Props. Reference ScopeGraph. Identifier ScopeGraph. Default (Span (Pos 2 0 ) (Pos 2 5 ))
224
- newReference " three" refProperties
225
-
226
- pure Complete
227
-
228
36
main :: IO ()
229
37
main = do
230
38
-- make sure we're in the root directory so the paths resolve properly
231
39
cwd <- Path. getCurrentDirectory
232
- when (Path. takeDirName cwd == Just (Path. relDir " semantic-python" ))
40
+ when
41
+ (Path. takeDirName cwd == Just (Path. relDir " semantic-python" ))
233
42
(Path. setCurrentDirectory (cwd </> Path. relDir " .." ))
234
43
235
44
Tasty. defaultMain $
236
- Tasty. testGroup " Tests" [
237
- Tasty. testGroup " scope graph" [
238
- Tasty. testGroup " declare" [
239
- HUnit. testCase " toplevel assignment" assertSimpleAssignment
240
- ],
241
- Tasty. testGroup " reference" [
242
- HUnit. testCase " simple reference" assertSimpleReference
243
- ],
244
- Tasty. testGroup " lexical scopes" [
245
- HUnit. testCase " simple function scope" assertLexicalScope
246
- , HUnit. testCase " simple function argument" assertFunctionArg
247
- ],
248
- Tasty. testGroup " imports" [
249
- HUnit. testCase " simple function argument" assertWildcardImport
250
- , HUnit. testCase " qualified imports" assertQualifiedImport
251
- , HUnit. testCase " qualified imports with symbols" assertImportFromSymbols
252
- , HUnit. testCase " qualified imports with symbol aliases" assertImportFromSymbolAliases
253
- ]
45
+ Tasty. testGroup
46
+ " Tests"
47
+ [ Tasty. testGroup
48
+ " scope graph"
49
+ []
254
50
]
255
- ]
0 commit comments