golang.org/x/tools/gopls@v0.15.3/internal/protocol/json_test.go (about) 1 // Copyright 2021 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package protocol_test 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "regexp" 11 "strings" 12 "testing" 13 14 "github.com/google/go-cmp/cmp" 15 "golang.org/x/tools/gopls/internal/protocol" 16 ) 17 18 // verify that type errors in Initialize lsp messages don't cause 19 // any other unmarshalling errors. The code looks at single values and the 20 // first component of array values. Each occurrence is replaced by something 21 // of a different type, the resulting string unmarshalled, and compared to 22 // the unmarshalling of the unchanged strings. The test passes if there is no 23 // more than a single difference reported. That is, if changing a single value 24 // in the message changes no more than a single value in the unmarshalled struct, 25 // it is safe to ignore *json.UnmarshalTypeError. 26 27 // strings are changed to numbers or bools (true) 28 // bools are changed to numbers or strings 29 // numbers are changed to strings or bools 30 31 // a recent Initialize message taken from a log (at some point 32 // some field incompatibly changed from bool to int32) 33 const input = `{"processId":46408,"clientInfo":{"name":"Visual Studio Code - Insiders","version":"1.76.0-insider"},"locale":"en-us","rootPath":"/Users/pjw/hakim","rootUri":"file:///Users/pjw/hakim","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional","normalizesLineEndings":true,"changeAnnotationSupport":{"groupsOnLabel":true}},"configuration":true,"didChangeWatchedFiles":{"dynamicRegistration":true,"relativePatternSupport":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[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]},"tagSupport":{"valueSet":[1]},"resolveSupport":{"properties":["location.range"]}},"codeLens":{"refreshSupport":true},"executeCommand":{"dynamicRegistration":true},"didChangeConfiguration":{"dynamicRegistration":true},"workspaceFolders":true,"semanticTokens":{"refreshSupport":true},"fileOperations":{"dynamicRegistration":true,"didCreate":true,"didRename":true,"didDelete":true,"willCreate":true,"willRename":true,"willDelete":true},"inlineValue":{"refreshSupport":true},"inlayHint":{"refreshSupport":true},"diagnostics":{"refreshSupport":true}},"textDocument":{"publishDiagnostics":{"relatedInformation":true,"versionSupport":false,"tagSupport":{"valueSet":[1,2]},"codeDescriptionSupport":true,"dataSupport":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true,"tagSupport":{"valueSet":[1]},"insertReplaceSupport":true,"resolveSupport":{"properties":["documentation","detail","additionalTextEdits"]},"insertTextModeSupport":{"valueSet":[1,2]},"labelDetailsSupport":true},"insertTextMode":2,"completionItemKind":{"valueSet":[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]},"completionList":{"itemDefaults":["commitCharacters","editRange","insertTextFormat","insertTextMode"]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true},"activeParameterSupport":true},"contextSupport":true},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[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]},"hierarchicalDocumentSymbolSupport":true,"tagSupport":{"valueSet":[1]},"labelSupport":true},"codeAction":{"dynamicRegistration":true,"isPreferredSupport":true,"disabledSupport":true,"dataSupport":true,"resolveSupport":{"properties":["edit"]},"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}},"honorsChangeAnnotations":false},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true,"prepareSupportDefaultBehavior":1,"honorsChangeAnnotations":true},"documentLink":{"dynamicRegistration":true,"tooltipSupport":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true,"foldingRangeKind":{"valueSet":["comment","imports","region"]},"foldingRange":{"collapsedText":false}},"declaration":{"dynamicRegistration":true,"linkSupport":true},"selectionRange":{"dynamicRegistration":true},"callHierarchy":{"dynamicRegistration":true},"semanticTokens":{"dynamicRegistration":true,"tokenTypes":["namespace","type","class","enum","interface","struct","typeParameter","parameter","variable","property","enumMember","event","function","method","macro","keyword","modifier","comment","string","number","regexp","operator","decorator"],"tokenModifiers":["declaration","definition","readonly","static","deprecated","abstract","async","modification","documentation","defaultLibrary"],"formats":["relative"],"requests":{"range":true,"full":{"delta":true}},"multilineTokenSupport":false,"overlappingTokenSupport":false,"serverCancelSupport":true,"augmentsSyntaxTokens":true},"linkedEditingRange":{"dynamicRegistration":true},"typeHierarchy":{"dynamicRegistration":true},"inlineValue":{"dynamicRegistration":true},"inlayHint":{"dynamicRegistration":true,"resolveSupport":{"properties":["tooltip","textEdits","label.tooltip","label.location","label.command"]}},"diagnostic":{"dynamicRegistration":true,"relatedDocumentSupport":false}},"window":{"showMessage":{"messageActionItem":{"additionalPropertiesSupport":true}},"showDocument":{"support":true},"workDoneProgress":true},"general":{"staleRequestSupport":{"cancel":true,"retryOnContentModified":["textDocument/semanticTokens/full","textDocument/semanticTokens/range","textDocument/semanticTokens/full/delta"]},"regularExpressions":{"engine":"ECMAScript","version":"ES2020"},"markdown":{"parser":"marked","version":"1.1.0"},"positionEncodings":["utf-16"]},"notebookDocument":{"synchronization":{"dynamicRegistration":true,"executionSummarySupport":true}}},"initializationOptions":{"usePlaceholders":true,"completionDocumentation":true,"verboseOutput":false,"build.directoryFilters":["-foof","-internal/protocol/typescript"],"codelenses":{"reference":true,"gc_details":true},"analyses":{"fillstruct":true,"staticcheck":true,"unusedparams":false,"composites":false},"semanticTokens":true,"noSemanticString":true,"noSemanticNumber":true,"templateExtensions":["tmpl","gotmpl"],"ui.completion.matcher":"Fuzzy","ui.inlayhint.hints":{"assignVariableTypes":false,"compositeLiteralFields":false,"compositeLiteralTypes":false,"constantValues":false,"functionTypeParameters":false,"parameterNames":false,"rangeVariableTypes":false},"ui.vulncheck":"Off","allExperiments":true},"trace":"off","workspaceFolders":[{"uri":"file:///Users/pjw/hakim","name":"hakim"}]}` 34 35 type DiffReporter struct { 36 path cmp.Path 37 diffs []string 38 } 39 40 func (r *DiffReporter) PushStep(ps cmp.PathStep) { 41 r.path = append(r.path, ps) 42 } 43 44 func (r *DiffReporter) Report(rs cmp.Result) { 45 if !rs.Equal() { 46 vx, vy := r.path.Last().Values() 47 r.diffs = append(r.diffs, fmt.Sprintf("%#v:\n\t-: %+v\n\t+: %+v\n", r.path, vx, vy)) 48 } 49 } 50 51 func (r *DiffReporter) PopStep() { 52 r.path = r.path[:len(r.path)-1] 53 } 54 55 func (r *DiffReporter) String() string { 56 return strings.Join(r.diffs, "\n") 57 } 58 59 func TestStringChanges(t *testing.T) { 60 // string as value 61 stringLeaf := regexp.MustCompile(`:("[^"]*")`) 62 leafs := stringLeaf.FindAllStringSubmatchIndex(input, -1) 63 allDeltas(t, leafs, "23", "true") 64 // string as first element of array 65 stringArray := regexp.MustCompile(`[[]("[^"]*")`) 66 arrays := stringArray.FindAllStringSubmatchIndex(input, -1) 67 allDeltas(t, arrays, "23", "true") 68 } 69 70 func TestBoolChanges(t *testing.T) { 71 boolLeaf := regexp.MustCompile(`:(true|false)(,|})`) 72 leafs := boolLeaf.FindAllStringSubmatchIndex(input, -1) 73 allDeltas(t, leafs, "23", `"xx"`) 74 boolArray := regexp.MustCompile(`:[[](true|false)(,|])`) 75 arrays := boolArray.FindAllStringSubmatchIndex(input, -1) 76 allDeltas(t, arrays, "23", `"xx"`) 77 } 78 79 func TestNumberChanges(t *testing.T) { 80 numLeaf := regexp.MustCompile(`:(\d+)(,|})`) 81 leafs := numLeaf.FindAllStringSubmatchIndex(input, -1) 82 allDeltas(t, leafs, "true", `"xx"`) 83 numArray := regexp.MustCompile(`:[[](\d+)(,|])`) 84 arrays := numArray.FindAllStringSubmatchIndex(input, -1) 85 allDeltas(t, arrays, "true", `"xx"`) 86 } 87 88 // v is a set of matches. check that substituting any repl never 89 // creates more than 1 unmarshaling error 90 func allDeltas(t *testing.T, v [][]int, repls ...string) { 91 t.Helper() 92 for _, repl := range repls { 93 for i, x := range v { 94 err := tryChange(x[2], x[3], repl) 95 if err != nil { 96 t.Errorf("%d:%q %v", i, input[x[2]:x[3]], err) 97 } 98 } 99 } 100 } 101 102 func tryChange(start, end int, repl string) error { 103 var p, q protocol.ParamInitialize 104 mod := input[:start] + repl + input[end:] 105 excerpt := func() (string, string) { 106 a := start - 5 107 if a < 0 { 108 a = 0 109 } 110 b := end + 5 111 if b > len(input) { 112 // trusting repl to be no longer than what it replaces 113 b = len(input) 114 } 115 ma := input[a:b] 116 mb := mod[a:b] 117 return ma, mb 118 } 119 120 if err := json.Unmarshal([]byte(input), &p); err != nil { 121 return fmt.Errorf("%s %v", repl, err) 122 } 123 switch err := json.Unmarshal([]byte(mod), &q).(type) { 124 case nil: //ok 125 case *json.UnmarshalTypeError: 126 break 127 case *protocol.UnmarshalError: 128 return nil // cmp.Diff produces several diffs for custom unmrshalers 129 default: 130 return fmt.Errorf("%T unexpected unmarshal error", err) 131 } 132 133 var r DiffReporter 134 cmp.Diff(p, q, cmp.Reporter(&r)) 135 if len(r.diffs) > 1 { // 0 is possible, e.g., for interface{} 136 ma, mb := excerpt() 137 return fmt.Errorf("got %d diffs for %q\n%s\n%s", len(r.diffs), repl, ma, mb) 138 } 139 return nil 140 }