<commit_before>// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package misc

import (
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/tools/gopls/internal/lsp"
	"golang.org/x/tools/gopls/internal/lsp/protocol"
	. "golang.org/x/tools/gopls/internal/lsp/regtest"
	"golang.org/x/tools/internal/typeparams"
)

func TestBadURICrash_VSCodeIssue1498(t *testing.T) {
	const src = `
-- go.mod --
module example.com

go 1.12

-- main.go --
package main

func main() {}

`
	WithOptions(
		Modes(Default),
		Settings{"allExperiments": true},
	).Run(t, src, func(t *testing.T, env *Env) {
		params := &protocol.SemanticTokensParams{}
		const badURI = "http://foo"
		params.TextDocument.URI = badURI
		// This call panicked in the past: golang/vscode-go#1498.
		if _, err := env.Editor.Server.SemanticTokensFull(env.Ctx, params); err != nil {
			// Requests to an invalid URI scheme shouldn't result in an error, we
			// simply don't support this so return empty result. This could be
			// changed, but for now assert on the current behavior.
			t.Errorf("SemanticTokensFull(%q): %v", badURI, err)
		}
	})
}

// fix bug involving type parameters and regular parameters
// (golang/vscode-go#2527)
func TestSemantic_2527(t *testing.T) {
	if !typeparams.Enabled {
		t.Skip("type parameters are needed for this test")
	}
	// these are the expected types of identfiers in textt order
	want := []result{
		{"package", "keyword", ""},
		{"foo", "namespace", ""},
		{"func", "keyword", ""},
		{"Add", "function", "definition deprecated"},
		{"T", "typeParameter", "definition"},
		{"int", "type", "defaultLibrary"},
		{"target", "parameter", "definition"},
		{"T", "typeParameter", ""},
		{"l", "parameter", "definition"},
		{"T", "typeParameter", ""},
		{"T", "typeParameter", ""},
		{"return", "keyword", ""},
		{"append", "function", "defaultLibrary"},
		{"l", "parameter", ""},
		{"target", "parameter", ""},
		{"for", "keyword", ""},
		{"range", "keyword", ""},
		{"l", "parameter", ""},
		{"return", "keyword", ""},
		{"nil", "variable", "readonly defaultLibrary"},
	}
	src := `
-- go.mod --
module example.com

go 1.19
-- main.go --
package foo
// Deprecated (for testing)
func Add[T int](target T, l []T) []T {
	return append(l, target)
	for range l {} // test coverage
	return nil
}
`
	WithOptions(
		Modes(Default),
		Settings{"semanticTokens": true},
	).Run(t, src, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		//env.Await(EmptyOrNoDiagnostics("main.go`"))
		env.Await(env.AfterChange(env.DiagnosticAtRegexp("main.go", "for range")))
		p := &protocol.SemanticTokensParams{
			TextDocument: protocol.TextDocumentIdentifier{
				URI: env.Sandbox.Workdir.URI("main.go"),
			},
		}
		v, err := env.Editor.Server.SemanticTokensFull(env.Ctx, p)
		if err != nil {<commit_msg>gopls/internal/regtest/misc: fix use of the AfterChange API<commit_after>// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package misc

import (
	"strings"
	"testing"

	"github.com/google/go-cmp/cmp"
	"golang.org/x/tools/gopls/internal/lsp"
	"golang.org/x/tools/gopls/internal/lsp/protocol"
	. "golang.org/x/tools/gopls/internal/lsp/regtest"
	"golang.org/x/tools/internal/typeparams"
)

func TestBadURICrash_VSCodeIssue1498(t *testing.T) {
	const src = `
-- go.mod --
module example.com

go 1.12

-- main.go --
package main

func main() {}

`
	WithOptions(
		Modes(Default),
		Settings{"allExperiments": true},
	).Run(t, src, func(t *testing.T, env *Env) {
		params := &protocol.SemanticTokensParams{}
		const badURI = "http://foo"
		params.TextDocument.URI = badURI
		// This call panicked in the past: golang/vscode-go#1498.
		if _, err := env.Editor.Server.SemanticTokensFull(env.Ctx, params); err != nil {
			// Requests to an invalid URI scheme shouldn't result in an error, we
			// simply don't support this so return empty result. This could be
			// changed, but for now assert on the current behavior.
			t.Errorf("SemanticTokensFull(%q): %v", badURI, err)
		}
	})
}

// fix bug involving type parameters and regular parameters
// (golang/vscode-go#2527)
func TestSemantic_2527(t *testing.T) {
	if !typeparams.Enabled {
		t.Skip("type parameters are needed for this test")
	}
	// these are the expected types of identfiers in textt order
	want := []result{
		{"package", "keyword", ""},
		{"foo", "namespace", ""},
		{"func", "keyword", ""},
		{"Add", "function", "definition deprecated"},
		{"T", "typeParameter", "definition"},
		{"int", "type", "defaultLibrary"},
		{"target", "parameter", "definition"},
		{"T", "typeParameter", ""},
		{"l", "parameter", "definition"},
		{"T", "typeParameter", ""},
		{"T", "typeParameter", ""},
		{"return", "keyword", ""},
		{"append", "function", "defaultLibrary"},
		{"l", "parameter", ""},
		{"target", "parameter", ""},
		{"for", "keyword", ""},
		{"range", "keyword", ""},
		{"l", "parameter", ""},
		{"return", "keyword", ""},
		{"nil", "variable", "readonly defaultLibrary"},
	}
	src := `
-- go.mod --
module example.com

go 1.19
-- main.go --
package foo
// Deprecated (for testing)
func Add[T int](target T, l []T) []T {
	return append(l, target)
	for range l {} // test coverage
	return nil
}
`
	WithOptions(
		Modes(Default),
		Settings{"semanticTokens": true},
	).Run(t, src, func(t *testing.T, env *Env) {
		env.OpenFile("main.go")
		env.AfterChange(env.DiagnosticAtRegexp("main.go", "for range"))
		p := &protocol.SemanticTokensParams{
			TextDocument: protocol.TextDocumentIdentifier{
				URI: env.Sandbox.Workdir.URI("main.go"),
			},
		}
		v, err := env.Editor.Server.SemanticTokensFull(env.Ctx, p)
		if err != nil {
{'fraction_non_alphanumeric': 0.18686433373556283, 'fraction_numerical': 0.008963971729012239, 'mean_word_length': 3.958974358974359, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5622581', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2016, 'n_words': 521}