xee-testrunner 0.1.6

Conformance testing for Xee's XPath and XSLT implementations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
use ahash::AHashMap;
use chrono::Offset;
use std::fmt;
use xee_xpath::context::{Collation, DynamicContext};
use xee_xpath::SerializationParameters;
use xot::xmlname::{NameStrInfo, OwnedName as Name};
use xot::Xot;

use xee_xpath::query::RecurseQuery;
use xee_xpath::{context, error, Documents, Item, Queries, Query, Recurse, Sequence};
use xee_xpath_load::{convert_boolean, convert_string, ContextLoadable};

use crate::catalog::LoadContext;

use super::outcome::{TestOutcome, UnexpectedError};

type XPathExpr = String;

pub(crate) trait Assertable {
    fn assert_result(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        match result {
            Ok(sequence) => self.assert_value(context, documents, sequence),
            Err(error) => TestOutcome::RuntimeError(error.clone()),
        }
    }

    fn assert_value(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome;
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertAnyOf(Vec<TestCaseResult>);

impl AssertAnyOf {
    pub(crate) fn new(test_case_results: Vec<TestCaseResult>) -> Self {
        Self(test_case_results)
    }

    pub(crate) fn assert_error(&self, error: &error::ErrorValue) -> TestOutcome {
        let mut failed_test_results = Vec::new();
        for test_case_result in &self.0 {
            if let TestCaseResult::AssertError(assert_error) = test_case_result {
                let result = assert_error.assert_error(error);
                match result {
                    TestOutcome::Passed => return result,
                    _ => failed_test_results.push(result),
                }
            } else {
                // any non-error is a failure, as we arrived with an error
                return TestOutcome::Failed(Failure::AnyOf(self.clone(), failed_test_results));
            }
        }
        TestOutcome::Failed(Failure::AnyOf(self.clone(), failed_test_results))
    }
}

impl Assertable for AssertAnyOf {
    fn assert_result(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        let mut failed_test_results = Vec::new();
        for test_case_result in &self.0 {
            let result = test_case_result.assert_result(context, documents, result);
            match result {
                TestOutcome::Passed => return result,
                _ => failed_test_results.push(result),
            }
        }
        match result {
            Ok(_value) => TestOutcome::Failed(Failure::AnyOf(self.clone(), failed_test_results)),
            Err(error) => TestOutcome::RuntimeError(error.clone()),
        }
    }

    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        _sequence: &Sequence,
    ) -> TestOutcome {
        unreachable!();
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AssertAllOf(Vec<TestCaseResult>);

impl AssertAllOf {
    pub(crate) fn new(test_case_results: Vec<TestCaseResult>) -> Self {
        Self(test_case_results)
    }
}

impl Assertable for AssertAllOf {
    fn assert_result(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        for test_case_result in &self.0 {
            let result = test_case_result.assert_result(context, documents, result);
            match result {
                TestOutcome::Passed | TestOutcome::UnexpectedError(..) => {}
                _ => return result,
            }
        }
        TestOutcome::Passed
    }

    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        _sequence: &Sequence,
    ) -> TestOutcome {
        unreachable!();
    }
}

#[derive(PartialEq, Clone, Eq)]
pub struct AssertNot(Box<TestCaseResult>);

impl AssertNot {
    pub(crate) fn new(test_case_result: TestCaseResult) -> Self {
        Self(Box::new(test_case_result))
    }
}

impl Assertable for AssertNot {
    fn assert_result(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        let result = self.0.assert_result(context, documents, result);
        match result {
            TestOutcome::Passed => {
                TestOutcome::Failed(Failure::Not(self.clone(), Box::new(result)))
            }
            TestOutcome::Failed(_) => TestOutcome::Passed,
            _ => result,
        }
    }

    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        _sequence: &Sequence,
    ) -> TestOutcome {
        unreachable!();
    }
}

impl fmt::Debug for AssertNot {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "AssertNot({:?})", self.0)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Assert(XPathExpr);

impl Assert {
    pub(crate) fn new(expr: XPathExpr) -> Self {
        Self(expr)
    }
}

impl Assertable for Assert {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let result_sequence = run_xpath_with_result(&self.0, sequence, documents);

