Exemple #1
0
func TestStringEscaping(t *testing.T) {
	testCases := []struct {
		in  *pb.Strings
		out string
	}{
		{
			// Test data from C++ test (TextFormatTest.StringEscape).
			// Single divergence: we don't escape apostrophes.
			&pb.Strings{StringField: proto.String("\"A string with ' characters \n and \r newlines and \t tabs and \001 slashes \\ and  multiple   spaces")},
			"string_field: \"\\\"A string with ' characters \\n and \\r newlines and \\t tabs and \\001 slashes \\\\ and  multiple   spaces\"\n",
		},
		{
			// Test data from the same C++ test.
			&pb.Strings{StringField: proto.String("\350\260\267\346\255\214")},
			"string_field: \"\\350\\260\\267\\346\\255\\214\"\n",
		},
	}

	for i, tc := range testCases {
		var buf bytes.Buffer
		proto.MarshalText(&buf, tc.in)
		if s := buf.String(); s != tc.out {
			t.Errorf("#%d: Got:\n%s\nExpected:\n%s\n", i, s, tc.out)
		}
	}
}
Exemple #2
0
func TestMarshalTextFull(t *testing.T) {
	buf := new(bytes.Buffer)
	proto.MarshalText(buf, newTestMessage())
	s := buf.String()
	if s != text {
		t.Errorf("Got:\n===\n%v===\nExpected:\n===\n%v===\n", s, text)
	}
}
Exemple #3
0
func main() {
	flag.Usage = usage
	flag.Parse()
	if *helpShort || *helpLong || flag.NArg() == 0 {
		flag.Usage()
		os.Exit(1)
	}

	fds, err := parser.ParseFiles(flag.Args(), strings.Split(*importPath, ",", -1))
	if err != nil {
		log.Exitf("Failed parsing: %v", err)
	}
	resolver.ResolveSymbols(fds)
	fmt.Println("-----")
	proto.MarshalText(os.Stdout, fds)
	fmt.Println("-----")

	// Find plugin.
	pluginPath := fullPath(*pluginBinary, strings.Split(os.Getenv("PATH"), ":", -1))
	if pluginPath == "" {
		log.Exitf("Failed finding plugin binary %q", *pluginBinary)
	}

	// Start plugin subprocess.
	pluginIn, meOut, err := os.Pipe()
	if err != nil {
		log.Exitf("Failed creating pipe: %v", err)
	}
	meIn, pluginOut, err := os.Pipe()
	if err != nil {
		log.Exitf("Failed creating pipe: %v", err)
	}
	pid, err := os.ForkExec(pluginPath, nil, nil, "/", []*os.File{pluginIn, pluginOut, os.Stderr})
	if err != nil {
		log.Exitf("Failed forking plugin: %v", err)
	}
	pluginIn.Close()
	pluginOut.Close()

	// Send request.
	cgRequest := &plugin.CodeGeneratorRequest{
		FileToGenerate: flag.Args(),
		// TODO: proto_file should be topologically sorted (bottom-up)
		ProtoFile: fds.File,
	}
	buf, err := proto.Marshal(cgRequest)
	if err != nil {
		log.Exitf("Failed marshaling CG request: %v", err)
	}
	_, err = meOut.Write(buf)
	if err != nil {
		log.Exitf("Failed writing CG request: %v", err)
	}
	meOut.Close()

	w, err := os.Wait(pid, 0)
	if err != nil {
		log.Exitf("Failed waiting for plugin: %v", err)
	}
	if w.ExitStatus() != 0 {
		log.Exitf("Plugin exited with status %d", w.ExitStatus())
	}

	// Read response.
	cgResponse := new(plugin.CodeGeneratorResponse)
	if buf, err = ioutil.ReadAll(meIn); err != nil {
		log.Exitf("Failed reading CG response: %v", err)
	}
	if err = proto.Unmarshal(buf, cgResponse); err != nil {
		log.Exitf("Failed unmarshaling CG response: %v", err)
	}

	// TODO: check cgResponse.Error

	// TODO: write files
	for _, f := range cgResponse.File {
		fmt.Printf("--[ %v ]--\n", proto.GetString(f.Name))
		fmt.Println(proto.GetString(f.Content))
	}
	fmt.Println("-----")
}
Exemple #4
0
func PrintToString(pb interface{}) string {
	buf := new(bytes.Buffer)
	proto.MarshalText(buf, pb)
	return buf.String()
}