diffview_test.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package diffview_test
  2. import (
  3. _ "embed"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "github.com/alecthomas/chroma/v2/styles"
  8. "github.com/charmbracelet/crush/internal/ui/diffview"
  9. "github.com/charmbracelet/x/ansi"
  10. "github.com/charmbracelet/x/exp/golden"
  11. )
  12. //go:embed testdata/TestDefault.before
  13. var TestDefaultBefore string
  14. //go:embed testdata/TestDefault.after
  15. var TestDefaultAfter string
  16. //go:embed testdata/TestMultipleHunks.before
  17. var TestMultipleHunksBefore string
  18. //go:embed testdata/TestMultipleHunks.after
  19. var TestMultipleHunksAfter string
  20. //go:embed testdata/TestNarrow.before
  21. var TestNarrowBefore string
  22. //go:embed testdata/TestNarrow.after
  23. var TestNarrowAfter string
  24. //go:embed testdata/TestTabs.before
  25. var TestTabsBefore string
  26. //go:embed testdata/TestTabs.after
  27. var TestTabsAfter string
  28. //go:embed testdata/TestLineBreakIssue.before
  29. var TestLineBreakIssueBefore string
  30. //go:embed testdata/TestLineBreakIssue.after
  31. var TestLineBreakIssueAfter string
  32. type (
  33. TestFunc func(dv *diffview.DiffView) *diffview.DiffView
  34. TestFuncs map[string]TestFunc
  35. )
  36. var (
  37. UnifiedFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  38. return dv.Unified()
  39. }
  40. SplitFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  41. return dv.Split()
  42. }
  43. DefaultFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  44. return dv.
  45. Before("main.go", TestDefaultBefore).
  46. After("main.go", TestDefaultAfter)
  47. }
  48. NoLineNumbersFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  49. return dv.
  50. Before("main.go", TestDefaultBefore).
  51. After("main.go", TestDefaultAfter).
  52. LineNumbers(false)
  53. }
  54. MultipleHunksFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  55. return dv.
  56. Before("main.go", TestMultipleHunksBefore).
  57. After("main.go", TestMultipleHunksAfter)
  58. }
  59. CustomContextLinesFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  60. return dv.
  61. Before("main.go", TestMultipleHunksBefore).
  62. After("main.go", TestMultipleHunksAfter).
  63. ContextLines(4)
  64. }
  65. NarrowFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  66. return dv.
  67. Before("text.txt", TestNarrowBefore).
  68. After("text.txt", TestNarrowAfter)
  69. }
  70. SmallWidthFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  71. return dv.
  72. Before("main.go", TestMultipleHunksBefore).
  73. After("main.go", TestMultipleHunksAfter).
  74. Width(40)
  75. }
  76. LargeWidthFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  77. return dv.
  78. Before("main.go", TestMultipleHunksBefore).
  79. After("main.go", TestMultipleHunksAfter).
  80. Width(120)
  81. }
  82. NoSyntaxHighlightFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  83. return dv.
  84. Before("main.go", TestMultipleHunksBefore).
  85. After("main.go", TestMultipleHunksAfter).
  86. ChromaStyle(nil)
  87. }
  88. LightModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  89. return dv.
  90. Style(diffview.DefaultLightStyle()).
  91. ChromaStyle(styles.Get("catppuccin-latte"))
  92. }
  93. DarkModeFunc = func(dv *diffview.DiffView) *diffview.DiffView {
  94. return dv.
  95. Style(diffview.DefaultDarkStyle()).
  96. ChromaStyle(styles.Get("catppuccin-macchiato"))
  97. }
  98. LayoutFuncs = TestFuncs{
  99. "Unified": UnifiedFunc,
  100. "Split": SplitFunc,
  101. }
  102. BehaviorFuncs = TestFuncs{
  103. "Default": DefaultFunc,
  104. "NoLineNumbers": NoLineNumbersFunc,
  105. "MultipleHunks": MultipleHunksFunc,
  106. "CustomContextLines": CustomContextLinesFunc,
  107. "Narrow": NarrowFunc,
  108. "SmallWidth": SmallWidthFunc,
  109. "LargeWidth": LargeWidthFunc,
  110. "NoSyntaxHighlight": NoSyntaxHighlightFunc,
  111. }
  112. ThemeFuncs = TestFuncs{
  113. "LightMode": LightModeFunc,
  114. "DarkMode": DarkModeFunc,
  115. }
  116. )
  117. func TestDiffView(t *testing.T) {
  118. for layoutName, layoutFunc := range LayoutFuncs {
  119. t.Run(layoutName, func(t *testing.T) {
  120. for behaviorName, behaviorFunc := range BehaviorFuncs {
  121. t.Run(behaviorName, func(t *testing.T) {
  122. for themeName, themeFunc := range ThemeFuncs {
  123. t.Run(themeName, func(t *testing.T) {
  124. t.Parallel()
  125. dv := diffview.New()
  126. dv = layoutFunc(dv)
  127. dv = themeFunc(dv)
  128. dv = behaviorFunc(dv)
  129. output := dv.String()
  130. golden.RequireEqual(t, []byte(output))
  131. switch behaviorName {
  132. case "SmallWidth":
  133. assertLineWidth(t, 40, output)
  134. case "LargeWidth":
  135. assertLineWidth(t, 120, output)
  136. }
  137. })
  138. }
  139. })
  140. }
  141. })
  142. }
  143. }
  144. func TestDiffViewTabs(t *testing.T) {
  145. t.Parallel()
  146. for layoutName, layoutFunc := range LayoutFuncs {
  147. t.Run(layoutName, func(t *testing.T) {
  148. t.Parallel()
  149. dv := diffview.New().
  150. Before("main.go", TestTabsBefore).
  151. After("main.go", TestTabsAfter).
  152. Style(diffview.DefaultLightStyle()).
  153. ChromaStyle(styles.Get("catppuccin-latte"))
  154. dv = layoutFunc(dv)
  155. output := dv.String()
  156. golden.RequireEqual(t, []byte(output))
  157. })
  158. }
  159. }
  160. func TestDiffViewLineBreakIssue(t *testing.T) {
  161. t.Parallel()
  162. for layoutName, layoutFunc := range LayoutFuncs {
  163. t.Run(layoutName, func(t *testing.T) {
  164. t.Parallel()
  165. dv := diffview.New().
  166. Before("index.js", TestLineBreakIssueBefore).
  167. After("index.js", TestLineBreakIssueAfter).
  168. Style(diffview.DefaultLightStyle()).
  169. ChromaStyle(styles.Get("catppuccin-latte"))
  170. dv = layoutFunc(dv)
  171. output := dv.String()
  172. golden.RequireEqual(t, []byte(output))
  173. })
  174. }
  175. }
  176. func TestDiffViewWidth(t *testing.T) {
  177. for layoutName, layoutFunc := range LayoutFuncs {
  178. t.Run(layoutName, func(t *testing.T) {
  179. for width := 1; width <= 110; width++ {
  180. if layoutName == "Unified" && width > 60 {
  181. continue
  182. }
  183. t.Run(fmt.Sprintf("WidthOf%03d", width), func(t *testing.T) {
  184. t.Parallel()
  185. dv := diffview.New().
  186. Before("main.go", TestMultipleHunksBefore).
  187. After("main.go", TestMultipleHunksAfter).
  188. Width(width).
  189. Style(diffview.DefaultLightStyle()).
  190. ChromaStyle(styles.Get("catppuccin-latte"))
  191. dv = layoutFunc(dv)
  192. output := dv.String()
  193. golden.RequireEqual(t, []byte(output))
  194. assertLineWidth(t, width, output)
  195. })
  196. }
  197. })
  198. }
  199. }
  200. func TestDiffViewHeight(t *testing.T) {
  201. for layoutName, layoutFunc := range LayoutFuncs {
  202. t.Run(layoutName, func(t *testing.T) {
  203. for height := 1; height <= 20; height++ {
  204. t.Run(fmt.Sprintf("HeightOf%03d", height), func(t *testing.T) {
  205. t.Parallel()
  206. dv := diffview.New().
  207. Before("main.go", TestMultipleHunksBefore).
  208. After("main.go", TestMultipleHunksAfter).
  209. Height(height).
  210. Style(diffview.DefaultLightStyle()).
  211. ChromaStyle(styles.Get("catppuccin-latte"))
  212. dv = layoutFunc(dv)
  213. output := dv.String()
  214. golden.RequireEqual(t, []byte(output))
  215. })
  216. }
  217. })
  218. }
  219. }
  220. func TestDiffViewXOffset(t *testing.T) {
  221. for layoutName, layoutFunc := range LayoutFuncs {
  222. t.Run(layoutName, func(t *testing.T) {
  223. for xOffset := range 21 {
  224. t.Run(fmt.Sprintf("XOffsetOf%02d", xOffset), func(t *testing.T) {
  225. t.Parallel()
  226. dv := diffview.New().
  227. Before("main.go", TestDefaultBefore).
  228. After("main.go", TestDefaultAfter).
  229. Style(diffview.DefaultLightStyle()).
  230. ChromaStyle(styles.Get("catppuccin-latte")).
  231. Width(60).
  232. XOffset(xOffset)
  233. dv = layoutFunc(dv)
  234. output := dv.String()
  235. golden.RequireEqual(t, []byte(output))
  236. assertLineWidth(t, 60, output)
  237. })
  238. }
  239. })
  240. }
  241. }
  242. func TestDiffViewYOffset(t *testing.T) {
  243. for layoutName, layoutFunc := range LayoutFuncs {
  244. t.Run(layoutName, func(t *testing.T) {
  245. for yOffset := range 17 {
  246. t.Run(fmt.Sprintf("YOffsetOf%02d", yOffset), func(t *testing.T) {
  247. t.Parallel()
  248. dv := diffview.New().
  249. Before("main.go", TestMultipleHunksBefore).
  250. After("main.go", TestMultipleHunksAfter).
  251. Style(diffview.DefaultLightStyle()).
  252. ChromaStyle(styles.Get("catppuccin-latte")).
  253. Height(5).
  254. YOffset(yOffset)
  255. dv = layoutFunc(dv)
  256. output := dv.String()
  257. golden.RequireEqual(t, []byte(output))
  258. })
  259. }
  260. })
  261. }
  262. }
  263. func TestDiffViewYOffsetInfinite(t *testing.T) {
  264. for layoutName, layoutFunc := range LayoutFuncs {
  265. t.Run(layoutName, func(t *testing.T) {
  266. for yOffset := range 17 {
  267. t.Run(fmt.Sprintf("YOffsetOf%02d", yOffset), func(t *testing.T) {
  268. t.Parallel()
  269. dv := diffview.New().
  270. Before("main.go", TestMultipleHunksBefore).
  271. After("main.go", TestMultipleHunksAfter).
  272. Style(diffview.DefaultLightStyle()).
  273. ChromaStyle(styles.Get("catppuccin-latte")).
  274. Height(5).
  275. YOffset(yOffset).
  276. InfiniteYScroll(true)
  277. dv = layoutFunc(dv)
  278. output := dv.String()
  279. golden.RequireEqual(t, []byte(output))
  280. })
  281. }
  282. })
  283. }
  284. }
  285. func assertLineWidth(t *testing.T, expected int, output string) {
  286. var lineWidth int
  287. for line := range strings.SplitSeq(output, "\n") {
  288. lineWidth = max(lineWidth, ansi.StringWidth(line))
  289. }
  290. if lineWidth != expected {
  291. t.Errorf("expected output width to be == %d, got %d", expected, lineWidth)
  292. }
  293. }
  294. func assertHeight(t *testing.T, expected int, output string) {
  295. output = strings.TrimSuffix(output, "\n")
  296. lines := strings.Count(output, "\n") + 1
  297. if lines != expected {
  298. t.Errorf("expected output height to be == %d, got %d", expected, lines)
  299. }
  300. }