test_apply_templates.py :  » XML » 4Suite » 4Suite-XML-1.0.2 » test » Xml » Xslt » Core » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » XML » 4Suite 
4Suite » 4Suite XML 1.0.2 » test » Xml » Xslt » Core » test_apply_templates.py
from Xml.Xslt import test_harness
from Ft.Xml.Xslt import XsltException,Error

simple_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:value-of select='.'/>
  </xsl:template>
</xsl:stylesheet>
"""

select_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates select='data/item[@in]'/>
    </docelem>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:value-of select='.'/>
  </xsl:template>
</xsl:stylesheet>
"""

select_attr_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates select='data/item/@in'/>
    </docelem>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='@*[. = "1"]'>!</xsl:template>
</xsl:stylesheet>
"""

sort_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates>
      <xsl:sort select='.'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:value-of select='.'/>
  </xsl:template>
</xsl:stylesheet>
"""

param_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates>
      <xsl:with-param name='foo' select='1'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:param name='foo'/>
    <xsl:value-of select='concat($foo,.)'/>
  </xsl:template>
</xsl:stylesheet>
"""

sort_param_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates>
      <xsl:sort select='.'/>
      <xsl:with-param name='foo' select='1'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:param name='foo'/>
    <xsl:value-of select='concat($foo,.)'/>
  </xsl:template>
</xsl:stylesheet>
"""


select_sort_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates select='item[@in]'>
      <xsl:sort select='.'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:value-of select='.'/>
  </xsl:template>
</xsl:stylesheet>
"""

select_param_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates select='item[@in]'>
      <xsl:with-param name='foo' select='1'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:param name='foo'/>
    <xsl:value-of select='concat($foo,.)'/>
  </xsl:template>
</xsl:stylesheet>
"""

select_sort_param_sheet_str = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match='/'>
    <docelem>
      <xsl:apply-templates/>
    </docelem>
  </xsl:template>
  <xsl:template match='data'>
    <xsl:apply-templates select='item[@in]'>
      <xsl:sort select='.'/>
      <xsl:with-param name='foo' select='1'/>
    </xsl:apply-templates>
  </xsl:template>
  <xsl:template match='text()'/>
  <xsl:template match='item'>
    <xsl:param name='foo'/>
    <xsl:value-of select='concat($foo,.)'/>
  </xsl:template>
</xsl:stylesheet>
"""

source_str_start = """<?xml version = "1.0"?>
<data>
"""
source_str_middle = """ <item>b</item>
 <item in='1'>a</item>
 <item>d</item>
 <item in='1'>c</item>
"""
source_str_end = """</data>
"""

MULTIPLIER = 50

source_str = source_str_start + source_str_middle*MULTIPLIER + source_str_end

simple_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""badc"""*MULTIPLIER + "</docelem>"
select_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""ac"""*MULTIPLIER+"</docelem>"
select_attr_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""!"""*MULTIPLIER*2+"""</docelem>"""
sort_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""a"""*MULTIPLIER + """b"""*MULTIPLIER + """c"""*MULTIPLIER + """d"""*MULTIPLIER+"</docelem>"
param_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""1b1a1d1c"""*MULTIPLIER+"</docelem>"
sort_param_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""1a"""*MULTIPLIER + """1b"""*MULTIPLIER + """1c"""*MULTIPLIER + """1d"""*MULTIPLIER+"</docelem>"
select_sort_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""a"""*MULTIPLIER + """c"""*MULTIPLIER+"</docelem>"
select_param_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""1a1c"""*MULTIPLIER+"</docelem>"
select_sort_param_expected = """<?xml version='1.0' encoding='UTF-8'?>\n<docelem>"""+"""1a"""*MULTIPLIER + """1c"""*MULTIPLIER+"</docelem>"

tests = [('Empty',simple_sheet_str,simple_expected),
         ('Empty using sort',sort_sheet_str,sort_expected),
         ('Empty using with-param',param_sheet_str,param_expected),
         ('Empty using sort and with-param',sort_param_sheet_str,sort_param_expected),
         ('Select',select_sheet_str,select_expected),
         ('Select of attributes',select_attr_sheet_str,select_attr_expected),
         ('Select using sort',select_sort_sheet_str,select_sort_expected),
         ('Select using with-param',select_param_sheet_str,select_param_expected),
         ('Select using sort and with-param',select_sort_param_sheet_str,select_sort_param_expected),
         ]

# Testing to ensure apply-templates applies to a node-set.
# If it's not a node-set, a particular XsltException is generated.
#
source_1="""<?xml version="1.0"?>
<foo/>"""

invalid_sheet_1="""<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="/">
    <xsl:variable name="fragment">
      <foo>hello</foo>
      <foo>world</foo>
    </xsl:variable>
    <!-- should produce a fatal error in XSLT 1.0 -->
    <xsl:apply-templates select="$fragment" mode="foo"/>
  </xsl:template>

  <xsl:template match="/" mode="foo">
    <result>
      <xsl:apply-templates mode="foo"/>
    </result>
  </xsl:template>

  <xsl:template match="foo" mode="foo">
    <bar>
      <xsl:value-of select="."/>
    </bar>
  </xsl:template>

</xsl:stylesheet>"""

invalid_sheet_2="""<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="/">
    <xsl:apply-templates select="'why is a string here?'"/>
  </xsl:template>

</xsl:stylesheet>"""

expected_1=""

def Test(tester):
    for (title, xsltSrc, expected) in tests:
        source = test_harness.FileInfo(string=source_str)
        sheet = test_harness.FileInfo(string=xsltSrc)
        test_harness.XsltTest(tester, source, [sheet], expected, title=title)

    source = test_harness.FileInfo(string=source_1)
    sheet = test_harness.FileInfo(string=invalid_sheet_1)
    test_harness.XsltTest(tester, source, [sheet], "",
                          exceptionCode=Error.ILLEGAL_APPLYTEMPLATE_NODESET,
                          title='Select invalid node-set 1'),

    source = test_harness.FileInfo(string=source_1)
    sheet = test_harness.FileInfo(string=invalid_sheet_2)
    test_harness.XsltTest(tester, source, [sheet], "",
                          exceptionCode=Error.ILLEGAL_APPLYTEMPLATE_NODESET,
                          title='Select invalid node-set 2'),
    return
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.