Skip to content

Commit

Permalink
Added unit test for analysis of simple C# code with FCS.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthid committed Jan 2, 2015
1 parent b544581 commit 8a00e44
Show file tree
Hide file tree
Showing 7 changed files with 316 additions and 0 deletions.
17 changes: 17 additions & 0 deletions FSharp.Compiler.Service.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30501.0
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "project", "project", "{B6B68AE6-E7A4-4D43-9B34-FFA74BFE192B}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -55,6 +56,7 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FSharp.Compiler.Service.Tes
EndProjectSection
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Fsc", "samples\FscExe\Fsc.fsproj", "{C94C257C-3C0A-4858-B5D8-D746498D1F08}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSharp_Analysis", "tests\service\CSharp_Analysis\CSharp_Analysis\CSharp_Analysis.csproj", "{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -189,6 +191,21 @@ Global
{C94C257C-3C0A-4858-B5D8-D746498D1F08}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{C94C257C-3C0A-4858-B5D8-D746498D1F08}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{C94C257C-3C0A-4858-B5D8-D746498D1F08}.Release|x86.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|x86.ActiveCfg = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Proto|Any CPU.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Proto|Any CPU.Build.0 = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Proto|Mixed Platforms.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Proto|Mixed Platforms.Build.0 = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Proto|x86.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Any CPU.Build.0 = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
90 changes: 90 additions & 0 deletions tests/service/CSharpProjectAnalysis.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#if INTERACTIVE
#r "../../bin/v4.5/FSharp.Compiler.Service.dll"
#r "../../packages/NUnit.2.6.3/lib/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 Microsoft.FSharp.Compiler
open Microsoft.FSharp.Compiler.SourceCodeServices

open FSharp.Compiler.Service.Tests.Common

let getProjectReferences (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, """module M""")
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 = Microsoft.FSharp.Compiler.Severity.Error then "error" else "warning") err.Message)
|> ignore
failwith (builder.ToString())

results.ProjectContext.GetReferencedAssemblies()
|> List.map(fun x -> x.SimpleName, x)
|> dict

[<Test>]
let ``Test that csharp references are recognized as such`` () =
let csharpAssembly = typeof<CSharpClass>.Assembly.Location
let table = getProjectReferences([csharpAssembly], None, None)
let ass = table.["CSharp_Analysis"]
match ass.Contents.Entities |> Seq.tryFind (fun e -> e.DisplayName = "CSharpClass") with
| Some found ->
// 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

//// Check that we get xml docs
//String.IsNullOrWhiteSpace(members.[".ctor"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["Method"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["Property"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["Event"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["InterfaceMethod"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["InterfaceProperty"].XmlDocSig) |> shouldEqual false
//String.IsNullOrWhiteSpace(members.["InterfaceEvent"].XmlDocSig) |> shouldEqual false

()
| None -> ()
22 changes: 22 additions & 0 deletions tests/service/CSharp_Analysis/CSharp_Analysis.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sample_VS2013_CSharp_Library_net45", "Sample_VS2013_CSharp_Library_net45\Sample_VS2013_CSharp_Library_net45.csproj", "{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
90 changes: 90 additions & 0 deletions tests/service/CSharp_Analysis/CSharp_Analysis/CSharpClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FSharp.Compiler.Service.Tests
{
/// <summary>
/// Documentation
/// </summary>
public interface ICSharpInterface
{
int InterfaceMethod(string parameter);
bool InterfaceProperty { get; }

event EventHandler InterfaceEvent;
}

public interface ICSharpExplicitInterface
{
int ExplicitMethod(string parameter);
bool ExplicitProperty { get; }

event EventHandler ExplicitEvent;
}

public class CSharpClass : ICSharpInterface, ICSharpExplicitInterface
{
/// <summary>
/// Documentaton
/// </summary>
/// <param name="param"></param>
public CSharpClass(int param)
{

}

/// <summary>
/// Documentaton
/// </summary>
/// <param name="first"></param>
/// <param name="param"></param>
public CSharpClass(int first, string param)
{

}

public int Method(string parameter)
{
throw new NotImplementedException();
}

public bool Property
{
get { throw new NotImplementedException(); }
}

public event EventHandler Event;


public int InterfaceMethod(string parameter)
{
throw new NotImplementedException();
}

public bool InterfaceProperty
{
get { throw new NotImplementedException(); }
}

public event EventHandler InterfaceEvent;

int ICSharpExplicitInterface.ExplicitMethod(string parameter)
{
throw new NotImplementedException();
}

bool ICSharpExplicitInterface.ExplicitProperty
{
get { throw new NotImplementedException(); }
}

event EventHandler ICSharpExplicitInterface.ExplicitEvent
{
add { throw new NotImplementedException(); }
remove { throw new NotImplementedException(); }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{887630A3-4B1D-40EA-B8B3-2D842E9C40DB}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSharp_Analysis</RootNamespace>
<AssemblyName>CSharp_Analysis</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CSharpClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// Allgemeine Informationen über eine Assembly werden über die folgenden
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die mit einer Assembly verknüpft sind.
[assembly: AssemblyTitle("CSharp_Analysis")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharp_Analysis")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Durch Festlegen von ComVisible auf "false" werden die Typen in dieser Assembly unsichtbar
// für COM-Komponenten. Wenn Sie auf einen Typ in dieser Assembly von
// COM zugreifen müssen, legen Sie das ComVisible-Attribut für diesen Typ auf "true" fest.
[assembly: ComVisible(false)]

// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird
[assembly: Guid("e1b15939-475d-4134-a76c-20845e07be39")]

// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten:
//
// Hauptversion
// Nebenversion
// Buildnummer
// Revision
//
// Sie können alle Werte angeben oder die standardmäßigen Build- und Revisionsnummern
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
8 changes: 8 additions & 0 deletions tests/service/FSharp.Compiler.Service.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<Compile Include="PerfTests.fs" />
<Compile Include="InteractiveCheckerTests.fs" />
<Compile Include="ExprTests.fs" />
<Compile Include="CSharpProjectAnalysis.fs" />
<None Include="packages.config" />
<None Include="app.config" />
</ItemGroup>
Expand All @@ -88,5 +89,12 @@
<Reference Include="System.Xml.Linq" />
<Reference Include="Microsoft.Build, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Condition=" '$(TargetFrameworkVersion)' == 'v4.5'" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="CSharp_Analysis\CSharp_Analysis\CSharp_Analysis.csproj">
<Name>CSharp_Analysis</Name>
<Project>{887630a3-4b1d-40ea-b8b3-2d842e9c40db}</Project>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
</Project>

0 comments on commit 8a00e44

Please sign in to comment.