github.com/getgauge/gauge@v1.6.9/api/lang/codeLens_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  	"reflect"
    14  
    15  	"github.com/sourcegraph/go-langserver/pkg/lsp"
    16  	"github.com/sourcegraph/jsonrpc2"
    17  )
    18  
    19  func TestGetCodeLens(t *testing.T) {
    20  	specText := `Specification Heading
    21  =====================
    22  
    23  Scenario Heading
    24  ----------------
    25  
    26  * Step text`
    27  
    28  	lRunner.lspID = "python"
    29  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
    30  	openFilesCache.add("foo.spec", specText)
    31  
    32  	b, _ := json.Marshal(lsp.CodeLensParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}})
    33  	p := json.RawMessage(b)
    34  
    35  	got, err := codeLenses(&jsonrpc2.Request{Params: &p})
    36  	if err != nil {
    37  		t.Errorf("Expected error to be nil. got : %s", err.Error())
    38  	}
    39  
    40  	specCodeLens := lsp.CodeLens{
    41  		Command: lsp.Command{
    42  			Command:   "gauge.execute",
    43  			Title:     "Run Spec",
    44  			Arguments: getExecutionArgs("foo.spec"),
    45  		},
    46  		Range: lsp.Range{
    47  			Start: lsp.Position{Line: 0, Character: 0},
    48  			End:   lsp.Position{Line: 0, Character: 8},
    49  		},
    50  	}
    51  
    52  	specDebugCodeLens := lsp.CodeLens{
    53  		Command: lsp.Command{
    54  			Command:   "gauge.debug",
    55  			Title:     "Debug Spec",
    56  			Arguments: getExecutionArgs("foo.spec"),
    57  		},
    58  		Range: lsp.Range{
    59  			Start: lsp.Position{Line: 0, Character: 0},
    60  			End:   lsp.Position{Line: 0, Character: 10},
    61  		},
    62  	}
    63  
    64  	scenCodeLens := lsp.CodeLens{
    65  		Command: lsp.Command{
    66  			Command:   "gauge.execute",
    67  			Title:     "Run Scenario",
    68  			Arguments: getExecutionArgs("foo.spec:4"),
    69  		},
    70  		Range: lsp.Range{
    71  			Start: lsp.Position{Line: 3, Character: 0},
    72  			End:   lsp.Position{Line: 3, Character: 12},
    73  		},
    74  	}
    75  
    76  	scenDebugCodeLens := lsp.CodeLens{
    77  		Command: lsp.Command{
    78  			Command:   "gauge.debug",
    79  			Title:     "Debug Scenario",
    80  			Arguments: getExecutionArgs("foo.spec:4"),
    81  		},
    82  		Range: lsp.Range{
    83  			Start: lsp.Position{Line: 3, Character: 0},
    84  			End:   lsp.Position{Line: 3, Character: 14},
    85  		},
    86  	}
    87  
    88  	want := []lsp.CodeLens{scenCodeLens, scenDebugCodeLens, specCodeLens, specDebugCodeLens}
    89  
    90  	if !reflect.DeepEqual(got, want) {
    91  		t.Errorf("want: `%v`,\n got: `%v`", want, got)
    92  	}
    93  }
    94  
    95  func TestGetCodeLensWithMultipleScenario(t *testing.T) {
    96  	specText := `Specification Heading
    97  =====================
    98  
    99  Scenario Heading
   100  ----------------
   101  
   102  * Step text
   103  
   104  Another Scenario
   105  ----------------
   106  
   107  * another step
   108  `
   109  
   110  	lRunner.lspID = "python"
   111  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
   112  	openFilesCache.add("foo.spec", specText)
   113  
   114  	b, _ := json.Marshal(lsp.CodeLensParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}})
   115  	p := json.RawMessage(b)
   116  	got, err := codeLenses(&jsonrpc2.Request{Params: &p})
   117  	if err != nil {
   118  		t.Errorf("Expected error to be nil. got : %s", err.Error())
   119  	}
   120  
   121  	specCodeLens := lsp.CodeLens{
   122  		Command: lsp.Command{
   123  			Command:   "gauge.execute",
   124  			Title:     "Run Spec",
   125  			Arguments: getExecutionArgs("foo.spec"),
   126  		},
   127  		Range: lsp.Range{
   128  			Start: lsp.Position{Line: 0, Character: 0},
   129  			End:   lsp.Position{Line: 0, Character: 8},
   130  		},
   131  	}
   132  
   133  	specDebugCodeLens := lsp.CodeLens{
   134  		Command: lsp.Command{
   135  			Command:   "gauge.debug",
   136  			Title:     "Debug Spec",
   137  			Arguments: getExecutionArgs("foo.spec"),
   138  		},
   139  		Range: lsp.Range{
   140  			Start: lsp.Position{Line: 0, Character: 0},
   141  			End:   lsp.Position{Line: 0, Character: 10},
   142  		},
   143  	}
   144  
   145  	scenCodeLens1 := lsp.CodeLens{
   146  		Command: lsp.Command{
   147  			Command:   "gauge.execute",
   148  			Title:     "Run Scenario",
   149  			Arguments: getExecutionArgs("foo.spec:4"),
   150  		},
   151  		Range: lsp.Range{
   152  			Start: lsp.Position{Line: 3, Character: 0},
   153  			End:   lsp.Position{Line: 3, Character: 12},
   154  		},
   155  	}
   156  
   157  	scenDebugCodeLens1 := lsp.CodeLens{
   158  		Command: lsp.Command{
   159  			Command:   "gauge.debug",
   160  			Title:     "Debug Scenario",
   161  			Arguments: getExecutionArgs("foo.spec:4"),
   162  		},
   163  		Range: lsp.Range{
   164  			Start: lsp.Position{Line: 3, Character: 0},
   165  			End:   lsp.Position{Line: 3, Character: 14},
   166  		},
   167  	}
   168  
   169  	scenCodeLens2 := lsp.CodeLens{
   170  		Command: lsp.Command{
   171  			Command:   "gauge.execute",
   172  			Title:     "Run Scenario",
   173  			Arguments: getExecutionArgs("foo.spec:9"),
   174  		},
   175  		Range: lsp.Range{
   176  			Start: lsp.Position{Line: 8, Character: 0},
   177  			End:   lsp.Position{Line: 8, Character: 12},
   178  		},
   179  	}
   180  
   181  	scenDebugCodeLens2 := lsp.CodeLens{
   182  		Command: lsp.Command{
   183  			Command:   "gauge.debug",
   184  			Title:     "Debug Scenario",
   185  			Arguments: getExecutionArgs("foo.spec:9"),
   186  		},
   187  		Range: lsp.Range{
   188  			Start: lsp.Position{Line: 8, Character: 0},
   189  			End:   lsp.Position{Line: 8, Character: 14},
   190  		},
   191  	}
   192  
   193  	want := []lsp.CodeLens{scenCodeLens1, scenDebugCodeLens1, scenCodeLens2, scenDebugCodeLens2, specCodeLens, specDebugCodeLens}
   194  
   195  	if !reflect.DeepEqual(got, want) {
   196  		t.Errorf("want: `%v`,\n got: `%v`", want, got)
   197  	}
   198  }
   199  
   200  func TestGetCodeLensWithDataTable(t *testing.T) {
   201  	specText := `Specification Heading
   202  =====================
   203  
   204  	|Word  |Vowel Count|
   205  	|------|-----------|
   206  	|Mingle|2          |
   207  	|Snap  |1          |
   208  	|GoCD  |1          |
   209  	|Rhythm|0          |
   210  
   211  
   212  Scenario Heading
   213  ----------------
   214  
   215  * The word <Word> has <Vowel Count> vowels.
   216  
   217  `
   218  
   219  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
   220  	openFilesCache.add("foo.spec", specText)
   221  
   222  	b, _ := json.Marshal(lsp.CodeLensParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}})
   223  	p := json.RawMessage(b)
   224  
   225  	got, err := codeLenses(&jsonrpc2.Request{Params: &p})
   226  	if err != nil {
   227  		t.Errorf("Expected error to be nil. got : %s", err.Error())
   228  	}
   229  
   230  	specCodeLens := lsp.CodeLens{
   231  		Command: lsp.Command{
   232  			Command:   "gauge.execute",
   233  			Title:     "Run Spec",
   234  			Arguments: getExecutionArgs("foo.spec"),
   235  		},
   236  		Range: lsp.Range{
   237  			Start: lsp.Position{Line: 0, Character: 0},
   238  			End:   lsp.Position{Line: 0, Character: 8},
   239  		},
   240  	}
   241  
   242  	specDebugCodeLens := lsp.CodeLens{
   243  		Command: lsp.Command{
   244  			Command:   "gauge.debug",
   245  			Title:     "Debug Spec",
   246  			Arguments: getExecutionArgs("foo.spec"),
   247  		},
   248  		Range: lsp.Range{
   249  			Start: lsp.Position{Line: 0, Character: 0},
   250  			End:   lsp.Position{Line: 0, Character: 10},
   251  		},
   252  	}
   253  
   254  	specCodeLens2 := lsp.CodeLens{
   255  		Command: lsp.Command{
   256  			Command:   "gauge.execute.inParallel",
   257  			Title:     "Run in parallel",
   258  			Arguments: getExecutionArgs("foo.spec"),
   259  		},
   260  		Range: lsp.Range{
   261  			Start: lsp.Position{Line: 0, Character: 0},
   262  			End:   lsp.Position{Line: 0, Character: 15},
   263  		},
   264  	}
   265  
   266  	scenCodeLens2 := lsp.CodeLens{
   267  		Command: lsp.Command{
   268  			Command:   "gauge.execute",
   269  			Title:     "Run Scenario",
   270  			Arguments: getExecutionArgs("foo.spec:12"),
   271  		},
   272  		Range: lsp.Range{
   273  			Start: lsp.Position{Line: 11, Character: 0},
   274  			End:   lsp.Position{Line: 11, Character: 12},
   275  		},
   276  	}
   277  
   278  	scenDebugCodeLens2 := lsp.CodeLens{
   279  		Command: lsp.Command{
   280  			Command:   "gauge.debug",
   281  			Title:     "Debug Scenario",
   282  			Arguments: getExecutionArgs("foo.spec:12"),
   283  		},
   284  		Range: lsp.Range{
   285  			Start: lsp.Position{Line: 11, Character: 0},
   286  			End:   lsp.Position{Line: 11, Character: 14},
   287  		},
   288  	}
   289  
   290  	want := []lsp.CodeLens{scenCodeLens2, scenDebugCodeLens2, specCodeLens, specDebugCodeLens, specCodeLens2}
   291  
   292  	if !reflect.DeepEqual(got, want) {
   293  		t.Errorf("want: `%v`,\n got: `%v`", want, got)
   294  	}
   295  }
   296  
   297  func TestGetDebugCodeLensForNonLspRunner(t *testing.T) {
   298  	specText := `Specification Heading
   299  =====================
   300  
   301  Scenario Heading
   302  ----------------
   303  
   304  * Step text`
   305  
   306  	lRunner.lspID = ""
   307  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
   308  	openFilesCache.add("foo.spec", specText)
   309  
   310  	b, _ := json.Marshal(lsp.CodeLensParams{TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}})
   311  	p := json.RawMessage(b)
   312  
   313  	got, err := codeLenses(&jsonrpc2.Request{Params: &p})
   314  	if err != nil {
   315  		t.Errorf("Expected error to be nil. got : %s", err.Error())
   316  	}
   317  
   318  	specCodeLens := lsp.CodeLens{
   319  		Command: lsp.Command{
   320  			Command:   "gauge.execute",
   321  			Title:     "Run Spec",
   322  			Arguments: getExecutionArgs("foo.spec"),
   323  		},
   324  		Range: lsp.Range{
   325  			Start: lsp.Position{Line: 0, Character: 0},
   326  			End:   lsp.Position{Line: 0, Character: 8},
   327  		},
   328  	}
   329  
   330  	scenCodeLens := lsp.CodeLens{
   331  		Command: lsp.Command{
   332  			Command:   "gauge.execute",
   333  			Title:     "Run Scenario",
   334  			Arguments: getExecutionArgs("foo.spec:4"),
   335  		},
   336  		Range: lsp.Range{
   337  			Start: lsp.Position{Line: 3, Character: 0},
   338  			End:   lsp.Position{Line: 3, Character: 12},
   339  		},
   340  	}
   341  
   342  	want := []lsp.CodeLens{scenCodeLens, specCodeLens}
   343  
   344  	if !reflect.DeepEqual(got, want) {
   345  		t.Errorf("want: `%v`,\n got: `%v`", want, got)
   346  	}
   347  }