package processors

import (
	"go/token"
	"path/filepath"
	"testing"

	"github.com/golangci/golangci-lint/pkg/result"
)

func newNolintFileIssue(line int, fromLinter string) result.Issue {
	return result.Issue{
		Pos: token.Position{
			Filename: filepath.Join("testdata", "nolint.go"),
			Line:     line,
		},
		FromLinter: fromLinter,
	}
}

func TestNolint(t *testing.T) {
	p := NewNolint(token.NewFileSet())
	processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt"))
	processAssertEmpty(t, p, newNolintFileIssue(3, "gofmt")) // check cached is ok
	processAssertSame(t, p, newNolintFileIssue(3, "gofmtA")) // check different name

	processAssertEmpty(t, p, newNolintFileIssue(4, "gofmt"))
	processAssertSame(t, p, newNolintFileIssue(4, "gofmtA")) // check different name

	processAssertEmpty(t, p, newNolintFileIssue(5, "gofmt"))
	processAssertEmpty(t, p, newNolintFileIssue(5, "govet"))
	processAssertSame(t, p, newNolintFileIssue(5, "gofmtA")) // check different name

	processAssertEmpty(t, p, newNolintFileIssue(6, "any"))
	processAssertEmpty(t, p, newNolintFileIssue(7, "any"))

	processAssertSame(t, p, newNolintFileIssue(1, "golint")) // no directive
}

func TestNoIssuesInAutogeneratedFiles(t *testing.T) {
	files := []string{
		"nolint_autogenerated.go",
		"nolint_autogenerated_alt_hdr.go",
		"nolint_autogenerated_alt_hdr2.go",
	}
	for _, file := range files {
		t.Run(file, func(t *testing.T) {
			i := result.Issue{
				Pos: token.Position{
					Filename: filepath.Join("testdata", file),
					Line:     4,
				},
			}
			p := NewNolint(token.NewFileSet())
			processAssertEmpty(t, p, i)
		})
	}
}