Beispiel #1
0
func TestUnexport(t *testing.T) {
	_, fset, info := parsePackage(pkg+"/testrename", t)
	defs := gounexport.GetDefinitions(info, fset)
	unusedDefs := gounexport.FindUnusedDefinitions(pkg, defs, nil)

	renamesCount := make(map[string]int)
	renameFunc := func(file string, offset int, source string, target string) error {
		log.Printf("renaming [%s] at %d from [%s] to [%s]", file, offset, source, target)
		renamesCount[source] = renamesCount[source] + 1
		return nil
	}

	for _, d := range unusedDefs {
		err := gounexport.Unexport(d, defs, renameFunc)
		if d.SimpleName == "UnusedStructConflict" && err == nil {
			t.Error("expected conflict error for UnusedStructConflict")
		}
		if d.SimpleName == "UnusedVarConflict" && err == nil {
			t.Error("expected conflict error for UnusedVarConflict")
		}
	}

	assertRename(renamesCount, "UnusedField", 1, t)
	assertRename(renamesCount, "UnusedMethod", 1, t)
	assertRename(renamesCount, "UsedInPackageMethod", 2, t)
}
Beispiel #2
0
func renameDefinitions(unused []*gounexport.Definition, allDefs map[string]*gounexport.Definition) {
	for _, def := range unused {
		gounexport.Unexport(def, allDefs, fs.ReplaceStringInFile)
	}
}