func TestStringBuffer(t *testing.T) { ss := stringbuffer.New() ss.Add("en") if ss.String() != "en" { t.Fatal(" stringbuffer.String() != 'en'\n") } ss.Add("to") if ss.String() != "ento" { t.Fatal(" stringbuffer.String() != 'ento'\n") } if ss.Len() != 4 { t.Fatal(" stringbuffer.Len() != 4\n") } ss.Add("øæå") // utf-8 multi-byte fun if ss.Len() != 10 { t.Fatal(" stringbuffer.Len() != 10\n") } if ss.String() != "entoøæå" { t.Fatal(" stringbuffer.String() != 'entoøæå'\n") } ss.ClearSize(2) if ss.Len() != 0 { t.Fatal(" stringbuffer.Len() != 0\n") } for i := 0; i < 20; i++ { if ss.Len() != i { t.Fatal(" stringbuffer.Len() != i") } ss.Add("a") } if ss.String() != "aaaaaaaaaaaaaaaaaaaa" { t.Fatal(" stringbuffer.String() != a * 20") } }
func (c *collector) String() string { sb := stringbuffer.New() sb.Add("\nimport(\n") for i := 0; i < len(c.deps); i++ { sb.Add(" " + c.deps[i] + "\n") } sb.Add(")\n\n") return sb.String() }
func Make(fname string, pkgs []*dag.Package, alien []string) { if handy.IsFile(fname) { modImport, iOk := hasModifiedImports(fname) if iOk { m[Imports] = modImport } modPlay, pOk := hasModifiedPlayground(fname) if pOk { m[Playground] = modPlay } if pOk || iOk { dn, fn := filepath.Split(fname) backupFname := filepath.Join(dn, "."+fn+".bak") e := os.Rename(fname, backupFname) if e != nil { log.Printf("[WARNING] failed to make backup of: %s\n", fname) } } } sb := stringbuffer.New() sb.Add(fmt.Sprintf(m[Header], time.UTC())) sb.Add(m[Imports]) sb.Add(m[Targets]) sb.Add("// PLAYGROUND START\n") sb.Add(m[Playground]) sb.Add("// PLAYGROUND STOP\n") sb.Add(m[Init]) for i := 0; i < len(alien); i++ { alien[i] = `"` + alien[i] + `"` } sb.Add(fmt.Sprintf(m[GoInstall], strings.Join(alien, ","))) sb.Add(m[Compile]) sb.Add(m[PackageDef]) sb.Add(m[PackageStart]) for i := 0; i < len(pkgs); i++ { sb.Add(pkgs[i].Rep()) } sb.Add("\n}\n") sb.Add(m[Main]) ioutil.WriteFile(fname, sb.Bytes(), 0644) }