diff options
Diffstat (limited to 'spec/cli/test_reserve_comments.sh')
| -rwxr-xr-x | spec/cli/test_reserve_comments.sh | 189 |
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 | |||
| 5 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| 6 | source "$SCRIPT_DIR/cli_test_helper.sh" | ||
| 7 | |||
| 8 | # Check binary | ||
| 9 | check_yue_binary | ||
| 10 | |||
| 11 | # Setup test environment | ||
| 12 | setup_test_env | ||
| 13 | TMP_DIR=$(get_test_tmp_dir) | ||
| 14 | |||
| 15 | echo "========================================" | ||
| 16 | echo "Testing Reserve Comments (-c) Option" | ||
| 17 | echo "========================================" | ||
| 18 | echo "" | ||
| 19 | |||
| 20 | # Test 1: Reserve top-level comments | ||
| 21 | echo "Testing top-level comments preservation..." | ||
| 22 | cat > "$TMP_DIR/top_level.yue" << 'EOF' | ||
| 23 | -- Top level comment | ||
| 24 | x = 1 | ||
| 25 | -- Another comment | ||
| 26 | y = 2 | ||
| 27 | EOF | ||
| 28 | |||
| 29 | assert_output_contains "Reserve top-level comments" "Top level comment" $YUE_BIN -c -p "$TMP_DIR/top_level.yue" | ||
| 30 | assert_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 | ||
| 33 | echo "" | ||
| 34 | echo "Testing comments are removed without -c option..." | ||
| 35 | assert_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 | ||
| 38 | echo "" | ||
| 39 | echo "Testing comments in tables..." | ||
| 40 | cat > "$TMP_DIR/table_comments.yue" << 'EOF' | ||
| 41 | t = { | ||
| 42 | -- First value comment | ||
| 43 | 1, | ||
| 44 | -- Second value comment | ||
| 45 | 2 | ||
| 46 | } | ||
| 47 | EOF | ||
| 48 | |||
| 49 | assert_output_contains "Table comments should be preserved" "First value comment" $YUE_BIN -c -p "$TMP_DIR/table_comments.yue" | ||
| 50 | assert_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 | ||
| 53 | echo "" | ||
| 54 | echo "Testing comments in if statements..." | ||
| 55 | cat > "$TMP_DIR/if_comments.yue" << 'EOF' | ||
| 56 | if true | ||
| 57 | -- Inside if block | ||
| 58 | print "test" | ||
| 59 | EOF | ||
| 60 | |||
| 61 | assert_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 | ||
| 64 | echo "" | ||
| 65 | echo "Testing comments in functions..." | ||
| 66 | cat > "$TMP_DIR/func_comments.yue" << 'EOF' | ||
| 67 | func = => | ||
| 68 | -- Inside function | ||
| 69 | print "hello" | ||
| 70 | EOF | ||
| 71 | |||
| 72 | assert_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 | ||
| 75 | echo "" | ||
| 76 | echo "Testing empty lines preservation..." | ||
| 77 | cat > "$TMP_DIR/empty_lines.yue" << 'EOF' | ||
| 78 | -- First comment | ||
| 79 | x = 1 | ||
| 80 | -- Second comment | ||
| 81 | EOF | ||
| 82 | |||
| 83 | OUTPUT_WITHOUT_C=$($YUE_BIN -p "$TMP_DIR/empty_lines.yue") | ||
| 84 | OUTPUT_WITH_C=$($YUE_BIN -c -p "$TMP_DIR/empty_lines.yue") | ||
| 85 | if [ $? -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)) | ||
| 98 | else | ||
| 99 | echo -e "${RED}✗${NC} Empty lines test failed" | ||
| 100 | TESTS_FAILED=$((TESTS_FAILED + 1)) | ||
| 101 | TESTS_RUN=$((TESTS_RUN + 1)) | ||
| 102 | fi | ||
| 103 | |||
| 104 | # Test 7: Reserve comments in table with TableBlock syntax | ||
| 105 | echo "" | ||
| 106 | echo "Testing comments in TableBlock..." | ||
| 107 | cat > "$TMP_DIR/tableblock_comments.yue" << 'EOF' | ||
| 108 | tbl = { | ||
| 109 | -- Key comment | ||
| 110 | key: "value" | ||
| 111 | -- Another key | ||
| 112 | another: 123 | ||
| 113 | } | ||
| 114 | EOF | ||
| 115 | |||
| 116 | assert_output_contains "TableBlock key comment preserved" "Key comment" $YUE_BIN -c -p "$TMP_DIR/tableblock_comments.yue" | ||
| 117 | assert_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 | ||
| 120 | echo "" | ||
| 121 | echo "Testing --reserve-comments long form option..." | ||
| 122 | assert_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 | ||
| 125 | echo "" | ||
| 126 | echo "Testing compilation to file with comments..." | ||
| 127 | cat > "$TMP_DIR/file_comment.yue" << 'EOF' | ||
| 128 | -- This is a test | ||
| 129 | value = 42 | ||
| 130 | EOF | ||
| 131 | |||
| 132 | assert_success "Compile with -c to file" $YUE_BIN -c "$TMP_DIR/file_comment.yue" -o "$TMP_DIR/file_comment.lua" | ||
| 133 | assert_file_exists "Output file should exist" "$TMP_DIR/file_comment.lua" | ||
| 134 | assert_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 | ||
| 137 | echo "" | ||
| 138 | echo "Testing comments with multiple statements..." | ||
| 139 | cat > "$TMP_DIR/multi_stmt.yue" << 'EOF' | ||
| 140 | -- Assign x | ||
| 141 | x = 1 | ||
| 142 | -- Assign y | ||
| 143 | y = 2 | ||
| 144 | -- Assign z | ||
| 145 | z = 3 | ||
| 146 | EOF | ||
| 147 | |||
| 148 | OUTPUT=$($YUE_BIN -c -p "$TMP_DIR/multi_stmt.yue") | ||
| 149 | if [ $? -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)) | ||
| 159 | else | ||
| 160 | echo -e "${RED}✗${NC} Multiple statements test failed" | ||
| 161 | TESTS_FAILED=$((TESTS_FAILED + 1)) | ||
| 162 | TESTS_RUN=$((TESTS_RUN + 1)) | ||
| 163 | fi | ||
| 164 | |||
| 165 | # Test 11: Comments in while loop | ||
| 166 | echo "" | ||
| 167 | echo "Testing comments in while loop..." | ||
| 168 | cat > "$TMP_DIR/while_comments.yue" << 'EOF' | ||
| 169 | while true | ||
| 170 | -- Loop body comment | ||
| 171 | print "looping" | ||
| 172 | break | ||
| 173 | EOF | ||
| 174 | |||
| 175 | assert_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 | ||
| 178 | echo "" | ||
| 179 | echo "Testing comments in for loop..." | ||
| 180 | cat > "$TMP_DIR/for_comments.yue" << 'EOF' | ||
| 181 | for i = 1, 3 | ||
| 182 | -- For loop comment | ||
| 183 | print i | ||
| 184 | EOF | ||
| 185 | |||
| 186 | assert_output_contains "For loop comments preserved" "For loop comment" $YUE_BIN -c -p "$TMP_DIR/for_comments.yue" | ||
| 187 | |||
| 188 | echo "" | ||
| 189 | print_summary | ||
