Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Unified Diff: client/cmd/rpc/printer_test.go

Issue 1587323003: client/cmd/rpc: RPC CLI (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@discovery
Patch Set: check status Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: client/cmd/rpc/printer_test.go
diff --git a/client/cmd/rpc/printer_test.go b/client/cmd/rpc/printer_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..551c761df33566f946b3fd1b5aaa6189e14574f1
--- /dev/null
+++ b/client/cmd/rpc/printer_test.go
@@ -0,0 +1,118 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package main
+
+import (
+ "bytes"
+ "io/ioutil"
+ "strings"
+ "testing"
+
+ "github.com/golang/protobuf/proto"
+
+ "github.com/luci/luci-go/common/proto/google/descriptor"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+func TestPrinter(t *testing.T) {
+ t.Parallel()
+
+ Convey("Printer", t, func() {
+ protoFile, err := ioutil.ReadFile("printer_test.proto")
+ So(err, ShouldBeNil)
+ protoFileLines := strings.Split(string(protoFile), "\n")
+
+ descFileBytes, err := ioutil.ReadFile("printer_test.desc")
+ So(err, ShouldBeNil)
+
+ var desc descriptor.FileDescriptorSet
+ err = proto.Unmarshal(descFileBytes, &desc)
+ So(err, ShouldBeNil)
+
+ So(desc.File, ShouldHaveLength, 1)
+ file := desc.File[0]
+
+ getExpectedDef := func(path ...int) string {
+ loc := file.SourceCodeInfo.FindLocation(path)
+ So(loc, ShouldNotBeNil)
+
+ startLine := loc.Span[0]
+ endLine := startLine
+ if len(loc.Span) > 3 {
+ endLine = loc.Span[2]
+ }
+
+ for startLine > 0 && strings.HasPrefix(strings.TrimSpace(protoFileLines[startLine-1]), "//") {
+ startLine--
+ }
+
+ unindent := (len(path) - 2) / 2
+ expected := make([]string, endLine-startLine+1)
+ for i := 0; i < len(expected); i++ {
+ expected[i] = protoFileLines[int(startLine)+i][unindent:]
+ }
+
+ return strings.Join(expected, "\n") + "\n"
+ }
+
+ var buf bytes.Buffer
+ printer := newPrinter(&buf)
+ printer.File = file
+
+ checkOutput := func(path ...int) {
+ So(buf.String(), ShouldEqual, getExpectedDef(path...))
+ }
+
+ Convey("package", func() {
+ printer.Package(file.GetPackage())
+ checkOutput(descriptor.NumberFileDescriptorProto_Package)
+ })
+
+ Convey("service", func() {
+ for i, s := range file.Service {
+ Convey(s.GetName(), func() {
+ printer.Service(s, i, -1)
+ checkOutput(descriptor.NumberFileDescriptorProto_Service, i)
+ })
+ }
+ })
+
+ testEnum := func(e *descriptor.EnumDescriptorProto, path []int) {
+ Convey(e.GetName(), func() {
+ printer.Enum(e, path)
+ checkOutput(path...)
+ })
+ }
+
+ Convey("enum", func() {
+ for i, e := range file.EnumType {
+ testEnum(e, []int{descriptor.NumberFileDescriptorProto_Enum, i})
+ }
+ })
+
+ Convey("message", func() {
+ var testMsg func(*descriptor.DescriptorProto, []int)
+ testMsg = func(m *descriptor.DescriptorProto, path []int) {
+ Convey(m.GetName(), func() {
+ if len(m.NestedType) == 0 && len(m.EnumType) == 0 {
+ printer.Message(m, path)
+ checkOutput(path...)
+ } else {
+ for i, m := range m.NestedType {
+ testMsg(m, append(path, descriptor.NumberDescriptorProto_NestedType, i))
+ }
+ for i, e := range m.EnumType {
+ testEnum(e, append(path, descriptor.NumberDescriptorProto_EnumType, i))
+ }
+ }
+ })
+ }
+ for i, m := range file.MessageType {
+ testMsg(m, []int{descriptor.NumberFileDescriptorProto_Message, i})
+ }
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698