|
| 1 | +{-# LANGUAGE DerivingStrategies #-} |
| 2 | +{-# LANGUAGE TypeOperators #-} |
| 3 | +module Ide.TypesTests |
| 4 | + ( tests |
| 5 | + ) where |
| 6 | +import Control.Lens ((?~)) |
| 7 | +import Data.Default (Default (def)) |
| 8 | +import Data.Function ((&)) |
| 9 | +import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty) |
| 10 | +import qualified Data.Text as Text |
| 11 | +import Ide.Types (Config (Config), |
| 12 | + PluginRequestMethod (combineResponses)) |
| 13 | +import qualified Language.LSP.Protocol.Lens as L |
| 14 | +import Language.LSP.Protocol.Message (Method (Method_TextDocumentDefinition), |
| 15 | + SMethod (..)) |
| 16 | +import Language.LSP.Protocol.Types (ClientCapabilities, |
| 17 | + Definition (Definition), |
| 18 | + DefinitionClientCapabilities (DefinitionClientCapabilities, _dynamicRegistration, _linkSupport), |
| 19 | + DefinitionLink (DefinitionLink), |
| 20 | + DefinitionParams (DefinitionParams, _partialResultToken, _position, _textDocument, _workDoneToken), |
| 21 | + Location (Location), |
| 22 | + LocationLink (LocationLink), |
| 23 | + Null (Null), |
| 24 | + Position (Position), |
| 25 | + Range (Range), |
| 26 | + TextDocumentClientCapabilities (TextDocumentClientCapabilities, _definition), |
| 27 | + TextDocumentIdentifier (TextDocumentIdentifier), |
| 28 | + Uri (Uri), filePathToUri, |
| 29 | + type (|?) (..)) |
| 30 | +import Test.Tasty (TestTree, testGroup) |
| 31 | +import Test.Tasty.HUnit (assertBool, testCase, (@=?)) |
| 32 | + |
| 33 | +tests :: TestTree |
| 34 | +tests = testGroup "PluginTypes" |
| 35 | + [ combineResponsesTests ] |
| 36 | + |
| 37 | +combineResponsesTests :: TestTree |
| 38 | +combineResponsesTests = testGroup "combineResponses" |
| 39 | + [ combineResponsesTextDocumentDefinitionTests |
| 40 | + ] |
| 41 | + |
| 42 | +combineResponsesTextDocumentDefinitionTests :: TestTree |
| 43 | +combineResponsesTextDocumentDefinitionTests = testGroup "TextDocumentDefinition" |
| 44 | + [ testCase "merges all single location responses into one response with all locations and upgrades them into links (with link support)" $ do |
| 45 | + let pluginResponses :: NonEmpty (Definition |? ([DefinitionLink] |? Null)) |
| 46 | + pluginResponses = |
| 47 | + (InL . Definition . InL . Location testFileUri $ range1) :| |
| 48 | + [ InL . Definition . InL . Location testFileUri $ range2 |
| 49 | + , InL . Definition . InL . Location testFileUri $ range3 |
| 50 | + ] |
| 51 | + |
| 52 | + result = combineResponses SMethod_TextDocumentDefinition def supportsLinkInDefinitionCaps definitionParams pluginResponses |
| 53 | + |
| 54 | + expectedResult :: Definition |? ([DefinitionLink] |? Null) |
| 55 | + expectedResult = InR . InL $ |
| 56 | + [ DefinitionLink $ LocationLink Nothing testFileUri range1 range1 |
| 57 | + , DefinitionLink $ LocationLink Nothing testFileUri range2 range2 |
| 58 | + , DefinitionLink $ LocationLink Nothing testFileUri range3 range3 |
| 59 | + ] |
| 60 | + expectedResult @=? result |
| 61 | + |
| 62 | + , testCase "merges all location link responses into one with all links (with link support)" $ do |
| 63 | + let pluginResponses :: NonEmpty (Definition |? ([DefinitionLink] |? Null)) |
| 64 | + pluginResponses = |
| 65 | + (InR . InL $ [DefinitionLink $ LocationLink Nothing testFileUri range1 range1]) :| |
| 66 | + [ InR . InL $ |
| 67 | + [ DefinitionLink $ LocationLink Nothing testFileUri range2 range2 |
| 68 | + , DefinitionLink $ LocationLink Nothing testFileUri range3 range3 |
| 69 | + ] |
| 70 | + ] |
| 71 | + |
| 72 | + result = combineResponses SMethod_TextDocumentDefinition def supportsLinkInDefinitionCaps definitionParams pluginResponses |
| 73 | + |
| 74 | + expectedResult :: Definition |? ([DefinitionLink] |? Null) |
| 75 | + expectedResult = InR . InL $ |
| 76 | + [ DefinitionLink $ LocationLink Nothing testFileUri range1 range1 |
| 77 | + , DefinitionLink $ LocationLink Nothing testFileUri range2 range2 |
| 78 | + , DefinitionLink $ LocationLink Nothing testFileUri range3 range3 |
| 79 | + ] |
| 80 | + expectedResult @=? result |
| 81 | + |
| 82 | + , testCase "merges location responses with link responses into link responses (with link support)" $ do |
| 83 | + let pluginResponses :: NonEmpty (Definition |? ([DefinitionLink] |? Null)) |
| 84 | + pluginResponses = |
| 85 | + (InL . Definition . InL . Location testFileUri $ range1) :| |
| 86 | + [ InR . InL $ [ DefinitionLink $ LocationLink Nothing testFileUri range2 range2 ] |
| 87 | + , InL . Definition . InR $ [Location testFileUri range3] |
| 88 | + ] |
| 89 | + |
| 90 | + result = combineResponses SMethod_TextDocumentDefinition def supportsLinkInDefinitionCaps definitionParams pluginResponses |
| 91 | + |
| 92 | + expectedResult :: Definition |? ([DefinitionLink] |? Null) |
| 93 | + expectedResult = InR . InL $ |
| 94 | + [ DefinitionLink $ LocationLink Nothing testFileUri range1 range1 |
| 95 | + , DefinitionLink $ LocationLink Nothing testFileUri range2 range2 |
| 96 | + , DefinitionLink $ LocationLink Nothing testFileUri range3 range3 |
| 97 | + ] |
| 98 | + expectedResult @=? result |
| 99 | + |
| 100 | + , testCase "ignores Null responses when other responses are available" $ do |
| 101 | + let pluginResponses :: NonEmpty (Definition |? ([DefinitionLink] |? Null)) |
| 102 | + pluginResponses = |
| 103 | + (InL . Definition . InL . Location testFileUri $ range1) :| |
| 104 | + [ InR . InR $ Null |
| 105 | + , InL . Definition . InR $ [Location testFileUri range3] |
| 106 | + ] |
| 107 | + |
| 108 | + result = combineResponses SMethod_TextDocumentDefinition def supportsLinkInDefinitionCaps definitionParams pluginResponses |
| 109 | + |
| 110 | + expectedResult :: Definition |? ([DefinitionLink] |? Null) |
| 111 | + expectedResult = InR . InL $ |
| 112 | + [ DefinitionLink $ LocationLink Nothing testFileUri range1 range1 |
| 113 | + , DefinitionLink $ LocationLink Nothing testFileUri range3 range3 |
| 114 | + ] |
| 115 | + expectedResult @=? result |
| 116 | + |
| 117 | + , testCase "returns Null when all responses are Null" $ do |
| 118 | + let pluginResponses :: NonEmpty (Definition |? ([DefinitionLink] |? Null)) |
| 119 | + pluginResponses = |
| 120 | + (InR . InR $ Null) :| |
| 121 | + [ InR . InR $ Null |
| 122 | + , InR . InR $ Null |
| 123 | + ] |
| 124 | + |
| 125 | + result = combineResponses SMethod_TextDocumentDefinition def supportsLinkInDefinitionCaps definitionParams pluginResponses |
| 126 | + |
| 127 | + expectedResult :: Definition |? ([DefinitionLink] |? Null) |
| 128 | + expectedResult = InR . InR $ Null |
| 129 | + expectedResult @=? result |
| 130 | + ] |
| 131 | + |
| 132 | +(range1, range2, range3) = (Range (Position 3 0) $ Position 3 5, Range (Position 5 7) $ Position 5 13, Range (Position 24 30) $ Position 24 40) |
| 133 | + |
| 134 | +supportsLinkInDefinitionCaps :: ClientCapabilities |
| 135 | +supportsLinkInDefinitionCaps = def & L.textDocument ?~ textDocumentCaps |
| 136 | + where |
| 137 | + textDocumentCaps :: TextDocumentClientCapabilities |
| 138 | + textDocumentCaps = def { _definition = Just DefinitionClientCapabilities { _linkSupport = Just True, _dynamicRegistration = Nothing }} |
| 139 | + |
| 140 | +definitionParams :: DefinitionParams |
| 141 | +definitionParams = DefinitionParams |
| 142 | + { _textDocument = TextDocumentIdentifier testFileUri |
| 143 | + , _position = Position 5 4 |
| 144 | + , _workDoneToken = Nothing |
| 145 | + , _partialResultToken = Nothing |
| 146 | + } |
| 147 | + |
| 148 | +testFileUri :: Uri |
| 149 | +testFileUri = filePathToUri "file://tester/Test.hs" |
0 commit comments