types_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package query
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/Jguer/yay/v11/pkg/db/mock"
  7. "github.com/Jguer/yay/v11/pkg/text"
  8. "github.com/Jguer/aur"
  9. )
  10. var (
  11. pkgA = aur.Pkg{
  12. Name: "package-a",
  13. Version: "1.0.0",
  14. Description: "Package A description",
  15. Maintainer: "Package A Maintainer",
  16. }
  17. pkgARepo = &mock.Package{
  18. PName: pkgA.Name,
  19. PVersion: pkgA.Version,
  20. PDescription: pkgA.Description,
  21. PSize: 1,
  22. PISize: 1,
  23. PDB: mock.NewDB("dba"),
  24. }
  25. pkgB = aur.Pkg{
  26. Name: "package-b",
  27. Version: "1.0.0",
  28. Description: "Package B description",
  29. Maintainer: "Package B Maintainer",
  30. }
  31. pkgBRepo = &mock.Package{
  32. PName: pkgB.Name,
  33. PVersion: pkgB.Version,
  34. PDescription: pkgB.Description,
  35. PSize: 1,
  36. PISize: 1,
  37. PDB: mock.NewDB("dbb"),
  38. }
  39. )
  40. func Test_aurQuery_printSearch(t *testing.T) {
  41. type args struct {
  42. searchMode SearchVerbosity
  43. singleLineResults bool
  44. }
  45. tests := []struct {
  46. name string
  47. q aurQuery
  48. args args
  49. useColor bool
  50. want string
  51. }{
  52. {
  53. name: "AUR,Minimal,NoColor",
  54. q: aurQuery{pkgA, pkgB},
  55. args: args{
  56. searchMode: Minimal,
  57. },
  58. want: "package-a\npackage-b\n",
  59. },
  60. {
  61. name: "AUR,DoubleLine,NumberMenu,NoColor",
  62. q: aurQuery{pkgA, pkgB},
  63. args: args{
  64. searchMode: NumberMenu,
  65. singleLineResults: false,
  66. },
  67. want: "1 aur/package-a 1.0.0 (+0 0.00) \n Package A description\n2 aur/package-b 1.0.0 (+0 0.00) \n Package B description\n",
  68. },
  69. {
  70. name: "AUR,SingleLine,NumberMenu,NoColor",
  71. q: aurQuery{pkgA, pkgB},
  72. args: args{
  73. searchMode: NumberMenu,
  74. singleLineResults: true,
  75. },
  76. want: "1 aur/package-a 1.0.0 (+0 0.00) \tPackage A description\n2 aur/package-b 1.0.0 (+0 0.00) \tPackage B description\n",
  77. },
  78. {
  79. name: "AUR,DoubleLine,Detailed,NoColor",
  80. q: aurQuery{pkgA, pkgB},
  81. args: args{
  82. searchMode: Detailed,
  83. singleLineResults: false,
  84. },
  85. want: "aur/package-a 1.0.0 (+0 0.00) \n Package A description\naur/package-b 1.0.0 (+0 0.00) \n Package B description\n",
  86. },
  87. {
  88. name: "AUR,SingleLine,Detailed,NoColor",
  89. q: aurQuery{pkgA, pkgB},
  90. args: args{
  91. searchMode: Detailed,
  92. singleLineResults: true,
  93. },
  94. want: "aur/package-a 1.0.0 (+0 0.00) \tPackage A description\naur/package-b 1.0.0 (+0 0.00) \tPackage B description\n",
  95. },
  96. {
  97. name: "AUR,DoubleLine,Detailed,Color",
  98. q: aurQuery{pkgA, pkgB},
  99. args: args{
  100. searchMode: Detailed,
  101. singleLineResults: false,
  102. },
  103. useColor: true,
  104. want: "\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mpackage-a\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (+0\x1b[0m \x1b[1m0.00) \x1b[0m\n Package A description\n\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mpackage-b\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (+0\x1b[0m \x1b[1m0.00) \x1b[0m\n Package B description\n",
  105. },
  106. {
  107. name: "AUR,SingleLine,Detailed,Color",
  108. q: aurQuery{pkgA, pkgB},
  109. args: args{
  110. searchMode: Detailed,
  111. singleLineResults: true,
  112. },
  113. useColor: true,
  114. want: "\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mpackage-a\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (+0\x1b[0m \x1b[1m0.00) \x1b[0m\tPackage A description\n\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mpackage-b\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (+0\x1b[0m \x1b[1m0.00) \x1b[0m\tPackage B description\n",
  115. },
  116. {
  117. name: "AUR,NoPackages",
  118. q: aurQuery{},
  119. args: args{
  120. searchMode: Detailed,
  121. singleLineResults: true,
  122. },
  123. useColor: true,
  124. want: "",
  125. },
  126. }
  127. for _, tt := range tests {
  128. t.Run(tt.name, func(t *testing.T) {
  129. w := &strings.Builder{}
  130. sortMode := 1
  131. executor := mock.DBExecutor{}
  132. text.UseColor = tt.useColor
  133. // Fire
  134. tt.q.printSearch(w, 1, executor, tt.args.searchMode, sortMode, tt.args.singleLineResults)
  135. got := w.String()
  136. assert.Equal(t, tt.want, got)
  137. })
  138. }
  139. }
  140. func Test_repoQuery_printSearch(t *testing.T) {
  141. type args struct {
  142. searchMode SearchVerbosity
  143. singleLineResults bool
  144. }
  145. tests := []struct {
  146. name string
  147. q repoQuery
  148. args args
  149. useColor bool
  150. want string
  151. }{
  152. {
  153. name: "REPO,Minimal,NoColor",
  154. q: repoQuery{pkgARepo, pkgBRepo},
  155. args: args{
  156. searchMode: Minimal,
  157. },
  158. want: "package-a\npackage-b\n",
  159. },
  160. {
  161. name: "REPO,DoubleLine,NumberMenu,NoColor",
  162. q: repoQuery{pkgARepo, pkgBRepo},
  163. args: args{
  164. searchMode: NumberMenu,
  165. singleLineResults: false,
  166. },
  167. want: "1 dba/package-a 1.0.0 (1.0 B 1.0 B) \n Package A description\n2 dbb/package-b 1.0.0 (1.0 B 1.0 B) \n Package B description\n",
  168. },
  169. {
  170. name: "REPO,SingleLine,NumberMenu,NoColor",
  171. q: repoQuery{pkgARepo, pkgBRepo},
  172. args: args{
  173. searchMode: NumberMenu,
  174. singleLineResults: true,
  175. },
  176. want: "1 dba/package-a 1.0.0 (1.0 B 1.0 B) \tPackage A description\n2 dbb/package-b 1.0.0 (1.0 B 1.0 B) \tPackage B description\n",
  177. },
  178. {
  179. name: "REPO,DoubleLine,Detailed,NoColor",
  180. q: repoQuery{pkgARepo, pkgBRepo},
  181. args: args{
  182. searchMode: Detailed,
  183. singleLineResults: false,
  184. },
  185. want: "dba/package-a 1.0.0 (1.0 B 1.0 B) \n Package A description\ndbb/package-b 1.0.0 (1.0 B 1.0 B) \n Package B description\n",
  186. },
  187. {
  188. name: "REPO,SingleLine,Detailed,NoColor",
  189. q: repoQuery{pkgARepo, pkgBRepo},
  190. args: args{
  191. searchMode: Detailed,
  192. singleLineResults: true,
  193. },
  194. want: "dba/package-a 1.0.0 (1.0 B 1.0 B) \tPackage A description\ndbb/package-b 1.0.0 (1.0 B 1.0 B) \tPackage B description\n",
  195. },
  196. {
  197. name: "AUR,DoubleLine,Detailed,Color",
  198. q: repoQuery{pkgARepo, pkgBRepo},
  199. args: args{
  200. searchMode: Detailed,
  201. singleLineResults: false,
  202. },
  203. useColor: true,
  204. want: "\x1b[1m\x1b[35mdba\x1b[0m\x1b[0m/\x1b[1mpackage-a\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n Package A description\n\x1b[1m\x1b[36mdbb\x1b[0m\x1b[0m/\x1b[1mpackage-b\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n Package B description\n",
  205. },
  206. {
  207. name: "REPO,SingleLine,Detailed,Color",
  208. q: repoQuery{pkgARepo, pkgBRepo},
  209. args: args{
  210. searchMode: Detailed,
  211. singleLineResults: true,
  212. },
  213. useColor: true,
  214. want: "\x1b[1m\x1b[35mdba\x1b[0m\x1b[0m/\x1b[1mpackage-a\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tPackage A description\n\x1b[1m\x1b[36mdbb\x1b[0m\x1b[0m/\x1b[1mpackage-b\x1b[0m \x1b[36m1.0.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tPackage B description\n",
  215. },
  216. {
  217. name: "REPO,NoPackages",
  218. q: repoQuery{},
  219. args: args{
  220. searchMode: Detailed,
  221. singleLineResults: true,
  222. },
  223. useColor: true,
  224. want: "",
  225. },
  226. }
  227. for _, tt := range tests {
  228. t.Run(tt.name, func(t *testing.T) {
  229. w := &strings.Builder{}
  230. sortMode := 1
  231. executor := mock.DBExecutor{}
  232. text.UseColor = tt.useColor
  233. // Fire
  234. tt.q.printSearch(w, executor, tt.args.searchMode, sortMode, tt.args.singleLineResults)
  235. got := w.String()
  236. assert.Equal(t, tt.want, got)
  237. })
  238. }
  239. }