-- Test file for hemar template parser -- Run with: psql -f test_template_parser.sql -- Load extension if not already loaded -- CREATE EXTENSION IF NOT EXISTS hemar; -- SAFETY(yukkop): !!! If you fix identation, you will ruin the tests. -- Create test function to validate template parsing CREATE OR REPLACE FUNCTION test_template_parse(template_text text, expected_structure text) RETURNS boolean AS $$ DECLARE parsed_result text; passed boolean; BEGIN BEGIN parsed_result := hemar.parse(template_text); IF parsed_result IS NULL THEN RAISE WARNING 'Parser returned NULL for template: %', template_text; RETURN false; END IF; passed := position(expected_structure in parsed_result) > 0; IF NOT passed THEN RAISE WARNING 'Template parsing test failed!'; RAISE WARNING 'Template: %', template_text; RAISE WARNING 'Expected to find: %', expected_structure; RAISE WARNING 'Actual result: %', parsed_result; END IF; RETURN passed; EXCEPTION WHEN OTHERS THEN RAISE WARNING 'Exception during parsing: % (state: %)', SQLERRM, SQLSTATE; RAISE WARNING 'Template: %', template_text; RETURN false; END; END; $$ LANGUAGE plpgsql; -- Run the tests DO $$ DECLARE total_tests integer := 0; passed_tests integer := 0; result boolean; BEGIN PERFORM pg_sleep(2); RAISE NOTICE 'Starting template parser tests...'; -- Test 1: Simple interpolation total_tests := total_tests + 1; result := test_template_parse( $hemar1${{ simple_var }}$hemar1$, $expected1$INTERPOLATE: "simple_var"$expected1$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Simple interpolation - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Simple interpolation - FAILED', total_tests; END IF; -- Test 2: Interpolation with surrounding text total_tests := total_tests + 1; result := test_template_parse( $hemar2$Hello, {{ name }}!$hemar2$, $expected2$TEXT: "Hello, " INTERPOLATE: "name" TEXT: "!"$expected2$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Interpolation with surrounding text - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Interpolation with surrounding text - FAILED', total_tests; END IF; -- Test 3: Simple section (for loop) total_tests := total_tests + 1; result := test_template_parse( $hemar3${{ for item in items }}{{ item }}{{ end }}$hemar3$, $expected3$SECTION: iterator="item", collection="items"$expected3$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Simple section (for loop) - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Simple section (for loop) - FAILED', total_tests; END IF; -- Test 4: Section with nested interpolation total_tests := total_tests + 1; result := test_template_parse( $hemar4${{ for item in items }}Name: {{ item.name }}{{ end }}$hemar4$, $expected4$SECTION: iterator="item", collection="items" TEXT: "Name: " INTERPOLATE: "item.name"$expected4$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Section with nested interpolation - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Section with nested interpolation - FAILED', total_tests; END IF; -- Test 5: Nested sections total_tests := total_tests + 1; result := test_template_parse( $hemar5${{ for item in items }}{{ for subitem in item.subitems }}{{ subitem }}{{ end }}{{ end }}$hemar5$, $expected5$SECTION: iterator="item", collection="items" SECTION: iterator="subitem", collection="item.subitems" INTERPOLATE: "subitem"$expected5$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Nested sections - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Nested sections - FAILED', total_tests; END IF; -- Test 6: Include tag total_tests := total_tests + 1; result := test_template_parse( $hemar6${{ include template_name }}$hemar6$, $expected6$INCLUDE: "template_name"$expected6$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Include tag - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Include tag - FAILED', total_tests; END IF; -- Test 7: Execute tag total_tests := total_tests + 1; result := test_template_parse( $hemar7${{ exec RETURN my_function(arg1, arg2) }}$hemar7$, $expected7$EXECUTE: "RETURN my_function(arg1, arg2)"$expected7$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Execute tag - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Execute tag - FAILED', total_tests; END IF; -- Test 8: Complex mixed template total_tests := total_tests + 1; result := test_template_parse( $hemar8$
{{ item.name }}
{{ include item.template }}{{ end }}" INTERPOLATE: "item.name" TEXT: "
" INCLUDE: "item.template" TEXT: "{{ item.description }}
{{ include item.footer }} {{ end }}$hemar12$, $expected12$SECTION: iterator="item", collection="items" TEXT: "" INTERPOLATE: "item.description" TEXT: "
" INCLUDE: "item.footer" TEXT: " "$expected12$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Section with multiple nested elements - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Section with multiple nested elements - FAILED', total_tests; END IF; -- Test 13: Empty template total_tests := total_tests + 1; result := test_template_parse( '', 'TEXT: ""' ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Empty template - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Empty template - FAILED', total_tests; END IF; -- Test 14: Just text, no tags total_tests := total_tests + 1; result := test_template_parse( $hemar14$Just plain text, no tags here.$hemar14$, $expected14$TEXT: "Just plain text, no tags here."$expected14$ ); IF result THEN passed_tests := passed_tests + 1; RAISE NOTICE 'Test %: Just text, no tags - PASSED', total_tests; ELSE RAISE WARNING 'Test %: Just text, no tags - FAILED', total_tests; END IF; -- Test 15: Complex example from documentation total_tests := total_tests + 1; result := test_template_parse( $template15$