deepcopy_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. package generators
  2. import (
  3. "reflect"
  4. "testing"
  5. "go-common/app/tool/gengo/types"
  6. )
  7. func Test_isRootedUnder(t *testing.T) {
  8. testCases := []struct {
  9. path string
  10. roots []string
  11. expect bool
  12. }{
  13. {
  14. path: "/foo/bar",
  15. roots: nil,
  16. expect: false,
  17. },
  18. {
  19. path: "/foo/bar",
  20. roots: []string{},
  21. expect: false,
  22. },
  23. {
  24. path: "/foo/bar",
  25. roots: []string{
  26. "/bad",
  27. },
  28. expect: false,
  29. },
  30. {
  31. path: "/foo/bar",
  32. roots: []string{
  33. "/foo",
  34. },
  35. expect: true,
  36. },
  37. {
  38. path: "/foo/bar",
  39. roots: []string{
  40. "/bad",
  41. "/foo",
  42. },
  43. expect: true,
  44. },
  45. {
  46. path: "/foo/bar/qux/zorb",
  47. roots: []string{
  48. "/foo/bar/qux",
  49. },
  50. expect: true,
  51. },
  52. {
  53. path: "/foo/bar",
  54. roots: []string{
  55. "/foo/bar",
  56. },
  57. expect: true,
  58. },
  59. {
  60. path: "/foo/barn",
  61. roots: []string{
  62. "/foo/bar",
  63. },
  64. expect: false,
  65. },
  66. {
  67. path: "/foo/bar",
  68. roots: []string{
  69. "/foo/barn",
  70. },
  71. expect: false,
  72. },
  73. {
  74. path: "/foo/bar",
  75. roots: []string{
  76. "",
  77. },
  78. expect: true,
  79. },
  80. }
  81. for i, tc := range testCases {
  82. r := isRootedUnder(tc.path, tc.roots)
  83. if r != tc.expect {
  84. t.Errorf("case[%d]: expected %t, got %t for %q in %q", i, tc.expect, r, tc.path, tc.roots)
  85. }
  86. }
  87. }
  88. func Test_deepCopyMethod(t *testing.T) {
  89. testCases := []struct {
  90. typ types.Type
  91. expect bool
  92. error bool
  93. }{
  94. {
  95. typ: types.Type{
  96. Name: types.Name{Package: "pkgname", Name: "typename"},
  97. Kind: types.Builtin,
  98. // No DeepCopy method.
  99. Methods: map[string]*types.Type{},
  100. },
  101. expect: false,
  102. },
  103. {
  104. typ: types.Type{
  105. Name: types.Name{Package: "pkgname", Name: "typename"},
  106. Kind: types.Builtin,
  107. Methods: map[string]*types.Type{
  108. // No DeepCopy method.
  109. "method": {
  110. Name: types.Name{Package: "pkgname", Name: "func()"},
  111. Kind: types.Func,
  112. Signature: &types.Signature{
  113. Receiver: &types.Type{
  114. Kind: types.Pointer,
  115. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  116. },
  117. Parameters: []*types.Type{},
  118. Results: []*types.Type{},
  119. },
  120. },
  121. },
  122. },
  123. expect: false,
  124. },
  125. {
  126. typ: types.Type{
  127. Name: types.Name{Package: "pkgname", Name: "typename"},
  128. Kind: types.Builtin,
  129. Methods: map[string]*types.Type{
  130. // Wrong signature (no result).
  131. "DeepCopy": {
  132. Name: types.Name{Package: "pkgname", Name: "func()"},
  133. Kind: types.Func,
  134. Signature: &types.Signature{
  135. Receiver: &types.Type{
  136. Kind: types.Pointer,
  137. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  138. },
  139. Parameters: []*types.Type{},
  140. Results: []*types.Type{},
  141. },
  142. },
  143. },
  144. },
  145. expect: false,
  146. error: true,
  147. },
  148. {
  149. typ: types.Type{
  150. Name: types.Name{Package: "pkgname", Name: "typename"},
  151. Kind: types.Builtin,
  152. Methods: map[string]*types.Type{
  153. // Wrong signature (wrong result).
  154. "DeepCopy": {
  155. Name: types.Name{Package: "pkgname", Name: "func() int"},
  156. Kind: types.Func,
  157. Signature: &types.Signature{
  158. Receiver: &types.Type{
  159. Kind: types.Pointer,
  160. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  161. },
  162. Parameters: []*types.Type{},
  163. Results: []*types.Type{
  164. {
  165. Name: types.Name{Name: "int"},
  166. Kind: types.Builtin,
  167. },
  168. },
  169. },
  170. },
  171. },
  172. },
  173. expect: false,
  174. error: true,
  175. },
  176. {
  177. typ: types.Type{
  178. Name: types.Name{Package: "pkgname", Name: "typename"},
  179. Kind: types.Builtin,
  180. Methods: map[string]*types.Type{
  181. // Wrong signature with pointer receiver, but non-pointer result.
  182. "DeepCopy": {
  183. Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
  184. Kind: types.Func,
  185. Signature: &types.Signature{
  186. Receiver: &types.Type{
  187. Kind: types.Pointer,
  188. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  189. },
  190. Parameters: []*types.Type{},
  191. Results: []*types.Type{
  192. {
  193. Name: types.Name{Package: "pkgname", Name: "typename"},
  194. Kind: types.Builtin,
  195. },
  196. },
  197. },
  198. },
  199. },
  200. },
  201. expect: false,
  202. error: true,
  203. },
  204. {
  205. typ: types.Type{
  206. Name: types.Name{Package: "pkgname", Name: "typename"},
  207. Kind: types.Builtin,
  208. Methods: map[string]*types.Type{
  209. // Wrong signature with non-pointer receiver, but pointer result.
  210. "DeepCopy": {
  211. Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
  212. Kind: types.Func,
  213. Signature: &types.Signature{
  214. Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  215. Parameters: []*types.Type{},
  216. Results: []*types.Type{
  217. {
  218. Kind: types.Pointer,
  219. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  220. },
  221. },
  222. },
  223. },
  224. },
  225. },
  226. expect: false,
  227. error: true,
  228. },
  229. {
  230. typ: types.Type{
  231. Name: types.Name{Package: "pkgname", Name: "typename"},
  232. Kind: types.Builtin,
  233. Methods: map[string]*types.Type{
  234. // Correct signature with non-pointer receiver.
  235. "DeepCopy": {
  236. Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
  237. Kind: types.Func,
  238. Signature: &types.Signature{
  239. Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  240. Parameters: []*types.Type{},
  241. Results: []*types.Type{
  242. {
  243. Name: types.Name{Package: "pkgname", Name: "typename"},
  244. Kind: types.Builtin,
  245. },
  246. },
  247. },
  248. },
  249. },
  250. },
  251. expect: true,
  252. },
  253. {
  254. typ: types.Type{
  255. Name: types.Name{Package: "pkgname", Name: "typename"},
  256. Kind: types.Builtin,
  257. Methods: map[string]*types.Type{
  258. // Correct signature with pointer receiver.
  259. "DeepCopy": {
  260. Name: types.Name{Package: "pkgname", Name: "func() pkgname.typename"},
  261. Kind: types.Func,
  262. Signature: &types.Signature{
  263. Receiver: &types.Type{
  264. Kind: types.Pointer,
  265. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  266. },
  267. Parameters: []*types.Type{},
  268. Results: []*types.Type{
  269. {
  270. Kind: types.Pointer,
  271. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  272. },
  273. },
  274. },
  275. },
  276. },
  277. },
  278. expect: true,
  279. },
  280. {
  281. typ: types.Type{
  282. Name: types.Name{Package: "pkgname", Name: "typename"},
  283. Kind: types.Builtin,
  284. Methods: map[string]*types.Type{
  285. // Wrong signature (has params).
  286. "DeepCopy": {
  287. Name: types.Name{Package: "pkgname", Name: "func(int) pkgname.typename"},
  288. Kind: types.Func,
  289. Signature: &types.Signature{
  290. Receiver: &types.Type{
  291. Kind: types.Pointer,
  292. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  293. },
  294. Parameters: []*types.Type{
  295. {
  296. Name: types.Name{Name: "int"},
  297. Kind: types.Builtin,
  298. },
  299. },
  300. Results: []*types.Type{
  301. {
  302. Name: types.Name{Package: "pkgname", Name: "typename"},
  303. Kind: types.Builtin,
  304. },
  305. },
  306. },
  307. },
  308. },
  309. },
  310. expect: false,
  311. error: true,
  312. },
  313. {
  314. typ: types.Type{
  315. Name: types.Name{Package: "pkgname", Name: "typename"},
  316. Kind: types.Builtin,
  317. Methods: map[string]*types.Type{
  318. // Wrong signature (extra results).
  319. "DeepCopy": {
  320. Name: types.Name{Package: "pkgname", Name: "func() (pkgname.typename, int)"},
  321. Kind: types.Func,
  322. Signature: &types.Signature{
  323. Receiver: &types.Type{
  324. Kind: types.Pointer,
  325. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  326. },
  327. Parameters: []*types.Type{},
  328. Results: []*types.Type{
  329. {
  330. Name: types.Name{Package: "pkgname", Name: "typename"},
  331. Kind: types.Builtin,
  332. },
  333. {
  334. Name: types.Name{Name: "int"},
  335. Kind: types.Builtin,
  336. },
  337. },
  338. },
  339. },
  340. },
  341. },
  342. expect: false,
  343. error: true,
  344. },
  345. }
  346. for i, tc := range testCases {
  347. r, err := deepCopyMethod(&tc.typ)
  348. if tc.error && err == nil {
  349. t.Errorf("case[%d]: expected an error, got none", i)
  350. } else if !tc.error && err != nil {
  351. t.Errorf("case[%d]: expected no error, got: %v", i, err)
  352. } else if !tc.error && (r != nil) != tc.expect {
  353. t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
  354. }
  355. }
  356. }
  357. func Test_deepCopyIntoMethod(t *testing.T) {
  358. testCases := []struct {
  359. typ types.Type
  360. expect bool
  361. error bool
  362. }{
  363. {
  364. typ: types.Type{
  365. Name: types.Name{Package: "pkgname", Name: "typename"},
  366. Kind: types.Builtin,
  367. // No DeepCopyInto method.
  368. Methods: map[string]*types.Type{},
  369. },
  370. expect: false,
  371. },
  372. {
  373. typ: types.Type{
  374. Name: types.Name{Package: "pkgname", Name: "typename"},
  375. Kind: types.Builtin,
  376. Methods: map[string]*types.Type{
  377. // No DeepCopyInto method.
  378. "method": {
  379. Name: types.Name{Package: "pkgname", Name: "func()"},
  380. Kind: types.Func,
  381. Signature: &types.Signature{
  382. Receiver: &types.Type{
  383. Kind: types.Pointer,
  384. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  385. },
  386. Parameters: []*types.Type{},
  387. Results: []*types.Type{},
  388. },
  389. },
  390. },
  391. },
  392. expect: false,
  393. },
  394. {
  395. typ: types.Type{
  396. Name: types.Name{Package: "pkgname", Name: "typename"},
  397. Kind: types.Builtin,
  398. Methods: map[string]*types.Type{
  399. // Wrong signature (no parameter).
  400. "DeepCopyInto": {
  401. Name: types.Name{Package: "pkgname", Name: "func()"},
  402. Kind: types.Func,
  403. Signature: &types.Signature{
  404. Receiver: &types.Type{
  405. Kind: types.Pointer,
  406. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  407. },
  408. Parameters: []*types.Type{},
  409. Results: []*types.Type{},
  410. },
  411. },
  412. },
  413. },
  414. expect: false,
  415. error: true,
  416. },
  417. {
  418. typ: types.Type{
  419. Name: types.Name{Package: "pkgname", Name: "typename"},
  420. Kind: types.Builtin,
  421. Methods: map[string]*types.Type{
  422. // Wrong signature (unexpected result).
  423. "DeepCopyInto": {
  424. Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename) int"},
  425. Kind: types.Func,
  426. Signature: &types.Signature{
  427. Receiver: &types.Type{
  428. Kind: types.Pointer,
  429. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  430. },
  431. Parameters: []*types.Type{
  432. {
  433. Kind: types.Pointer,
  434. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  435. },
  436. },
  437. Results: []*types.Type{
  438. {
  439. Name: types.Name{Name: "int"},
  440. Kind: types.Builtin,
  441. },
  442. },
  443. },
  444. },
  445. },
  446. },
  447. expect: false,
  448. error: true,
  449. },
  450. {
  451. typ: types.Type{
  452. Name: types.Name{Package: "pkgname", Name: "typename"},
  453. Kind: types.Builtin,
  454. Methods: map[string]*types.Type{
  455. // Wrong signature (non-pointer parameter, pointer receiver).
  456. "DeepCopyInto": {
  457. Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
  458. Kind: types.Func,
  459. Signature: &types.Signature{
  460. Receiver: &types.Type{
  461. Kind: types.Pointer,
  462. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  463. },
  464. Parameters: []*types.Type{
  465. {Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  466. },
  467. Results: []*types.Type{},
  468. },
  469. },
  470. },
  471. },
  472. expect: false,
  473. error: true,
  474. },
  475. {
  476. typ: types.Type{
  477. Name: types.Name{Package: "pkgname", Name: "typename"},
  478. Kind: types.Builtin,
  479. Methods: map[string]*types.Type{
  480. // Wrong signature (non-pointer parameter, non-pointer receiver).
  481. "DeepCopyInto": {
  482. Name: types.Name{Package: "pkgname", Name: "func(pkgname.typename)"},
  483. Kind: types.Func,
  484. Signature: &types.Signature{
  485. Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  486. Parameters: []*types.Type{
  487. {Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  488. },
  489. Results: []*types.Type{},
  490. },
  491. },
  492. },
  493. },
  494. expect: false,
  495. error: true,
  496. },
  497. {
  498. typ: types.Type{
  499. Name: types.Name{Package: "pkgname", Name: "typename"},
  500. Kind: types.Builtin,
  501. Methods: map[string]*types.Type{
  502. // Correct signature with non-pointer receiver.
  503. "DeepCopyInto": {
  504. Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
  505. Kind: types.Func,
  506. Signature: &types.Signature{
  507. Receiver: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  508. Parameters: []*types.Type{
  509. {
  510. Kind: types.Pointer,
  511. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  512. },
  513. },
  514. Results: []*types.Type{},
  515. },
  516. },
  517. },
  518. },
  519. expect: true,
  520. },
  521. {
  522. typ: types.Type{
  523. Name: types.Name{Package: "pkgname", Name: "typename"},
  524. Kind: types.Builtin,
  525. Methods: map[string]*types.Type{
  526. // Correct signature with pointer receiver.
  527. "DeepCopyInto": {
  528. Name: types.Name{Package: "pkgname", Name: "func(*pkgname.typename)"},
  529. Kind: types.Func,
  530. Signature: &types.Signature{
  531. Receiver: &types.Type{
  532. Kind: types.Pointer,
  533. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  534. },
  535. Parameters: []*types.Type{
  536. {
  537. Kind: types.Pointer,
  538. Elem: &types.Type{Kind: types.Struct, Name: types.Name{Package: "pkgname", Name: "typename"}},
  539. },
  540. },
  541. Results: []*types.Type{},
  542. },
  543. },
  544. },
  545. },
  546. expect: true,
  547. },
  548. }
  549. for i, tc := range testCases {
  550. r, err := deepCopyIntoMethod(&tc.typ)
  551. if tc.error && err == nil {
  552. t.Errorf("case[%d]: expected an error, got none", i)
  553. } else if !tc.error && err != nil {
  554. t.Errorf("case[%d]: expected no error, got: %v", i, err)
  555. } else if !tc.error && (r != nil) != tc.expect {
  556. t.Errorf("case[%d]: expected result %v, got: %v", i, tc.expect, r)
  557. }
  558. }
  559. }
  560. func Test_extractTagParams(t *testing.T) {
  561. testCases := []struct {
  562. comments []string
  563. expect *tagValue
  564. }{
  565. {
  566. comments: []string{
  567. "Human comment",
  568. },
  569. expect: nil,
  570. },
  571. {
  572. comments: []string{
  573. "Human comment",
  574. "+bili:deepcopy-gen",
  575. },
  576. expect: &tagValue{
  577. value: "",
  578. register: false,
  579. },
  580. },
  581. {
  582. comments: []string{
  583. "Human comment",
  584. "+bili:deepcopy-gen=package",
  585. },
  586. expect: &tagValue{
  587. value: "package",
  588. register: false,
  589. },
  590. },
  591. {
  592. comments: []string{
  593. "Human comment",
  594. "+bili:deepcopy-gen=package,register",
  595. },
  596. expect: &tagValue{
  597. value: "package",
  598. register: true,
  599. },
  600. },
  601. {
  602. comments: []string{
  603. "Human comment",
  604. "+bili:deepcopy-gen=package,register=true",
  605. },
  606. expect: &tagValue{
  607. value: "package",
  608. register: true,
  609. },
  610. },
  611. {
  612. comments: []string{
  613. "Human comment",
  614. "+bili:deepcopy-gen=package,register=false",
  615. },
  616. expect: &tagValue{
  617. value: "package",
  618. register: false,
  619. },
  620. },
  621. }
  622. for i, tc := range testCases {
  623. r := extractTag(tc.comments)
  624. if r == nil && tc.expect != nil {
  625. t.Errorf("case[%d]: expected non-nil", i)
  626. }
  627. if r != nil && tc.expect == nil {
  628. t.Errorf("case[%d]: expected nil, got %v", i, *r)
  629. }
  630. if r != nil && *r != *tc.expect {
  631. t.Errorf("case[%d]: expected %v, got %v", i, *tc.expect, *r)
  632. }
  633. }
  634. }
  635. func Test_extractInterfacesTag(t *testing.T) {
  636. testCases := []struct {
  637. comments []string
  638. expect []string
  639. }{
  640. {
  641. comments: []string{},
  642. expect: nil,
  643. },
  644. {
  645. comments: []string{
  646. "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
  647. },
  648. expect: []string{
  649. "k8s.io/kubernetes/runtime.Object",
  650. },
  651. },
  652. {
  653. comments: []string{
  654. "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
  655. "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.List",
  656. },
  657. expect: []string{
  658. "k8s.io/kubernetes/runtime.Object",
  659. "k8s.io/kubernetes/runtime.List",
  660. },
  661. },
  662. {
  663. comments: []string{
  664. "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
  665. "+bili:deepcopy-gen:interfaces=k8s.io/kubernetes/runtime.Object",
  666. },
  667. expect: []string{
  668. "k8s.io/kubernetes/runtime.Object",
  669. "k8s.io/kubernetes/runtime.Object",
  670. },
  671. },
  672. }
  673. for i, tc := range testCases {
  674. r := extractInterfacesTag(tc.comments)
  675. if r == nil && tc.expect != nil {
  676. t.Errorf("case[%d]: expected non-nil", i)
  677. }
  678. if r != nil && tc.expect == nil {
  679. t.Errorf("case[%d]: expected nil, got %v", i, r)
  680. }
  681. if r != nil && !reflect.DeepEqual(r, tc.expect) {
  682. t.Errorf("case[%d]: expected %v, got %v", i, tc.expect, r)
  683. }
  684. }
  685. }