diffview_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/tui/exp/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. assertHeight(t, height, output)
  216. })
  217. }
  218. })
  219. }
  220. }
  221. func TestDiffViewXOffset(t *testing.T) {
  222. for layoutName, layoutFunc := range LayoutFuncs {
  223. t.Run(layoutName, func(t *testing.T) {
  224. for xOffset := range 21 {
  225. t.Run(fmt.Sprintf("XOffsetOf%02d", xOffset), func(t *testing.T) {
  226. t.Parallel()
  227. dv := diffview.New().
  228. Before("main.go", TestDefaultBefore).
  229. After("main.go", TestDefaultAfter).
  230. Style(diffview.DefaultLightStyle()).
  231. ChromaStyle(styles.Get("catppuccin-latte")).
  232. Width(60).
  233. XOffset(xOffset)
  234. dv = layoutFunc(dv)
  235. output := dv.String()
  236. golden.RequireEqual(t, []byte(output))
  237. assertLineWidth(t, 60, output)
  238. })
  239. }
  240. })
  241. }
  242. }
  243. func TestDiffViewYOffset(t *testing.T) {
  244. for layoutName, layoutFunc := range LayoutFuncs {
  245. t.Run(layoutName, func(t *testing.T) {
  246. for yOffset := range 17 {
  247. t.Run(fmt.Sprintf("YOffsetOf%02d", yOffset), func(t *testing.T) {
  248. t.Parallel()
  249. dv := diffview.New().
  250. Before("main.go", TestMultipleHunksBefore).
  251. After("main.go", TestMultipleHunksAfter).
  252. Style(diffview.DefaultLightStyle()).
  253. ChromaStyle(styles.Get("catppuccin-latte")).
  254. Height(5).
  255. YOffset(yOffset)
  256. dv = layoutFunc(dv)
  257. output := dv.String()
  258. golden.RequireEqual(t, []byte(output))
  259. assertHeight(t, 5, output)
  260. })
  261. }
  262. })
  263. }
  264. }
  265. func TestDiffViewYOffsetInfinite(t *testing.T) {
  266. for layoutName, layoutFunc := range LayoutFuncs {
  267. t.Run(layoutName, func(t *testing.T) {
  268. for yOffset := range 17 {
  269. t.Run(fmt.Sprintf("YOffsetOf%02d", yOffset), func(t *testing.T) {
  270. t.Parallel()
  271. dv := diffview.New().
  272. Before("main.go", TestMultipleHunksBefore).
  273. After("main.go", TestMultipleHunksAfter).
  274. Style(diffview.DefaultLightStyle()).
  275. ChromaStyle(styles.Get("catppuccin-latte")).
  276. Height(5).
  277. YOffset(yOffset).
  278. InfiniteYScroll(true)
  279. dv = layoutFunc(dv)
  280. output := dv.String()
  281. golden.RequireEqual(t, []byte(output))
  282. assertHeight(t, 5, output)
  283. })
  284. }
  285. })
  286. }
  287. }
  288. func assertLineWidth(t *testing.T, expected int, output string) {
  289. var lineWidth int
  290. for line := range strings.SplitSeq(output, "\n") {
  291. lineWidth = max(lineWidth, ansi.StringWidth(line))
  292. }
  293. if lineWidth != expected {
  294. t.Errorf("expected output width to be == %d, got %d", expected, lineWidth)
  295. }
  296. }
  297. func assertHeight(t *testing.T, expected int, output string) {
  298. output = strings.TrimSuffix(output, "\n")
  299. lines := strings.Count(output, "\n") + 1
  300. if lines != expected {
  301. t.Errorf("expected output height to be == %d, got %d", expected, lines)
  302. }
  303. }