DocumentHighlightsServiceTests.fs 3.9 KB
Newer Older
1 2 3

// To run the tests in this file:
//
4
// Technique 1: Compile VisualFSharp.UnitTests.dll and run it as a set of unit tests
5 6 7 8 9 10 11 12 13
//
// Technique 2:
//
//   Enable some tests in the #if EXE section at the end of the file, 
//   then compile this file as an EXE that has InternalsVisibleTo access into the
//   appropriate DLLs.  This can be the quickest way to get turnaround on updating the tests
//   and capturing large amounts of structured output.
(*
    cd Debug\net40\bin
14
    .\fsc.exe --define:EXE -r:.\Microsoft.Build.Utilities.Core.dll -o VisualFSharp.UnitTests.exe -g --optimize- -r .\FSharp.Compiler.Service.dll  -r .\FSharp.Editor.dll -r nunit.framework.dll ..\..\..\tests\service\FsUnit.fs ..\..\..\tests\service\Common.fs /delaysign /keyfile:..\..\..\src\fsharp\msft.pubkey ..\..\..\vsintegration\tests\UnitTests\CompletionProviderTests.fs 
15
    .\VisualFSharp.UnitTests.exe 
16 17 18
*)
// Technique 3: 
// 
19
//    Use F# Interactive.  This only works for FSharp.Compiler.Service.dll which has a public API
20

21
// Copyright (c) Microsoft Corporation.  All Rights Reserved.  See License.txt in the project root for license information.
22
[<NUnit.Framework.Category "Roslyn Services">]
23 24 25 26 27 28 29 30 31 32 33
module Microsoft.VisualStudio.FSharp.Editor.Tests.Roslyn.DocumentHighlightsServiceTests

open System
open System.Threading

open NUnit.Framework

open Microsoft.CodeAnalysis
open Microsoft.CodeAnalysis.Text
open Microsoft.VisualStudio.FSharp.Editor

D
Don Syme 已提交
34
open FSharp.Compiler.SourceCodeServices
D
Don Syme 已提交
35
open FSharp.Compiler.Text
D
Don Syme 已提交
36
open UnitTests.TestLib.LanguageService
37 38 39

let filePath = "C:\\test.fs"

40
let internal projectOptions = { 
41
    ProjectFileName = "C:\\test.fsproj"
42
    ProjectId = None
43
    SourceFiles =  [| filePath |]
44 45 46 47 48 49
    ReferencedProjects = [| |]
    OtherOptions = [| |]
    IsIncompleteTypeCheckEnvironment = true
    UseScriptResolutionRules = false
    LoadTime = DateTime.MaxValue
    UnresolvedReferences = None
50
    OriginalLoadReferences = []
51
    ExtraProjectInfo = None
52
    Stamp = None
53 54 55 56
}

let private getSpans (sourceText: SourceText) (caretPosition: int) =
    let documentId = DocumentId.CreateNewId(ProjectId.CreateNewId())
57
    FSharpDocumentHighlightsService.GetDocumentHighlights(checker, documentId, sourceText, filePath, caretPosition, [], projectOptions, 0, LanguageServicePerformanceOptions.Default)
58
    |> Async.RunSynchronously
59
    |> Option.defaultValue [||]
60 61

let private span sourceText isDefinition (startLine, startCol) (endLine, endCol) =
D
Don Syme 已提交
62
    let range = Range.mkRange filePath (Pos.mkPos startLine startCol) (Pos.mkPos endLine endCol)
63
    { IsDefinition = isDefinition
J
Jared Hester 已提交
64
      TextSpan = RoslynHelpers.FSharpRangeToTextSpan(sourceText, range) }
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

[<Test>]
let ShouldHighlightAllSimpleLocalSymbolReferences() =
    let fileContents = """
    let foo x = 
        x + x
    let y = foo 2
    """
    let sourceText = SourceText.From(fileContents)
    let caretPosition = fileContents.IndexOf("foo") + 1
    let spans = getSpans sourceText caretPosition
    
    let expected =
        [| span sourceText true (2, 8) (2, 11)
           span sourceText false (4, 12) (4, 15) |]
    
    Assert.AreEqual(expected, spans)

[<Test>]
let ShouldHighlightAllQualifiedSymbolReferences() =
    let fileContents = """
    let x = System.DateTime.Now
    let y = System.DateTime.MaxValue
    """
    let sourceText = SourceText.From(fileContents)
    let caretPosition = fileContents.IndexOf("DateTime") + 1
    let documentId = DocumentId.CreateNewId(ProjectId.CreateNewId())
    
    let spans = getSpans sourceText caretPosition
    
    let expected =
        [| span sourceText false (2, 19) (2, 27)
           span sourceText false (3, 19) (3, 27) |]
    
    Assert.AreEqual(expected, spans)

    let caretPosition = fileContents.IndexOf("Now") + 1
    let documentId = DocumentId.CreateNewId(ProjectId.CreateNewId())
    let spans = getSpans sourceText caretPosition
    let expected = [| span sourceText false (2, 28) (2, 31) |]
    
    Assert.AreEqual(expected, spans)