Пример #1
0
func gocov_test(ir *gothic.Interpreter) {
	var buf bytes.Buffer
	cmd := exec.Command("gocov", "test")
	cmd.Stdout = &buf
	err := cmd.Run()
	if err != nil {
		gocov_test_error(ir, err)
		return
	}

	result := struct{ Packages []*gocov.Package }{}
	err = json.Unmarshal(buf.Bytes(), &result)
	if err != nil {
		gocov_test_error(ir, err)
		return
	}

	sel := ""
	current = result.Packages
	for pi, p := range result.Packages {
		for fi, f := range p.Functions {
			r := reached(f)
			n := len(f.Statements)
			fun := fmt.Sprintf("%s.%s", p.Name, f.Name)
			cov := fmt.Sprintf("%.2f%% (%d/%d)", percentage(r, n), r, n)
			file := fmt.Sprintf("%s/%s", p.Name, filepath.Base(f.File))
			id := fmt.Sprintf("f_%d_%d", pi, fi)
			if prevsel != "" && prevsel == fun {
				sel = id
			}
			ir.Eval(`.f2.funcs insert {} end -id `, id,
				` -values {`, strconv.Quote(fun),
				` `, strconv.Quote(file),
				` `, strconv.Quote(cov), `}`)
		}
	}

	dir := filepath.Dir(current[0].Functions[0].File)
	ir.Set("pathtext", dir)

	done := 0
	total := 0
	for _, p := range result.Packages {
		for _, f := range p.Functions {
			done += reached(f)
			total += len(f.Statements)
		}
	}
	ir.Set("covtext", fmt.Sprintf("Overall coverage: %.2f%% (%d/%d)",
		percentage(done, total), done, total))

	if sel == "" {
		sel = "f_0_0"
	}
	ir.Eval(".f2.funcs selection set ", sel)
}
Пример #2
0
func gocov_selection(ir *gothic.Interpreter) {
	var selection string
	ir.EvalAs(&selection, ".f2.funcs selection")

	var pi, fi int
	_, err := fmt.Sscanf(selection, "f_%d_%d", &pi, &fi)
	if err != nil {
		panic(err)
	}

	f := current[pi].Functions[fi]
	prevsel = fmt.Sprintf("%s.%s", current[pi].Name, f.Name)

	data, err := ioutil.ReadFile(f.File)
	if err != nil {
		panic(err)
	}

	ir.Set("source", string(data[f.Start:f.End]))
	ir.Eval(`
		.f1.sourceview configure -state normal
		.f1.sourceview delete 1.0 end
		.f1.sourceview insert end $source
		.f1.sourceview configure -state disabled
	`)
	for _, s := range f.Statements {
		if s.Reached != 0 {
			continue
		}
		ls, le := s.Start-f.Start, s.End-f.Start
		ir.Eval(`.f1.sourceview tag add red {1.0 +`, ls, `chars} {1.0 +`, le, `chars}`)
	}

	if xsourceview != "" {
		ir.Eval(".f1.sourceview xview moveto [lindex {", xsourceview, "} 0]")
		ir.Eval(".f1.sourceview yview moveto [lindex {", ysourceview, "} 0]")
		xsourceview = ""
		ysourceview = ""
	}
}