diff --git a/test/test01.md b/test/parser/test01.md similarity index 100% rename from test/test01.md rename to test/parser/test01.md diff --git a/test/test02.md b/test/parser/test02.md similarity index 100% rename from test/test02.md rename to test/parser/test02.md diff --git a/test/parser/test_md_parser.sh b/test/parser/test_md_parser.sh new file mode 100755 index 0000000..a13e813 --- /dev/null +++ b/test/parser/test_md_parser.sh @@ -0,0 +1,157 @@ +#!/usr/bin/env bash + +# Text color variables +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' # No Color (reset) + +# Ensure a script is provided +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi + + +PARSER="$1" + +# Check if the parser script exists +if [ ! -f "$PARSER" ]; then + echo "Error: $PARSER not found!" + exit 1 +fi + +# Determine if it's AWK or Shell (optional second argument) +PARSER_TYPE="bash" # Default to bash script +if [ -n "$2" ]; then + PARSER_TYPE="$2" +fi + +# Define test cases as an array of markdown inputs and expected outputs +declare -a tests=( + "Header 1" + "# Header 1" + "

Header 1

" + + "Header 2" + "## Header 2" + "

Header 2

" + + "Header 3" + "### Header 3" + "

Header 3

" + + "Header 4" + "#### Header 4" + "

Header 4

" + + "Header 5" + "##### Header 5" + "
Header 5
" + + "Header 6" + "###### Header 6" + "
Header 6
" + + "Multiple headers 1" + $'# Header 1\n## Header 2' + "

Header 1

Header 2

" + + # "Multiple headers 2" + # $'### Header 3\n\n## Header 2\n\n# Header 1' + # "

Header 3

Header 2

Header 1

" + + "Unordered List" + $'- item\n* item' + "" + + "Ordered List" + $'1. item1\n1. item1\n3. item3' + "
  1. item1
  2. item1
  3. item3
" + + "Blockquote 1" + "> test of blockquote" + "
test of blockquote
" + + "Blockquote 2" + $'> line1\n> line2' + "
line1line2
" + + "Blockquote 2" + $'> line1\nline2' + "
line1line2
" + + "Code Block 1" + $' code1' + "
code1
" + + "Code Block 2" + $'\tcode1' + "
code1
" + + "Paragraph 1" + "paragraph 1" + "

paragraph 1

" + + "Paragraph 2" + "paragraph *emphasis* and **strong**" + "

paragraph emphasis and strong

" + + "Mix Code blocks and paragraphs 1" + $'First paragraph\n\n code block' + "

First paragraph

code block
" + + "Mix Code blocks and paragraphs 2" + $'First paragraph\n\n code1\n code2\n\nSecond paragraph' + "

First paragraph

code1code2

Second paragraph

" + + # You can add more test cases following the same format... +) + +input="# test" +expected="

test

" + +# Function to run a single test case +run_test() { + local input="$1" + local expected="$2" + local actual="" + + # Get the actual output from the parser + if [ "$PARSER_TYPE" == "awk" ]; then + # Run AWK script with the input + actual=$(echo "$input" | awk -f "$PARSER" | tr -d '\n') + else + # Assume it's a shell script, run it + actual=$(echo "$input" | bash "$PARSER" | tr -d '\n') + fi + + # Compare the actual output with the expected output + if [ "$actual" == "$expected" ]; then + echo -e "${GREEN}Test Passed!${NC}" + return 0 + else + echo -e "${RED}Test Failed!${NC}" + echo "Input:" + echo "$input" + echo "Expected:" + echo "$expected" + echo "Got:" + echo "$actual" + return 1 + fi +} + +STATUS=0 + +# Main loop to run all test cases +num_tests=$((${#tests[@]} / 3)) # Divide by 2 because each test has input/output pair +for ((i = 0; i < num_tests; i++)); do + input="${tests[i * 3 + 1]}" + expected="${tests[i * 3 + 2]}" + echo "Test $((i + 1)):" ${tests[i * 3]} + run_test "$input" "$expected" + STATUS_i=$? + STATUS=$((STATUS+STATUS_i)) +done + +exit $STATUS \ No newline at end of file diff --git a/test/test.sh b/test/test.sh index 26f4f95..dcbab40 100755 --- a/test/test.sh +++ b/test/test.sh @@ -3,147 +3,9 @@ # Text color variables RED='\033[0;31m' GREEN='\033[0;32m' +YELLOW='\033[0;33m' NC='\033[0m' # No Color (reset) -# Ensure a script is provided -if [ -z "$1" ]; then - echo "Usage: $0 " - exit 1 -fi +echo -e "${YELLOW}Testing markown parser :${NC}\n" - -PARSER="$1" - -# Check if the parser script exists -if [ ! -f "$PARSER" ]; then - echo "Error: $PARSER not found!" - exit 1 -fi - -# Determine if it's AWK or Shell (optional second argument) -PARSER_TYPE="bash" # Default to bash script -if [ -n "$2" ]; then - PARSER_TYPE="$2" -fi - -# Define test cases as an array of markdown inputs and expected outputs -declare -a tests=( - "Header 1" - "# Header 1" - "

