github.com/getgauge/gauge@v1.6.9/api/lang/completionStep_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 "fmt" 11 "testing" 12 "time" 13 14 "github.com/getgauge/gauge-proto/go/gauge_messages" 15 "github.com/getgauge/gauge/gauge" 16 "github.com/getgauge/gauge/runner" 17 "github.com/sourcegraph/go-langserver/pkg/lsp" 18 ) 19 20 func TestGetPrefix(t *testing.T) { 21 want := " " 22 got := getPrefix("line1\n*") 23 24 if got != want { 25 t.Errorf("GetPrefix failed for autocomplete, want: `%s`, got: `%s`", want, got) 26 } 27 } 28 29 func TestGetPrefixWithSpace(t *testing.T) { 30 want := "" 31 got := getPrefix("* ") 32 33 if got != want { 34 t.Errorf("GetPrefix failed for autocomplete, want: `%s`, got: `%s`", want, got) 35 } 36 } 37 38 func TestGetFilterTextWithStaticParam(t *testing.T) { 39 got := getStepFilterText("Text with {}", []string{"param1"}, []gauge.StepArg{{Name: "Args1", Value: "Args1", ArgType: gauge.Static}}) 40 want := `Text with "Args1"` 41 if got != want { 42 t.Errorf("Parameters not replaced properly; got : %+v, want : %+v", got, want) 43 } 44 } 45 46 func TestGetFilterTextWithDynamicParam(t *testing.T) { 47 got := getStepFilterText("Text with {}", []string{"param1"}, []gauge.StepArg{{Name: "Args1", Value: "Args1", ArgType: gauge.Dynamic}}) 48 want := `Text with <Args1>` 49 if got != want { 50 t.Errorf("Parameters not replaced properly; got : %+v, want : %+v", got, want) 51 } 52 } 53 54 func TestGetFilterTextShouldNotReplaceIfNoStepArgsGiven(t *testing.T) { 55 got := getStepFilterText("Text with {}", []string{"param1"}, []gauge.StepArg{}) 56 want := `Text with <param1>` 57 if got != want { 58 t.Errorf("Parameters not replaced properly; got : %+v, want : %+v", got, want) 59 } 60 } 61 62 func TestGetFilterTextWithLesserNumberOfStepArgsGiven(t *testing.T) { 63 stepArgs := []gauge.StepArg{ 64 {Name: "Args1", Value: "Args1", ArgType: gauge.Dynamic}, 65 {Name: "Args2", Value: "Args2", ArgType: gauge.Static}, 66 } 67 got := getStepFilterText("Text with {} {} and {}", []string{"param1", "param2", "param3"}, stepArgs) 68 want := `Text with <Args1> "Args2" and <param3>` 69 if got != want { 70 t.Errorf("Parameters not replaced properly; got : %+v, want : %+v", got, want) 71 } 72 } 73 74 var testEditPosition = []struct { 75 input string 76 cursorPos lsp.Position 77 wantStart lsp.Position 78 wantEnd lsp.Position 79 }{ 80 { 81 input: "*", 82 cursorPos: lsp.Position{Line: 0, Character: len(`*`)}, 83 wantStart: lsp.Position{Line: 0, Character: len(`*`)}, 84 wantEnd: lsp.Position{Line: 0, Character: len(`*`)}, 85 }, 86 { 87 input: "* ", 88 cursorPos: lsp.Position{Line: 0, Character: len(`*`)}, 89 wantStart: lsp.Position{Line: 0, Character: len(`*`)}, 90 wantEnd: lsp.Position{Line: 0, Character: len(`* `)}, 91 }, 92 { 93 input: "* Step", 94 cursorPos: lsp.Position{Line: 10, Character: len(`*`)}, 95 wantStart: lsp.Position{Line: 10, Character: len(`*`)}, 96 wantEnd: lsp.Position{Line: 10, Character: len(`* Step`)}, 97 }, 98 { 99 input: "* Step", 100 cursorPos: lsp.Position{Line: 0, Character: len(`* `)}, 101 wantStart: lsp.Position{Line: 0, Character: len(`* `)}, 102 wantEnd: lsp.Position{Line: 0, Character: len(`* Step`)}, 103 }, 104 { 105 input: "* Step", 106 cursorPos: lsp.Position{Line: 0, Character: len(`* St`)}, 107 wantStart: lsp.Position{Line: 0, Character: len(`* `)}, 108 wantEnd: lsp.Position{Line: 0, Character: len(`* Step`)}, 109 }, 110 { 111 input: " * Step", 112 cursorPos: lsp.Position{Line: 0, Character: len(` * S`)}, 113 wantStart: lsp.Position{Line: 0, Character: len(` * `)}, 114 wantEnd: lsp.Position{Line: 0, Character: len(` * Step`)}, 115 }, 116 { 117 input: " * Step ", 118 cursorPos: lsp.Position{Line: 0, Character: len(` * Step `) + 2}, 119 wantStart: lsp.Position{Line: 0, Character: len(` * `)}, 120 wantEnd: lsp.Position{Line: 0, Character: len(` * Step `) + 2}, 121 }, 122 } 123 124 func TestGetEditPosition(t *testing.T) { 125 for _, test := range testEditPosition { 126 gotRange := getStepEditRange(test.input, test.cursorPos) 127 if gotRange.Start.Line != test.wantStart.Line || gotRange.Start.Character != test.wantStart.Character { 128 t.Errorf(`Incorrect Edit Start Position got: %+v , want : %+v, input : "%s"`, gotRange.Start, test.wantStart, test.input) 129 } 130 if gotRange.End.Line != test.wantEnd.Line || gotRange.End.Character != test.wantEnd.Character { 131 t.Errorf(`Incorrect Edit End Position got: %+v , want : %+v, input : "%s"`, gotRange.End, test.wantEnd, test.input) 132 } 133 } 134 } 135 136 func TestGetAllImplementedStepValues(t *testing.T) { 137 stepValues := []gauge.StepValue{ 138 { 139 StepValue: "hello world", 140 Args: []string{}, 141 ParameterizedStepValue: "hello world", 142 }, 143 { 144 StepValue: "hello {}", 145 Args: []string{"world"}, 146 ParameterizedStepValue: "hello <world>", 147 }, 148 } 149 responses := map[gauge_messages.Message_MessageType]interface{}{} 150 responses[gauge_messages.Message_StepNamesResponse] = &gauge_messages.StepNamesResponse{ 151 Steps: []string{ 152 "hello world", 153 "hello <world>", 154 }, 155 } 156 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 157 158 got, err := allImplementedStepValues() 159 160 if err != nil { 161 t.Errorf("expected getAllImplementedStepValues() to not have errors, got %v", err) 162 } 163 for _, sv := range stepValues { 164 if !contains(got, sv) { 165 t.Errorf("expected getAllImplementedStepValues() to contain %v.\ngetAllImplementedStepValues() == %v", sv, got) 166 } 167 } 168 } 169 170 func TestGetAllImplementedStepValuesShouldGivesEmptyIfRunnerRespondWithError(t *testing.T) { 171 responses := map[gauge_messages.Message_MessageType]interface{}{} 172 responses[gauge_messages.Message_StepNamesResponse] = &gauge_messages.StepNamesResponse{} 173 lRunner.runner = &runner.GrpcRunner{Timeout: time.Second * 30, LegacyClient: &mockClient{responses: responses, err: fmt.Errorf("can't get steps")}} 174 got, err := allImplementedStepValues() 175 176 if err == nil { 177 t.Error("expected getAllImplementedStepValues() to have errors, got nil") 178 } 179 if len(got) > 0 { 180 t.Errorf("expected 0 values. got %v", len(got)) 181 } 182 } 183 184 func TestRemoveDuplicateStepValues(t *testing.T) { 185 stepValues := []gauge.StepValue{ 186 { 187 Args: []string{}, 188 ParameterizedStepValue: "hello world", 189 StepValue: "hello world", 190 }, { 191 Args: []string{"world"}, 192 ParameterizedStepValue: "hello <world>", 193 StepValue: "hello {}", 194 }, 195 { 196 Args: []string{"gauge"}, 197 ParameterizedStepValue: "hello <gauge>", 198 StepValue: "hello {}", 199 }, 200 } 201 202 result := removeDuplicates(stepValues) 203 204 if len(result) != 2 { 205 t.Errorf("exppected 2 steps got %v", len(result)) 206 } 207 } 208 209 func contains(list []gauge.StepValue, v gauge.StepValue) bool { 210 for _, e := range list { 211 if e.ParameterizedStepValue == v.ParameterizedStepValue && e.StepValue == v.StepValue && len(e.Args) == len(v.Args) { 212 return true 213 } 214 } 215 return false 216 }