// GenerateTestMain returns the source code for a test program that will run // the specified test functions from the specified package. func GenerateTestMain(packageName string, funcs *set.StringSet) string { var funcVec vector.StringVector for val := range funcs.Iter() { funcVec.Push(val) } result := "" result += "package main\n\n" result += "import \"testing\"\n" if funcVec.Len() > 0 { result += fmt.Sprintf("import \"./%s\"\n\n", packageName) } result += "var tests = []testing.Test {\n" for _, val := range funcVec.Data() { result += fmt.Sprintf("\ttesting.Test{\"%s\", %s.%s},\n", val, packageName, val) } result += "}\n\n" result += "func main() {\n" result += "\ttesting.Main(tests)\n" result += "}\n" return result }
func getSorted(set *StringSet) []string { var sorted vector.StringVector for val := range set.Iter() { sorted.Push(val) } sort.SortStrings(sorted) return sorted.Data() }
func (self *Regex) Matches(s string) []string { res := new(vector.StringVector) self.l.StartString(s) for !self.l.Eof() { if self.l.Next() == 0 { res.Push(self.l.String()) } } return res.Data() }
func expectContentsEqual(t *testing.T, set *set.StringSet, expected []string) { var contents vector.StringVector for val := range set.Iter() { contents.Push(val) } sort.SortStrings(contents) sort.SortStrings(expected) if !reflect.DeepEqual(contents.Data(), expected) { t.Errorf("Expected:%v\nGot: %v", expected, contents) } }
func (p *MatchRule) _ToString() string { svec := new(vector.StringVector) v := reflect.Indirect(reflect.NewValue(p)).(*reflect.StructValue) t := v.Type().(*reflect.StructType) for i := 0; i < v.NumField(); i++ { str, ok := v.Field(i).Interface().(string) if ok && "" != str { svec.Push(fmt.Sprintf("%s='%s'", strings.ToLower(t.Field(i).Name), str)) } } return strings.Join(svec.Data(), ",") }
func (self *Regex) Replace(s string, f func(string) string) string { res := new(vector.StringVector) buf := bytes.Runes([]byte(s)) last := 0 self.l.StartString(s) for !self.l.Eof() { if self.l.Next() == 0 { res.Push(string(buf[last:self.l.Pos()])) res.Push(f(self.l.String())) last = self.l.Pos() + self.l.Len() } } res.Push(string(buf[last:])) return strings.Join(res.Data(), "") }
// linkBinary calls 6l to link the binary of the given name, which must have // already been compiled with compileFiles. func linkBinary(name string) { linkerName, ok := linkers[os.Getenv("GOARCH")] if !ok { fmt.Println("Could not determine the correct linker to run.") fmt.Println("Please ensure that $GOARCH is set.") os.Exit(1) } linkerPath := path.Join(os.Getenv("GOBIN"), linkerName) var linkerArgs vector.StringVector linkerArgs.Push("-o") linkerArgs.Push(name) linkerArgs.Push(name + "." + linkerName[0:1]) if !executeCommand(linkerPath, linkerArgs.Data(), "igo-out/") { os.Exit(1) } }
// compileFiles invokes 6g with the appropriate arguments for compiling the // supplied set of .go files, and exits the program if the subprocess fails. func compileFiles(files *set.StringSet, targetBaseName string) { compilerName, ok := compilers[os.Getenv("GOARCH")] if !ok { fmt.Println("Could not determine the correct compiler to run.") fmt.Println("Please ensure that $GOARCH is set.") os.Exit(1) } compilerPath := path.Join(os.Getenv("GOBIN"), compilerName) gopackPath := path.Join(os.Getenv("GOBIN"), "gopack") targetDir, _ := path.Split(targetBaseName) if targetDir != "" { os.MkdirAll(path.Join("igo-out", targetDir), 0700) } // Compile var compilerArgs vector.StringVector compilerArgs.Push("-o") compilerArgs.Push(targetBaseName + ".6") for file := range files.Iter() { compilerArgs.Push(path.Join("../", file)) } if !executeCommand(compilerPath, compilerArgs.Data(), "igo-out/") { os.Exit(1) } // Pack var gopackArgs vector.StringVector gopackArgs.Push("grc") gopackArgs.Push(targetBaseName + ".a") gopackArgs.Push(targetBaseName + ".6") if !executeCommand(gopackPath, gopackArgs.Data(), "igo-out/") { os.Exit(1) } }
func Parse(line string) *Message { m := &Message{} matches := re.MatchStrings(line) if len(matches) != 4 { return nil } prefix, command, params := matches[1], matches[2], matches[3] if len(prefix) > 2 { m.Prefix = prefix[1 : len(prefix)-1] } m.Command = strings.ToUpper(command) paramArray := new(vector.StringVector) param := "" for i, c := range params { switch { case c == ' ': if param != "" { paramArray.Push(param) param = "" } case c == ':' && param == "": paramArray.Push(params[i+1 : len(params)]) goto done default: param += string(c) } } if param != "" { paramArray.Push(param) param = "" } done: m.Params = paramArray.Data() return m }
// executeCommand runs the specified tool with the supplied arguments (not // including the path to the tool itself), chdir'ing to the specified directory // first. It returns true if and only if the child process returns zero. func executeCommand(tool string, args []string, dir string) bool { fmt.Printf("%s %s\n", tool, strings.Join(args, " ")) var fullArgs vector.StringVector fullArgs.Push(tool) fullArgs.AppendVector(&args) pid, err := os.ForkExec( tool, fullArgs.Data(), os.Environ(), dir, []*os.File{os.Stdin, os.Stdout, os.Stderr}) if err != nil { panic(err) } waitMsg, err := os.Wait(pid, 0) if err != nil { panic(err) } return waitMsg.ExitStatus() == 0 }
func join(sv *vector.StringVector, sep string) string { return strings.Join(sv.Data(), sep) }
func main() { sqlite3.Initialize() defer func() { log.Stdout("closing sqlite3") sqlite3.Shutdown() }() dbh := new(sqlite3.Handle) dbh.Open("bbs.db") defer dbh.Close() InitTables(dbh) flag.Parse() templ := func() *template.Template { templateStr, err := io.ReadFile("tmpl/top.tmpl") if err != nil { log.Exit(err) } return template.MustParse(string(templateStr), nil) }() http.Handle("/", http.HandlerFunc(func(c *http.Conn, req *http.Request) { params := new(struct{ msgs []string }) storage := new(vector.StringVector) func() { st, err := dbh.Prepare("SELECT * from entry ORDER BY id DESC limit 30;") func() { if err != "" { log.Exit(err) } for { rv := st.Step() switch { case rv == sqlite3.SQLITE_DONE: return case rv == sqlite3.SQLITE_ROW: body := st.ColumnText(1) storage.Push(body) default: println(rv) log.Exit(dbh.ErrMsg()) } } }() if st.Finalize() != sqlite3.SQLITE_OK { log.Exit(dbh.ErrMsg()) } }() params.msgs = storage.Data() err := templ.Execute(params, c) if err != nil { log.Exit("templ.Execute:", err) } })) http.Handle("/post", http.HandlerFunc(func(c *http.Conn, req *http.Request) { req.ParseForm() body := req.Form["body"][0] st, err := dbh.Prepare("INSERT INTO entry (body) VALUES (?)") if err != "" { log.Exit(err) } if st.BindText(1, body) != sqlite3.SQLITE_OK { log.Exit("cannot bind: ", dbh.ErrMsg()) } if st.Step() != sqlite3.SQLITE_DONE { log.Exit(dbh.ErrMsg()) } if st.Finalize() != sqlite3.SQLITE_OK { log.Exit(dbh.ErrMsg()) } http.Redirect(c, "/", 302) })) http.Handle("/css/", http.HandlerFunc(func(c *http.Conn, req *http.Request) { http.ServeFile(c, req, "."+req.URL.Path) })) // run httpd func() { fmt.Printf("http://%s/\n", *addr) err := http.ListenAndServe(*addr, nil) if err != nil { log.Exit("ListenAndServe:", err) } }() }