legacy_file_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package impl_test
  5. import (
  6. "bytes"
  7. "compress/gzip"
  8. "io/ioutil"
  9. "reflect"
  10. "testing"
  11. "github.com/google/go-cmp/cmp"
  12. "google.golang.org/protobuf/internal/impl"
  13. "google.golang.org/protobuf/internal/pragma"
  14. "google.golang.org/protobuf/proto"
  15. pdesc "google.golang.org/protobuf/reflect/protodesc"
  16. pref "google.golang.org/protobuf/reflect/protoreflect"
  17. proto2_20160225 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160225-2fc053c5"
  18. proto2_20160519 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v0.0.0-20160519-a4ab9ec5"
  19. proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152"
  20. proto2_20180430 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.1.0-20180430-b4deda09"
  21. proto2_20180814 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.2.0-20180814-aa810b61"
  22. proto2_20181126 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.2.1-20181126-8d0c54c1"
  23. proto3_20160225 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v0.0.0-20160225-2fc053c5"
  24. proto3_20160519 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v0.0.0-20160519-a4ab9ec5"
  25. proto3_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.0.0-20180125-92554152"
  26. proto3_20180430 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.1.0-20180430-b4deda09"
  27. proto3_20180814 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.2.0-20180814-aa810b61"
  28. proto3_20181126 "google.golang.org/protobuf/internal/testprotos/legacy/proto3.v1.2.1-20181126-8d0c54c1"
  29. "google.golang.org/protobuf/types/descriptorpb"
  30. )
  31. func mustLoadFileDesc(b []byte, _ []int) pref.FileDescriptor {
  32. zr, err := gzip.NewReader(bytes.NewReader(b))
  33. if err != nil {
  34. panic(err)
  35. }
  36. b, err = ioutil.ReadAll(zr)
  37. if err != nil {
  38. panic(err)
  39. }
  40. p := new(descriptorpb.FileDescriptorProto)
  41. err = proto.UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, p)
  42. if err != nil {
  43. panic(err)
  44. }
  45. fd, err := pdesc.NewFile(p, nil)
  46. if err != nil {
  47. panic(err)
  48. }
  49. return fd
  50. }
  51. func TestDescriptor(t *testing.T) {
  52. var tests []struct{ got, want pref.Descriptor }
  53. fileDescP2_20160225 := mustLoadFileDesc(new(proto2_20160225.Message).Descriptor())
  54. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  55. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160225.SiblingEnum(0))),
  56. want: fileDescP2_20160225.Enums().ByName("SiblingEnum"),
  57. }, {
  58. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160225.Message_ChildEnum(0))),
  59. want: fileDescP2_20160225.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  60. }, {
  61. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.SiblingMessage))),
  62. want: fileDescP2_20160225.Messages().ByName("SiblingMessage"),
  63. }, {
  64. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ChildMessage))),
  65. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  66. }, {
  67. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message))),
  68. want: fileDescP2_20160225.Messages().ByName("Message"),
  69. }, {
  70. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_NamedGroup))),
  71. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  72. }, {
  73. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_OptionalGroup))),
  74. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  75. }, {
  76. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_RequiredGroup))),
  77. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  78. }, {
  79. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_RepeatedGroup))),
  80. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  81. }, {
  82. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_OneofGroup))),
  83. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  84. }, {
  85. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ExtensionOptionalGroup))),
  86. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  87. }, {
  88. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160225.Message_ExtensionRepeatedGroup))),
  89. want: fileDescP2_20160225.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  90. }}...)
  91. fileDescP3_20160225 := mustLoadFileDesc(new(proto3_20160225.Message).Descriptor())
  92. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  93. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160225.SiblingEnum(0))),
  94. want: fileDescP3_20160225.Enums().ByName("SiblingEnum"),
  95. }, {
  96. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160225.Message_ChildEnum(0))),
  97. want: fileDescP3_20160225.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  98. }, {
  99. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.SiblingMessage))),
  100. want: fileDescP3_20160225.Messages().ByName("SiblingMessage"),
  101. }, {
  102. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.Message_ChildMessage))),
  103. want: fileDescP3_20160225.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  104. }, {
  105. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160225.Message))),
  106. want: fileDescP3_20160225.Messages().ByName("Message"),
  107. }}...)
  108. fileDescP2_20160519 := mustLoadFileDesc(new(proto2_20160519.Message).Descriptor())
  109. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  110. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160519.SiblingEnum(0))),
  111. want: fileDescP2_20160519.Enums().ByName("SiblingEnum"),
  112. }, {
  113. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20160519.Message_ChildEnum(0))),
  114. want: fileDescP2_20160519.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  115. }, {
  116. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.SiblingMessage))),
  117. want: fileDescP2_20160519.Messages().ByName("SiblingMessage"),
  118. }, {
  119. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ChildMessage))),
  120. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  121. }, {
  122. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message))),
  123. want: fileDescP2_20160519.Messages().ByName("Message"),
  124. }, {
  125. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_NamedGroup))),
  126. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  127. }, {
  128. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_OptionalGroup))),
  129. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  130. }, {
  131. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_RequiredGroup))),
  132. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  133. }, {
  134. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_RepeatedGroup))),
  135. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  136. }, {
  137. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_OneofGroup))),
  138. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  139. }, {
  140. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ExtensionOptionalGroup))),
  141. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  142. }, {
  143. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20160519.Message_ExtensionRepeatedGroup))),
  144. want: fileDescP2_20160519.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  145. }}...)
  146. fileDescP3_20160519 := mustLoadFileDesc(new(proto3_20160519.Message).Descriptor())
  147. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  148. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160519.SiblingEnum(0))),
  149. want: fileDescP3_20160519.Enums().ByName("SiblingEnum"),
  150. }, {
  151. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20160519.Message_ChildEnum(0))),
  152. want: fileDescP3_20160519.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  153. }, {
  154. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.SiblingMessage))),
  155. want: fileDescP3_20160519.Messages().ByName("SiblingMessage"),
  156. }, {
  157. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.Message_ChildMessage))),
  158. want: fileDescP3_20160519.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  159. }, {
  160. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20160519.Message))),
  161. want: fileDescP3_20160519.Messages().ByName("Message"),
  162. }}...)
  163. fileDescP2_20180125 := mustLoadFileDesc(new(proto2_20180125.Message).Descriptor())
  164. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  165. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180125.SiblingEnum(0))),
  166. want: fileDescP2_20180125.Enums().ByName("SiblingEnum"),
  167. }, {
  168. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180125.Message_ChildEnum(0))),
  169. want: fileDescP2_20180125.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  170. }, {
  171. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.SiblingMessage))),
  172. want: fileDescP2_20180125.Messages().ByName("SiblingMessage"),
  173. }, {
  174. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ChildMessage))),
  175. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  176. }, {
  177. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message))),
  178. want: fileDescP2_20180125.Messages().ByName("Message"),
  179. }, {
  180. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_NamedGroup))),
  181. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  182. }, {
  183. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_OptionalGroup))),
  184. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  185. }, {
  186. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_RequiredGroup))),
  187. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  188. }, {
  189. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_RepeatedGroup))),
  190. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  191. }, {
  192. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_OneofGroup))),
  193. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  194. }, {
  195. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ExtensionOptionalGroup))),
  196. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  197. }, {
  198. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180125.Message_ExtensionRepeatedGroup))),
  199. want: fileDescP2_20180125.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  200. }}...)
  201. fileDescP3_20180125 := mustLoadFileDesc(new(proto3_20180125.Message).Descriptor())
  202. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  203. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180125.SiblingEnum(0))),
  204. want: fileDescP3_20180125.Enums().ByName("SiblingEnum"),
  205. }, {
  206. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180125.Message_ChildEnum(0))),
  207. want: fileDescP3_20180125.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  208. }, {
  209. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.SiblingMessage))),
  210. want: fileDescP3_20180125.Messages().ByName("SiblingMessage"),
  211. }, {
  212. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.Message_ChildMessage))),
  213. want: fileDescP3_20180125.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  214. }, {
  215. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180125.Message))),
  216. want: fileDescP3_20180125.Messages().ByName("Message"),
  217. }}...)
  218. fileDescP2_20180430 := mustLoadFileDesc(new(proto2_20180430.Message).Descriptor())
  219. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  220. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180430.SiblingEnum(0))),
  221. want: fileDescP2_20180430.Enums().ByName("SiblingEnum"),
  222. }, {
  223. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180430.Message_ChildEnum(0))),
  224. want: fileDescP2_20180430.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  225. }, {
  226. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.SiblingMessage))),
  227. want: fileDescP2_20180430.Messages().ByName("SiblingMessage"),
  228. }, {
  229. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ChildMessage))),
  230. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  231. }, {
  232. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message))),
  233. want: fileDescP2_20180430.Messages().ByName("Message"),
  234. }, {
  235. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_NamedGroup))),
  236. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  237. }, {
  238. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_OptionalGroup))),
  239. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  240. }, {
  241. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_RequiredGroup))),
  242. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  243. }, {
  244. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_RepeatedGroup))),
  245. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  246. }, {
  247. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_OneofGroup))),
  248. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  249. }, {
  250. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ExtensionOptionalGroup))),
  251. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  252. }, {
  253. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180430.Message_ExtensionRepeatedGroup))),
  254. want: fileDescP2_20180430.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  255. }}...)
  256. fileDescP3_20180430 := mustLoadFileDesc(new(proto3_20180430.Message).Descriptor())
  257. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  258. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180430.SiblingEnum(0))),
  259. want: fileDescP3_20180430.Enums().ByName("SiblingEnum"),
  260. }, {
  261. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180430.Message_ChildEnum(0))),
  262. want: fileDescP3_20180430.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  263. }, {
  264. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.SiblingMessage))),
  265. want: fileDescP3_20180430.Messages().ByName("SiblingMessage"),
  266. }, {
  267. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.Message_ChildMessage))),
  268. want: fileDescP3_20180430.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  269. }, {
  270. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180430.Message))),
  271. want: fileDescP3_20180430.Messages().ByName("Message"),
  272. }}...)
  273. fileDescP2_20180814 := mustLoadFileDesc(new(proto2_20180814.Message).Descriptor())
  274. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  275. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180814.SiblingEnum(0))),
  276. want: fileDescP2_20180814.Enums().ByName("SiblingEnum"),
  277. }, {
  278. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20180814.Message_ChildEnum(0))),
  279. want: fileDescP2_20180814.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  280. }, {
  281. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.SiblingMessage))),
  282. want: fileDescP2_20180814.Messages().ByName("SiblingMessage"),
  283. }, {
  284. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ChildMessage))),
  285. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  286. }, {
  287. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message))),
  288. want: fileDescP2_20180814.Messages().ByName("Message"),
  289. }, {
  290. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_NamedGroup))),
  291. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  292. }, {
  293. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_OptionalGroup))),
  294. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  295. }, {
  296. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_RequiredGroup))),
  297. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  298. }, {
  299. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_RepeatedGroup))),
  300. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  301. }, {
  302. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_OneofGroup))),
  303. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  304. }, {
  305. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ExtensionOptionalGroup))),
  306. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  307. }, {
  308. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20180814.Message_ExtensionRepeatedGroup))),
  309. want: fileDescP2_20180814.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  310. }}...)
  311. fileDescP3_20180814 := mustLoadFileDesc(new(proto3_20180814.Message).Descriptor())
  312. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  313. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180814.SiblingEnum(0))),
  314. want: fileDescP3_20180814.Enums().ByName("SiblingEnum"),
  315. }, {
  316. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20180814.Message_ChildEnum(0))),
  317. want: fileDescP3_20180814.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  318. }, {
  319. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.SiblingMessage))),
  320. want: fileDescP3_20180814.Messages().ByName("SiblingMessage"),
  321. }, {
  322. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.Message_ChildMessage))),
  323. want: fileDescP3_20180814.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  324. }, {
  325. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20180814.Message))),
  326. want: fileDescP3_20180814.Messages().ByName("Message"),
  327. }}...)
  328. fileDescP2_20181126 := mustLoadFileDesc(new(proto2_20181126.Message).Descriptor())
  329. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  330. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20181126.SiblingEnum(0))),
  331. want: fileDescP2_20181126.Enums().ByName("SiblingEnum"),
  332. }, {
  333. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto2_20181126.Message_ChildEnum(0))),
  334. want: fileDescP2_20181126.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  335. }, {
  336. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.SiblingMessage))),
  337. want: fileDescP2_20181126.Messages().ByName("SiblingMessage"),
  338. }, {
  339. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_ChildMessage))),
  340. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  341. }, {
  342. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message))),
  343. want: fileDescP2_20181126.Messages().ByName("Message"),
  344. }, {
  345. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_NamedGroup))),
  346. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("NamedGroup"),
  347. }, {
  348. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_OptionalGroup))),
  349. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("OptionalGroup"),
  350. }, {
  351. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_RequiredGroup))),
  352. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("RequiredGroup"),
  353. }, {
  354. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_RepeatedGroup))),
  355. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("RepeatedGroup"),
  356. }, {
  357. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_OneofGroup))),
  358. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("OneofGroup"),
  359. }, {
  360. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_ExtensionOptionalGroup))),
  361. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("ExtensionOptionalGroup"),
  362. }, {
  363. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto2_20181126.Message_ExtensionRepeatedGroup))),
  364. want: fileDescP2_20181126.Messages().ByName("Message").Messages().ByName("ExtensionRepeatedGroup"),
  365. }}...)
  366. fileDescP3_20181126 := mustLoadFileDesc(new(proto3_20181126.Message).Descriptor())
  367. tests = append(tests, []struct{ got, want pref.Descriptor }{{
  368. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20181126.SiblingEnum(0))),
  369. want: fileDescP3_20181126.Enums().ByName("SiblingEnum"),
  370. }, {
  371. got: impl.LegacyLoadEnumDesc(reflect.TypeOf(proto3_20181126.Message_ChildEnum(0))),
  372. want: fileDescP3_20181126.Messages().ByName("Message").Enums().ByName("ChildEnum"),
  373. }, {
  374. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20181126.SiblingMessage))),
  375. want: fileDescP3_20181126.Messages().ByName("SiblingMessage"),
  376. }, {
  377. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20181126.Message_ChildMessage))),
  378. want: fileDescP3_20181126.Messages().ByName("Message").Messages().ByName("ChildMessage"),
  379. }, {
  380. got: impl.LegacyLoadMessageDesc(reflect.TypeOf(new(proto3_20181126.Message))),
  381. want: fileDescP3_20181126.Messages().ByName("Message"),
  382. }}...)
  383. // TODO: We need a test package to compare descriptors.
  384. type list interface {
  385. Len() int
  386. pragma.DoNotImplement
  387. }
  388. opts := cmp.Options{
  389. cmp.Transformer("", func(x list) []interface{} {
  390. out := make([]interface{}, x.Len())
  391. v := reflect.ValueOf(x)
  392. for i := 0; i < x.Len(); i++ {
  393. m := v.MethodByName("Get")
  394. out[i] = m.Call([]reflect.Value{reflect.ValueOf(i)})[0].Interface()
  395. }
  396. return out
  397. }),
  398. cmp.Transformer("", func(x pref.Descriptor) map[string]interface{} {
  399. out := make(map[string]interface{})
  400. v := reflect.ValueOf(x)
  401. for i := 0; i < v.NumMethod(); i++ {
  402. name := v.Type().Method(i).Name
  403. if m := v.Method(i); m.Type().NumIn() == 0 && m.Type().NumOut() == 1 {
  404. switch name {
  405. case "ParentFile", "Parent":
  406. // Ignore parents to avoid recursive cycle.
  407. case "Index":
  408. // Ignore index since legacy descriptors have no parent.
  409. case "Options":
  410. // Ignore descriptor options since protos are not cmperable.
  411. case "Enums", "Messages", "Extensions":
  412. // Ignore nested message and enum declarations since
  413. // legacy descriptors are all created standalone.
  414. case "HasJSONName":
  415. // Ignore this since the semantics of the field has
  416. // changed across protoc and protoc-gen-go releases.
  417. case "ContainingOneof", "ContainingMessage", "Enum", "Message":
  418. // Avoid descending into a dependency to avoid a cycle.
  419. // Just record the full name if available.
  420. //
  421. // TODO: Cycle support in cmp would be useful here.
  422. v := m.Call(nil)[0]
  423. if !v.IsNil() {
  424. out[name] = v.Interface().(pref.Descriptor).FullName()
  425. }
  426. default:
  427. out[name] = m.Call(nil)[0].Interface()
  428. }
  429. }
  430. }
  431. return out
  432. }),
  433. cmp.Transformer("", func(v pref.Value) interface{} {
  434. return v.Interface()
  435. }),
  436. }
  437. for _, tt := range tests {
  438. t.Run(string(tt.want.FullName()), func(t *testing.T) {
  439. if diff := cmp.Diff(&tt.want, &tt.got, opts); diff != "" {
  440. t.Errorf("descriptor mismatch (-want, +got):\n%s", diff)
  441. }
  442. })
  443. }
  444. }