summaryrefslogtreecommitdiff
path: root/internal/showcase/code_extractor_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/showcase/code_extractor_test.go')
-rw-r--r--internal/showcase/code_extractor_test.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/showcase/code_extractor_test.go b/internal/showcase/code_extractor_test.go
new file mode 100644
index 0000000..d091f66
--- /dev/null
+++ b/internal/showcase/code_extractor_test.go
@@ -0,0 +1,56 @@
+package showcase
+
+import "testing"
+
+func TestIsCommentLine(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ trimmed string
+ want bool
+ }{
+ {name: "slash comment", trimmed: "// comment", want: true},
+ {name: "hash comment", trimmed: "# comment", want: true},
+ {name: "include directive", trimmed: "#include <stdio.h>", want: false},
+ {name: "define directive", trimmed: "#define FOO 1", want: false},
+ {name: "html comment", trimmed: "<!-- comment -->", want: true},
+ {name: "block comment inner line", trimmed: "* comment", want: true},
+ {name: "single asterisk", trimmed: "*", want: false},
+ {name: "code line", trimmed: "fmt.Println(\"ok\")", want: false},
+ }
+
+ for _, tc := range tests {
+ tc := tc
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ got := isCommentLine(tc.trimmed)
+ if got != tc.want {
+ t.Fatalf("isCommentLine(%q) = %v, want %v", tc.trimmed, got, tc.want)
+ }
+ })
+ }
+}
+
+func TestStripComments_PreservesPreprocessorDirectives(t *testing.T) {
+ t.Parallel()
+
+ code := `#include <stdio.h>
+#define VALUE 1
+# this is a comment
+int main() {
+ return VALUE; // trailing comment
+}`
+
+ got := stripComments(code)
+ want := `#include <stdio.h>
+#define VALUE 1
+int main() {
+ return VALUE;
+}`
+
+ if got != want {
+ t.Fatalf("stripComments() = %q, want %q", got, want)
+ }
+}