| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "io/ioutil" | 9 "io/ioutil" |
| 10 "strings" | 10 "strings" |
| 11 "testing" | 11 "testing" |
| 12 | 12 |
| 13 "github.com/luci/luci-go/common/proto/google/descutil" |
| 14 |
| 13 "github.com/golang/protobuf/proto" | 15 "github.com/golang/protobuf/proto" |
| 14 | 16 |
| 15 » "github.com/luci/luci-go/common/proto/google/descriptor" | 17 » "google.golang.org/genproto/protobuf" |
| 16 | 18 |
| 17 . "github.com/smartystreets/goconvey/convey" | 19 . "github.com/smartystreets/goconvey/convey" |
| 18 ) | 20 ) |
| 19 | 21 |
| 20 func TestPrinter(t *testing.T) { | 22 func TestPrinter(t *testing.T) { |
| 21 t.Parallel() | 23 t.Parallel() |
| 22 | 24 |
| 23 Convey("Printer", t, func() { | 25 Convey("Printer", t, func() { |
| 24 protoFile, err := ioutil.ReadFile("printer_test.proto") | 26 protoFile, err := ioutil.ReadFile("printer_test.proto") |
| 25 So(err, ShouldBeNil) | 27 So(err, ShouldBeNil) |
| 26 protoFileLines := strings.Split(string(protoFile), "\n") | 28 protoFileLines := strings.Split(string(protoFile), "\n") |
| 27 | 29 |
| 28 descFileBytes, err := ioutil.ReadFile("printer_test.desc") | 30 descFileBytes, err := ioutil.ReadFile("printer_test.desc") |
| 29 So(err, ShouldBeNil) | 31 So(err, ShouldBeNil) |
| 30 | 32 |
| 31 var desc descriptor.FileDescriptorSet | 33 var desc descriptor.FileDescriptorSet |
| 32 err = proto.Unmarshal(descFileBytes, &desc) | 34 err = proto.Unmarshal(descFileBytes, &desc) |
| 33 So(err, ShouldBeNil) | 35 So(err, ShouldBeNil) |
| 34 | 36 |
| 35 So(desc.File, ShouldHaveLength, 1) | 37 So(desc.File, ShouldHaveLength, 1) |
| 36 file := desc.File[0] | 38 file := desc.File[0] |
| 37 | 39 |
| 38 getExpectedDef := func(path ...int) string { | 40 getExpectedDef := func(path ...int) string { |
| 39 » » » loc := file.SourceCodeInfo.FindLocation(path) | 41 » » » loc := descutil.FindLocation(file.SourceCodeInfo, path) |
| 40 So(loc, ShouldNotBeNil) | 42 So(loc, ShouldNotBeNil) |
| 41 | 43 |
| 42 startLine := loc.Span[0] | 44 startLine := loc.Span[0] |
| 43 endLine := startLine | 45 endLine := startLine |
| 44 if len(loc.Span) > 3 { | 46 if len(loc.Span) > 3 { |
| 45 endLine = loc.Span[2] | 47 endLine = loc.Span[2] |
| 46 } | 48 } |
| 47 | 49 |
| 48 for startLine > 0 && strings.HasPrefix(strings.TrimSpace
(protoFileLines[startLine-1]), "//") { | 50 for startLine > 0 && strings.HasPrefix(strings.TrimSpace
(protoFileLines[startLine-1]), "//") { |
| 49 startLine-- | 51 startLine-- |
| (...skipping 11 matching lines...) Expand all Loading... |
| 61 var buf bytes.Buffer | 63 var buf bytes.Buffer |
| 62 printer := newPrinter(&buf) | 64 printer := newPrinter(&buf) |
| 63 printer.File = file | 65 printer.File = file |
| 64 | 66 |
| 65 checkOutput := func(path ...int) { | 67 checkOutput := func(path ...int) { |
| 66 So(buf.String(), ShouldEqual, getExpectedDef(path...)) | 68 So(buf.String(), ShouldEqual, getExpectedDef(path...)) |
| 67 } | 69 } |
| 68 | 70 |
| 69 Convey("package", func() { | 71 Convey("package", func() { |
| 70 printer.Package(file.GetPackage()) | 72 printer.Package(file.GetPackage()) |
| 71 » » » checkOutput(descriptor.NumberFileDescriptorProto_Package
) | 73 » » » checkOutput(descutil.FileDescriptorProtoPackageTag) |
| 72 }) | 74 }) |
| 73 | 75 |
| 74 Convey("service", func() { | 76 Convey("service", func() { |
| 75 for i, s := range file.Service { | 77 for i, s := range file.Service { |
| 76 Convey(s.GetName(), func() { | 78 Convey(s.GetName(), func() { |
| 77 printer.Service(s, i, -1) | 79 printer.Service(s, i, -1) |
| 78 » » » » » checkOutput(descriptor.NumberFileDescrip
torProto_Service, i) | 80 » » » » » checkOutput(descutil.FileDescriptorProto
ServiceTag, i) |
| 79 }) | 81 }) |
| 80 } | 82 } |
| 81 }) | 83 }) |
| 82 | 84 |
| 83 testEnum := func(e *descriptor.EnumDescriptorProto, path []int)
{ | 85 testEnum := func(e *descriptor.EnumDescriptorProto, path []int)
{ |
| 84 Convey(e.GetName(), func() { | 86 Convey(e.GetName(), func() { |
| 85 printer.Enum(e, path) | 87 printer.Enum(e, path) |
| 86 checkOutput(path...) | 88 checkOutput(path...) |
| 87 }) | 89 }) |
| 88 } | 90 } |
| 89 | 91 |
| 90 Convey("enum", func() { | 92 Convey("enum", func() { |
| 91 for i, e := range file.EnumType { | 93 for i, e := range file.EnumType { |
| 92 » » » » testEnum(e, []int{descriptor.NumberFileDescripto
rProto_Enum, i}) | 94 » » » » testEnum(e, []int{descutil.FileDescriptorProtoEn
umTag, i}) |
| 93 } | 95 } |
| 94 }) | 96 }) |
| 95 | 97 |
| 96 Convey("message", func() { | 98 Convey("message", func() { |
| 97 var testMsg func(*descriptor.DescriptorProto, []int) | 99 var testMsg func(*descriptor.DescriptorProto, []int) |
| 98 testMsg = func(m *descriptor.DescriptorProto, path []int
) { | 100 testMsg = func(m *descriptor.DescriptorProto, path []int
) { |
| 99 Convey(m.GetName(), func() { | 101 Convey(m.GetName(), func() { |
| 100 if len(m.NestedType) == 0 && len(m.EnumT
ype) == 0 { | 102 if len(m.NestedType) == 0 && len(m.EnumT
ype) == 0 { |
| 101 printer.Message(m, path) | 103 printer.Message(m, path) |
| 102 checkOutput(path...) | 104 checkOutput(path...) |
| 103 } else { | 105 } else { |
| 104 for i, m := range m.NestedType { | 106 for i, m := range m.NestedType { |
| 105 » » » » » » » testMsg(m, append(path,
descriptor.NumberDescriptorProto_NestedType, i)) | 107 » » » » » » » testMsg(m, append(path,
descutil.DescriptorProtoNestedTypeTag, i)) |
| 106 } | 108 } |
| 107 for i, e := range m.EnumType { | 109 for i, e := range m.EnumType { |
| 108 » » » » » » » testEnum(e, append(path,
descriptor.NumberDescriptorProto_EnumType, i)) | 110 » » » » » » » testEnum(e, append(path,
descutil.DescriptorProtoEnumTypeTag, i)) |
| 109 } | 111 } |
| 110 } | 112 } |
| 111 }) | 113 }) |
| 112 } | 114 } |
| 113 for i, m := range file.MessageType { | 115 for i, m := range file.MessageType { |
| 114 » » » » testMsg(m, []int{descriptor.NumberFileDescriptor
Proto_Message, i}) | 116 » » » » testMsg(m, []int{descutil.FileDescriptorProtoMes
sageTag, i}) |
| 115 } | 117 } |
| 116 }) | 118 }) |
| 117 }) | 119 }) |
| 118 } | 120 } |
| OLD | NEW |