format_test.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. package format
  2. import (
  3. "bytes"
  4. _ "embed"
  5. "os"
  6. "strings"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/assertx"
  10. "github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/parser"
  11. )
  12. type formatData struct {
  13. input string
  14. expected string
  15. converter formatResultConvert
  16. }
  17. type formatResultConvert func(s string) string
  18. // EXPERIMENTAL: just for view format code.
  19. func TestFormat(t *testing.T) {
  20. assert.NoError(t, File("testdata/test_format.api"))
  21. }
  22. //go:embed testdata/test_type_struct_lit.api
  23. var testStructLitData string
  24. //go:embed testdata/expected_type_struct_lit.api
  25. var expectedStructLitData string
  26. func TestFormat_ImportLiteralStmt(t *testing.T) {
  27. testRun(t, []formatData{
  28. {
  29. input: `import ""`,
  30. expected: ``,
  31. },
  32. {
  33. input: `import"aa"`,
  34. expected: `import "aa"`,
  35. },
  36. {
  37. input: `/*aa*/import "aa"`,
  38. expected: `/*aa*/
  39. import "aa"`,
  40. },
  41. {
  42. input: `/*aa*/import /*bb*/"aa"`,
  43. expected: `/*aa*/
  44. import "aa"`,
  45. },
  46. {
  47. input: `/*aa*/import /*bb*/"aa"// cc`,
  48. expected: `/*aa*/
  49. import "aa" // cc`,
  50. },
  51. })
  52. }
  53. func TestFormat_ImportGroupStmt(t *testing.T) {
  54. testRun(t, []formatData{
  55. {
  56. input: `import()`,
  57. expected: ``,
  58. },
  59. {
  60. input: `import("aa")`,
  61. expected: `import (
  62. "aa"
  63. )`,
  64. },
  65. {
  66. input: `import(
  67. "aa")`,
  68. expected: `import (
  69. "aa"
  70. )`,
  71. },
  72. {
  73. input: `import(
  74. "aa"
  75. )`,
  76. expected: `import (
  77. "aa"
  78. )`,
  79. },
  80. {
  81. input: `import("aa""bb")`,
  82. expected: `import (
  83. "aa"
  84. "bb"
  85. )`,
  86. },
  87. {
  88. input: `/*aa*/import("aa""bb")`,
  89. expected: `/*aa*/
  90. import (
  91. "aa"
  92. "bb"
  93. )`,
  94. },
  95. {
  96. input: `/*aa*/import("aa""bb")// bb`,
  97. expected: `/*aa*/
  98. import (
  99. "aa"
  100. "bb"
  101. ) // bb`,
  102. },
  103. {
  104. input: `/*aa*/import(// bb
  105. "aa""bb")// cc`,
  106. expected: `/*aa*/
  107. import ( // bb
  108. "aa"
  109. "bb"
  110. ) // cc`,
  111. },
  112. {
  113. input: `import(// aa
  114. "aa" // bb
  115. "bb" // cc
  116. )// dd`,
  117. expected: `import ( // aa
  118. "aa" // bb
  119. "bb" // cc
  120. ) // dd`,
  121. },
  122. {
  123. input: `import (// aa
  124. /*bb*/
  125. "aa" // cc
  126. /*dd*/
  127. "bb" // ee
  128. ) // ff`,
  129. expected: `import ( // aa
  130. /*bb*/
  131. "aa" // cc
  132. /*dd*/
  133. "bb" // ee
  134. ) // ff`,
  135. },
  136. })
  137. }
  138. func TestFormat_InfoStmt(t *testing.T) {
  139. testRun(t, []formatData{
  140. {
  141. input: `info()`,
  142. expected: ``,
  143. },
  144. {
  145. input: `info(foo:"foo")`,
  146. expected: `info (
  147. foo: "foo"
  148. )`,
  149. },
  150. {
  151. input: `info(foo:"foo" bar:"bar")`,
  152. expected: `info (
  153. foo: "foo"
  154. bar: "bar"
  155. )`,
  156. },
  157. {
  158. input: `info(foo:"foo" bar:"bar" quux:"quux")`,
  159. expected: `info (
  160. foo: "foo"
  161. bar: "bar"
  162. quux: "quux"
  163. )`,
  164. },
  165. {
  166. input: `info(foo:"foo"
  167. bar: "bar")`,
  168. expected: `info (
  169. foo: "foo"
  170. bar: "bar"
  171. )`,
  172. },
  173. {
  174. input: `info(foo:"foo"// aa
  175. bar: "bar"// bb
  176. )`,
  177. expected: `info (
  178. foo: "foo" // aa
  179. bar: "bar" // bb
  180. )`,
  181. },
  182. {
  183. input: `info(// aa
  184. foo:"foo"// bb
  185. bar: "bar"// cc
  186. )`,
  187. expected: `info ( // aa
  188. foo: "foo" // bb
  189. bar: "bar" // cc
  190. )`,
  191. },
  192. {
  193. input: `/*aa*/info(// bb
  194. foo:"foo"// cc
  195. bar: "bar"// dd
  196. )`,
  197. expected: `/*aa*/
  198. info ( // bb
  199. foo: "foo" // cc
  200. bar: "bar" // dd
  201. )`,
  202. },
  203. {
  204. input: `/*aa*/
  205. info(// bb
  206. foo:"foo"// cc
  207. bar: "bar"// dd
  208. )// ee`,
  209. expected: `/*aa*/
  210. info ( // bb
  211. foo: "foo" // cc
  212. bar: "bar" // dd
  213. ) // ee`,
  214. },
  215. {
  216. input: `/*aa*/
  217. info ( // bb
  218. /*cc*/foo: "foo" // dd
  219. /*ee*/bar: "bar" // ff
  220. ) // gg`,
  221. expected: `/*aa*/
  222. info ( // bb
  223. /*cc*/
  224. foo: "foo" // dd
  225. /*ee*/
  226. bar: "bar" // ff
  227. ) // gg`,
  228. },
  229. {
  230. input: `/*aa*/
  231. info/*xx*/( // bb
  232. /*cc*/foo:/*xx*/ "foo" // dd
  233. /*ee*/bar:/*xx*/ "bar" // ff
  234. ) // gg`,
  235. expected: `/*aa*/
  236. info ( // bb
  237. /*cc*/
  238. foo: "foo" // dd
  239. /*ee*/
  240. bar: "bar" // ff
  241. ) // gg`,
  242. },
  243. })
  244. }
  245. func TestFormat_SyntaxStmt(t *testing.T) {
  246. testRun(t, []formatData{
  247. {
  248. input: `syntax="v1"`,
  249. expected: `syntax = "v1"`,
  250. },
  251. {
  252. input: `syntax="v1"// aa`,
  253. expected: `syntax = "v1" // aa`,
  254. },
  255. {
  256. input: `syntax
  257. ="v1"// aa`,
  258. expected: `syntax = "v1" // aa`,
  259. },
  260. {
  261. input: `syntax=
  262. "v1"// aa`,
  263. expected: `syntax = "v1" // aa`,
  264. },
  265. {
  266. input: `/*aa*/syntax="v1"// bb`,
  267. expected: `/*aa*/
  268. syntax = "v1" // bb`,
  269. },
  270. {
  271. input: `/*aa*/
  272. syntax="v1"// bb`,
  273. expected: `/*aa*/
  274. syntax = "v1" // bb`,
  275. },
  276. {
  277. input: `syntax/*xx*/=/*xx*/"v1"// bb`,
  278. expected: `syntax = "v1" // bb`,
  279. },
  280. })
  281. }
  282. func TestFormat_TypeLiteralStmt(t *testing.T) {
  283. t.Run("any", func(t *testing.T) {
  284. testRun(t, []formatData{
  285. {
  286. input: `type Any any`,
  287. expected: `type Any any`,
  288. },
  289. {
  290. input: `type
  291. Any
  292. any
  293. `,
  294. expected: `type Any any`,
  295. },
  296. {
  297. input: `type Any=any`,
  298. expected: `type Any = any`,
  299. },
  300. {
  301. input: `
  302. type
  303. Any
  304. =
  305. any
  306. `,
  307. expected: `type Any = any`,
  308. },
  309. {
  310. input: `type // aa
  311. Any // bb
  312. any // cc
  313. `,
  314. expected: `type // aa
  315. Any // bb
  316. any // cc`,
  317. },
  318. {
  319. input: `
  320. type
  321. Any
  322. =
  323. any`,
  324. expected: `type Any = any`,
  325. },
  326. {
  327. input: `
  328. type
  329. Any
  330. =
  331. any
  332. `,
  333. expected: `type Any = any`,
  334. },
  335. {
  336. input: `type Any any// aa`,
  337. expected: `type Any any // aa`,
  338. },
  339. {
  340. input: `type Any=any// aa`,
  341. expected: `type Any = any // aa`,
  342. },
  343. {
  344. input: `type Any any/*aa*/// bb`,
  345. expected: `type Any any /*aa*/ // bb`,
  346. },
  347. {
  348. input: `type Any = any/*aa*/// bb`,
  349. expected: `type Any = any /*aa*/ // bb`,
  350. },
  351. {
  352. input: `type Any/*aa*/ =/*bb*/ any/*cc*/// dd`,
  353. expected: `type Any /*aa*/ = /*bb*/ any /*cc*/ // dd`,
  354. },
  355. {
  356. input: `/*aa*/type Any any/*bb*/// cc`,
  357. expected: `/*aa*/
  358. type Any any /*bb*/ // cc`,
  359. },
  360. {
  361. input: `/*aa*/
  362. type
  363. /*bb*/
  364. Any
  365. /*cc*/
  366. any/*dd*/// ee`,
  367. expected: `/*aa*/
  368. type
  369. /*bb*/
  370. Any
  371. /*cc*/
  372. any /*dd*/ // ee`,
  373. },
  374. })
  375. })
  376. t.Run("array", func(t *testing.T) {
  377. testRun(t, []formatData{
  378. {
  379. input: `type A [2]int`,
  380. expected: `type A [2]int`,
  381. },
  382. {
  383. input: `type
  384. A
  385. [2]int
  386. `,
  387. expected: `type A [2]int`,
  388. },
  389. {
  390. input: `type A=[2]int`,
  391. expected: `type A = [2]int`,
  392. },
  393. {
  394. input: `type
  395. A
  396. =
  397. [2]int
  398. `,
  399. expected: `type A = [2]int`,
  400. },
  401. {
  402. input: `type A [/*xx*/2/*xx*/]/*xx*/int// aa`,
  403. expected: `type A [2]int // aa`,
  404. },
  405. {
  406. input: `/*aa*/type/*bb*/A/*cc*/[/*xx*/2/*xx*/]/*xx*/int// dd`,
  407. expected: `/*aa*/
  408. type /*bb*/ A /*cc*/ [2]int // dd`,
  409. },
  410. {
  411. input: `/*aa*/type
  412. /*bb*/A
  413. /*cc*/[/*xx*/2/*xx*/]/*xx*/int// dd`,
  414. expected: `/*aa*/
  415. type
  416. /*bb*/
  417. A
  418. /*cc*/
  419. [2]int // dd`,
  420. },
  421. {
  422. input: `type A [ 2 ] int`,
  423. expected: `type A [2]int`,
  424. },
  425. {
  426. input: `type A [
  427. 2
  428. ]
  429. int`,
  430. expected: `type A [2]int`,
  431. },
  432. {
  433. input: `type A [// aa
  434. 2 // bb
  435. ] // cc
  436. int`,
  437. expected: `type A [2]int`,
  438. },
  439. {
  440. input: `type A [// aa
  441. /*xx*/
  442. 2 // bb
  443. /*xx*/
  444. ] // cc
  445. /*xx*/
  446. int`,
  447. expected: `type A [2]int`,
  448. },
  449. {
  450. input: `type A [...]int`,
  451. expected: `type A [...]int`,
  452. },
  453. {
  454. input: `type A=[...]int`,
  455. expected: `type A = [...]int`,
  456. },
  457. {
  458. input: `type A/*aa*/[/*xx*/.../*xx*/]/*xx*/int// bb`,
  459. expected: `type A /*aa*/ [...]int // bb`,
  460. },
  461. {
  462. input: `/*aa*/
  463. // bb
  464. type /*cc*/
  465. // dd
  466. A /*ee*/
  467. // ff
  468. [/*xx*/.../*xx*/]/*xx*/int// bb`,
  469. expected: `/*aa*/
  470. // bb
  471. type /*cc*/
  472. // dd
  473. A /*ee*/
  474. // ff
  475. [...]int // bb`,
  476. },
  477. {
  478. input: `type A [2][2]int`,
  479. expected: `type A [2][2]int`,
  480. },
  481. {
  482. input: `type A=[2][2]int`,
  483. expected: `type A = [2][2]int`,
  484. },
  485. {
  486. input: `type A [2][]int`,
  487. expected: `type A [2][]int`,
  488. },
  489. {
  490. input: `type A=[2][]int`,
  491. expected: `type A = [2][]int`,
  492. },
  493. })
  494. })
  495. t.Run("base", func(t *testing.T) {
  496. testRun(t, []formatData{
  497. // base
  498. {
  499. input: `type A int`,
  500. expected: `type A int`,
  501. },
  502. {
  503. input: `type A =int`,
  504. expected: `type A = int`,
  505. },
  506. {
  507. input: `type/*aa*/A/*bb*/ int// cc`,
  508. expected: `type /*aa*/ A /*bb*/ int // cc`,
  509. },
  510. {
  511. input: `type/*aa*/A/*bb*/ =int// cc`,
  512. expected: `type /*aa*/ A /*bb*/ = int // cc`,
  513. },
  514. {
  515. input: `type A int// aa`,
  516. expected: `type A int // aa`,
  517. },
  518. {
  519. input: `type A=int// aa`,
  520. expected: `type A = int // aa`,
  521. },
  522. {
  523. input: `/*aa*/type A int`,
  524. expected: `/*aa*/
  525. type A int`,
  526. },
  527. {
  528. input: `/*aa*/type A = int`,
  529. expected: `/*aa*/
  530. type A = int`,
  531. },
  532. {
  533. input: `/*aa*/type/*bb*/ A/*cc*/ int// dd`,
  534. expected: `/*aa*/
  535. type /*bb*/ A /*cc*/ int // dd`,
  536. },
  537. {
  538. input: `/*aa*/type/*bb*/ A/*cc*/ = /*dd*/int// ee`,
  539. expected: `/*aa*/
  540. type /*bb*/ A /*cc*/ = /*dd*/ int // ee`,
  541. },
  542. {
  543. input: `/*aa*/
  544. type
  545. /*bb*/
  546. A
  547. /*cc*/
  548. int`,
  549. expected: `/*aa*/
  550. type
  551. /*bb*/
  552. A
  553. /*cc*/
  554. int`,
  555. },
  556. })
  557. })
  558. t.Run("interface", func(t *testing.T) {
  559. testRun(t, []formatData{
  560. {
  561. input: `type any interface{}`,
  562. expected: `type any interface{}`,
  563. },
  564. {
  565. input: `type any=interface{}`,
  566. expected: `type any = interface{}`,
  567. },
  568. {
  569. input: `type
  570. any
  571. interface{}
  572. `,
  573. expected: `type any interface{}`,
  574. },
  575. {
  576. input: `/*aa*/type /*bb*/any /*cc*/interface{} // dd`,
  577. expected: `/*aa*/
  578. type /*bb*/ any /*cc*/ interface{} // dd`,
  579. },
  580. {
  581. input: `/*aa*/type
  582. /*bb*/any
  583. /*cc*/interface{} // dd`,
  584. expected: `/*aa*/
  585. type
  586. /*bb*/
  587. any
  588. /*cc*/
  589. interface{} // dd`,
  590. },
  591. {
  592. input: `/*aa*/type
  593. // bb
  594. any
  595. // cc
  596. interface{} // dd`,
  597. expected: `/*aa*/
  598. type
  599. // bb
  600. any
  601. // cc
  602. interface{} // dd`,
  603. },
  604. })
  605. })
  606. t.Run("map", func(t *testing.T) {
  607. testRun(t, []formatData{
  608. {
  609. input: `type M map[int]int`,
  610. expected: `type M map[int]int`,
  611. },
  612. {
  613. input: `type M map [ int ] int`,
  614. expected: `type M map[int]int`,
  615. },
  616. {
  617. input: `type M map [/*xx*/int/*xx*/]/*xx*/int // aa`,
  618. expected: `type M map[int]int // aa`,
  619. },
  620. {
  621. input: `/*aa*/type /*bb*/ M/*cc*/map[int]int // dd`,
  622. expected: `/*aa*/
  623. type /*bb*/ M /*cc*/ map[int]int // dd`,
  624. },
  625. {
  626. input: `/*aa*/type// bb
  627. // cc
  628. M // dd
  629. // ee
  630. map // ff
  631. [int]// gg
  632. // hh
  633. int // dd`,
  634. expected: `/*aa*/
  635. type // bb
  636. // cc
  637. M // dd
  638. // ee
  639. map[int]int // dd`,
  640. },
  641. {
  642. input: `type M map[string][2]int // aa`,
  643. expected: `type M map[string][2]int // aa`,
  644. },
  645. {
  646. input: `type M map[string]any`,
  647. expected: `type M map[string]any`,
  648. },
  649. {
  650. input: `type M /*aa*/map/*xx*/[/*xx*/string/*xx*/]/*xx*/[/*xx*/2/*xx*/]/*xx*/int// bb`,
  651. expected: `type M /*aa*/ map[string][2]int // bb`,
  652. },
  653. {
  654. input: `type M /*aa*/
  655. // bb
  656. map/*xx*/
  657. //
  658. [/*xx*/
  659. //
  660. string/*xx*/
  661. //
  662. ]/*xx*/
  663. //
  664. [/*xx*/
  665. //
  666. 2/*xx*/
  667. //
  668. ]/*xx*/
  669. //
  670. int// bb`,
  671. expected: `type M /*aa*/
  672. // bb
  673. map[string][2]int // bb`,
  674. },
  675. {
  676. input: `type M map[int]map[string]int`,
  677. expected: `type M map[int]map[string]int`,
  678. },
  679. {
  680. input: `type M map/*xx*/[/*xx*/int/*xx*/]/*xx*/map/*xx*/[/*xx*/string/*xx*/]/*xx*/int// aa`,
  681. expected: `type M map[int]map[string]int // aa`,
  682. },
  683. {
  684. input: `type M map/*xx*/[/*xx*/map/*xx*/[/*xx*/string/*xx*/]/*xx*/int/*xx*/]/*xx*/string // aa`,
  685. expected: `type M map[map[string]int]string // aa`,
  686. },
  687. {
  688. input: `type M map[[2]int]int`,
  689. expected: `type M map[[2]int]int`,
  690. },
  691. {
  692. input: `type M map/*xx*/[/*xx*/[/*xx*/2/*xx*/]/*xx*/int/*xx*/]/*xx*/int// aa`,
  693. expected: `type M map[[2]int]int // aa`,
  694. },
  695. })
  696. })
  697. t.Run("pointer", func(t *testing.T) {
  698. testRun(t, []formatData{
  699. {
  700. input: `type P *int`,
  701. expected: `type P *int`,
  702. },
  703. {
  704. input: `type P=*int`,
  705. expected: `type P = *int`,
  706. },
  707. {
  708. input: `type
  709. P
  710. *int
  711. `,
  712. expected: `type P *int`,
  713. },
  714. {
  715. input: `/*aa*/type // bb
  716. /*cc*/
  717. P // dd
  718. /*ee*/
  719. */*ff*/int // gg
  720. `,
  721. expected: `/*aa*/
  722. type // bb
  723. /*cc*/
  724. P // dd
  725. /*ee*/
  726. *int // gg`,
  727. },
  728. {
  729. input: `type P *bool`,
  730. expected: `type P *bool`,
  731. },
  732. {
  733. input: `type P *[2]int`,
  734. expected: `type P *[2]int`,
  735. },
  736. {
  737. input: `type P=*[2]int`,
  738. expected: `type P = *[2]int`,
  739. },
  740. {
  741. input: `/*aa*/type /*bb*/P /*cc*/*/*xx*/[/*xx*/2/*xx*/]/*xx*/int // dd`,
  742. expected: `/*aa*/
  743. type /*bb*/ P /*cc*/ *[2]int // dd`,
  744. },
  745. {
  746. input: `type P *[...]int`,
  747. expected: `type P *[...]int`,
  748. },
  749. {
  750. input: `type P=*[...]int`,
  751. expected: `type P = *[...]int`,
  752. },
  753. {
  754. input: `/*aa*/type /*bb*/P /*cc*/*/*xx*/[/*xx*/.../*xx*/]/*xx*/int // dd`,
  755. expected: `/*aa*/
  756. type /*bb*/ P /*cc*/ *[...]int // dd`,
  757. },
  758. {
  759. input: `type P *map[string]int`,
  760. expected: `type P *map[string]int`,
  761. },
  762. {
  763. input: `type P=*map[string]int`,
  764. expected: `type P = *map[string]int`,
  765. },
  766. {
  767. input: `type P /*aa*/*/*xx*/map/*xx*/[/*xx*/string/*xx*/]/*xx*/int// bb`,
  768. expected: `type P /*aa*/ *map[string]int // bb`,
  769. },
  770. {
  771. input: `type P *interface{}`,
  772. expected: `type P *interface{}`,
  773. },
  774. {
  775. input: `type P=*interface{}`,
  776. expected: `type P = *interface{}`,
  777. },
  778. {
  779. input: `type P /*aa*/*/*xx*/interface{}// bb`,
  780. expected: `type P /*aa*/ *interface{} // bb`,
  781. },
  782. {
  783. input: `type P *any`,
  784. expected: `type P *any`,
  785. },
  786. {
  787. input: `type P=*any`,
  788. expected: `type P = *any`,
  789. },
  790. {
  791. input: `type P *map[int][2]int`,
  792. expected: `type P *map[int][2]int`,
  793. },
  794. {
  795. input: `type P=*map[int][2]int`,
  796. expected: `type P = *map[int][2]int`,
  797. },
  798. {
  799. input: `type P /*aa*/*/*xx*/map/*xx*/[/*xx*/int/*xx*/]/*xx*/[/*xx*/2/*xx*/]/*xx*/int// bb`,
  800. expected: `type P /*aa*/ *map[int][2]int // bb`,
  801. },
  802. {
  803. input: `type P *map[[2]int]int`,
  804. expected: `type P *map[[2]int]int`,
  805. },
  806. {
  807. input: `type P=*map[[2]int]int`,
  808. expected: `type P = *map[[2]int]int`,
  809. },
  810. {
  811. input: `type P /*aa*/*/*xx*/map/*xx*/[/*xx*/[/*xx*/2/*xx*/]/*xx*/int/*xx*/]/*xx*/int// bb`,
  812. expected: `type P /*aa*/ *map[[2]int]int // bb`,
  813. },
  814. })
  815. })
  816. t.Run("slice", func(t *testing.T) {
  817. testRun(t, []formatData{
  818. {
  819. input: `type S []int`,
  820. expected: `type S []int`,
  821. },
  822. {
  823. input: `type S=[]int`,
  824. expected: `type S = []int`,
  825. },
  826. {
  827. input: `type S [ ] int `,
  828. expected: `type S []int`,
  829. },
  830. {
  831. input: `type S [ /*xx*/ ] /*xx*/ int `,
  832. expected: `type S []int`,
  833. },
  834. {
  835. input: `type S [][]int`,
  836. expected: `type S [][]int`,
  837. },
  838. {
  839. input: `type S=[][]int`,
  840. expected: `type S = [][]int`,
  841. },
  842. {
  843. input: `type S [ ] [ ] int`,
  844. expected: `type S [][]int`,
  845. },
  846. {
  847. input: `type S [/*xx*/]/*xx*/[/*xx*/]/*xx*/int`,
  848. expected: `type S [][]int`,
  849. },
  850. {
  851. input: `type S [//
  852. ]//
  853. [//
  854. ]//
  855. int`,
  856. expected: `type S [][]int`,
  857. },
  858. {
  859. input: `type S []map[string]int`,
  860. expected: `type S []map[string]int`,
  861. },
  862. {
  863. input: `type S=[]map[string]int`,
  864. expected: `type S = []map[string]int`,
  865. },
  866. {
  867. input: `type S [ ]
  868. map [ string ]
  869. int`,
  870. expected: `type S []map[string]int`,
  871. },
  872. {
  873. input: `type S [/*xx*/]/*xx*/map/*xx*/[/*xx*/string/*xx*/]/*xx*/int`,
  874. expected: `type S []map[string]int`,
  875. },
  876. {
  877. input: `/*aa*/type// bb
  878. // cc
  879. S// dd
  880. // ff
  881. /*gg*/[ // hh
  882. /*xx*/] // ii
  883. /*xx*/map// jj
  884. /*xx*/[/*xx*/string/*xx*/]/*xx*/int// mm`,
  885. expected: `/*aa*/
  886. type // bb
  887. // cc
  888. S // dd
  889. // ff
  890. /*gg*/
  891. []map[string]int // mm`,
  892. },
  893. {
  894. input: `type S []map[[2]int]int`,
  895. expected: `type S []map[[2]int]int`,
  896. },
  897. {
  898. input: `type S=[]map[[2]int]int`,
  899. expected: `type S = []map[[2]int]int`,
  900. },
  901. {
  902. input: `type S [/*xx*/]/*xx*/map/*xx*/[/*xx*/[/*xx*/2/*xx*/]/*xx*/int/*xx*/]/*xx*/int`,
  903. expected: `type S []map[[2]int]int`,
  904. },
  905. {
  906. input: `/*aa*/type// bb
  907. // cc
  908. /*dd*/S// ee
  909. // ff
  910. /*gg*/[//
  911. /*xx*/]//
  912. /*xx*/map//
  913. /*xx*/[//
  914. /*xx*/[//
  915. /*xx*/2//
  916. /*xx*/]//
  917. /*xx*/int//
  918. /*xx*/]//
  919. /*xx*/int // hh`,
  920. expected: `/*aa*/
  921. type // bb
  922. // cc
  923. /*dd*/
  924. S // ee
  925. // ff
  926. /*gg*/
  927. []map[[2]int]int // hh`,
  928. },
  929. {
  930. input: `type S []map[[2]int]map[int]string`,
  931. expected: `type S []map[[2]int]map[int]string`,
  932. },
  933. {
  934. input: `type S=[]map[[2]int]map[int]string`,
  935. expected: `type S = []map[[2]int]map[int]string`,
  936. },
  937. {
  938. input: `type S [/*xx*/]/*xx*/map/*xx*/[/*xx*/[/*xx*/2/*xx*/]/*xx*/int/*xx*/]/*xx*/map/*xx*/[/*xx*/int/*xx*/]/*xx*/string`,
  939. expected: `type S []map[[2]int]map[int]string`,
  940. },
  941. {
  942. input: `/*aa*/type// bb
  943. // cc
  944. /*dd*/S// ee
  945. /*ff*/[//
  946. /*xx*/]//
  947. /*xx*/map
  948. /*xx*/[//
  949. /*xx*/[//
  950. /*xx*/2//
  951. /*xx*/]//
  952. /*xx*/int//
  953. /*xx*/]//
  954. /*xx*/map//
  955. /*xx*/[//
  956. /*xx*/int//
  957. /*xx*/]//
  958. /*xx*/string// gg`,
  959. expected: `/*aa*/
  960. type // bb
  961. // cc
  962. /*dd*/
  963. S // ee
  964. /*ff*/
  965. []map[[2]int]map[int]string // gg`,
  966. },
  967. {
  968. input: `type S []*P`,
  969. expected: `type S []*P`,
  970. },
  971. {
  972. input: `type S=[]*P`,
  973. expected: `type S = []*P`,
  974. },
  975. {
  976. input: `type S [/*xx*/]/*xx*/*/*xx*/P`,
  977. expected: `type S []*P`,
  978. },
  979. {
  980. input: `/*aa*/type// bb
  981. // cc
  982. /*dd*/S// ee
  983. /*ff*/[//
  984. /*xx*/]//
  985. /*xx*/*//
  986. /*xx*/P // gg`,
  987. expected: `/*aa*/
  988. type // bb
  989. // cc
  990. /*dd*/
  991. S // ee
  992. /*ff*/
  993. []*P // gg`,
  994. },
  995. {
  996. input: `type S []*[]int`,
  997. expected: `type S []*[]int`,
  998. },
  999. {
  1000. input: `type S=[]*[]int`,
  1001. expected: `type S = []*[]int`,
  1002. },
  1003. {
  1004. input: `type S [/*xx*/]/*xx*/*/*xx*/[/*xx*/]/*xx*/int`,
  1005. expected: `type S []*[]int`,
  1006. },
  1007. {
  1008. input: `/*aa*/
  1009. type // bb
  1010. // cc
  1011. /*dd*/S// ee
  1012. /*ff*/[//
  1013. /*xx*/]//
  1014. /*xx*/*//
  1015. /*xx*/[//
  1016. /*xx*/]//
  1017. /*xx*/int // gg`,
  1018. expected: `/*aa*/
  1019. type // bb
  1020. // cc
  1021. /*dd*/
  1022. S // ee
  1023. /*ff*/
  1024. []*[]int // gg`,
  1025. },
  1026. })
  1027. })
  1028. t.Run("struct", func(t *testing.T) {
  1029. testRun(t, []formatData{
  1030. {
  1031. input: `type T {}`,
  1032. expected: `type T {}`,
  1033. },
  1034. {
  1035. input: `type T {
  1036. } `,
  1037. expected: `type T {}`,
  1038. },
  1039. {
  1040. input: `type T={}`,
  1041. expected: `type T = {}`,
  1042. },
  1043. {
  1044. input: `type T /*aa*/{/*xx*/}// cc`,
  1045. expected: `type T /*aa*/ {} // cc`,
  1046. },
  1047. {
  1048. input: `/*aa*/type// bb
  1049. // cc
  1050. /*dd*/T // ee
  1051. /*ff*/{//
  1052. /*xx*/}// cc`,
  1053. expected: `/*aa*/
  1054. type // bb
  1055. // cc
  1056. /*dd*/
  1057. T // ee
  1058. /*ff*/
  1059. {} // cc`,
  1060. },
  1061. {
  1062. input: `type T {
  1063. Name string
  1064. }`,
  1065. expected: `type T {
  1066. Name string
  1067. }`,
  1068. },
  1069. {
  1070. input: `type T {
  1071. Foo
  1072. }`,
  1073. expected: `type T {
  1074. Foo
  1075. }`,
  1076. },
  1077. {
  1078. input: `type T {
  1079. *Foo
  1080. }`,
  1081. expected: `type T {
  1082. *Foo
  1083. }`,
  1084. },
  1085. {
  1086. input: testStructLitData,
  1087. expected: expectedStructLitData,
  1088. converter: func(s string) string {
  1089. return strings.ReplaceAll(s, "\t", " ")
  1090. },
  1091. },
  1092. })
  1093. })
  1094. }
  1095. //go:embed testdata/test_type_struct_group.api
  1096. var testStructGroupData string
  1097. //go:embed testdata/expected_type_struct_group.api
  1098. var expectedStructgroupData string
  1099. func TestFormat_TypeGroupStmt(t *testing.T) {
  1100. testRun(t, []formatData{
  1101. {
  1102. input: testStructGroupData,
  1103. expected: expectedStructgroupData,
  1104. converter: func(s string) string {
  1105. return strings.ReplaceAll(s, "\t", " ")
  1106. },
  1107. },
  1108. })
  1109. }
  1110. func TestFormat_AtServerStmt(t *testing.T) {
  1111. testRunStmt(t, []formatData{
  1112. {
  1113. input: `@server()`,
  1114. expected: ``,
  1115. },
  1116. {
  1117. input: `@server(foo:foo)`,
  1118. expected: `@server (
  1119. foo: foo
  1120. )`,
  1121. },
  1122. {
  1123. input: `@server(foo:foo quux:quux)`,
  1124. expected: `@server (
  1125. foo: foo
  1126. quux: quux
  1127. )`,
  1128. },
  1129. {
  1130. input: `@server(
  1131. foo:
  1132. foo
  1133. quux:
  1134. quux
  1135. )`,
  1136. expected: `@server (
  1137. foo: foo
  1138. quux: quux
  1139. )`,
  1140. },
  1141. {
  1142. input: `/*aa*/@server/*bb*/(/*cc*/foo:/**/foo /*dd*/quux:/**/quux/*ee*/)`,
  1143. expected: `/*aa*/
  1144. @server ( /*cc*/
  1145. foo: foo /*dd*/
  1146. quux: quux /*ee*/
  1147. )`,
  1148. },
  1149. {
  1150. input: `/*aa*/
  1151. @server
  1152. /*bb*/(// cc
  1153. /*dd*/foo:/**/foo// ee
  1154. /*ff*/quux:/**/quux// gg
  1155. )`,
  1156. expected: `/*aa*/
  1157. @server
  1158. /*bb*/
  1159. ( // cc
  1160. /*dd*/
  1161. foo: foo // ee
  1162. /*ff*/
  1163. quux: quux // gg
  1164. )`,
  1165. },
  1166. })
  1167. }
  1168. func TestFormat_AtDocStmt(t *testing.T) {
  1169. t.Run("AtDocLiteralStmt", func(t *testing.T) {
  1170. testRunStmt(t, []formatData{
  1171. {
  1172. input: `@doc ""`,
  1173. expected: ``,
  1174. },
  1175. {
  1176. input: `@doc "foo"`,
  1177. expected: `@doc "foo"`,
  1178. },
  1179. {
  1180. input: `@doc "foo"`,
  1181. expected: `@doc "foo"`,
  1182. },
  1183. {
  1184. input: `@doc"foo"`,
  1185. expected: `@doc "foo"`,
  1186. },
  1187. {
  1188. input: `/*aa*/@doc/**/"foo"// bb`,
  1189. expected: `/*aa*/
  1190. @doc "foo" // bb`,
  1191. },
  1192. {
  1193. input: `/*aa*/
  1194. /*bb*/@doc // cc
  1195. "foo"// ee`,
  1196. expected: `/*aa*/
  1197. /*bb*/
  1198. @doc "foo" // ee`,
  1199. },
  1200. })
  1201. })
  1202. t.Run("AtDocGroupStmt", func(t *testing.T) {
  1203. testRunStmt(t, []formatData{
  1204. {
  1205. input: `@doc()`,
  1206. expected: ``,
  1207. },
  1208. {
  1209. input: `@doc(foo:"foo")`,
  1210. expected: `@doc (
  1211. foo: "foo"
  1212. )`,
  1213. },
  1214. {
  1215. input: `@doc(foo:"foo" bar:"bar")`,
  1216. expected: `@doc (
  1217. foo: "foo"
  1218. bar: "bar"
  1219. )`,
  1220. },
  1221. {
  1222. input: `@doc(foo:"foo" bar:"bar" quux:"quux")`,
  1223. expected: `@doc (
  1224. foo: "foo"
  1225. bar: "bar"
  1226. quux: "quux"
  1227. )`,
  1228. },
  1229. {
  1230. input: `@doc(foo:"foo"
  1231. bar: "bar")`,
  1232. expected: `@doc (
  1233. foo: "foo"
  1234. bar: "bar"
  1235. )`,
  1236. },
  1237. {
  1238. input: `@doc(foo:"foo"// aa
  1239. bar: "bar"// bb
  1240. )`,
  1241. expected: `@doc (
  1242. foo: "foo" // aa
  1243. bar: "bar" // bb
  1244. )`,
  1245. },
  1246. {
  1247. input: `@doc(// aa
  1248. foo:"foo"// bb
  1249. bar: "bar"// cc
  1250. )`,
  1251. expected: `@doc ( // aa
  1252. foo: "foo" // bb
  1253. bar: "bar" // cc
  1254. )`,
  1255. },
  1256. {
  1257. input: `/*aa*/@doc(// bb
  1258. foo:"foo"// cc
  1259. bar: "bar"// dd
  1260. )`,
  1261. expected: `/*aa*/
  1262. @doc ( // bb
  1263. foo: "foo" // cc
  1264. bar: "bar" // dd
  1265. )`,
  1266. },
  1267. {
  1268. input: `/*aa*/
  1269. @doc(// bb
  1270. foo:"foo"// cc
  1271. bar: "bar"// dd
  1272. )// ee`,
  1273. expected: `/*aa*/
  1274. @doc ( // bb
  1275. foo: "foo" // cc
  1276. bar: "bar" // dd
  1277. ) // ee`,
  1278. },
  1279. {
  1280. input: `/*aa*/
  1281. @doc ( // bb
  1282. /*cc*/foo: "foo" // dd
  1283. /*ee*/bar: "bar" // ff
  1284. ) // gg`,
  1285. expected: `/*aa*/
  1286. @doc ( // bb
  1287. /*cc*/
  1288. foo: "foo" // dd
  1289. /*ee*/
  1290. bar: "bar" // ff
  1291. ) // gg`,
  1292. },
  1293. {
  1294. input: `/*aa*/
  1295. @doc/*xx*/( // bb
  1296. /*cc*/foo:/*xx*/ "foo" // dd
  1297. /*ee*/bar:/*xx*/ "bar" // ff
  1298. ) // gg`,
  1299. expected: `/*aa*/
  1300. @doc ( // bb
  1301. /*cc*/
  1302. foo: "foo" // dd
  1303. /*ee*/
  1304. bar: "bar" // ff
  1305. ) // gg`,
  1306. },
  1307. })
  1308. })
  1309. }
  1310. func TestFormat_AtHandlerStmt(t *testing.T) {
  1311. testRunStmt(t, []formatData{
  1312. {
  1313. input: `@handler foo`,
  1314. expected: `@handler foo`,
  1315. },
  1316. {
  1317. input: `@handler foo`,
  1318. expected: `@handler foo`,
  1319. },
  1320. {
  1321. input: `/*aa*/@handler/**/foo// bb`,
  1322. expected: `/*aa*/
  1323. @handler foo // bb`,
  1324. },
  1325. {
  1326. input: `/*aa*/
  1327. /*bb*/@handler // cc
  1328. foo// ee`,
  1329. expected: `/*aa*/
  1330. /*bb*/
  1331. @handler foo // ee`,
  1332. },
  1333. })
  1334. }
  1335. //go:embed testdata/test_service.api
  1336. var testServiceData string
  1337. //go:embed testdata/expected_service.api
  1338. var expectedServiceData string
  1339. func TestFormat_ServiceStmt(t *testing.T) {
  1340. testRun(t, []formatData{
  1341. {
  1342. input: `service foo{}`,
  1343. expected: `service foo {}`,
  1344. },
  1345. {
  1346. input: `service foo { }`,
  1347. expected: `service foo {}`,
  1348. },
  1349. {
  1350. input: `@server()service foo { }`,
  1351. expected: `service foo {}`,
  1352. },
  1353. {
  1354. input: `@server(foo:foo quux:quux)service foo { }`,
  1355. expected: `@server (
  1356. foo: foo
  1357. quux: quux
  1358. )
  1359. service foo {}`,
  1360. },
  1361. {
  1362. input: `service foo-api { }`,
  1363. expected: `service foo-api {}`,
  1364. },
  1365. {
  1366. input: `service foo-api {
  1367. @doc "foo"
  1368. @handler foo
  1369. post /ping
  1370. }`,
  1371. expected: `service foo-api {
  1372. @doc "foo"
  1373. @handler foo
  1374. post /ping
  1375. }`,
  1376. },
  1377. {
  1378. input: `service foo-api {
  1379. @doc(foo: "foo" bar: "bar")
  1380. @handler foo
  1381. post /ping
  1382. }`,
  1383. expected: `service foo-api {
  1384. @doc (
  1385. foo: "foo"
  1386. bar: "bar"
  1387. )
  1388. @handler foo
  1389. post /ping
  1390. }`,
  1391. },
  1392. {
  1393. input: `service foo-api {
  1394. @doc(foo: "foo" bar: "bar"
  1395. quux: "quux"
  1396. )
  1397. @handler foo
  1398. post /ping
  1399. }`,
  1400. expected: `service foo-api {
  1401. @doc (
  1402. foo: "foo"
  1403. bar: "bar"
  1404. quux: "quux"
  1405. )
  1406. @handler foo
  1407. post /ping
  1408. }`,
  1409. },
  1410. {
  1411. input: `service
  1412. foo-api
  1413. {
  1414. @doc
  1415. (foo: "foo" bar: "bar"
  1416. quux: "quux"
  1417. )
  1418. @handler
  1419. foo
  1420. post
  1421. /aa/:bb/cc-dd/ee
  1422. @handler bar
  1423. get /bar () returns (Bar);
  1424. @handler baz
  1425. get /bar (Baz) returns ();
  1426. }`,
  1427. expected: `service foo-api {
  1428. @doc (
  1429. foo: "foo"
  1430. bar: "bar"
  1431. quux: "quux"
  1432. )
  1433. @handler foo
  1434. post /aa/:bb/cc-dd/ee
  1435. @handler bar
  1436. get /bar returns (Bar)
  1437. @handler baz
  1438. get /bar (Baz)
  1439. }`,
  1440. },
  1441. {
  1442. input: testServiceData,
  1443. expected: expectedServiceData,
  1444. converter: func(s string) string {
  1445. return strings.ReplaceAll(s, "\t", " ")
  1446. },
  1447. },
  1448. })
  1449. }
  1450. func TestFormat_error(t *testing.T) {
  1451. err := Source([]byte("aaa"), os.Stdout)
  1452. assertx.Error(t, err)
  1453. }
  1454. func testRun(t *testing.T, testData []formatData) {
  1455. for _, v := range testData {
  1456. buffer := bytes.NewBuffer(nil)
  1457. err := formatForUnitTest([]byte(v.input), buffer)
  1458. assert.NoError(t, err)
  1459. var result = buffer.String()
  1460. if v.converter != nil {
  1461. result = v.converter(result)
  1462. }
  1463. assert.Equal(t, v.expected, result)
  1464. }
  1465. }
  1466. func testRunStmt(t *testing.T, testData []formatData) {
  1467. for _, v := range testData {
  1468. p := parser.New("foo.api", v.input)
  1469. ast := p.ParseForUintTest()
  1470. assert.NoError(t, p.CheckErrors())
  1471. assert.True(t, len(ast.Stmts) > 0)
  1472. one := ast.Stmts[0]
  1473. actual := one.Format()
  1474. if v.converter != nil {
  1475. actual = v.converter(actual)
  1476. }
  1477. assert.Equal(t, v.expected, actual)
  1478. }
  1479. }