detect_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package diffdetect
  2. import "testing"
  3. func TestInspect(t *testing.T) {
  4. t.Parallel()
  5. tests := []struct {
  6. name string
  7. content string
  8. want Signal
  9. }{
  10. {
  11. name: "git unified diff",
  12. content: `diff --git a/main.go b/main.go
  13. --- a/main.go
  14. +++ b/main.go
  15. @@ -1,2 +1,3 @@
  16. package main
  17. +import "fmt"
  18. `,
  19. want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: true},
  20. },
  21. {
  22. name: "non-git unified diff",
  23. content: `--- old.c
  24. +++ old.c
  25. @@ -1 +1 @@
  26. -old
  27. +new
  28. `,
  29. want: Signal{HasHunk: true, HasFileHeader: true, HasGitHeader: false},
  30. },
  31. {
  32. name: "plain text",
  33. content: "hello world",
  34. want: Signal{},
  35. },
  36. {
  37. name: "hunk only",
  38. content: "@@ -1 +1 @@\n-old\n+new\n",
  39. want: Signal{HasHunk: true},
  40. },
  41. {
  42. name: "headers only",
  43. content: "--- a/file\n+++ b/file\n",
  44. want: Signal{HasFileHeader: true},
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. t.Parallel()
  50. got := Inspect(tt.content)
  51. if got != tt.want {
  52. t.Errorf("Inspect() = %+v, want %+v", got, tt.want)
  53. }
  54. })
  55. }
  56. }
  57. func TestIsUnifiedDiff(t *testing.T) {
  58. t.Parallel()
  59. tests := []struct {
  60. name string
  61. content string
  62. want bool
  63. }{
  64. {
  65. name: "github-style multi-file diff",
  66. content: `diff --git a/one.txt b/one.txt
  67. --- a/one.txt
  68. +++ b/one.txt
  69. @@ -1 +1 @@
  70. -a
  71. +b
  72. diff --git a/two.txt b/two.txt
  73. --- a/two.txt
  74. +++ b/two.txt
  75. @@ -1 +1 @@
  76. -c
  77. +d
  78. `,
  79. want: true,
  80. },
  81. {
  82. name: "non-git unified patch",
  83. content: `--- a/old.c
  84. +++ b/old.c
  85. @@ -1,3 +1,4 @@
  86. #include <stdio.h>
  87. -int main() {
  88. +int main(int argc, char **argv) {
  89. return 0;
  90. }
  91. `,
  92. want: true,
  93. },
  94. {
  95. name: "new file from dev null",
  96. content: `--- /dev/null
  97. +++ newfile.txt
  98. @@ -0,0 +1,2 @@
  99. +hello
  100. +world
  101. `,
  102. want: true,
  103. },
  104. {
  105. name: "markdown false positive candidate",
  106. content: `- Item one
  107. - Item two
  108. + Bonus item
  109. - Item three
  110. `,
  111. want: false,
  112. },
  113. {
  114. name: "headers without hunk",
  115. content: `--- a/somefile.txt
  116. +++ b/somefile.txt
  117. Just some content here
  118. No hunk markers at all
  119. `,
  120. want: false,
  121. },
  122. {
  123. name: "hunk without headers",
  124. content: `@@ -1,3 +1,4 @@
  125. some line
  126. -another line
  127. +changed line
  128. `,
  129. want: false,
  130. },
  131. {
  132. name: "empty",
  133. content: "",
  134. want: false,
  135. },
  136. }
  137. for _, tt := range tests {
  138. t.Run(tt.name, func(t *testing.T) {
  139. t.Parallel()
  140. if got := IsUnifiedDiff(tt.content); got != tt.want {
  141. t.Errorf("IsUnifiedDiff() = %v, want %v", got, tt.want)
  142. }
  143. })
  144. }
  145. }