示例#1
0
// TestLineComments, using a simple test case, checks that consequtive line
// comments are properly terminated with a newline even if the AST position
// information is incorrect.
//
func TestLineComments(t *testing.T) {
	const src = `// comment 1
	// comment 2
	// comment 3
	package main
	`

	fset := token.NewFileSet()
	ast1, err1 := parser.ParseFile(fset, "", src, parser.ParseComments)
	if err1 != nil {
		panic(err1)
	}

	var buf bytes.Buffer
	fset = token.NewFileSet() // use the wrong file set
	printer.Fprint(&buf, fset, ast1)

	nlines := 0
	for _, ch := range buf.Bytes() {
		if ch == '\n' {
			nlines++
		}
	}

	const expected = 3
	if nlines < expected {
		t.Errorf("got %d, expected %d\n", nlines, expected)
	}
}
示例#2
0
func TestParse3(t *testing.T) {
	for _, filename := range validFiles {
		_, err := parser.ParseFile(fset, filename, nil, 0)
		if err != nil {
			t.Errorf("ParseFile(%s): %v", filename, err)
		}
	}
}
示例#3
0
func TestParseValidPrograms(t *testing.T) {
	for _, src := range validPrograms {
		_, err := parser.ParseFile(fset, "", src, 0)
		if err != nil {
			t.Errorf("ParseFile(%q): %v", src, err)
		}
	}
}
示例#4
0
func TestParseIllegalInputs(t *testing.T) {
	for _, src := range illegalInputs {
		_, err := parser.ParseFile(fset, "", src, 0)
		if err == nil {
			t.Errorf("ParseFile(%v) should have failed", src)
		}
	}
}
示例#5
0
文件: main.go 项目: serussell/kgc
func main() {

	goopt.Version = fmt.Sprintf("%d.%d", VER_MAJOR, VER_MINOR)
	goopt.Summary = PROG_NAME
	goopt.Parse(nil)

	if *ver {
		fmt.Printf("\n%s version %d.%d", PROG_NAME, VER_MAJOR, VER_MINOR)
		fmt.Printf("\nCopyright (c) 2011 Chanwit Kaewkasi / SUT\n\n")
		return
	}

	var filename string = ""
	if len(goopt.Args) == 1 {
		filename = goopt.Args[0]
	} else {
		fmt.Print(goopt.Usage())
		return
	}

	fset := token.NewFileSet()
	astf, err := parser.ParseFile(fset, filename, nil, 0)
	if err == nil {
		v := NewVisitor(astf)
		Walk(v, astf)
		tempfile, err := os.OpenFile(filename+"k", os.O_WRONLY|os.O_CREATE, 0665)
		if err == nil {
			printer.Fprint(tempfile, fset, astf)
			tempfile.Close()
			newArgs := make([]string, len(os.Args))
			copy(newArgs, os.Args)
			for i, v := range newArgs {
				if v == filename {
					newArgs[i] = filename + "k"
				}
			}
			gc := "8g"
			switch os.Getenv("GOARCH") {
			case "386":
				gc = "8g"
			case "amd64":
				gc = "6g"
			case "arm":
				gc = "5g"
			}
			newArgs[0] = os.Getenv("GOROOT") + "/bin/" + gc
			StdExecve(newArgs, true)
			os.Remove(filename + "k")
		}
	} else {
		fmt.Printf("%s\n", err)
	}
}
示例#6
0
func check(t *testing.T, source, golden string, mode checkMode) {
	// parse source
	prog, err := parser.ParseFile(fset, source, nil, parser.ParseComments)
	if err != nil {
		t.Error(err)
		return
	}

	// filter exports if necessary
	if mode&export != 0 {
		ast.FileExports(prog) // ignore result
		prog.Comments = nil   // don't print comments that are not in AST
	}

	// determine printer configuration
	cfg := printer.Config{Tabwidth: tabwidth}
	if mode&rawFormat != 0 {
		cfg.Mode |= printer.RawFormat
	}

	// format source
	var buf bytes.Buffer
	if _, err := cfg.Fprint(&buf, fset, prog); err != nil {
		t.Error(err)
	}
	res := buf.Bytes()

	// update golden files if necessary
	if *update {
		if err := ioutil.WriteFile(golden, res, 0644); err != nil {
			t.Error(err)
		}
		return
	}

	// get golden
	gld, err := ioutil.ReadFile(golden)
	if err != nil {
		t.Error(err)
		return
	}

	// compare lengths
	if len(res) != len(gld) {
		t.Errorf("len = %d, expected %d (= len(%s))", len(res), len(gld), golden)
	}

	// compare contents
	for i, line, offs := 0, 1, 0; i < len(res) && i < len(gld); i++ {
		ch := res[i]
		if ch != gld[i] {
			t.Errorf("%s:%d:%d: %s", source, line, i-offs+1, lineString(res, offs))
			t.Errorf("%s:%d:%d: %s", golden, line, i-offs+1, lineString(gld, offs))
			t.Error()
			return
		}
		if ch == '\n' {
			line++
			offs = i + 1
		}
	}
}