• Publicity: Public Only All

parse-test-procs.tcl

Tests for adp parsing

Location:
packages/acs-templating/tcl/test/parse-test-procs.tcl
Created:
2005-01-01
Author:
Dave Bauer <dave@thedesignexperience.org>
CVS Identification:
$Id: parse-test-procs.tcl,v 1.12 2024/09/11 06:15:48 gustafn Exp $

Procedures in this file

Detailed information

_acs-templating__adp_level (private)

 _acs-templating__adp_level

Partial Call Graph (max 5 caller/called nodes):
%3 aa_equals aa_equals (public) aa_log aa_log (public) aa_log_result aa_log_result (public) aa_true aa_true (public) template::adp_level template::adp_level (public) _acs-templating__adp_level _acs-templating__adp_level _acs-templating__adp_level->aa_equals _acs-templating__adp_level->aa_log _acs-templating__adp_level->aa_log_result _acs-templating__adp_level->aa_true _acs-templating__adp_level->template::adp_level

Testcases:
No testcase defined.

_acs-templating__adp_parse_string (private)

 _acs-templating__adp_parse_string

Partial Call Graph (max 5 caller/called nodes):
%3 aa_equals aa_equals (public) aa_log aa_log (public) aa_log_result aa_log_result (public) template::adp_parse_string template::adp_parse_string (public) _acs-templating__adp_parse_string _acs-templating__adp_parse_string _acs-templating__adp_parse_string->aa_equals _acs-templating__adp_parse_string->aa_log _acs-templating__adp_parse_string->aa_log_result _acs-templating__adp_parse_string->template::adp_parse_string

Testcases:
No testcase defined.

_acs-templating__adp_parse_tags (private)

 _acs-templating__adp_parse_tags

Partial Call Graph (max 5 caller/called nodes):
%3 aa_log aa_log (public) aa_log_result aa_log_result (public) aa_true aa_true (public) template::adp_parse_tags template::adp_parse_tags (public) _acs-templating__adp_parse_tags _acs-templating__adp_parse_tags _acs-templating__adp_parse_tags->aa_log _acs-templating__adp_parse_tags->aa_log_result _acs-templating__adp_parse_tags->aa_true _acs-templating__adp_parse_tags->template::adp_parse_tags

Testcases:
No testcase defined.

_acs-templating__expand_percentage_signs (private)

 _acs-templating__expand_percentage_signs

Partial Call Graph (max 5 caller/called nodes):
%3 aa_equals aa_equals (public) aa_false aa_false (public) aa_log aa_log (public) aa_log_result aa_log_result (public) template::expand_percentage_signs template::expand_percentage_signs (public) _acs-templating__expand_percentage_signs _acs-templating__expand_percentage_signs _acs-templating__expand_percentage_signs->aa_equals _acs-templating__expand_percentage_signs->aa_false _acs-templating__expand_percentage_signs->aa_log _acs-templating__expand_percentage_signs->aa_log_result _acs-templating__expand_percentage_signs->template::expand_percentage_signs

Testcases:
No testcase defined.

_acs-templating__template_variable (private)

 _acs-templating__template_variable

Partial Call Graph (max 5 caller/called nodes):
%3 aa_equals aa_equals (public) aa_log aa_log (public) aa_log_result aa_log_result (public) aa_section aa_section (public) aa_true aa_true (public) _acs-templating__template_variable _acs-templating__template_variable _acs-templating__template_variable->aa_equals _acs-templating__template_variable->aa_log _acs-templating__template_variable->aa_log_result _acs-templating__template_variable->aa_section _acs-templating__template_variable->aa_true

Testcases:
No testcase defined.

_acs-templating__templates_and_scripts (private)

 _acs-templating__templates_and_scripts

Partial Call Graph (max 5 caller/called nodes):
%3 _ _ (public) aa_equals aa_equals (public) aa_log aa_log (public) aa_log_result aa_log_result (public) aa_section aa_section (public) _acs-templating__templates_and_scripts _acs-templating__templates_and_scripts _acs-templating__templates_and_scripts->_ _acs-templating__templates_and_scripts->aa_equals _acs-templating__templates_and_scripts->aa_log _acs-templating__templates_and_scripts->aa_log_result _acs-templating__templates_and_scripts->aa_section

