github.com/getgauge/gauge@v1.6.9/api/lang/format_test.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package lang 8 9 import ( 10 "testing" 11 12 "encoding/json" 13 "fmt" 14 "reflect" 15 16 "github.com/sourcegraph/go-langserver/pkg/lsp" 17 "github.com/sourcegraph/jsonrpc2" 18 ) 19 20 func TestFormat(t *testing.T) { 21 specText := `# Specification Heading 22 23 ## Scenario Heading 24 25 * Step text` 26 27 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 28 openFilesCache.add("foo.spec", specText) 29 30 want := []lsp.TextEdit{ 31 { 32 Range: lsp.Range{ 33 Start: lsp.Position{Line: 0, Character: 0}, 34 End: lsp.Position{Line: 5, Character: 57}, 35 }, 36 NewText: specText + "\n", 37 }, 38 } 39 40 b, _ := json.Marshal(lsp.DocumentFormattingParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, Options: lsp.FormattingOptions{}}) 41 p := json.RawMessage(b) 42 43 got, err := format(&jsonrpc2.Request{Params: &p}) 44 if err != nil { 45 t.Fatalf("Expected error == nil in format, got %s", err.Error()) 46 } 47 48 if !reflect.DeepEqual(got, want) { 49 t.Errorf("format failed, want: `%v`, got: `%v`", want, got) 50 } 51 } 52 53 func TestFormatParseError(t *testing.T) { 54 specText := `Specification Heading 55 ===================== 56 57 # Scenario Heading 58 59 60 * Step text` 61 62 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 63 openFilesCache.add("foo.spec", specText) 64 65 specFile := lsp.DocumentURI("foo.spec") 66 67 b, _ := json.Marshal(lsp.DocumentFormattingParams{TextDocument: lsp.TextDocumentIdentifier{URI: specFile}, Options: lsp.FormattingOptions{}}) 68 p := json.RawMessage(b) 69 70 expectedError := fmt.Errorf("failed to format document. Fix all the problems first") 71 72 data, err := format(&jsonrpc2.Request{Params: &p}) 73 if data != nil { 74 t.Fatalf("Expected data == nil in format, got %s", data) 75 } 76 if err.Error() != expectedError.Error() { 77 t.Fatalf(" want : %s\ngot : %s", expectedError.Error(), err.Error()) 78 } 79 80 }