github.com/getgauge/gauge@v1.6.9/api/lang/customResponses_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 "encoding/json" 11 "testing" 12 13 "github.com/getgauge/gauge/api/infoGatherer" 14 "github.com/getgauge/gauge/gauge" 15 16 "reflect" 17 18 "github.com/sourcegraph/go-langserver/pkg/lsp" 19 "github.com/sourcegraph/jsonrpc2" 20 ) 21 22 func TestGetScenariosShouldGiveTheScenarioAtCurrentCursorPosition(t *testing.T) { 23 provider = &dummyInfoProvider{} 24 specText := `Specification Heading 25 ===================== 26 27 Scenario Heading 28 ---------------- 29 30 * Step text 31 32 Scenario Heading2 33 ----------------- 34 35 * Step text` 36 37 uri := lsp.DocumentURI("foo.spec") 38 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 39 openFilesCache.add(uri, specText) 40 41 position := lsp.Position{Line: 5, Character: 1} 42 b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}, Position: position}) 43 p := json.RawMessage(b) 44 45 got, err := scenarios(&jsonrpc2.Request{Params: &p}) 46 47 if err != nil { 48 t.Errorf("expected errror to be nil. Got: \n%v", err.Error()) 49 } 50 51 info := got.(ScenarioInfo) 52 53 want := ScenarioInfo{ 54 Heading: "Scenario Heading", 55 LineNo: 4, 56 ExecutionIdentifier: "foo.spec:4", 57 } 58 if !reflect.DeepEqual(info, want) { 59 t.Errorf("expected %v to be equal %v", info, want) 60 } 61 openFilesCache.remove(uri) 62 } 63 64 func TestGetScenariosShouldGiveTheScenariosIfCursorPositionIsNotInSpan(t *testing.T) { 65 specText := `Specification Heading 66 ===================== 67 68 Scenario Heading 69 ---------------- 70 71 * Step text 72 73 Scenario Heading2 74 ----------------- 75 76 * Step text 77 ` 78 79 uri := lsp.DocumentURI("foo.spec") 80 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 81 openFilesCache.add(uri, specText) 82 83 position := lsp.Position{Line: 2, Character: 1} 84 b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}, Position: position}) 85 p := json.RawMessage(b) 86 87 got, err := scenarios(&jsonrpc2.Request{Params: &p}) 88 89 if err != nil { 90 t.Errorf("expected errror to be nil. Got: \n%v", err.Error()) 91 } 92 93 info := got.([]ScenarioInfo) 94 95 want := []ScenarioInfo{ 96 { 97 Heading: "Scenario Heading", 98 LineNo: 4, 99 ExecutionIdentifier: "foo.spec:4", 100 }, 101 { 102 Heading: "Scenario Heading2", 103 LineNo: 9, 104 ExecutionIdentifier: "foo.spec:9", 105 }, 106 } 107 if !reflect.DeepEqual(info, want) { 108 t.Errorf("expected %v to be equal %v", info, want) 109 } 110 openFilesCache.remove(uri) 111 } 112 113 func TestGetScenariosShouldGiveTheScenariosIfDocumentIsNotOpened(t *testing.T) { 114 provider = &dummyInfoProvider{ 115 specsFunc: func(specs []string) []*infoGatherer.SpecDetail { 116 return []*infoGatherer.SpecDetail{ 117 &infoGatherer.SpecDetail{ 118 Spec: &gauge.Specification{ 119 Heading: &gauge.Heading{Value: "Specification 1"}, 120 FileName: "foo.spec", 121 Scenarios: []*gauge.Scenario{ 122 &gauge.Scenario{Heading: &gauge.Heading{Value: "Scenario 1", LineNo: 4}, Span: &gauge.Span{Start: 4, End: 7}}, 123 &gauge.Scenario{Heading: &gauge.Heading{Value: "Scenario 2", LineNo: 9}, Span: &gauge.Span{Start: 9, End: 12}}, 124 }, 125 }, 126 }, 127 } 128 }, 129 } 130 131 position := lsp.Position{Line: 2, Character: 1} 132 b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, Position: position}) 133 p := json.RawMessage(b) 134 135 got, err := scenarios(&jsonrpc2.Request{Params: &p}) 136 137 if err != nil { 138 t.Errorf("expected error to be nil. Got: \n%v", err.Error()) 139 } 140 141 info := got.([]ScenarioInfo) 142 143 want := []ScenarioInfo{ 144 { 145 Heading: "Scenario 1", 146 LineNo: 4, 147 ExecutionIdentifier: "foo.spec:4", 148 }, 149 { 150 Heading: "Scenario 2", 151 LineNo: 9, 152 ExecutionIdentifier: "foo.spec:9", 153 }, 154 } 155 if !reflect.DeepEqual(info, want) { 156 t.Errorf("expected %v to be equal %v", info, want) 157 } 158 } 159 160 func TestGetSpecsShouldReturnAllSpecsInDirectory(t *testing.T) { 161 provider = &dummyInfoProvider{ 162 specsFunc: func(specs []string) []*infoGatherer.SpecDetail { 163 return []*infoGatherer.SpecDetail{ 164 &infoGatherer.SpecDetail{ 165 Spec: &gauge.Specification{ 166 Heading: &gauge.Heading{Value: "Specification 1"}, 167 FileName: "foo1.spec", 168 }, 169 }, 170 &infoGatherer.SpecDetail{ 171 Spec: &gauge.Specification{ 172 Heading: &gauge.Heading{Value: "Specification 2"}, 173 FileName: "foo2.spec", 174 }, 175 }, 176 } 177 }, 178 } 179 180 want := []specInfo{ 181 { 182 Heading: "Specification 1", 183 ExecutionIdentifier: "foo1.spec", 184 }, 185 { 186 Heading: "Specification 2", 187 ExecutionIdentifier: "foo2.spec", 188 }, 189 } 190 got, err := specs() 191 192 if err != nil { 193 t.Errorf("expected error to be nil. Got: \n%v", err.Error()) 194 } 195 196 info := got.([]specInfo) 197 198 if !reflect.DeepEqual(info, want) { 199 t.Errorf("expected %v to be equal %v", info, want) 200 } 201 }