<commit_before>USING: kernel xml sequences assocs tools.test io arrays namespaces
    xml.data xml.utilities xml.writer generic sequences.deep ;
IN: xml.tests

: sub-tag
    T{ name f f "sub" "http://littledan.onigirihouse.com/namespaces/replace" } ;

SYMBOL: ref-table

GENERIC: (r-ref) ( xml -- )
M: tag (r-ref)
    sub-tag over at* [
        ref-table get at
        swap set-tag-children
    ] [ 2drop ] if ;
M: object (r-ref) drop ;

: template ( xml -- )
    [ (r-ref) ] deep-each ;

! Example

: sample-doc
    {
        "<html xmlns:f='http://littledan.onigirihouse.com/namespaces/replace'>"
        "<body>"
        "<span f:sub='foo'/>"
        "<div f:sub='bar'/>"
        "<p f:sub='baz'>paragraph</p>"
        "</body></html>"
    } concat ;

: test-refs ( -- string )
    [
        H{
            { "foo" { "foo" } }
            { "bar" { "blah" T{ tag T{ name f "" "a" "" } V{ } f } } }
            { "baz" f }
        } ref-table set
        sample-doc string>xml dup template xml>string
    ] with-scope ;

[ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns:f=\"http://littledan.onigirihouse.com/namespaces/replace\"><body><span f:sub=\"foo\">foo</span><div f:sub=\"bar\">blah<a/></div><p f:sub=\"baz\"/></body></html>" ] [ test-refs ] unit-test<commit_msg>Fixing more XML unit tests<commit_after>USING: kernel xml sequences assocs tools.test io arrays namespaces
accessors xml.data xml.utilities xml.writer generic sequences.deep ;
IN: xml.tests

: sub-tag
    T{ name f f "sub" "http://littledan.onigirihouse.com/namespaces/replace" } ;

SYMBOL: ref-table

GENERIC: (r-ref) ( xml -- )
M: tag (r-ref)
    sub-tag over at* [
        ref-table get at
        >>children drop
    ] [ 2drop ] if ;
M: object (r-ref) drop ;

: template ( xml -- )
    [ (r-ref) ] deep-each ;

! Example

: sample-doc
    {
        "<html xmlns:f='http://littledan.onigirihouse.com/namespaces/replace'>"
        "<body>"
        "<span f:sub='foo'/>"
        "<div f:sub='bar'/>"
        "<p f:sub='baz'>paragraph</p>"
        "</body></html>"
    } concat ;

: test-refs ( -- string )
    [
        H{
            { "foo" { "foo" } }
            { "bar" { "blah" T{ tag f T{ name f "" "a" "" } f f } } }
            { "baz" f }
        } ref-table set
        sample-doc string>xml dup template xml>string
    ] with-scope ;

[ "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns:f=\"http://littledan.onigirihouse.com/namespaces/replace\"><body><span f:sub=\"foo\">foo</span><div f:sub=\"bar\">blah<a/></div><p f:sub=\"baz\"/></body></html>" ] [ test-refs ] unit-test
{'fraction_non_alphanumeric': 0.20139968895800933, 'fraction_numerical': 0.003110419906687403, 'mean_word_length': 2.3157216494845363, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 2, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '648076', 'n_tokens_mistral': 959, 'n_tokens_neox': 948, 'n_words': 197}