Skip to content

Commit

Permalink
[fix] castable as for untyped atomic value(s)
Browse files Browse the repository at this point in the history
It appears perfectly acceptable for UntypedAtomicValue.convertTo to convert to subtypes of Type.STRING as StringValue(… value, requiredType). The conversion was omitted, and the omission was breaking “castable as” for Type.NCNAME in particular.

These conversions are already acceptable for StringValue(s), and are borrowed from there.
  • Loading branch information
alanpaxton committed May 2, 2023
1 parent 650609f commit de8e4e8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ public static AtomicValue convertTo(UntypedAtomicValue strVal, String value, int
return strVal == null ? new UntypedAtomicValue(expression, value) : strVal;
case Type.STRING:
return new StringValue(expression, value);
case Type.NORMALIZED_STRING:
case Type.TOKEN:
case Type.LANGUAGE:
case Type.NMTOKEN:
case Type.NAME:
case Type.NCNAME:
case Type.ID:
case Type.IDREF:
case Type.ENTITY:
return new StringValue(expression, value, requiredType);
case Type.ANY_URI:
return new AnyURIValue(expression, value);
case Type.BOOLEAN:
Expand Down
79 changes: 79 additions & 0 deletions exist-core/src/test/xquery/xquery3/castable-untyped-atomic.xqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

(:
: eXist-db Open Source Native XML Database
: Copyright (C) 2001 The eXist-db Authors
:
: [email protected]
: http://www.exist-db.org
:
: This library is free software; you can redistribute it and/or
: modify it under the terms of the GNU Lesser General Public
: License as published by the Free Software Foundation; either
: version 2.1 of the License, or (at your option) any later version.
:
: This library is distributed in the hope that it will be useful,
: but WITHOUT ANY WARRANTY; without even the implied warranty of
: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
: Lesser General Public License for more details.
:
: You should have received a copy of the GNU Lesser General Public
: License along with this library; if not, write to the Free Software
: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
:)
(:~
: @see https://github.com/eXist-db/exist/issues/4518
:)
xquery version "3.1";

module namespace ca="http://exist-db.org/xquery/test";

declare namespace test="http://exist-db.org/xquery/xqsuite";

(: atomic values not explicitly converted to strings were falsely returning false to _castable_as_
in other words, all these tests were returning nc="false"
:)
declare
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
%test:args("123") %test:assertEquals("<result name=""123"" nc=""false""/>")
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
%test:args("pfx:suffix") %test:assertEquals("<result name=""pfx:suffix"" nc=""false""/>")
function ca:n-castable($d) {
let $doc := <e name="e"><f n="{$d}"/></e>
let $el := $doc/descendant::*[exists(@n)]
let $n := $el/@n
return <result name="{$n}" nc="{$n castable as xs:NCName}"/>
};

(: explicit string conversion was always working - for reference :)
declare
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
%test:args("123") %test:assertEquals("<result name=""123"" nc=""false""/>")
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
%test:args("pfx:suffix") %test:assertEquals("<result name=""pfx:suffix"" nc=""false""/>")
function ca:s-castable($d) {
let $doc := <e name="e"><f n="{$d}"/></e>
let $el := $doc/descendant::*[exists(@n)]
let $n := $el/@n, $s := string($n)
return <result name="{$n}" nc="{$s castable as xs:NCName}"/>
};

(: sanity check that casting accepts the same values as castable as :)
declare
%test:args("ixml") %test:assertEquals("<result name=""ixml"" nc=""true""/>")
%test:args("comment") %test:assertEquals("<result name=""comment"" nc=""true""/>")
%test:args("apple-jack") %test:assertEquals("<result name=""apple-jack"" nc=""true""/>")
%test:args("123") %test:assertError("err:")
%test:args("élève") %test:assertEquals("<result name=""élève"" nc=""true""/>")
%test:args("pfx:suffix") %test:assertError("err:")
function ca:s-cast-castable($d) {
let $doc := <e name="e"><f n="{$d}"/></e>
let $el := $doc/descendant::*[exists(@n)]
let $n := $el/@n, $ncname := xs:NCName($n)
return <result name="{$n}" nc="{$ncname castable as xs:NCName}"/>
};


0 comments on commit de8e4e8

Please sign in to comment.