aboutsummaryrefslogtreecommitdiff
path: root/spec/cli/test_reserve_comments.sh
diff options
context:
space:
mode:
Diffstat (limited to 'spec/cli/test_reserve_comments.sh')
-rwxr-xr-xspec/cli/test_reserve_comments.sh189
1 files changed, 189 insertions, 0 deletions
diff --git a/spec/cli/test_reserve_comments.sh b/spec/cli/test_reserve_comments.sh
new file mode 100755
index 0000000..6c0c1a7
--- /dev/null
+++ b/spec/cli/test_reserve_comments.sh
@@ -0,0 +1,189 @@
1#!/bin/bash
2# Test Reserve Comments Functionality for YueScript CLI
3# Tests: -c, --reserve-comments option
4
5SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6source "$SCRIPT_DIR/cli_test_helper.sh"
7
8# Check binary
9check_yue_binary
10
11# Setup test environment
12setup_test_env
13TMP_DIR=$(get_test_tmp_dir)
14
15echo "========================================"
16echo "Testing Reserve Comments (-c) Option"
17echo "========================================"
18echo ""
19
20# Test 1: Reserve top-level comments
21echo "Testing top-level comments preservation..."
22cat > "$TMP_DIR/top_level.yue" << 'EOF'
23-- Top level comment
24x = 1
25-- Another comment
26y = 2
27EOF
28
29assert_output_contains "Reserve top-level comments" "Top level comment" $YUE_BIN -c -p "$TMP_DIR/top_level.yue"
30assert_output_contains "Reserve second comment" "Another comment" $YUE_BIN -c -p "$TMP_DIR/top_level.yue"
31
32# Test 2: Without -c option, comments should not appear
33echo ""
34echo "Testing comments are removed without -c option..."
35assert_output_not_contains "Comments should be removed without -c" "Top level comment" $YUE_BIN -p "$TMP_DIR/top_level.yue"
36
37# Test 3: Reserve comments in table
38echo ""
39echo "Testing comments in tables..."
40cat > "$TMP_DIR/table_comments.yue" << 'EOF'
41t = {
42 -- First value comment
43 1,
44 -- Second value comment
45 2
46}
47EOF
48
49assert_output_contains "Table comments should be preserved" "First value comment" $YUE_BIN -c -p "$TMP_DIR/table_comments.yue"
50assert_output_contains "Table second comment preserved" "Second value comment" $YUE_BIN -c -p "$TMP_DIR/table_comments.yue"
51
52# Test 4: Reserve comments in if statement
53echo ""
54echo "Testing comments in if statements..."
55cat > "$TMP_DIR/if_comments.yue" << 'EOF'
56if true
57 -- Inside if block
58 print "test"
59EOF
60
61assert_output_contains "If block comments should be preserved" "Inside if block" $YUE_BIN -c -p "$TMP_DIR/if_comments.yue"
62
63# Test 5: Reserve comments in function
64echo ""
65echo "Testing comments in functions..."
66cat > "$TMP_DIR/func_comments.yue" << 'EOF'
67func = =>
68 -- Inside function
69 print "hello"
70EOF
71
72assert_output_contains "Function comments should be preserved" "Inside function" $YUE_BIN -c -p "$TMP_DIR/func_comments.yue"
73
74# Test 6: Reserve comments with empty lines
75echo ""
76echo "Testing empty lines preservation..."
77cat > "$TMP_DIR/empty_lines.yue" << 'EOF'
78-- First comment
79x = 1
80-- Second comment
81EOF
82
83OUTPUT_WITHOUT_C=$($YUE_BIN -p "$TMP_DIR/empty_lines.yue")
84OUTPUT_WITH_C=$($YUE_BIN -c -p "$TMP_DIR/empty_lines.yue")
85if [ $? -eq 0 ]; then
86 # Count newlines - with -c should have more (or equal) lines due to comment preservation
87 LINES_WITHOUT_C=$(echo "$OUTPUT_WITHOUT_C" | wc -l)
88 LINES_WITH_C=$(echo "$OUTPUT_WITH_C" | wc -l)
89 if [ $LINES_WITH_C -ge $LINES_WITHOUT_C ]; then
90 echo -e "${GREEN}✓${NC} Empty lines and comments should be preserved"
91 TESTS_PASSED=$((TESTS_PASSED + 1))
92 else
93 echo -e "${RED}✗${NC} Empty lines and comments should be preserved"
94 echo -e " ${YELLOW}Lines without -c: $LINES_WITHOUT_C, with -c: $LINES_WITH_C${NC}"
95 TESTS_FAILED=$((TESTS_FAILED + 1))
96 fi
97 TESTS_RUN=$((TESTS_RUN + 1))
98else
99 echo -e "${RED}✗${NC} Empty lines test failed"
100 TESTS_FAILED=$((TESTS_FAILED + 1))
101 TESTS_RUN=$((TESTS_RUN + 1))
102fi
103
104# Test 7: Reserve comments in table with TableBlock syntax
105echo ""
106echo "Testing comments in TableBlock..."
107cat > "$TMP_DIR/tableblock_comments.yue" << 'EOF'
108tbl = {
109 -- Key comment
110 key: "value"
111 -- Another key
112 another: 123
113}
114EOF
115
116assert_output_contains "TableBlock key comment preserved" "Key comment" $YUE_BIN -c -p "$TMP_DIR/tableblock_comments.yue"
117assert_output_contains "TableBlock second comment preserved" "Another key" $YUE_BIN -c -p "$TMP_DIR/tableblock_comments.yue"
118
119# Test 8: Reserve comments - long form option
120echo ""
121echo "Testing --reserve-comments long form option..."
122assert_output_contains "Long form option should preserve comments" "First value comment" $YUE_BIN --reserve-comments -p "$TMP_DIR/table_comments.yue"
123
124# Test 9: Compile to file with reserve comments
125echo ""
126echo "Testing compilation to file with comments..."
127cat > "$TMP_DIR/file_comment.yue" << 'EOF'
128-- This is a test
129value = 42
130EOF
131
132assert_success "Compile with -c to file" $YUE_BIN -c "$TMP_DIR/file_comment.yue" -o "$TMP_DIR/file_comment.lua"
133assert_file_exists "Output file should exist" "$TMP_DIR/file_comment.lua"
134assert_output_contains "Compiled file should contain comments" "This is a test" cat "$TMP_DIR/file_comment.lua"
135
136# Test 10: Reserve comments with multiple statements
137echo ""
138echo "Testing comments with multiple statements..."
139cat > "$TMP_DIR/multi_stmt.yue" << 'EOF'
140-- Assign x
141x = 1
142-- Assign y
143y = 2
144-- Assign z
145z = 3
146EOF
147
148OUTPUT=$($YUE_BIN -c -p "$TMP_DIR/multi_stmt.yue")
149if [ $? -eq 0 ]; then
150 if echo "$OUTPUT" | grep -q "Assign x" && echo "$OUTPUT" | grep -q "Assign y" && echo "$OUTPUT" | grep -q "Assign z"; then
151 echo -e "${GREEN}✓${NC} All comments should be preserved"
152 TESTS_PASSED=$((TESTS_PASSED + 1))
153 else
154 echo -e "${RED}✗${NC} All comments should be preserved"
155 echo -e " ${YELLOW}Output: $OUTPUT${NC}"
156 TESTS_FAILED=$((TESTS_FAILED + 1))
157 fi
158 TESTS_RUN=$((TESTS_RUN + 1))
159else
160 echo -e "${RED}✗${NC} Multiple statements test failed"
161 TESTS_FAILED=$((TESTS_FAILED + 1))
162 TESTS_RUN=$((TESTS_RUN + 1))
163fi
164
165# Test 11: Comments in while loop
166echo ""
167echo "Testing comments in while loop..."
168cat > "$TMP_DIR/while_comments.yue" << 'EOF'
169while true
170 -- Loop body comment
171 print "looping"
172 break
173EOF
174
175assert_output_contains "While loop comments preserved" "Loop body comment" $YUE_BIN -c -p "$TMP_DIR/while_comments.yue"
176
177# Test 12: Comments in for loop
178echo ""
179echo "Testing comments in for loop..."
180cat > "$TMP_DIR/for_comments.yue" << 'EOF'
181for i = 1, 3
182 -- For loop comment
183 print i
184EOF
185
186assert_output_contains "For loop comments preserved" "For loop comment" $YUE_BIN -c -p "$TMP_DIR/for_comments.yue"
187
188echo ""
189print_summary