-
Notifications
You must be signed in to change notification settings - Fork 870
Expand file tree
/
Copy pathCSharpProjectAnalysis.fs
More file actions
198 lines (176 loc) · 8.19 KB
/
Copy pathCSharpProjectAnalysis.fs
File metadata and controls
198 lines (176 loc) · 8.19 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#if INTERACTIVE
#r "../../artifacts/bin/fcs/net461/FSharp.Compiler.Service.dll" // note, build FSharp.Compiler.Service.Tests.fsproj to generate this, this DLL has a public API so can be used from F# Interactive
#r "../../bin/v4.5/CSharp_Analysis.dll"
#r "../../artifacts/bin/fcs/net461/nunit.framework.dll"
#load "FsUnit.fs"
#load "Common.fs"
#else
module FSharp.Compiler.Service.Tests.CSharpProjectAnalysis
#endif
open NUnit.Framework
open FsUnit
open System
open System.IO
open System.Collections.Generic
open FSharp.Compiler
open FSharp.Compiler.Service.Tests
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Service.Tests.Common
let internal getProjectReferences (content, dllFiles, libDirs, otherFlags) =
let otherFlags = defaultArg otherFlags []
let libDirs = defaultArg libDirs []
let base1 = Path.GetTempFileName()
let dllName = Path.ChangeExtension(base1, ".dll")
let fileName1 = Path.ChangeExtension(base1, ".fs")
let projFileName = Path.ChangeExtension(base1, ".fsproj")
File.WriteAllText(fileName1, content)
let options =
checker.GetProjectOptionsFromCommandLineArgs(projFileName,
[| yield "--debug:full"
yield "--define:DEBUG"
yield "--optimize-"
yield "--out:" + dllName
yield "--doc:test.xml"
yield "--warn:3"
yield "--fullpaths"
yield "--flaterrors"
yield "--target:library"
for dllFile in dllFiles do
yield "-r:"+dllFile
for libDir in libDirs do
yield "-I:"+libDir
yield! otherFlags
yield fileName1 |])
let results = checker.ParseAndCheckProject(options) |> Async.RunSynchronously
if results.HasCriticalErrors then
let builder = new System.Text.StringBuilder()
for err in results.Errors do
builder.AppendLine(sprintf "**** %s: %s" (if err.Severity = FSharpErrorSeverity.Error then "error" else "warning") err.Message)
|> ignore
failwith (builder.ToString())
let assemblies =
results.ProjectContext.GetReferencedAssemblies()
|> List.map(fun x -> x.SimpleName, x)
|> dict
results, assemblies
[<Test>]
#if NETCOREAPP2_0
[<Ignore("SKIPPED: need to check if these tests can be enabled for .NET Core testing of FSharp.Compiler.Service")>]
#endif
let ``Test that csharp references are recognized as such`` () =
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
let _, table = getProjectReferences("""module M""", [csharpAssembly], None, None)
let assembly = table.["CSharp_Analysis"]
let search = assembly.Contents.Entities |> Seq.tryFind (fun e -> e.DisplayName = "CSharpClass")
Assert.True search.IsSome
let found = search.Value
// this is no F# thing
found.IsFSharp |> shouldEqual false
// Check that we have members
let members = found.MembersFunctionsAndValues |> Seq.map (fun e -> e.CompiledName, e) |> dict
members.ContainsKey ".ctor" |> shouldEqual true
members.ContainsKey "Method" |> shouldEqual true
members.ContainsKey "Property" |> shouldEqual true
members.ContainsKey "Event" |> shouldEqual true
members.ContainsKey "InterfaceMethod" |> shouldEqual true
members.ContainsKey "InterfaceProperty" |> shouldEqual true
members.ContainsKey "InterfaceEvent" |> shouldEqual true
members.["Event"].IsEvent |> shouldEqual true
members.["Event"].EventIsStandard |> shouldEqual true
members.["Event"].EventAddMethod.DisplayName |> shouldEqual "add_Event"
members.["Event"].EventRemoveMethod.DisplayName |> shouldEqual "remove_Event"
members.["Event"].EventDelegateType.ToString() |> shouldEqual "type System.EventHandler"
//// Check that we get xml docs
members.[".ctor"].XmlDocSig |> shouldEqual "M:FSharp.Compiler.Service.Tests.CSharpClass.#ctor(System.Int32,System.String)"
members.["Method"].XmlDocSig |> shouldEqual "M:FSharp.Compiler.Service.Tests.CSharpClass.Method(System.String)"
members.["Property"].XmlDocSig |> shouldEqual "P:FSharp.Compiler.Service.Tests.CSharpClass.Property"
members.["Event"].XmlDocSig |> shouldEqual "E:FSharp.Compiler.Service.Tests.CSharpClass.Event"
members.["InterfaceMethod"].XmlDocSig |> shouldEqual "M:FSharp.Compiler.Service.Tests.CSharpClass.InterfaceMethod(System.String)"
members.["InterfaceProperty"].XmlDocSig |> shouldEqual "P:FSharp.Compiler.Service.Tests.CSharpClass.InterfaceProperty"
members.["InterfaceEvent"].XmlDocSig |> shouldEqual "E:FSharp.Compiler.Service.Tests.CSharpClass.InterfaceEvent"
[<Test>]
#if NETCOREAPP2_0
[<Ignore("SKIPPED: need to check if these tests can be enabled for .NET Core testing of FSharp.Compiler.Service")>]
#endif
let ``Test that symbols of csharp inner classes/enums are reported`` () =
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
let content = """
module NestedEnumClass
open FSharp.Compiler.Service.Tests
let _ = CSharpOuterClass.InnerEnum.Case1
let _ = CSharpOuterClass.InnerClass.StaticMember()
"""
let results, _ = getProjectReferences(content, [csharpAssembly], None, None)
results.GetAllUsesOfAllSymbols()
|> Async.RunSynchronously
|> Array.map (fun su -> su.Symbol.ToString())
|> shouldEqual
[|"FSharp"; "Compiler"; "Service"; "Tests"; "FSharp"; "InnerEnum";
"CSharpOuterClass"; "field Case1"; "InnerClass"; "CSharpOuterClass";
"member StaticMember"; "NestedEnumClass"|]
[<Test>]
#if NETCOREAPP2_0
[<Ignore("SKIPPED: need to check if these tests can be enabled for .NET Core testing of FSharp.Compiler.Service")>]
#endif
let ``Ctor test`` () =
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
let content = """
module CtorTest
open FSharp.Compiler.Service.Tests
let _ = CSharpClass(0)
"""
let results, _ = getProjectReferences(content, [csharpAssembly], None, None)
let ctor =
results.GetAllUsesOfAllSymbols()
|> Async.RunSynchronously
|> Seq.map (fun su -> su.Symbol)
|> Seq.find (function :? FSharpMemberOrFunctionOrValue as mfv -> mfv.IsConstructor | _ -> false)
match (ctor :?> FSharpMemberOrFunctionOrValue).DeclaringEntity with
| Some e ->
let members = e.MembersFunctionsAndValues
Seq.exists (fun (mfv : FSharpMemberOrFunctionOrValue) -> mfv.IsConstructor) members |> should be True
Seq.exists (fun (mfv : FSharpMemberOrFunctionOrValue) -> mfv.IsEffectivelySameAs ctor) members |> should be True
| None -> failwith "Expected Some for DeclaringEntity"
let getEntitiesUses source =
let csharpAssembly = PathRelativeToTestAssembly "CSharp_Analysis.dll"
let results, _ = getProjectReferences(source, [csharpAssembly], None, None)
results.GetAllUsesOfAllSymbols()
|> Async.RunSynchronously
|> Seq.choose (fun su ->
match su.Symbol with
| :? FSharpEntity as entity -> Some entity
| _ -> None)
|> List.ofSeq
[<Test>]
#if NETCOREAPP2_0
[<Ignore("SKIPPED: need to check if these tests can be enabled for .NET Core testing of FSharp.Compiler.Service")>]
#endif
let ``Different types with the same short name equality check`` () =
let source = """
module CtorTest
let (s1: System.String) = null
let (s2: FSharp.Compiler.Service.Tests.String) = null
"""
let stringSymbols =
getEntitiesUses source
|> List.filter (fun entity -> entity.LogicalName = "String")
match stringSymbols with
| e1 :: e2 :: [] -> e1.IsEffectivelySameAs(e2) |> should be False
| _ -> sprintf "Expecting two symbols, got %A" stringSymbols |> failwith
[<Test>]
#if NETCOREAPP2_0
[<Ignore("SKIPPED: need to check if these tests can be enabled for .NET Core testing of FSharp.Compiler.Service")>]
#endif
let ``Different namespaces with the same short name equality check`` () =
let source = """
module CtorTest
open System.Linq
open FSharp.Compiler.Service.Tests.Linq
"""
let stringSymbols =
getEntitiesUses source
|> List.filter (fun entity -> entity.LogicalName = "Linq")
match stringSymbols with
| e1 :: e2 :: [] -> e1.IsEffectivelySameAs(e2) |> should be False
| _ -> sprintf "Expecting two symbols, got %A" stringSymbols |> failwith