forked from dotnet/fsharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSharpExprPatternsTests.fs
175 lines (148 loc) · 7.65 KB
/
FSharpExprPatternsTests.fs
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
module FSharp.Compiler.Service.Tests.FSharpExprPatternsTests
open FSharp.Test
#nowarn "57"
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Text
open FSharp.Compiler.Symbols
open Xunit
module TASTCollecting =
open FSharp.Compiler.Symbols.FSharpExprPatterns
type Handler = NewUnionCaseHandler of (string -> unit)
let rec visitExpr (handler: Handler) (e: FSharpExpr) =
match e with
| AddressOf lvalueExpr -> visitExpr handler lvalueExpr
| AddressSet (lvalueExpr, rvalueExpr) ->
visitExpr handler lvalueExpr
visitExpr handler rvalueExpr
| Application (funcExpr, _typeArgs, argExprs) ->
visitExpr handler funcExpr
visitExprs handler argExprs
| Call (objExprOpt, memberOrFunc, _typeArgs1, _typeArgs2, argExprs) ->
visitObjArg handler objExprOpt
visitExprs handler argExprs
| Coerce (_targetType, inpExpr) -> visitExpr handler inpExpr
| FastIntegerForLoop (startExpr, limitExpr, consumeExpr, _isUp, _debugPointAtFor, _debugPointAtInOrTo) ->
visitExpr handler startExpr
visitExpr handler limitExpr
visitExpr handler consumeExpr
| ILAsm (_asmCode, _typeArgs, argExprs) -> visitExprs handler argExprs
| ILFieldGet (objExprOpt, _fieldType, _fieldName) -> visitObjArg handler objExprOpt
| ILFieldSet (objExprOpt, _fieldType, _fieldName, _valueExpr) -> visitObjArg handler objExprOpt
| IfThenElse (guardExpr, thenExpr, elseExpr) ->
visitExpr handler guardExpr
visitExpr handler thenExpr
visitExpr handler elseExpr
| Lambda (_lambdaVar, bodyExpr) -> visitExpr handler bodyExpr
| Let ((_bindingVar, bindingExpr, _debugPointAtBinding), bodyExpr) ->
visitExpr handler bindingExpr
visitExpr handler bodyExpr
| LetRec (recursiveBindings, bodyExpr) ->
let recursiveBindings' =
recursiveBindings |> List.map (fun (mfv, expr, _dp) -> (mfv, expr))
List.iter (snd >> visitExpr handler) recursiveBindings'
visitExpr handler bodyExpr
| NewArray (_arrayType, argExprs) -> visitExprs handler argExprs
| NewDelegate (_delegateType, delegateBodyExpr) -> visitExpr handler delegateBodyExpr
| NewObject (_objType, _typeArgs, argExprs) -> visitExprs handler argExprs
| NewRecord (_recordType, argExprs) -> visitExprs handler argExprs
| NewTuple (_tupleType, argExprs) -> visitExprs handler argExprs
| NewUnionCase (_unionType, unionCase, argExprs) ->
match handler with
| NewUnionCaseHandler h -> h unionCase.Name
visitExprs handler argExprs
| Quote quotedExpr -> visitExpr handler quotedExpr
| FSharpFieldGet (objExprOpt, _recordOrClassType, _fieldInfo) -> visitObjArg handler objExprOpt
| FSharpFieldSet (objExprOpt, _recordOrClassType, _fieldInfo, argExpr) ->
visitObjArg handler objExprOpt
visitExpr handler argExpr
| Sequential (firstExpr, secondExpr) ->
visitExpr handler firstExpr
visitExpr handler secondExpr
| TryFinally (bodyExpr, finalizeExpr, _debugPointAtTry, _debugPointAtFinally) ->
visitExpr handler bodyExpr
visitExpr handler finalizeExpr
| TryWith (bodyExpr, _, _, _catchVar, catchExpr, _debugPointAtTry, _debugPointAtWith) ->
visitExpr handler bodyExpr
visitExpr handler catchExpr
| TupleGet (_tupleType, _tupleElemIndex, tupleExpr) -> visitExpr handler tupleExpr
| DecisionTree (decisionExpr, decisionTargets) ->
visitExpr handler decisionExpr
List.iter (snd >> visitExpr handler) decisionTargets
| DecisionTreeSuccess (_decisionTargetIdx, decisionTargetExprs) -> visitExprs handler decisionTargetExprs
| TypeLambda (_genericParam, bodyExpr) -> visitExpr handler bodyExpr
| TypeTest (_ty, inpExpr) -> visitExpr handler inpExpr
| UnionCaseSet (unionExpr, _unionType, _unionCase, _unionCaseField, valueExpr) ->
visitExpr handler unionExpr
visitExpr handler valueExpr
| UnionCaseGet (unionExpr, _unionType, _unionCase, _unionCaseField) -> visitExpr handler unionExpr
| UnionCaseTest (unionExpr, _unionType, _unionCase) -> visitExpr handler unionExpr
| UnionCaseTag (unionExpr, _unionType) -> visitExpr handler unionExpr
| ObjectExpr (_objType, baseCallExpr, overrides, interfaceImplementations) ->
visitExpr handler baseCallExpr
List.iter (visitObjMember handler) overrides
List.iter (snd >> List.iter (visitObjMember handler)) interfaceImplementations
| TraitCall (_sourceTypes, _traitName, _typeArgs, _typeInstantiation, _argTypes, argExprs) -> visitExprs handler argExprs
| ValueSet (_valToSet, valueExpr) -> visitExpr handler valueExpr
| WhileLoop (guardExpr, bodyExpr, _debugPointAtWhile) ->
visitExpr handler guardExpr
visitExpr handler bodyExpr
| BaseValue _baseType -> ()
| DefaultValue _defaultType -> ()
| ThisValue _thisType -> ()
| Const (_constValueObj, _constType) -> ()
| Value _valueToGet -> ()
| CallWithWitnesses (expr, _mfv, _typeLst, _typeLst2, exprs1, exprs2) ->
expr |> Option.iter (visitExpr handler)
exprs1 |> List.iter (visitExpr handler)
exprs2 |> List.iter (visitExpr handler)
| NewAnonRecord (_, exprLst) -> exprLst |> List.iter (visitExpr handler)
| AnonRecordGet (expr, _, _) -> visitExpr handler expr
| DebugPoint (_d, expr) -> visitExpr handler expr
| WitnessArg _ -> ()
| _ -> ()
and visitExprs f exprs = List.iter (visitExpr f) exprs
and visitObjArg f objOpt = Option.iter (visitExpr f) objOpt
and visitObjMember f memb = visitExpr f memb.Body
let rec visitDeclaration f d =
match d with
| FSharpImplementationFileDeclaration.Entity (_e, subDecls) ->
for subDecl in subDecls do
visitDeclaration f subDecl
| FSharpImplementationFileDeclaration.MemberOrFunctionOrValue (_v, _vs, e) -> visitExpr f e
| FSharpImplementationFileDeclaration.InitAction e -> visitExpr f e
let testPatterns handler source =
let files = Map.ofArray [| "A.fs", SourceText.ofString source |]
let documentSource fileName =
Map.tryFind fileName files |> async.Return
let projectOptions =
let _, projectOptions = mkTestFileAndOptions "" Array.empty
{ projectOptions with
SourceFiles = [| "A.fs" |]
}
let checker =
FSharpChecker.Create(documentSource = DocumentSource.Custom documentSource, keepAssemblyContents = true, useTransparentCompiler = CompilerAssertHelpers.UseTransparentCompiler)
let checkResult =
checker.ParseAndCheckFileInProject("A.fs", 0, Map.find "A.fs" files, projectOptions)
|> Async.RunImmediate
match checkResult with
| _, FSharpCheckFileAnswer.Succeeded (checkResults) ->
match checkResults.ImplementationFile with
| Some implFile ->
for decl in implFile.Declarations do
TASTCollecting.visitDeclaration handler decl
| _ -> ()
| _, _ -> ()
[<Fact>]
let ``union case with type`` () =
let implSource =
"""
module M
type T = Case1 of string
let x = Case1 "bla"
"""
let lst = ResizeArray<string>()
let handler: TASTCollecting.Handler =
TASTCollecting.Handler.NewUnionCaseHandler lst.Add
testPatterns handler implSource // check this doesn't throw
Assert.Contains("Case1", lst)