github.com/getgauge/gauge@v1.6.9/api/lang/symbols_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  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/getgauge/gauge/api/infoGatherer"
    15  	"github.com/getgauge/gauge/gauge"
    16  	"github.com/getgauge/gauge/util"
    17  	"github.com/sourcegraph/go-langserver/pkg/lsp"
    18  	"github.com/sourcegraph/jsonrpc2"
    19  )
    20  
    21  func TestWorkspaceSymbolsGetsFromAllSpecs(t *testing.T) {
    22  	provider = &dummyInfoProvider{
    23  		specsFunc: func(specs []string) []*infoGatherer.SpecDetail {
    24  			return []*infoGatherer.SpecDetail{
    25  				&infoGatherer.SpecDetail{
    26  					Spec: &gauge.Specification{
    27  						Heading:  &gauge.Heading{Value: "Specification 1", LineNo: 1},
    28  						FileName: "foo1.spec",
    29  					},
    30  				},
    31  				&infoGatherer.SpecDetail{
    32  					Spec: &gauge.Specification{
    33  						Heading:  &gauge.Heading{Value: "Specification 2", LineNo: 2},
    34  						FileName: "foo2.spec",
    35  					},
    36  				},
    37  			}
    38  		},
    39  	}
    40  
    41  	want := []string{
    42  		"# Specification 1",
    43  		"# Specification 2",
    44  	}
    45  
    46  	b, _ := json.Marshal(lsp.WorkspaceSymbolParams{Limit: 5, Query: "Spec"})
    47  	p := json.RawMessage(b)
    48  	got, err := workspaceSymbols(&jsonrpc2.Request{Params: &p})
    49  
    50  	if err != nil {
    51  		t.Errorf("expected error to be nil. Got: \n%v", err.Error())
    52  	}
    53  
    54  	info := mapName(got.([]*lsp.SymbolInformation))
    55  
    56  	if !reflect.DeepEqual(info, want) {
    57  		t.Errorf("expected %v to be equal %v", info, want)
    58  	}
    59  }
    60  
    61  func TestWorkspaceSymbolsGetsFromScenarios(t *testing.T) {
    62  	provider = &dummyInfoProvider{
    63  		specsFunc: func(specs []string) []*infoGatherer.SpecDetail {
    64  			return []*infoGatherer.SpecDetail{
    65  				&infoGatherer.SpecDetail{
    66  					Spec: &gauge.Specification{
    67  						Heading:  &gauge.Heading{Value: "Sample 1", LineNo: 1},
    68  						FileName: "foo1.spec",
    69  						Scenarios: []*gauge.Scenario{
    70  							{
    71  								Heading: &gauge.Heading{Value: "Sample Scenario 1", LineNo: 10},
    72  							},
    73  							{
    74  								Heading: &gauge.Heading{Value: "Sample Scenario 2", LineNo: 20},
    75  							},
    76  							{
    77  								Heading: &gauge.Heading{Value: "Random Scenario 1", LineNo: 30},
    78  							},
    79  						},
    80  					},
    81  				},
    82  				&infoGatherer.SpecDetail{
    83  					Spec: &gauge.Specification{
    84  						Heading:  &gauge.Heading{Value: "Sample 2", LineNo: 2},
    85  						FileName: "foo2.spec",
    86  						Scenarios: []*gauge.Scenario{
    87  							{
    88  								Heading: &gauge.Heading{Value: "Sample Scenario 5", LineNo: 10},
    89  							},
    90  							{
    91  								Heading: &gauge.Heading{Value: "Sample Scenario 6", LineNo: 20},
    92  							},
    93  							{
    94  								Heading: &gauge.Heading{Value: "Random Scenario 9", LineNo: 30},
    95  							},
    96  						},
    97  					},
    98  				},
    99  			}
   100  		},
   101  	}
   102  
   103  	want := []string{
   104  		"# Sample 1",
   105  		"# Sample 2",
   106  		"## Sample Scenario 1",
   107  		"## Sample Scenario 2",
   108  		"## Sample Scenario 5",
   109  		"## Sample Scenario 6",
   110  	}
   111  
   112  	b, _ := json.Marshal(lsp.WorkspaceSymbolParams{Limit: 5, Query: "Sample"})
   113  	p := json.RawMessage(b)
   114  	got, err := workspaceSymbols(&jsonrpc2.Request{Params: &p})
   115  
   116  	if err != nil {
   117  		t.Errorf("expected error to be nil. Got: \n%v", err.Error())
   118  	}
   119  
   120  	info := mapName(got.([]*lsp.SymbolInformation))
   121  
   122  	if !reflect.DeepEqual(info, want) {
   123  		t.Errorf("expected %v to be equal %v", info, want)
   124  	}
   125  }
   126  
   127  func TestWorkspaceSymbolsEmptyWhenLessThanTwoCharsGiven(t *testing.T) {
   128  	provider = &dummyInfoProvider{
   129  		specsFunc: func(specs []string) []*infoGatherer.SpecDetail {
   130  			return []*infoGatherer.SpecDetail{
   131  				&infoGatherer.SpecDetail{
   132  					Spec: &gauge.Specification{
   133  						Heading:  &gauge.Heading{Value: "Sample 1", LineNo: 1},
   134  						FileName: "foo1.spec",
   135  						Scenarios: []*gauge.Scenario{
   136  							{
   137  								Heading: &gauge.Heading{Value: "Sample Scenario 1", LineNo: 10},
   138  							},
   139  							{
   140  								Heading: &gauge.Heading{Value: "Sample Scenario 2", LineNo: 20},
   141  							},
   142  							{
   143  								Heading: &gauge.Heading{Value: "Random Scenario 1", LineNo: 30},
   144  							},
   145  						},
   146  					},
   147  				},
   148  				&infoGatherer.SpecDetail{
   149  					Spec: &gauge.Specification{
   150  						Heading:  &gauge.Heading{Value: "Sample 2", LineNo: 2},
   151  						FileName: "foo2.spec",
   152  						Scenarios: []*gauge.Scenario{
   153  							{
   154  								Heading: &gauge.Heading{Value: "Sample Scenario 5", LineNo: 10},
   155  							},
   156  							{
   157  								Heading: &gauge.Heading{Value: "Sample Scenario 6", LineNo: 20},
   158  							},
   159  							{
   160  								Heading: &gauge.Heading{Value: "Random Scenario 9", LineNo: 30},
   161  							},
   162  						},
   163  					},
   164  				},
   165  			}
   166  		},
   167  	}
   168  
   169  	b, _ := json.Marshal(lsp.WorkspaceSymbolParams{Limit: 5, Query: "S"})
   170  	p := json.RawMessage(b)
   171  	got, err := workspaceSymbols(&jsonrpc2.Request{Params: &p})
   172  
   173  	if err != nil {
   174  		t.Errorf("expected error to be nil. Got: \n%v", err.Error())
   175  	}
   176  
   177  	if got != nil {
   178  		t.Errorf("expected %v to be nil", got)
   179  	}
   180  }
   181  
   182  func TestWorkspaceSymbolsSortsAndGroupsBySpecsAndScenarios(t *testing.T) {
   183  	provider = &dummyInfoProvider{
   184  		specsFunc: func(specs []string) []*infoGatherer.SpecDetail {
   185  			return []*infoGatherer.SpecDetail{
   186  				&infoGatherer.SpecDetail{
   187  					Spec: &gauge.Specification{
   188  						Heading:  &gauge.Heading{Value: "Sample 1", LineNo: 1},
   189  						FileName: "foo1.spec",
   190  						Scenarios: []*gauge.Scenario{
   191  							{
   192  								Heading: &gauge.Heading{Value: "Sample Scenario 1", LineNo: 10},
   193  							},
   194  							{
   195  								Heading: &gauge.Heading{Value: "Scenario Sample 2", LineNo: 20},
   196  							},
   197  							{
   198  								Heading: &gauge.Heading{Value: "Random Scenario 1", LineNo: 30},
   199  							},
   200  						},
   201  					},
   202  				},
   203  				&infoGatherer.SpecDetail{
   204  					Spec: &gauge.Specification{
   205  						Heading:  &gauge.Heading{Value: "Sample 2", LineNo: 2},
   206  						FileName: "foo2.spec",
   207  						Scenarios: []*gauge.Scenario{
   208  							{
   209  								Heading: &gauge.Heading{Value: "Scenario Sample 5", LineNo: 10},
   210  							},
   211  							{
   212  								Heading: &gauge.Heading{Value: "Sample Scenario 6", LineNo: 20},
   213  							},
   214  							{
   215  								Heading: &gauge.Heading{Value: "Random Scenario 9", LineNo: 30},
   216  							},
   217  						},
   218  					},
   219  				},
   220  			}
   221  		},
   222  	}
   223  
   224  	want := []string{
   225  		"# Sample 1",
   226  		"# Sample 2",
   227  		"## Sample Scenario 1",
   228  		"## Sample Scenario 6",
   229  		"## Scenario Sample 2",
   230  		"## Scenario Sample 5",
   231  	}
   232  
   233  	b, _ := json.Marshal(lsp.WorkspaceSymbolParams{Limit: 5, Query: "Sample"})
   234  	p := json.RawMessage(b)
   235  	got, err := workspaceSymbols(&jsonrpc2.Request{Params: &p})
   236  
   237  	if err != nil {
   238  		t.Errorf("expected error to be nil. Got: \n%v", err.Error())
   239  	}
   240  
   241  	info := mapName(got.([]*lsp.SymbolInformation))
   242  
   243  	if !reflect.DeepEqual(info, want) {
   244  		t.Errorf("expected %v to be equal %v", info, want)
   245  	}
   246  }
   247  
   248  func TestDocumentSymbols(t *testing.T) {
   249  	provider = &dummyInfoProvider{}
   250  	specText := `Specification Heading
   251  =====================
   252  
   253  Scenario Heading
   254  ----------------
   255  
   256  * Step text
   257  
   258  Scenario Heading2
   259  -----------------
   260  
   261  * Step text`
   262  
   263  	uri := util.ConvertPathToURI("foo.spec")
   264  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
   265  	openFilesCache.add(uri, specText)
   266  	b, _ := json.Marshal(lsp.DocumentSymbolParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}})
   267  	p := json.RawMessage(b)
   268  
   269  	got, err := documentSymbols(&jsonrpc2.Request{Params: &p})
   270  
   271  	if err != nil {
   272  		t.Errorf("expected errror to be nil. Got: \n%v", err.Error())
   273  	}
   274  
   275  	info := mapName(got.([]*lsp.SymbolInformation))
   276  
   277  	want := []string{
   278  		"# Specification Heading",
   279  		"## Scenario Heading",
   280  		"## Scenario Heading2",
   281  	}
   282  	if !reflect.DeepEqual(info, want) {
   283  		t.Errorf("expected %v to be equal %v", info, want)
   284  	}
   285  
   286  	openFilesCache.remove(uri)
   287  }
   288  
   289  func TestDocumentSymbolsForConcept(t *testing.T) {
   290  	provider = &dummyInfoProvider{}
   291  	cptText := `
   292  	# Concept 1
   293  
   294  	* foo
   295  	* bar
   296  
   297  	Concept 2 <param1>
   298  	==================
   299  
   300  	* baz
   301  	`
   302  
   303  	uri := util.ConvertPathToURI("foo.cpt")
   304  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
   305  	openFilesCache.add(uri, cptText)
   306  	b, _ := json.Marshal(lsp.DocumentSymbolParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}})
   307  	p := json.RawMessage(b)
   308  
   309  	got, err := documentSymbols(&jsonrpc2.Request{Params: &p})
   310  
   311  	if err != nil {
   312  		t.Errorf("expected errror to be nil. Got: \n%v", err.Error())
   313  	}
   314  
   315  	info := mapName(got.([]*lsp.SymbolInformation))
   316  
   317  	want := []string{
   318  		"# Concept 1",
   319  		"# Concept 2 <param1>",
   320  	}
   321  	if !reflect.DeepEqual(info, want) {
   322  		t.Errorf("expected %v to be equal %v", info, want)
   323  	}
   324  
   325  	openFilesCache.remove(uri)
   326  }
   327  
   328  func TestGetSpecSymbol(t *testing.T) {
   329  	spec := &gauge.Specification{
   330  		Heading:  &gauge.Heading{Value: "Sample 1", LineNo: 1},
   331  		FileName: "foo1.spec",
   332  	}
   333  
   334  	want := &lsp.SymbolInformation{
   335  		Name: "# Sample 1",
   336  		Kind: lsp.SKNamespace,
   337  		Location: lsp.Location{
   338  			URI: util.ConvertPathToURI("foo1.spec"),
   339  			Range: lsp.Range{
   340  				Start: lsp.Position{Line: 0, Character: 0},
   341  				End:   lsp.Position{Line: 0, Character: len("Sample 1")},
   342  			},
   343  		},
   344  	}
   345  
   346  	got := getSpecSymbol(spec)
   347  
   348  	if !reflect.DeepEqual(got, want) {
   349  		t.Errorf("expected %v to be equal %v", got, want)
   350  	}
   351  }
   352  
   353  func TestGetScenarioSymbol(t *testing.T) {
   354  	scenario := &gauge.Scenario{
   355  		Heading: &gauge.Heading{Value: "Sample Scenario 5", LineNo: 10},
   356  	}
   357  
   358  	want := &lsp.SymbolInformation{
   359  		Name: "## Sample Scenario 5",
   360  		Kind: lsp.SKNamespace,
   361  		Location: lsp.Location{
   362  			URI: util.ConvertPathToURI("foo.spec"),
   363  			Range: lsp.Range{
   364  				Start: lsp.Position{Line: 9, Character: 0},
   365  				End:   lsp.Position{Line: 9, Character: len("Scenario Heading2")},
   366  			},
   367  		},
   368  	}
   369  
   370  	got := getScenarioSymbol(scenario, "foo.spec")
   371  
   372  	if !reflect.DeepEqual(got, want) {
   373  		t.Errorf("expected %v to be equal %v", got, want)
   374  	}
   375  }
   376  
   377  func TestGetConceptSymbols(t *testing.T) {
   378  	conceptText := `
   379  	# Concept 1
   380  
   381  	* foo
   382  	* bar
   383  
   384  	Concept 2 <param1>
   385  	==================
   386  
   387  	* baz
   388  	`
   389  	want := []*lsp.SymbolInformation{
   390  		{
   391  			Name: "# Concept 1",
   392  			Kind: lsp.SKNamespace,
   393  			Location: lsp.Location{
   394  				URI: util.ConvertPathToURI("foo.cpt"),
   395  				Range: lsp.Range{
   396  					Start: lsp.Position{Line: 1, Character: 0},
   397  					End:   lsp.Position{Line: 1, Character: len("Concept 1")},
   398  				},
   399  			},
   400  		},
   401  		{
   402  			Name: "# Concept 2 <param1>",
   403  			Kind: lsp.SKNamespace,
   404  			Location: lsp.Location{
   405  				URI: util.ConvertPathToURI("foo.cpt"),
   406  				Range: lsp.Range{
   407  					Start: lsp.Position{Line: 6, Character: 0},
   408  					End:   lsp.Position{Line: 6, Character: len("Concept 2 <param1>")},
   409  				},
   410  			},
   411  		},
   412  	}
   413  	got := getConceptSymbols(conceptText, "foo.cpt")
   414  
   415  	if !reflect.DeepEqual(got, want) {
   416  		t.Errorf("expected %v to be equal %v", got, want)
   417  	}
   418  }
   419  
   420  func mapName(vs []*lsp.SymbolInformation) []string {
   421  	val := make([]string, len(vs))
   422  	for i, v := range vs {
   423  		val[i] = v.Name
   424  	}
   425  	return val
   426  }