コード例 #1
0
ファイル: dag.go プロジェクト: emergenesis/godag
func (d Dag) MakeDotGraph(filename string) {

	var file *os.File
	var fileinfo *os.FileInfo
	var e os.Error
	var sb *stringbuffer.StringBuffer

	fileinfo, e = os.Stat(filename)

	if e == nil {
		if fileinfo.IsRegular() {
			e = os.Remove(fileinfo.Name)
			if e != nil {
				log.Fatalf("[ERROR] failed to remove: %s\n", filename)
			}
		}
	}

	sb = stringbuffer.NewSize(500)
	file, e = os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0644)

	if e != nil {
		log.Fatalf("[ERROR] %s\n", e)
	}

	sb.Add("digraph depgraph {\n\trankdir=LR;\n")

	for _, v := range d {
		v.DotGraph(sb)
	}

	sb.Add("}\n")

	file.WriteString(sb.String())

	file.Close()

}
コード例 #2
0
ファイル: dag.go プロジェクト: emergenesis/godag
func (d Dag) MakeMainTest(root string) ([]*Package, string, string) {

	var max, i int
	var isTest bool
	var lname, sname, tmplib, tmpdir, tmpstub, tmpfile string

	sbImports := stringbuffer.NewSize(300)
	imprtSet := stringset.New()
	sbTests := stringbuffer.NewSize(1000)
	sbBench := stringbuffer.NewSize(1000)

	sbImports.Add("\n// autogenerated code\n\n")
	sbImports.Add("package main\n\n")
	imprtSet.Add("import \"regexp\"\n")
	imprtSet.Add("import \"testing\"\n")

	sbTests.Add("\n\nvar tests = []testing.InternalTest{\n")
	sbBench.Add("\n\nvar benchmarks = []testing.InternalBenchmark{\n")

	for _, v := range d {

		isTest = false
		sname = v.ShortName
		lname = v.ShortName
		max = len(v.ShortName)

		if max > 5 && sname[max-5:] == "_test" {
			collector := newTestCollector()
			for i = 0; i < len(v.Files); i++ {
				tree := getSyntaxTreeOrDie(v.Files[i], 0)
				ast.Walk(collector, tree)
			}

			if len(collector.Names) > 0 {
				isTest = true
				if hasSlash(v.Name) {
					lname = removeSlashAndDot(v.Name)
					imprtSet.Add(fmt.Sprintf("import %s \"%s\"\n", lname, v.Name))
				} else {
					imprtSet.Add(fmt.Sprintf("import \"%s\"\n", v.Name))
				}
				for i = 0; i < len(collector.Names); i++ {
					testFunc := collector.Names[i]
					if len(testFunc) >= 4 && testFunc[0:4] == "Test" {
						sbTests.Add(fmt.Sprintf("testing.InternalTest{\"%s.%s\", %s.%s },\n",
							sname, testFunc, lname, testFunc))
					} else if len(testFunc) >= 9 && testFunc[0:9] == "Benchmark" {
						sbBench.Add(fmt.Sprintf("testing.InternalBenchmark{\"%s.%s\", %s.%s },\n",
							sname, testFunc, lname, testFunc))

					}
				}
			}
		}

		if !isTest {

			collector := newTestCollector()

			for i = 0; i < len(v.Files); i++ {
				fname := v.Files[i]
				if len(fname) > 8 && fname[len(fname)-8:] == "_test.go" {
					tree := getSyntaxTreeOrDie(fname, 0)
					ast.Walk(collector, tree)
				}
			}

			if len(collector.Names) > 0 {
				if hasSlash(v.Name) {
					lname = removeSlashAndDot(v.Name)
					imprtSet.Add(fmt.Sprintf("import %s \"%s\"\n", lname, v.Name))
				} else {
					imprtSet.Add(fmt.Sprintf("import \"%s\"\n", v.Name))
				}
				for i = 0; i < len(collector.Names); i++ {
					testFunc := collector.Names[i]
					if len(testFunc) >= 4 && testFunc[0:4] == "Test" {
						sbTests.Add(fmt.Sprintf("testing.InternalTest{\"%s.%s\", %s.%s },\n",
							sname, testFunc, lname, testFunc))
					} else if len(testFunc) >= 9 && testFunc[0:9] == "Benchmark" {
						sbBench.Add(fmt.Sprintf("testing.InternalBenchmark{\"%s.%s\", %s.%s },\n",
							sname, testFunc, lname, testFunc))
					}
				}
			}
		}
	}

	sbTests.Add("};\n")
	sbBench.Add("};\n\n")

	for im := range imprtSet.Iter() {
		sbImports.Add(im)
	}

	sbTotal := stringbuffer.NewSize(sbImports.Len() +
		sbTests.Len() +
		sbBench.Len() + 100)
	sbTotal.Add(sbImports.String())
	sbTotal.Add(sbTests.String())
	sbTotal.Add(sbBench.String())

	sbTotal.Add("func main(){\n")
	sbTotal.Add("testing.Main(regexp.MatchString, tests, benchmarks);\n}\n\n")

	tmpstub = fmt.Sprintf("tmp%d", time.Seconds())
	tmpdir = filepath.Join(root, tmpstub)
	if global.GetString("-lib") != "" {
		tmplib = filepath.Join(global.GetString("-lib"), tmpstub)
	}

	dir, e1 := os.Stat(tmpdir)

	if e1 == nil && dir.IsDirectory() {
		log.Printf("[ERROR] directory: %s already exists\n", tmpdir)
	} else {
		e_mk := os.Mkdir(tmpdir, 0777)
		if e_mk != nil {
			log.Fatal("[ERROR] failed to create directory for testing")
		}
	}

	tmpfile = filepath.Join(tmpdir, "_main.go")

	fil, e2 := os.OpenFile(tmpfile, os.O_WRONLY|os.O_CREATE, 0777)

	if e2 != nil {
		log.Fatalf("[ERROR] %s\n", e2)
	}

	n, e3 := fil.WriteString(sbTotal.String())

	if e3 != nil {
		log.Fatalf("[ERROR] %s\n", e3)
	} else if n != sbTotal.Len() {
		log.Fatal("[ERROR] failed to write test")
	}

	fil.Close()

	p := newPackage()
	p.Name = filepath.Join(tmpstub, "main")
	p.ShortName = "main"
	p.Files = append(p.Files, tmpfile)

	vec := make([]*Package, 1)
	vec[0] = p
	return vec, tmpdir, tmplib
}
コード例 #3
0
ファイル: dag.go プロジェクト: bjarneh/godag
func (d Dag) MakeMainTest(root string) ([]*Package, string, string) {

	var (
		i         int
		lname     string
		sname     string
		tmplib    string
		tmpdir    string
		tmpstub   string
		tmpfile   string
		collector *TestCollector
	)

	sbImports := stringbuffer.NewSize(300)
	imprtSet := stringset.New()
	sbTests := stringbuffer.NewSize(1000)
	sbBench := stringbuffer.NewSize(1000)
	sbExample := stringbuffer.NewSize(1000)

	sbImports.Add("\n// autogenerated code\n\n")
	sbImports.Add("package main\n\n")
	imprtSet.Add("import \"regexp\"\n")
	imprtSet.Add("import \"testing\"\n")

	sbTests.Add("\n\nvar tests = []testing.InternalTest{\n")
	sbBench.Add("\n\nvar benchmarks = []testing.InternalBenchmark{\n")
	sbExample.Add("\n\nvar examples = []testing.InternalExample{\n")

	for _, v := range d {

		sname = v.ShortName
		lname = v.ShortName

		collector = newTestCollector()

		if strings.HasSuffix(v.ShortName, "_test") {

			for i = 0; i < len(v.Files); i++ {
				tree := getSyntaxTreeOrDie(v.Files[i], parser.ParseComments)
				ast.Walk(collector, tree)
			}

		} else {

			for i = 0; i < len(v.Files); i++ {
				if strings.HasSuffix(v.Files[i], "_test.go") {
					tree := getSyntaxTreeOrDie(v.Files[i], parser.ParseComments)
					ast.Walk(collector, tree)
				}
			}
		}

		if collector.FoundAnything() {

			if hasSlash(v.Name) {
				lname = removeSlashAndDot(v.Name)
				imprtSet.Add(fmt.Sprintf("import %s \"%s\"\n", lname, v.Name))
			} else {
				imprtSet.Add(fmt.Sprintf("import \"%s\"\n", v.Name))
			}

			// add tests
			for i := 0; i < len(collector.TestFuncs); i++ {
				fn := collector.TestFuncs[i]
				sbTests.Add(fmt.Sprintf(
					"testing.InternalTest{\"%s.%s\", %s.%s },\n",
					sname, fn, lname, fn))
			}

			// add benchmarks
			for i := 0; i < len(collector.BenchFuncs); i++ {
				fn := collector.BenchFuncs[i]
				sbBench.Add(fmt.Sprintf(
					"testing.InternalBenchmark{\"%s.%s\", %s.%s },\n",
					sname, fn, lname, fn))
			}

			// add examples ( not really )
			for i := 0; i < len(collector.ExampleFuncs); i++ {
				// fn := collector.ExampleFuncs[i] //TODO add comment which seems to be what we compare against..
				// sbExample.Add(fmt.Sprintf("testing.InternalExample{\"%s.%s\", %s.%s },\n", sname, fn, lname, fn))
			}
		}

	}

	sbTests.Add("};\n")
	sbBench.Add("};\n")
	sbExample.Add("};\n")

	for im := range imprtSet.Iter() {
		sbImports.Add(im)
	}

	sbTotal := stringbuffer.NewSize(sbImports.Len() +
		sbTests.Len() +
		sbBench.Len() + 100)
	sbTotal.Add(sbImports.String())
	sbTotal.Add(sbTests.String())
	sbTotal.Add(sbBench.String())
	sbTotal.Add(sbExample.String())

	sbTotal.Add("func main(){\n")
	sbTotal.Add("testing.Main(regexp.MatchString, tests, benchmarks, examples);\n}\n\n")

	tmpstub = fmt.Sprintf("tmp%d", time.Now().Unix())
	tmpdir = filepath.Join(root, tmpstub)
	if global.GetString("-lib") != "" {
		tmplib = filepath.Join(global.GetString("-lib"), tmpstub)
	}

	dir, e1 := os.Stat(tmpdir)

	if e1 == nil && dir.IsDir() {
		log.Printf("[ERROR] directory: %s already exists\n", tmpdir)
	} else {
		e_mk := os.Mkdir(tmpdir, 0777)
		if e_mk != nil {
			log.Fatal("[ERROR] failed to create directory for testing")
		}
	}

	tmpfile = filepath.Join(tmpdir, "_main.go")

	fil, e2 := os.OpenFile(tmpfile, os.O_WRONLY|os.O_CREATE, 0777)

	if e2 != nil {
		log.Fatalf("[ERROR] %s\n", e2)
	}

	n, e3 := fil.WriteString(sbTotal.String())

	if e3 != nil {
		log.Fatalf("[ERROR] %s\n", e3)
	} else if n != sbTotal.Len() {
		log.Fatal("[ERROR] failed to write test")
	}

	fil.Close()

	p := newPackage()
	p.Name = filepath.Join(tmpstub, "main")
	p.ShortName = "main"
	p.Files = append(p.Files, tmpfile)

	vec := make([]*Package, 1)
	vec[0] = p
	return vec, tmpdir, tmplib
}