Testcases:
No testcase defined.
[ hide source ] | [ make this the default ]

Content File Source

ad_library {

    Tests for adp parsing

    @author Dave Bauer (dave@thedesignexperience.org)
    @creation-date 2005-01-01
    @cvs-id $Id: parse-test-procs.tcl,v 1.12 2024/09/11 06:15:48 gustafn Exp $
}

aa_register_case \
    -cats {
        api
        production_safe
    } \
    -procs {
        template::adp_variable_regexp
        template::adp_variable_regexp_noquote
        template::adp_variable_regexp_noi18n
        template::adp_variable_regexp_literal
        template::adp_array_variable_regexp
        template::adp_array_variable_regexp_noquote
        template::adp_array_variable_regexp_noi18n
        template::adp_array_variable_regexp_literal
    } \
    template_variable {
        test adp variable parsing procedures
    } {
        aa_section "Testing plain variable"

        set code "=@test_var@"
        aa_true "Variable detected" \
            [regexp [template::adp_variable_regexp$code discard pre var]
        aa_equals "Preceding char is '${pre}'"  "=" $pre
        aa_equals "Variable name is '${var}'"  \
            "test_var" $var

        set code "=@test_array.test_key@"
        aa_true "Regular array var name detected" \
            [regexp [template::adp_array_variable_regexp$code discard pre arr var]
        aa_equals "Preceding char is '${pre}'"  "=" $pre
        aa_equals "Array name is '${arr}'"  \
            "test_array" $arr
        aa_equals "Variable name is '${var}'"  \
            "test_key" $var

        set code "=@formerror.test_array.test_key@"
        aa_true "Formerror regular array var name detected" \
            [regexp [template::adp_array_variable_regexp$code discard pre arr var]
        aa_equals "Preceding char is '${pre}'"  "=" $pre
        aa_equals "Array name is '${arr}'"  \
            "formerror" $arr
        aa_equals "Variable name is '${var}'"  \
            "test_array.test_key" $var

        foreach modifier {noquote literal noi18n} {
            aa_section "Testing ;$modifier modifier"

            set code "=@test_var;$modifier@"
            aa_true "Variable detected" \
                [regexp [template::adp_variable_regexp_$modifier$code discard pre var]
            aa_equals "Preceding char is '${pre}'"  "=" $pre
            aa_equals "Variable name is '${var}'"  \
                "test_var" $var

            set code "=@test_array.test_key;$modifier@"
            aa_true "$modifier array var name detected" \
                [regexp [template::adp_array_variable_regexp_$modifier$code discard pre arr var]
            aa_equals "Preceding char is '${pre}'"  "=" $pre
            aa_equals "Array name is '${arr}'"  \
                "test_array" $arr
            aa_equals "Variable name is '${var}'"  \
                "test_key" $var

            set code "=@formerror.test_array.test_key;$modifier@"
            aa_true "$modifier formerror array var name detected" \
                [regexp [template::adp_array_variable_regexp_$modifier$code discard pre arr var]
            aa_equals "Preceding char is '${pre}'"  "=" $pre
            aa_equals "Array name is '${arr}'"  \
                "formerror" $arr
            aa_equals "Variable name is '${var}'"  \
                "test_array.test_key" $var
        }
    }

aa_register_case \
    -cats {api smoke production_safe} \
    -procs {
        template::expand_percentage_signs
    } \
    expand_percentage_signs {
        Test expand percentage signs to make sure it substitutes correctly

        @author Dave Bauer
        @creation-date 2005-11-20
    } {
        set orig_message "Test message %one%"
        set one "\[__does_not_exist__\]"
        set message $orig_message

        aa_false "Expanded square bracket text" \
            [catch {set expanded_message [template::expand_percentage_signs $message]} errmsg]
        aa_log $errmsg
        aa_equals "square brackets safe" $expanded_message "Test message \[__does_not_exist__\]"

        set one "\$__does_not_exist"
        aa_false "Expanded dollar test" \
            [catch {set expanded_message [template::expand_percentage_signs $message]} errmsg]
        aa_log $errmsg
        aa_equals "dollar sign safe" $expanded_message "Test message \$__does_not_exist"

        set one "\$two(\$three(\[__does_not_exist\]))"

        aa_false "Square bracket in array key test" \
            [catch {set expanded_message [template::expand_percentage_signs $message]} errmsg]
        aa_log $errmsg
        aa_equals "square brackets in array key safe" \
            $expanded_message "Test message \$two(\$three(\[__does_not_exist\]))"

    }

aa_register_case \
    -cats {api smoke production_safe} \
    -procs {
        ::template::adp_parse_tags
        ::template::adp_compile_chunk
        ::template::icon
        ::template::adp_append_code
    } \
    adp_parse_tags {

        Checks the helper template::adp_parse_tags, which performs a
        subset of template::adp_compile.

    } {
        set HTML {<p>foo <adp:icon name="edit">bar}
        set result [::template::adp_parse_tags $HTML]
        aa_log [ns_quotehtml $result]
        aa_true "test substitution of adp:icon contains either 'class' or 'src' attribute" \
            [regexp {(class=|src=)} $result]
        set HTML {<p>foo @a@ <adp:icon name="edit">bar @b@}
        set result [::template::adp_parse_tags $HTML]
        aa_log [ns_quotehtml $result]
        aa_true "test substitution contains still template variables" \
            [regexp {@} $result]
    }

aa_register_case \
    -cats {api smoke production_safe} \
    -procs {
        ::template::adp_parse_string
    } \
    adp_parse_string {

        Test template::adp_parse_string

    } {
        set adp {2 + 2 = <%= [expr {2 + 2}] %> !!!}
        aa_equals "Result is correct" \
            [template::adp_parse_string $adp] {2 + 2 = 4 !!!}
    }

aa_register_case \
    -cats {api smoke production_safe} \
    -procs {
        ::template::adp_level
    } \
    adp_level {

        Test template::adp_level

    } {
        set template_adp_level $::template::parse_level
        try {
            unset -nocomplain ::template::parse_level
            aa_equals "When no parse level is set, result is empty" \
                [template::adp_level""

            set ::template::parse_level [list 1 2 3]

            aa_equals "With a parse level, result is the last level" \
                [template::adp_level] 3

            aa_true "Up must be an integer" [catch {
                template::adp_level broken
            } errmsg]

            aa_equals "Up is the number of elements we go up in the parse level" \
                [template::adp_level 3] 1

        } finally {
            set ::template::parse_level $template_adp_level
        }
    }

aa_register_case \
    -cats {api smoke production_safe} \
    -procs {
        template::adp_include
        template::add_body_handler
        template::add_body_script
        template::add_confirm_handler
        template::add_event_listener
        template::add_refresh_on_history_handler
        template::get_body_event_handlers
        template::prepare_body_script_multirow
        template::multirow
        template::collect_body_scripts
    } \
    templates_and_scripts {

        Test api to introduce javascript handlers inside a template.

    } {
        #
        # Other tests might already have set handlers on the
        # page. Let's reset them beforehand.
        #
        template::prepare_body_script_multirow
        template::get_body_event_handlers
        template::prepare_body_script_multirow
        set starting_body_scripts [template::multirow size body_script]

        #
        # Note: we use placeholders instead of real values to better
        # find them in the output.
        #

        aa_section template::add_body_handler

        template::add_body_handler \
            -event __template::add_body_handler_event \
            -script __template::add_body_handler_script \
            -identifier __template::add_body_handler_identifier

        template::add_body_handler \
            -event __template::add_body_handler_event \
            -script __template::add_body_handler_script \
            -identifier __template::add_body_handler_identifier

        template::add_body_handler \
            -event __template::add_body_handler_event2 \
            -script __template::add_body_handler_script2 \
            -identifier __template::add_body_handler_identifier2


        aa_section template::add_body_script

        template::add_body_script \
            -charset __template::add_body_script_charset \
            -crossorigin __template::add_body_script_crossorigin \
            -integrity __template::add_body_script_integrity \
            -script __template::add_body_script_script \
            -src __template::add_body_script_src \
            -type __template::add_body_script_type \
            -async=false \
            -defer=false


        aa_section template::add_confirm_handler

        template::add_confirm_handler \
            -event __template::add_confirm_handler_event \
            -message __template::add_confirm_handler_message \
            -id __template::add_confirm_handler_id

        template::add_confirm_handler \
            -event __template::add_confirm_handler_event \
            -message __template::add_confirm_handler_message \
            -CSSclass __template::add_confirm_handler_CSSclass

        template::add_confirm_handler \
            -event __template::add_confirm_handler_event \
            -message __template::add_confirm_handler_message \
            -formfield {
                __template::add_confirm_handler_formfield1
                __template::add_confirm_handler_formfield2
            }

        template::add_confirm_handler \
            -event __template::add_confirm_handler_event \
            -message __template::add_confirm_handler_message \
            -selector __template::add_confirm_handler_selector


        aa_section template::add_event_listener

        template::add_event_listener \
            -event __template::add_event_listener_event \
            -id __template::add_event_listener_id \
            -script __template::add_event_listener_script

        template::add_event_listener \
            -event __template::add_event_listener_event \
            -CSSclass __template::add_event_listener_CSSclass \
            -script __template::add_event_listener_script

        template::add_event_listener \
            -event __template::add_event_listener_event \
            -formfield {
                __template::add_event_listener_formfield1
                __template::add_event_listener_formfield2
            } \
            -script __template::add_event_listener_script

        template::add_event_listener \
            -event __template::add_event_listener_event \
            -selector __template::add_event_listener_selector \
            -script __template::add_event_listener_script


        aa_section template::add_refresh_on_history_handler

        template::add_refresh_on_history_handler

        set page [template::collect_body_scripts]

        aa_equals "Page contains exactly 11 additional scripts (body handlers are grouped together)" \
            [regsub -all "<script" $page {} _] [expr {$starting_body_scripts + 11}]

        aa_true "There is a nonce declaration in the page " [regexp {nonce="\w*"} $page nonce]
        if {[info exists nonce]} {
            aa_equals "All nonces on the page are the same, one per script" \
                [regsub -all $nonce $page {} _] [expr {$starting_body_scripts + 11}]
        }

        aa_equals "Page contains only 1 handler per identifier" \
            [regsub -all __template::add_body_handler_script\[^2\] $page {} _] 1

        foreach expected {
            __template::add_body_handler_event
            __template::add_body_handler_script
            __template::add_body_handler_event2
            __template::add_body_handler_script2
            {charset="__template::add_body_script_charset"}
            {crossorigin="__template::add_body_script_crossorigin"}
            {integrity="__template::add_body_script_integrity"}
            __template::add_body_script_script
            {src="__template::add_body_script_src"}
            {type="__template::add_body_script_type"}
            __template::add_confirm_handler_event
            "if (!confirm(`__template::add_confirm_handler_message`)) \{"
            __template::add_confirm_handler_id
            __template::add_confirm_handler_CSSclass
            __template::add_confirm_handler_formfield1
            __template::add_confirm_handler_formfield2
            __template::add_confirm_handler_selector
            __template::add_event_listener_event
            __template::add_event_listener_id
            __template::add_event_listener_script
            __template::add_event_listener_CSSclass
            __template::add_event_listener_formfield1
            __template::add_event_listener_formfield2
            __template::add_event_listener_selector
            "window.addEventListener( \"pageshow\""
        } {
            aa_true "'$expected' was rendered by the template" \
                {[string first $expected $page] >= 0}
        }
    }


# Local variables:
#    mode: tcl
#    tcl-indent-level: 4
#    indent-tabs-mode: nil
# End: