Copyright 2019 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 sample provides functionality for generating sample values of the types contained in the internal package.
package sample

import (
	
	
	
	
	
	
	
	
	

	
	
	
	oldlicensecheck 
	
	
	
	
	
)
These sample values can be used to construct test cases.
var (
	ModulePath                = "github.com/valid/module_name"
	RepositoryURL             = "https://github.com/valid/module_name"
	VersionString             = "v1.0.0"
	CommitTime                = NowTruncated()
	LicenseType               = "MIT"
	LicenseFilePath           = "LICENSE"
	NonRedistributableLicense = &licenses.License{
		Metadata: &licenses.Metadata{
			FilePath: "NONREDIST_LICENSE",
			Types:    []string{"UNKNOWN"},
		},
		Contents: []byte(`unknown`),
	}
	PackageName    = "foo"
	Suffix         = "foo"
	PackagePath    = path.Join(ModulePath, Suffix)
	V1Path         = PackagePath
	ReadmeFilePath = "README.md"
	ReadmeContents = "readme"
	GOOS           = internal.All
	GOARCH         = internal.All
	Doc            = Documentation(GOOS, GOARCH, DocContents)
	API            = []*internal.Symbol{
		{
			SymbolMeta: internal.SymbolMeta{
				Name:       "V",
				ParentName: "V",
				Synopsis:   "var V int",
				Section:    internal.SymbolSectionVariables,
				Kind:       internal.SymbolKindVariable,
			},
			GOOS:   GOOS,
			GOARCH: GOARCH,
		},
	}
	DocContents = `
		// Package p is a package.
		//
		//
		// Links
		//
		// - pkg.go.dev, https://pkg.go.dev
 		package p
		var V int
	`
	Constant = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Constant",
			Synopsis: "const Constant",
			Section:  internal.SymbolSectionConstants,
			Kind:     internal.SymbolKindConstant,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Variable = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Variable",
			Synopsis: "var Variable",
			Section:  internal.SymbolSectionVariables,
			Kind:     internal.SymbolKindVariable,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Function = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Function",
			Synopsis: "func Function() error",
			Section:  internal.SymbolSectionFunctions,
			Kind:     internal.SymbolKindFunction,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	FunctionNew = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:       "New",
			Synopsis:   "func New() *Type",
			Section:    internal.SymbolSectionTypes,
			Kind:       internal.SymbolKindFunction,
			ParentName: "Type",
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
	}
	Type = &internal.Symbol{
		SymbolMeta: internal.SymbolMeta{
			Name:     "Type",
			Synopsis: "type Type struct",
			Section:  internal.SymbolSectionTypes,
			Kind:     internal.SymbolKindType,
		},
		GOOS:   internal.All,
		GOARCH: internal.All,
		Children: []*internal.SymbolMeta{
			func() *internal.SymbolMeta {
				 := FunctionNew.SymbolMeta
				return &
			}(),
			{
				Name:       "Type.Field",
				Synopsis:   "field",
				Section:    internal.SymbolSectionTypes,
				Kind:       internal.SymbolKindField,
				ParentName: "Type",
			},
			{
				Name:       "Type.Method",
				Synopsis:   "method",
				Section:    internal.SymbolSectionTypes,
				Kind:       internal.SymbolKindMethod,
				ParentName: "Type",
			},
		},
	}
)
LicenseCmpOpts are options to use when comparing licenses with the cmp package.
coveragePercentEqual considers two floats the same if they are within 4 percentage points, and both are on the same side of 90% (our threshold).
func (,  float64) bool {
	if ( >= 90) != ( >= 90) {
		return false
	}
	return math.Abs(-) <= 4
}
NowTruncated returns time.Now() truncated to Microsecond precision. This makes it easier to work with timestamps in PostgreSQL, which have Microsecond precision: https://www.postgresql.org/docs/9.1/datatype-datetime.html
Module creates a Module with the given path and version. The list of suffixes is used to create Units within the module.
func (,  string,  ...string) *internal.Module {
	 := ModuleInfo(, )
	 := &internal.Module{
		ModuleInfo: *,
		Licenses:   Licenses(),
	}
	.Units = []*internal.Unit{UnitForModuleRoot()}
	for ,  := range  {
		 := constructFullPath(, )
		 := UnitForPackage(, , VersionString, path.Base(), .IsRedistributable)
		if  != "" {
			AddPackage(, )
		} else {
			 := UnitForPackage(.Path, , , .Name, .IsRedistributable)
			.Units[0].Documentation = .Documentation
			.Units[0].Name = .Name
		}
	}
	if  == stdlib.ModulePath {
		.Units[0].Readme = nil
Fill in license contents.
UnitForPackage constructs a unit with the given module path and suffix. If modulePath is the standard library, the package path is the suffix, which must not be empty. Otherwise, the package path is the concatenation of modulePath and suffix. The package name is last component of the package path.
Copy Doc because some tests modify it.
	 := *Doc
	 := Imports()
	return &internal.Unit{
		UnitMeta:        *UnitMeta(, , , , ),
		Documentation:   []*internal.Documentation{&},
		LicenseContents: Licenses(),
		Imports:         ,
		NumImports:      len(),
	}
}

func ( *internal.Module,  *internal.Unit) *internal.Module {
	if .ModulePath != stdlib.ModulePath && !strings.HasPrefix(.Path, .ModulePath) {
		panic(fmt.Sprintf("package path %q not a prefix of module path %q",
			.Path, .ModulePath))
	}
	AddUnit(, UnitForPackage(.Path, .ModulePath, .Version, .Name, .IsRedistributable))
	 := len(.ModulePath)
	if .ModulePath == stdlib.ModulePath {
		 = 1
	}
	for  := .Path; len() > ;  = path.Dir() {
		 := false
		for ,  := range .Units {
			if .Path ==  {
				 = true
				break
			}
		}
		if ! {
			AddUnit(, UnitEmpty(, .ModulePath, .Version))
		}
	}
	return 
}

func ( string) *internal.PackageMeta {
	return &internal.PackageMeta{
		Path:              ,
		IsRedistributable: true,
		Name:              path.Base(),
		Synopsis:          Doc.Synopsis,
		Licenses:          LicenseMetadata(),
	}
}

func (,  string) *internal.ModuleInfo {
	return &internal.ModuleInfo{
		ModulePath: ,
		Version:    ,
Assume the module path is a GitHub-like repo name.
		SourceInfo:        source.NewGitHubInfo("https://"+, "", ),
		IsRedistributable: true,
		HasGoMod:          true,
	}
}

func () *internal.VersionMap {
	return &internal.VersionMap{
		ModulePath:       ModulePath,
		RequestedVersion: VersionString,
		ResolvedVersion:  VersionString,
		Status:           http.StatusOK,
		GoModPath:        "",
		Error:            "",
	}
}

func ( *internal.Module,  *internal.Unit) {
	for ,  := range .Units {
		if .Path == .Path {
			panic(fmt.Sprintf("module already has path %q", .Path))
		}
	}
	.Units = append(.Units, )
}

func ( *internal.Module,  *licenses.License) {
	.Licenses = append(.Licenses, )
	 := path.Dir(.FilePath)
	if  == "." {
		 = ""
	}
	for ,  := range .Units {
		if strings.TrimPrefix(.Path, .ModulePath+"/") ==  {
			.Licenses = append(.Licenses, .Metadata)
			.LicenseContents = append(.LicenseContents, )
		}
	}
}
ReplaceLicense replaces all licenses having the same file path as lic with lic.
func ( *internal.Module,  *licenses.License) {
	replaceLicense(, .Licenses)
	for ,  := range .Units {
		for ,  := range .Licenses {
			if .FilePath == .FilePath {
				.Licenses[] = .Metadata
			}
		}
		replaceLicense(, .LicenseContents)
	}
}

func ( *licenses.License,  []*licenses.License) {
	for ,  := range  {
		if .FilePath == .FilePath {
			[] = 
		}
	}
}

func (, ,  string) *internal.Unit {
	return &internal.Unit{
		UnitMeta: *UnitMeta(, , , "", true),
	}
}

func (, , ,  string,  bool) *internal.UnitMeta {
	return &internal.UnitMeta{
		Path:              ,
		Name:              ,
		IsRedistributable: ,
		Licenses:          LicenseMetadata(),
		ModuleInfo: internal.ModuleInfo{
			ModulePath:        ,
			Version:           ,
			CommitTime:        NowTruncated(),
			IsRedistributable: ,
			SourceInfo:        source.NewGitHubInfo("https://"+, "", ),
		},
	}
}

func (,  string) string {
	if  != stdlib.ModulePath {
		return path.Join(, )
	}
	return 
}
Documentation returns a Documentation value for the given Go source. It panics if there are errors parsing or encoding the source.
func (, ,  string) *internal.Documentation {
	 := token.NewFileSet()
	,  := parser.ParseFile(, "sample.go", , parser.ParseComments)
	if  != nil {
		panic()
	}
	 := godoc.NewPackage(, nil)
	.AddFile(, true)
	,  := .Encode(context.Background())
	if  != nil {
		panic()
	}

	return &internal.Documentation{
		GOOS:     ,
		GOARCH:   ,
		Synopsis: fmt.Sprintf("This is a package synopsis for GOOS=%s, GOARCH=%s", , ),
		Source:   ,
	}
}

func () []*licenses.Metadata {
	return []*licenses.Metadata{
		{
			Types:    []string{LicenseType},
			FilePath: LicenseFilePath,
			OldCoverage: oldlicensecheck.Coverage{
				Percent: 100,
				Match:   []oldlicensecheck.Match{{Name: LicenseType, Type: oldlicensecheck.MIT, Percent: 100}},
			},
		},
	}
}

func () []*licenses.License {
	return []*licenses.License{
		{Metadata: LicenseMetadata()[0], Contents: []byte(`Lorem Ipsum`)},
	}
}

func () []string {
	return []string{"fmt", "path/to/bar"}