Example #1
0
func doTest(t *testing.T, dir, inPath string) {
	base := filepath.Base(inPath)
	lang, err := ParseLangFilename(base)
	if err != nil {
		t.Errorf("Could not infer language: %v", err)
		return
	}
	outPath := filepath.Join(testsDir, dir, "out"+filepath.Ext(base))
	in, err := os.Open(inPath)
	if err != nil {
		t.Errorf("Failed opening in file %s: %v", inPath, err)
		return
	}
	defer in.Close()
	var out bytes.Buffer
	if err := CheckAndCommentCode(lang, in, &out); err != nil {
		t.Errorf("Failed checking and commenting code '%s': %v", inPath, err)
		return
	}
	got := out.Bytes()
	if *write {
		out, err := os.Create(outPath)
		if err != nil {
			t.Errorf("Failed opening out file '%s': %v", outPath, err)
			return
		}
		defer out.Close()
		_, err = out.Write(got)
		if err != nil {
			t.Errorf("Failed writing out file '%s': %v", outPath, err)
			return
		}
	} else {
		out, err := os.Open(outPath)
		if err != nil {
			t.Errorf("Failed opening out file '%s': %v", outPath, err)
			return
		}
		defer out.Close()
		want, err := ioutil.ReadAll(out)
		if err != nil {
			t.Errorf("Failed reading out file '%s': %v", outPath, err)
			return
		}
		if string(want) != string(got) {
			t.Errorf("Mismatching outputs for '%s'", inPath)
			return
		}
	}
}