<?xml version="1.0" encoding="utf-8"?>
<t:test xmlns:t="http://xproc.org/ns/testsuite/3.0"
        xml:base="/try-catch-008.xml"
        name="try-catch-008.xml"
        expected="pass">
   <t:info>
      <t:title>Try/catch 008</t:title>
      <t:revision-history>
         <t:revision>
            <t:date>2021-06-10</t:date>
            <t:author>
               <t:name>Achim Berndzen</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Added attribute 'queryBinding' to schematron's schema.</p>
            </t:description>
         </t:revision>
         <t:revision>
            <t:date>2017-09-24T17:47:42+01:00</t:date>
            <t:author>
               <t:name>Norman Walsh</t:name>
            </t:author>
            <t:description xmlns="http://www.w3.org/1999/xhtml">
               <p>Port my test suite; add documentation and schemas</p>
            </t:description>
         </t:revision>
      </t:revision-history>
   </t:info>
   <t:pipeline>
      <p:declare-step xmlns:cx="http://xmlcalabash.com/ns/extensions"
                      xmlns:p="http://www.w3.org/ns/xproc"
                      name="main"
                      version="3.0"><!-- we should replace cx with something better -->
         <p:option name="error"/>
         <p:output port="result"/>
         <p:try name="try">
            <p:group>
               <p:choose>
                  <p:when test="$error = 0">
                     <p:identity>
                        <p:with-input port="source">
                           <p:inline>
                              <doc>try succeeded</doc>
                           </p:inline>
                        </p:with-input>
                     </p:identity>
                  </p:when>
                  <p:when test="$error = 1">
                     <p:error code="cx:error">
                        <p:with-input port="source">
                           <p:empty/>
                        </p:with-input>
                     </p:error>
                  </p:when>
                  <p:otherwise>
                     <p:error code="cx:error2">
                        <p:with-input port="source">
                           <p:empty/>
                        </p:with-input>
                     </p:error>
                  </p:otherwise>
               </p:choose>
            </p:group>
            <p:catch code="cx:error cx:error2">
               <p:identity>
                  <p:with-input port="source">
                     <p:inline>
                        <doc>caught cx:error or cx:error2</doc>
                     </p:inline>
                  </p:with-input>
               </p:identity>
            </p:catch>
            <p:catch>
               <p:identity>
                  <p:with-input port="source">
                     <p:inline>
                        <doc>caught any</doc>
                     </p:inline>
                  </p:with-input>
               </p:identity>
            </p:catch>
            <p:finally>
               <p:output port="finally" primary="false">
                  <p:pipe step="fident" port="result"/>
               </p:output>
               <p:identity name="fident">
                  <p:with-input port="source">
                     <p:inline>
                        <doc>finally</doc>
                     </p:inline>
                  </p:with-input>
               </p:identity>
            </p:finally>
         </p:try>
      </p:declare-step>
   </t:pipeline>
   <t:option name="error" select="2"/>
   <t:schematron>
      <s:schema xmlns="http://www.w3.org/1999/xhtml"
                xmlns:s="http://purl.oclc.org/dsdl/schematron"
                queryBinding="xslt2">
         <s:ns prefix="c" uri="http://www.w3.org/ns/xproc-step"/>
         <s:pattern>
            <s:rule context="/*">
               <s:assert test="self::doc">The pipeline root is not doc.</s:assert>
               <s:assert test=". = 'caught cx:error or cx:error2'">The result isn't "finally"</s:assert>
            </s:rule>
         </s:pattern>
      </s:schema>
   </t:schematron>
   <t:result name="try-catch-008.xml">
      <doc xmlns:cx="http://xmlcalabash.com/ns/extensions">caught cx:error or cx:error2</doc>
   </t:result>
</t:test>
