diff --git a/package/c/hemar/test/test_jsonb_path.sql b/package/c/hemar/test/test_jsonb_path.sql index 98c71e0..d247438 100755 --- a/package/c/hemar/test/test_jsonb_path.sql +++ b/package/c/hemar/test/test_jsonb_path.sql @@ -478,7 +478,7 @@ BEGIN RAISE WARNING 'Test %: Empty array/object edge cases (path="%"): % | PASSED: % (expected: empty array)', total_tests, current_path, result, passed; END IF; - + -- Print summary IF passed_tests = total_tests THEN RAISE NOTICE '------------------------------------'; @@ -497,4 +497,4 @@ BEGIN IF passed_tests != total_tests THEN RAISE EXCEPTION 'Tests failed: % of % tests did not pass', (total_tests - passed_tests), total_tests; END IF; -END $$; +END $$; diff --git a/package/c/hemar/test/test_template_parser.sql b/package/c/hemar/test/test_template_parser.sql new file mode 100755 index 0000000..8d83f44 --- /dev/null +++ b/package/c/hemar/test/test_template_parser.sql @@ -0,0 +1,405 @@ +-- 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; + +-- 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); + passed := position(expected_structure in parsed_result) > 0; + EXCEPTION + WHEN OTHERS THEN + passed := false; + END; + + 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; +END; +$$ LANGUAGE plpgsql; + +-- Run the tests +DO $$ +DECLARE + total_tests integer := 0; + passed_tests integer := 0; + result boolean; +BEGIN + RAISE NOTICE 'Starting template parser tests...'; + + -- Test 1: Simple interpolation + total_tests := total_tests + 1; + result := test_template_parse( + '{{ simple_var }}', + 'INTERPOLATE: "simple_var"' + ); + 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( + 'Hello, {{ name }}!', + 'TEXT: "Hello, " +INTERPOLATE: "name" +TEXT: "!"' + ); + 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( + '{{ for item in items do }}{{ item }}{{ end }}', + 'SECTION: iterator="item", collection="items"' + ); + 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( + '{{ for item in items do }}Name: {{ item.name }}{{ end }}', + 'SECTION: iterator="item", collection="items" + TEXT: "Name: " + INTERPOLATE: "item.name"' + ); + 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( + '{{ for item in items do }}{{ for subitem in item.subitems do }}{{ subitem }}{{ end }}{{ end }}', + 'SECTION: iterator="item", collection="items" + SECTION: iterator="subitem", collection="item.subitems" + INTERPOLATE: "subitem"' + ); + 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( + '{{ include template_name }}', + 'INCLUDE: "template_name"' + ); + 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( + '{{ exec RETURN my_function(arg1, arg2) }}', + 'EXECUTE: "RETURN my_function(arg1, arg2)"' + ); + 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( + '
{{ item.name }}
{{ include item.template }}{{ end }}" + INTERPOLATE: "item.name" + TEXT: "
" + INCLUDE: "item.template" +TEXT: "{{ item.description }}
+ {{ include item.footer }} + {{ end }}', + 'SECTION: iterator="item", collection="items" + TEXT: " +" + INTERPOLATE: "item.description" + TEXT: "
+ " + INCLUDE: "item.footer" + TEXT: " + "' + ); + 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( + 'Just plain text, no tags here.', + 'TEXT: "Just plain text, no tags here."' + ); + 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( + '