Skip to content

Commit 845fc74

Browse files
chris-evolvedbinaryadamretter
authored andcommitted
[test] Add additional fn:collation-key tests.
1 parent 4886fe5 commit 845fc74

File tree

2 files changed

+76
-25
lines changed

2 files changed

+76
-25
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FunCollationKey.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,10 @@ public FunCollationKey(final XQueryContext context, final FunctionSignature sign
6565
}
6666

6767
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
68-
final BinaryValue result;
6968
final String source = (args.length >= 1) ? args[0].toString() : "";
70-
final Collator collator;
71-
if (args.length >= 2) {
72-
collator = Collations.getCollationFromURI(args[1].toString());
73-
if (collator == null) {
74-
throw new XPathException(ErrorCodes.FOCH0002, "Unsupported collation: " + args[1]);
75-
}
76-
} else {
77-
collator = context.getDefaultCollator();
78-
if (collator == null) {
79-
throw new XPathException(ErrorCodes.FOCH0002, "Could not get default collator.");
80-
}
81-
}
82-
result = new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(collator.getCollationKey(source).toByteArray()));
83-
return result;
69+
final Collator collator = (args.length >= 2) ? Collations.getCollationFromURI(args[1].toString()) : null;
70+
71+
return new BinaryValueFromBinaryString(new Base64BinaryValueType(), Base64.encodeBase64String(
72+
(collator == null) ? source.getBytes(StandardCharsets.UTF_8) : new String(collator.getCollationKey(source).toByteArray()).getBytes(StandardCharsets.UTF_8)));
8473
}
8574
}

exist-core/src/test/xquery/xquery3/fnCollationKey.xqm

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,92 @@
2121
:)
2222
xquery version "3.1";
2323

24-
module namespace fnp="http://exist-db.org/xquery/test/function_collation_key";
24+
module namespace fnck="http://exist-db.org/xquery/test/function_collation_key";
2525

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

2828
declare
2929
%test:assertTrue
30-
function fnp:equal() {
31-
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
32-
let $second := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
30+
function fnck:default-equal-case() {
31+
let $first := fn:collation-key("a")
32+
let $second := fn:collation-key("a")
3333
return $first = $second
3434
};
3535

3636
declare
3737
%test:assertTrue
38-
function fnp:equal-ignore-case() {
39-
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
40-
let $second := fn:collation-key("A", "http://www.w3.org/2013/collation/UCA?strength=primary")
38+
function fnck:default-equal() {
39+
let $first := fn:collation-key("a")
40+
let $second := fn:collation-key("a")
4141
return $first = $second
4242
};
4343

4444
declare
4545
%test:assertTrue
46-
function fnp:not-equal() {
47-
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA?strength=primary")
48-
let $second := fn:collation-key("b", "http://www.w3.org/2013/collation/UCA?strength=primary")
46+
function fnck:default-not-equal() {
47+
let $first := fn:collation-key("a")
48+
let $second := fn:collation-key("b")
49+
return $first != $second
50+
};
51+
52+
declare
53+
%test:assertTrue
54+
function fnck:default-not-equal-ignore-case() {
55+
let $first := fn:collation-key("a")
56+
let $second := fn:collation-key("A")
57+
return $first != $second
58+
};
59+
60+
declare
61+
%test:assertTrue
62+
function fnck:exist-equal() {
63+
let $first := fn:collation-key("a", "http://exist-db.org/collation")
64+
let $second := fn:collation-key("a", "http://exist-db.org/collation")
65+
return $first = $second
66+
};
67+
68+
declare
69+
%test:assertTrue
70+
function fnck:exist-equal-ignore-case() {
71+
let $first := fn:collation-key("a", "http://exist-db.org/collation")
72+
let $second := fn:collation-key("A", "http://exist-db.org/collation")
73+
return $first = $second
74+
};
75+
76+
declare
77+
%test:assertTrue
78+
function fnck:exist-not-equal() {
79+
let $first := fn:collation-key("a", "http://exist-db.org/collation")
80+
let $second := fn:collation-key("b", "http://exist-db.org/collation")
81+
return $first != $second
82+
};
83+
84+
declare
85+
%test:assertError("FOCH0002")
86+
function fnck:invalid-uri() {
87+
fn:collation-key("a", "")
88+
};
89+
90+
declare
91+
%test:assertTrue
92+
function fnck:uca-equal() {
93+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
94+
let $second := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
95+
return $first = $second
96+
};
97+
98+
declare
99+
%test:assertTrue
100+
function fnck:uca-equal-ignore-case() {
101+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
102+
let $second := fn:collation-key("A", "http://www.w3.org/2013/collation/UCA")
103+
return $first = $second
104+
};
105+
106+
declare
107+
%test:assertTrue
108+
function fnck:uca-not-equal() {
109+
let $first := fn:collation-key("a", "http://www.w3.org/2013/collation/UCA")
110+
let $second := fn:collation-key("b", "http://www.w3.org/2013/collation/UCA")
49111
return $first != $second
50112
};

0 commit comments

Comments
 (0)