        match result_sequence {
            Ok(result_sequence) => match result_sequence.effective_boolean_value() {
                Ok(value) => {
                    if value {
                        TestOutcome::Passed
                    } else {
                        TestOutcome::Failed(Failure::Assert(self.clone(), sequence.clone()))
                    }
                }
                Err(error) => TestOutcome::RuntimeError(error),
            },
            Err(error) => TestOutcome::UnsupportedExpression(error.value()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertEq(XPathExpr);

impl AssertEq {
    pub(crate) fn new(expr: XPathExpr) -> Self {
        Self(expr)
    }
}

impl Assertable for AssertEq {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let expected_sequence = run_xpath(&self.0);

        match expected_sequence {
            Ok(expected_sequence) => {
                let atom = xee_xpath::iter::one(sequence.atomized(documents.xot()));
                let atom = match atom {
                    Ok(atom) => atom,
                    Err(error) => return TestOutcome::RuntimeError(error),
                };
                let expected_atom =
                    xee_xpath::iter::one(expected_sequence.atomized(documents.xot()))
                        .expect("Should get single atom in sequence");
                if expected_atom.simple_equal(&atom) {
                    TestOutcome::Passed
                } else {
                    TestOutcome::Failed(Failure::Eq(self.clone(), sequence.clone()))
                }
            }
            Err(error) => TestOutcome::UnsupportedExpression(error.value()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertDeepEq(XPathExpr);

impl AssertDeepEq {
    pub(crate) fn new(expr: XPathExpr) -> Self {
        Self(expr)
    }
}

impl Assertable for AssertDeepEq {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let expected_sequence = run_xpath(&self.0);

        match expected_sequence {
            Ok(expected_sequence) => {
                if expected_sequence
                    .deep_equal(
                        sequence,
                        &Collation::CodePoint,
                        chrono::offset::Utc.fix(),
                        documents.xot(),
                    )
                    .unwrap_or(false)
                {
                    TestOutcome::Passed
                } else {
                    TestOutcome::Failed(Failure::DeepEq(self.clone(), sequence.clone()))
                }
            }
            Err(error) => TestOutcome::UnsupportedExpression(error.value()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertCount(usize);

impl AssertCount {
    pub(crate) fn new(count: usize) -> Self {
        Self(count)
    }
}

impl Assertable for AssertCount {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let found_len = sequence.len();
        if found_len == self.0 {
            TestOutcome::Passed
        } else {
            TestOutcome::Failed(Failure::Count(self.clone(), AssertCountFailure(found_len)))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertPermutation(XPathExpr);

impl AssertPermutation {
    pub(crate) fn new(expr: XPathExpr) -> Self {
        Self(expr)
    }
}

impl Assertable for AssertPermutation {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        // we won't use the sequence sorting mechanisms defined in XPath
        // here, as in them we can't compare bools with integers for instance,
        // and that gives rise to a test failure because we cannot sort

        // we use a frequency comparison algorithm, by counting how
        // many times we see a particular atom in a hashmap, and checking
        // whether the result sequence has the same counts.

        let mut frequency = AHashMap::new();
        for atom in sequence.atomized(documents.xot()) {
            let atom = match atom {
                Ok(atom) => atom,
                Err(err) => return TestOutcome::RuntimeError(err),
            };
            let count = frequency.entry(atom).or_insert(0);
            *count += 1;
        }

        let result_sequence = run_xpath(&self.0);
        let result_sequence = match result_sequence {
            Ok(result_sequence) => result_sequence,
            Err(error) => return TestOutcome::UnsupportedExpression(error.value()),
        };

        for atom in result_sequence.atomized(documents.xot()) {
            let atom = match atom {
                Ok(atom) => atom,
                Err(err) => return TestOutcome::RuntimeError(err),
            };
            let count = frequency.entry(atom.clone()).or_insert(0);
            *count -= 1;
            if *count == 0 {
                frequency.remove(&atom);
            }
        }

        if frequency.is_empty() {
            TestOutcome::Passed
        } else {
            TestOutcome::Failed(Failure::Permutation(self.clone(), sequence.clone()))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertXml(String);

impl AssertXml {
    pub(crate) fn new(xml: String) -> Self {
        Self(xml)
    }
}

impl Assertable for AssertXml {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let xml = sequence.serialize(
            SerializationParameters {
                omit_xml_declaration: true,
                ..Default::default()
            },
            documents.xot_mut(),
        );

        let xml = match xml {
            Ok(xml) => xml,
            Err(_error) => {
                return TestOutcome::Failed(Failure::Xml(
                    self.clone(),
                    AssertXmlFailure::WrongValue(sequence.clone()),
                ));
            }
        };

        let mut compare_xot = Xot::new();

        let found = compare_xot.parse_fragment(&xml);
        let found = match found {
            Ok(found) => found,
            Err(_err) => {
                return TestOutcome::EnvironmentError("Cannot parse result XML".to_string());
            }
        };
        let expected = compare_xot.parse_fragment(&self.0).unwrap();

        // and compare
        let c = compare_xot.deep_equal(expected, found);

        if c {
            TestOutcome::Passed
        } else {
            TestOutcome::Failed(Failure::Xml(self.clone(), AssertXmlFailure::WrongXml(xml)))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertEmpty;

impl AssertEmpty {
    pub(crate) fn new() -> Self {
        Self
    }
}

impl Assertable for AssertEmpty {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        if sequence.is_empty() {
            TestOutcome::Passed
        } else {
            TestOutcome::Failed(Failure::Empty(self.clone(), sequence.clone()))
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AssertSerializationMatches;

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AssertSerializationError(String);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertType(String);

impl AssertType {
    pub(crate) fn new(type_name: String) -> Self {
        Self(type_name)
    }
}

impl Assertable for AssertType {
    fn assert_value(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let matches = sequence.matches_type(&self.0, documents.xot(), &|function| {
            context.function_info(function).signature()
        });
        match matches {
            Ok(matches) => {
                if matches {
                    TestOutcome::Passed
                } else {
                    TestOutcome::Failed(Failure::Type(self.clone(), sequence.clone()))
                }
            }
            Err(_) => {
                // we don't support this sequence type expression yet
                // this should resolve itself once we do and we can parse it
                TestOutcome::Unsupported
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertTrue;

impl AssertTrue {
    pub(crate) fn new() -> Self {
        Self
    }
}

impl Assertable for AssertTrue {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        if let Ok(item) = sequence.clone().one() {
            if let Ok(atomic) = item.to_atomic() {
                let b: error::ValueResult<bool> = atomic.try_into();
                if let Ok(b) = b {
                    if b {
                        return TestOutcome::Passed;
                    }
                }
            }
        }
        TestOutcome::Failed(Failure::True(self.clone(), sequence.clone()))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertFalse;

impl AssertFalse {
    pub(crate) fn new() -> Self {
        Self
    }
}

impl Assertable for AssertFalse {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        if let Ok(item) = sequence.clone().one() {
            if let Ok(atomic) = item.to_atomic() {
                let b: error::ValueResult<bool> = atomic.try_into();
                if let Ok(b) = b {
                    if !b {
                        return TestOutcome::Passed;
                    }
                }
            }
        }
        TestOutcome::Failed(Failure::False(self.clone(), sequence.clone()))
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertStringValue(String, bool);

impl AssertStringValue {
    pub(crate) fn new(string: String, normalize_space: bool) -> Self {
        Self(string, normalize_space)
    }
}

impl Assertable for AssertStringValue {
    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        documents: &mut Documents,
        sequence: &Sequence,
    ) -> TestOutcome {
        let items = sequence.iter();

        let strings = items
            .map(|item| item.string_value(documents.xot()))
            .collect::<error::ValueResult<Vec<_>>>();
        match strings {
            Ok(strings) => {
                let joined = strings.join(" ");
                let joined = if self.1 {
                    // normalize space
                    joined
                        .split_ascii_whitespace()
                        .collect::<Vec<_>>()
                        .join(" ")
                } else {
                    joined
                };
                if joined == self.0 {
                    TestOutcome::Passed
                } else {
                    // the string value is not what we expected
                    TestOutcome::Failed(Failure::StringValue(
                        self.clone(),
                        AssertStringValueFailure::WrongStringValue(joined),
                    ))
                }
            }
            // we weren't able to produce a string value
            Err(_) => TestOutcome::Failed(Failure::StringValue(
                self.clone(),
                AssertStringValueFailure::WrongValue(sequence.clone()),
            )),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AssertError(String);

impl AssertError {
    pub(crate) fn new(code: String) -> Self {
        Self(code)
    }

    pub(crate) fn assert_error(&self, error: &error::ErrorValue) -> TestOutcome {
        if self.0 == "*" {
            return TestOutcome::Passed;
        }
        // all errors are officially a pass, but we check whether the error
        // code matches too
        let code = error.code_qname();
        // FIXME: there is no checking for the correct namespace here, should
        // there be?
        if code.local_name() == self.0 {
            TestOutcome::Passed
        } else {
            TestOutcome::UnexpectedError(UnexpectedError(code.local_name().to_string()))
        }
    }
}

impl Assertable for AssertError {
    fn assert_result(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        match result {
            Ok(sequence) => TestOutcome::Failed(Failure::Error(self.clone(), sequence.clone())),
            Err(error) => self.assert_error(error),
        }
    }

    fn assert_value(
        &self,
        _context: &DynamicContext<'_>,
        _documents: &mut Documents,
        _sequence: &Sequence,
    ) -> TestOutcome {
        unreachable!();
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TestCaseResult {
    AnyOf(AssertAnyOf),
    AllOf(AssertAllOf),
    Not(AssertNot),
    // The assert element contains an XPath expression whose effective boolean
    // value must be true; usually the expression will use the variable $result
    // which references the result of the expression.
    Assert(Assert),
    // The assert element contains an XPath expression (usually a simple string
    // or numeric literal) which must be equal to the result of the test case
    // under the rules of the XPath 'eq' operator.
    AssertEq(AssertEq),
    // Asserts that the result must be a sequence of atomic values that is
    // deep-equal to the supplied sequence under the rules of the deep-equal()
    // function.
    AssertDeepEq(AssertDeepEq),
    // Asserts that the result must be a sequence containing a given number of
    // items. The value of the element is an integer giving the expected length
    // of the sequence.
    AssertCount(AssertCount),
    //  Asserts that the result must be a sequence of atomic values that has
    //  some permutation (reordering) that is deep-equal to the supplied
    //  sequence under the rules of the deep-equal() function.
    // Note this implies that NaN is equal to NaN.
    AssertPermutation(AssertPermutation),
    // Asserts the result of the query by providing a serialization of the
    // expression result using the default serialization parameters
    // method="xml" indent="no" omit-xml-declaration="yes".
    AssertXml(AssertXml),
    //  Asserts that the result of the test is an empty sequence.
    AssertEmpty(AssertEmpty),
    // Asserts the result of serializing the query matches a given regular
    // expression.
    // XXX values not right
    #[allow(dead_code)]
    SerializationMatches(AssertSerializationMatches),
    // Asserts that the query can be executed without error, but serializing
    // the result produces a serialization error. The result of the query must
    // be serialized using the serialization options specified within the query
    // (if any).
    #[allow(dead_code)]
    AssertSerializationError(AssertSerializationError),
    // Asserts that the result of the test matches the sequence type given as
    // the value of the assert-type element.
    AssertType(AssertType),
    // Asserts that the result of the test is the singleton boolean value
    // false(). Note, the test expression must actually evaluate to false: this
    // is not an assertion on the effective boolean value.
    AssertTrue(AssertTrue),
    // Asserts that the result of the test is the singleton boolean value
    // false(). Note, the test expression must actually evaluate to false: this
    // is not an assertion on the effective boolean value.
    AssertFalse(AssertFalse),
    // Asserts that the result of the test, after conversion to a string by
    // applying the expression string-join(for $r in $result return string($r),
    // " ") is equal to the string value of the assert-string-value element.
    // Note that this test cannot be used if the result includes items that do
    // not have a string value (elements with element-only content; function
    // items) If the normalize-space attribute is present with the value true,
    // then both the string value of the query result and the value of the
    // assert-string-value element should be processed as if by the XPath
    // normalize-space() function before the comparison.
    AssertStringValue(AssertStringValue),
    //  Asserts that the test is expected to fail with a static or dynamic
    //  error condition. The "code" attribute gives the expected error code.
    //
    // For the purpose of official test reporting, an implementation is
    // considered to pass a test if the test expects and error and the
    // implementation raises an error, regardless whether the error codes
    // match.
    AssertError(AssertError),
    // This assertion type is as of yet unsupported, and will automatically error
    Unsupported,
}

impl TestCaseResult {
    pub(crate) fn assert_result(
        &self,
        context: &DynamicContext<'_>,
        documents: &mut Documents,
        result: &error::ValueResult<Sequence>,
    ) -> TestOutcome {
        match self {
            TestCaseResult::AnyOf(a) => a.assert_result(context, documents, result),
            TestCaseResult::AllOf(a) => a.assert_result(context, documents, result),
            TestCaseResult::Not(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertEq(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertDeepEq(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertTrue(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertFalse(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertCount(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertStringValue(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertXml(a) => a.assert_result(context, documents, result),
            TestCaseResult::Assert(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertPermutation(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertError(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertEmpty(a) => a.assert_result(context, documents, result),
            TestCaseResult::AssertType(a) => a.assert_result(context, documents, result),
            TestCaseResult::Unsupported => TestOutcome::Unsupported,
            _ => {
                panic!("unimplemented test case result {:?}", self);
            }
        }
    }
}

impl ContextLoadable<LoadContext> for TestCaseResult {
    fn static_context_builder(context: &LoadContext) -> context::StaticContextBuilder {
        let mut builder = context::StaticContextBuilder::default();
        builder.default_element_namespace(context.catalog_ns);
        builder
    }

    fn load_with_context(
        queries: &Queries,
        _context: &LoadContext,
    ) -> anyhow::Result<impl Query<Self>> {
        let code_query = queries.one("@code/string()", convert_string)?;
        let error_query = queries.one(".", move |documents, item| {
            Ok(TestCaseResult::AssertError(AssertError::new(
                code_query.execute(documents, item)?,
            )))
        })?;
        let assert_count_query = queries.one("string()", |_, item| {
            let count: String = item.to_atomic()?.try_into()?;
            // XXX unwrap is a hack
            let count = count.parse::<usize>().unwrap();
            Ok(TestCaseResult::AssertCount(AssertCount::new(count)))
        })?;

        let assert_xml_query = queries.one("string()", |_, item| {
            let xml: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::AssertXml(AssertXml::new(xml)))
        })?;

        let assert_eq_query = queries.one("string()", |_, item| {
            let eq: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::AssertEq(AssertEq::new(eq)))
        })?;

        let assert_deep_eq_query = queries.one("string()", |_, item| {
            let eq: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::AssertDeepEq(AssertDeepEq::new(eq)))
        })?;

        let string_value_contents = queries.one("string()", convert_string)?;
        let normalize_space_query = queries.option("@normalize-space/string()", convert_boolean)?;

        let assert_string_value_query = queries.one(".", move |documents, item| {
            let string_value = string_value_contents.execute(documents, item)?;
            let normalize_space = normalize_space_query
                .execute(documents, item)?
                .unwrap_or(false);
            Ok(TestCaseResult::AssertStringValue(AssertStringValue::new(
                string_value,
                normalize_space,
            )))
        })?;

        let assert_type_query = queries.one("string()", |_, item| {
            let string_value: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::AssertType(AssertType::new(string_value)))
        })?;

        let assert_query = queries.one("string()", |_, item| {
            let xpath: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::Assert(Assert::new(xpath)))
        })?;

        let assert_permutation_query = queries.one("string()", |_, item| {
            let xpath: String = item.to_atomic()?.try_into()?;
            Ok(TestCaseResult::AssertPermutation(AssertPermutation::new(
                xpath,
            )))
        })?;

        let any_all_recurse = queries.many_recurse("*")?;
        let not_recurse = queries.one_recurse("*")?;

        // we use a local-name query here as it's the easiest way support this:
        // there is a single entry in the "result" element, but this may be
        // "any-of" and this contains a list of entries Using a relative path with
        // `query.option()` to detect entries (like "error", "assert-true", etc)
        // doesn't work for "any-of", as it contains a list of entries.
        let local_name_query = queries.one("local-name()", convert_string)?;
        let result_query =
            queries.one("result/*", move |documents: &mut Documents, item: &Item| {
                let f =
                    |documents: &mut Documents, item: &Item, recurse: &Recurse<TestCaseResult>| {
                        let local_name = local_name_query.execute(documents, item)?;
                        let r = match local_name.as_ref() {
                            "any-of" => {
                                let contents = any_all_recurse.execute(documents, item, recurse)?;
                                TestCaseResult::AnyOf(AssertAnyOf::new(contents))
                            }
                            "all-of" => {
                                let contents = any_all_recurse.execute(documents, item, recurse)?;
                                TestCaseResult::AllOf(AssertAllOf::new(contents))
                            }
                            "not" => {
                                let contents = not_recurse.execute(documents, item, recurse)?;
                                TestCaseResult::Not(AssertNot::new(contents))
                            }
                            "error" => error_query.execute(documents, item)?,
                            "assert-true" => TestCaseResult::AssertTrue(AssertTrue::new()),
                            "assert-false" => TestCaseResult::AssertFalse(AssertFalse::new()),
                            "assert-count" => assert_count_query.execute(documents, item)?,
                            "assert-xml" => assert_xml_query.execute(documents, item)?,
                            "assert-eq" => assert_eq_query.execute(documents, item)?,
                            "assert-deep-eq" => assert_deep_eq_query.execute(documents, item)?,
                            "assert-string-value" => {
                                assert_string_value_query.execute(documents, item)?
                            }
                            "assert" => assert_query.execute(documents, item)?,
                            "assert-permutation" => {
                                assert_permutation_query.execute(documents, item)?
                            }
                            "assert-empty" => TestCaseResult::AssertEmpty(AssertEmpty::new()),
                            "assert-type" => assert_type_query.execute(documents, item)?,
                            _ => TestCaseResult::Unsupported,
                        };
                        Ok(r)
                    };
                let recurse = Recurse::new(&f);
                recurse.execute(documents, item)
            })?;
        Ok(result_query)
    }
}
#[derive(Debug, PartialEq)]
pub struct AssertCountFailure(usize);

#[derive(Debug, PartialEq)]
pub enum AssertStringValueFailure {
    WrongStringValue(String),
    WrongValue(Sequence),
}

#[derive(Debug, PartialEq)]
pub enum AssertXmlFailure {
    WrongXml(String),
    WrongValue(Sequence),
}

#[derive(Debug, PartialEq)]
pub enum Failure {
    AnyOf(AssertAnyOf, Vec<TestOutcome>),
    Not(AssertNot, Box<TestOutcome>),
    Eq(AssertEq, Sequence),
    DeepEq(AssertDeepEq, Sequence),
    True(AssertTrue, Sequence),
    False(AssertFalse, Sequence),
    Count(AssertCount, AssertCountFailure),
    StringValue(AssertStringValue, AssertStringValueFailure),
    Xml(AssertXml, AssertXmlFailure),
    Assert(Assert, Sequence),
    Permutation(AssertPermutation, Sequence),
    Empty(AssertEmpty, Sequence),
    Error(AssertError, Sequence),
    Type(AssertType, Sequence),
}

impl fmt::Display for Failure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Failure::AnyOf(_, outcomes) => {
                writeln!(f, "any of:")?;
                for outcome in outcomes {
                    match outcome {
                        TestOutcome::Failed(failure) => {
                            writeln!(f, "  {}", failure)?;
                        }
                        _ => {
                            writeln!(f, "  Unexpected test outcome {:?}", outcome)?;
                        }
                    }
                }
                Ok(())
            }
            Failure::Not(_a, _outcome) => {
                writeln!(f, "not:")?;
                // writeln!(f, "  {}", outcome)?;
                Ok(())
            }
            Failure::Eq(a, value) => {
                writeln!(f, "eq:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
            Failure::DeepEq(a, value) => {
                writeln!(f, "deep-eq:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
            Failure::True(_a, value) => {
                writeln!(f, "true:")?;
                writeln!(f, "  expected: true")?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
            Failure::False(_a, value) => {
                writeln!(f, "false:")?;
                writeln!(f, "  expected: false")?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
            Failure::Count(a, failure) => {
                writeln!(f, "count:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", failure)?;
                Ok(())
            }
            Failure::StringValue(a, failure) => {
                writeln!(f, "string-value:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", failure)?;
                Ok(())
            }
            Failure::Xml(a, failure) => {
                writeln!(f, "xml:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", failure)?;
                Ok(())
            }
            Failure::Assert(_a, failure) => {
                writeln!(f, "assert:")?;
                writeln!(f, "  actual: {:?}", failure)?;
                Ok(())
            }
            Failure::Permutation(a, failure) => {
                writeln!(f, "permutation:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", failure)?;
                Ok(())
            }
            Failure::Empty(_a, value) => {
                writeln!(f, "empty:")?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
            Failure::Type(_a, value) => {
                writeln!(f, "type:")?;
                writeln!(f, "  expected type: {:?}", _a.0)?;
                writeln!(f, "  value of wrong type: {:?}", value)?;
                Ok(())
            }
            Failure::Error(a, value) => {
                writeln!(f, "error:")?;
                writeln!(f, "  expected: {:?}", a.0)?;
                writeln!(f, "  actual: {:?}", value)?;
                Ok(())
            }
        }
    }
}

fn run_xpath(expr: &XPathExpr) -> error::Result<Sequence> {
    let queries = Queries::default();
    let q = queries.sequence(expr)?;

    let mut documents = Documents::default();

    // we don't need any particular context to execute this query
    q.execute_build_context(&mut documents, |_build| {})
}

fn run_xpath_with_result(
    expr: &XPathExpr,
    sequence: &Sequence,
    documents: &mut Documents,
) -> error::Result<Sequence> {
    let mut builder = context::StaticContextBuilder::default();
    let name = Name::name("result");
    builder.variable_names([name.clone()]);
    let static_context = builder.build();

    let queries = Queries::default();
    let q = queries.sequence_with_context(expr, static_context)?;

    let variables = AHashMap::from([(name, sequence.clone())]);

    q.execute_build_context(documents, |build| {
        build.variables(variables);
    })
}

#[cfg(test)]
mod tests {
    use std::{path::PathBuf, vec};

    use super::*;

    use crate::{language::XPathLanguage, ns::XPATH_TEST_NS, paths::Mode};

    #[test]
    fn test_test_case_result() {
        let xml = format!(
            r#"<doc xmlns="{}"><result><assert-eq>0</assert-eq></result></doc>"#,
            XPATH_TEST_NS
        );
        let context = LoadContext {
            path: PathBuf::new(),
            catalog_ns: XPATH_TEST_NS,
            mode: Mode::XPath,
        };
        let test_case_result = TestCaseResult::load_from_xml_with_context(&xml, &context).unwrap();
        assert_eq!(
            test_case_result,
            TestCaseResult::AssertEq(AssertEq::new("0".to_string()))
        );
    }

    #[test]
    fn test_test_case_result2() {
        let xml = format!(
            r#"
<doc xmlns="{}">
  <result>
    <any-of>
      <assert>$result/x = ('http://www.example.com', 'http://www.example.com/')</assert>
      <assert>$result/x = 'http://www.example.com/base'</assert>
   </any-of>
  </result>
</doc>"#,
            XPATH_TEST_NS
        );
        let context = LoadContext::new::<XPathLanguage>(PathBuf::new());
        let test_case_result = TestCaseResult::load_from_xml_with_context(&xml, &context).unwrap();
        assert_eq!(
            test_case_result,
            TestCaseResult::AnyOf(AssertAnyOf::new(vec![
                TestCaseResult::Assert(Assert::new(
                    "$result/x = ('http://www.example.com', 'http://www.example.com/')".to_string()
                )),
                TestCaseResult::Assert(Assert::new(
                    "$result/x = 'http://www.example.com/base'".to_string()
                )),
            ]))
        );
    }
}