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