Header 1

" - - "Header 2" - "## Header 2" - "

Header 2

" - - "Header 3" - "### Header 3" - "

Header 3

" - - "Header 4" - "#### Header 4" - "

Header 4

" - - "Header 5" - "##### Header 5" - "
Header 5
" - - "Header 6" - "###### Header 6" - "
Header 6
" - - "Multiple headers 1" - $'# Header 1\n## Header 2' - "

Header 1

Header 2

" - - # "Multiple headers 2" - # $'### Header 3\n\n## Header 2\n\n# Header 1' - # "

Header 3

Header 2

Header 1

" - - "Unordered List" - $'- item\n* item' - "" - - "Ordered List" - $'1. item1\n1. item1\n3. item3' - "
  1. item1
  2. item1
  3. item3
" - - "Blockquote 1" - "> test of blockquote" - "
test of blockquote
" - - "Blockquote 2" - $'> line1\n> line2' - "
line1line2
" - - "Blockquote 2" - $'> line1\nline2' - "
line1line2
" - - "Code Block 1" - $' code1' - "
code1
" - - "Code Block 2" - $'\tcode1' - "
code1
" - - "Paragraph 1" - "paragraph 1" - "

paragraph 1

" - - "Paragraph 2" - "paragraph *emphasis* and **strong**" - "

paragraph emphasis and strong

" - - "Mix Code blocks and paragraphs 1" - $'First paragraph\n\n code block' - "

First paragraph

code block
" - - "Mix Code blocks and paragraphs 2" - $'First paragraph\n\n code1\n code2\n\nSecond paragraph' - "

First paragraph

code1code2

Second paragraph

" - - # You can add more test cases following the same format... -) - -input="# test" -expected="

test

" - -# Function to run a single test case -run_test() { - local input="$1" - local expected="$2" - local actual="" - - # Get the actual output from the parser - if [ "$PARSER_TYPE" == "awk" ]; then - # Run AWK script with the input - actual=$(echo "$input" | awk -f "$PARSER" | tr -d '\n') - else - # Assume it's a shell script, run it - actual=$(echo "$input" | bash "$PARSER" | tr -d '\n') - fi - - # Compare the actual output with the expected output - if [ "$actual" == "$expected" ]; then - echo -e "${GREEN}Test Passed!${NC}" - else - echo -e "${RED}Test Failed!${NC}" - echo "Input:" - echo "$input" - echo "Expected:" - echo "$expected" - echo "Got:" - echo "$actual" - fi -} - -# Main loop to run all test cases -num_tests=$((${#tests[@]} / 3)) # Divide by 2 because each test has input/output pair -for ((i = 0; i < num_tests; i++)); do - input="${tests[i * 3 + 1]}" - expected="${tests[i * 3 + 2]}" - echo "Test $((i + 1)):" ${tests[i * 3]} - run_test "$input" "$expected" -done +./parser/test_md_parser.sh ../lib/markdown.awk awk