skyscraper 0.7.0

XPath for HTML web scraping
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
//! <https://www.w3.org/TR/2017/REC-xpath-31-20170321/#id-comparisons>

use std::fmt::Display;

use nom::{
    branch::alt, bytes::complete::tag, character::complete::multispace0, combinator::opt,
    error::context, sequence::tuple,
};

use ordered_float::OrderedFloat;

use crate::{
    xpath::{
        grammar::{
            data_model::{AnyAtomicType, XpathItem},
            XpathItemTreeNode,
            expressions::string_concat_expressions::string_concat_expr,
            recipes::Res,
            terminal_symbols::symbol_separator,
        },
        xpath_item_set::XpathItemSet,
        ExpressionApplyError, XpathExpressionContext,
    },
    xpath_item_set,
};

use super::{
    primary_expressions::static_function_calls::func_data,
    string_concat_expressions::StringConcatExpr,
};

pub fn comparison_expr(input: &str) -> Res<&str, ComparisonExpr> {
    // https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-ComparisonExpr

    fn value_comp_map(input: &str) -> Res<&str, ComparisonType> {
        value_comp(input).map(|(next_input, res)| (next_input, ComparisonType::ValueComp(res)))
    }

    fn general_comp_map(input: &str) -> Res<&str, ComparisonType> {
        general_comp(input).map(|(next_input, res)| (next_input, ComparisonType::GeneralComp(res)))
    }

    fn node_comp_map(input: &str) -> Res<&str, ComparisonType> {
        node_comp(input).map(|(next_input, res)| (next_input, ComparisonType::NodeComp(res)))
    }

    context(
        "comparison_expr",
        tuple((
            string_concat_expr,
            opt(tuple((
                alt((value_comp_map, node_comp_map, general_comp_map)),
                string_concat_expr,
            ))),
        )),
    )(input)
    .map(|(next_input, res)| {
        let comparison = res.1.map(|res| ComparisonExprPair(res.0, res.1));
        (
            next_input,
            ComparisonExpr {
                expr: res.0,
                comparison,
            },
        )
    })
}

#[derive(PartialEq, Debug, Clone)]
pub struct ComparisonExpr {
    pub expr: StringConcatExpr,
    pub comparison: Option<ComparisonExprPair>,
}

impl Display for ComparisonExpr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.expr)?;

        if let Some(x) = &self.comparison {
            write!(f, "{}", x)?;
        }

        Ok(())
    }
}

impl ComparisonExpr {
    pub(crate) fn eval<'tree>(
        &self,
        context: &XpathExpressionContext<'tree>,
    ) -> Result<XpathItemSet<'tree>, ExpressionApplyError> {
        // Evaluate the first expression.
        let result = self.expr.eval(context)?;

        // If there's only one parameter, return it's eval.
        let comparison = if let Some(comparison) = &self.comparison {
            comparison
        } else {
            return Ok(result);
        };

        // Otherwise, do the comparison op.
        // Get the second expression result.
        let second_result = comparison.1.eval(context)?;

        // Atomize both results.
        let atomized1 = func_data(&result, context.item_tree)?;
        let atomized2 = func_data(&second_result, context.item_tree)?;

        let bool_value = match comparison.0 {
            ComparisonType::GeneralComp(comp) => {
                // XPath 3.1 §3.7.2: General comparisons are existentially quantified.
                // The result is true if any pair (a, b) from atomized1 x atomized2
                // satisfies the corresponding value comparison.
                if atomized1.is_empty() || atomized2.is_empty() {
                    false
                } else {
                    let mut found = false;
                    for a in atomized1.iter() {
                        for b in atomized2.iter() {
                            if comp.is_match(a, b) {
                                found = true;
                                break;
                            }
                        }
                        if found {
                            break;
                        }
                    }
                    found
                }
            }
            ComparisonType::ValueComp(comp) => {
                // XPath 3.1 §3.7.1: Value comparisons require singleton operands.
                if atomized1.is_empty() || atomized2.is_empty() {
                    return Ok(XpathItemSet::new());
                }
                if atomized1.len() > 1 || atomized2.len() > 1 {
                    return Err(ExpressionApplyError {
                        msg: String::from("err:XPTY0004 The first operand of a value comparison is a sequence of length greater than one")
                    });
                }
                comp.is_match(&atomized1[0], &atomized2[0])
            }
            ComparisonType::NodeComp(comp) => {
                // Node comparisons require both operands to be single nodes.
                if result.is_empty() || second_result.is_empty() {
                    return Ok(XpathItemSet::new());
                }
                if result.len() > 1 || second_result.len() > 1 {
                    return Err(ExpressionApplyError {
                        msg: String::from("err:XPTY0004 Node comparison requires singleton node operands"),
                    });
                }
                let node1 = match &result[0] {
                    XpathItem::Node(n) => n,
                    _ => {
                        return Err(ExpressionApplyError {
                            msg: String::from(
                                "err:XPTY0004 Node comparison requires node operands",
                            ),
                        })
                    }
                };
                let node2 = match &second_result[0] {
                    XpathItem::Node(n) => n,
                    _ => {
                        return Err(ExpressionApplyError {
                            msg: String::from(
                                "err:XPTY0004 Node comparison requires node operands",
                            ),
                        })
                    }
                };
                comp.is_match(node1, node2)
            }
        };

        Ok(xpath_item_set![XpathItem::AnyAtomicType(
            AnyAtomicType::Boolean(bool_value),
        )])
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct ComparisonExprPair(pub ComparisonType, pub StringConcatExpr);

impl Display for ComparisonExprPair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{}", self.0, self.1)
    }
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum ComparisonType {
    ValueComp(ValueComp),
    GeneralComp(GeneralComp),
    NodeComp(NodeComp),
}

impl Display for ComparisonType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ComparisonType::ValueComp(x) => write!(f, "{}", x),
            ComparisonType::GeneralComp(x) => write!(f, "{}", x),
            ComparisonType::NodeComp(x) => write!(f, "{}", x),
        }
    }
}

fn value_comp(input: &str) -> Res<&str, ValueComp> {
    // https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-ValueComp

    fn equal(input: &str) -> Res<&str, ValueComp> {
        tag("eq")(input).map(|(next_input, _res)| (next_input, ValueComp::Equal))
    }

    fn not_equal(input: &str) -> Res<&str, ValueComp> {
        tag("ne")(input).map(|(next_input, _res)| (next_input, ValueComp::NotEqual))
    }

    fn less_than(input: &str) -> Res<&str, ValueComp> {
        tag("lt")(input).map(|(next_input, _res)| (next_input, ValueComp::LessThan))
    }

    fn less_than_equal_to(input: &str) -> Res<&str, ValueComp> {
        tag("le")(input).map(|(next_input, _res)| (next_input, ValueComp::LessThanEqualTo))
    }

    fn greater_than(input: &str) -> Res<&str, ValueComp> {
        tag("gt")(input).map(|(next_input, _res)| (next_input, ValueComp::GreaterThan))
    }

    fn greater_than_equal_to(input: &str) -> Res<&str, ValueComp> {
        tag("ge")(input).map(|(next_input, _res)| (next_input, ValueComp::GreaterThanEqualTo))
    }

    context(
        "value_comp",
        tuple((
            symbol_separator,
            alt((
                equal,
                not_equal,
                less_than,
                less_than_equal_to,
                greater_than,
                greater_than_equal_to,
            )),
            symbol_separator,
        )),
    )(input)
    .map(|(next_input, res)| (next_input, res.1))
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum ValueComp {
    Equal,
    NotEqual,
    LessThan,
    LessThanEqualTo,
    GreaterThan,
    GreaterThanEqualTo,
}

impl Display for ValueComp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValueComp::Equal => write!(f, " eq "),
            ValueComp::NotEqual => write!(f, " ne "),
            ValueComp::LessThan => write!(f, " lt "),
            ValueComp::LessThanEqualTo => write!(f, " le "),
            ValueComp::GreaterThan => write!(f, " gt "),
            ValueComp::GreaterThanEqualTo => write!(f, " ge "),
        }
    }
}

impl ValueComp {
    pub(crate) fn is_match(&self, first: &AnyAtomicType, second: &AnyAtomicType) -> bool {
        let (eq, lt, gt) = match self {
            ValueComp::Equal => (true, false, false),
            ValueComp::NotEqual => (false, true, true),
            ValueComp::LessThan => (false, true, false),
            ValueComp::LessThanEqualTo => (true, true, false),
            ValueComp::GreaterThan => (false, false, true),
            ValueComp::GreaterThanEqualTo => (true, false, true),
        };
        compare_atomic(first, second, eq, lt, gt)
    }
}

fn general_comp(input: &str) -> Res<&str, GeneralComp> {
    // https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-GeneralComp

    fn equal(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag("="), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::Equal))
    }

    fn not_equal(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag("!="), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::NotEqual))
    }

    fn less_than(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag("<"), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::LessThan))
    }

    fn less_than_equal_to(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag("<="), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::LessThanEqualTo))
    }

    fn greater_than(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag(">"), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::GreaterThan))
    }

    fn greater_than_equal_to(input: &str) -> Res<&str, GeneralComp> {
        tuple((multispace0, tag(">="), multispace0))(input)
            .map(|(next_input, _res)| (next_input, GeneralComp::GreaterThanEqualTo))
    }

    // Multi-character operators must be tried before their single-character
    // prefixes (e.g. "<=" before "<") so the shorter match doesn't greedily
    // consume part of the longer token.
    context(
        "general_comp",
        alt((
            not_equal,
            less_than_equal_to,
            greater_than_equal_to,
            equal,
            less_than,
            greater_than,
        )),
    )(input)
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum GeneralComp {
    Equal,
    NotEqual,
    LessThan,
    LessThanEqualTo,
    GreaterThan,
    GreaterThanEqualTo,
}

impl Display for GeneralComp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GeneralComp::Equal => write!(f, "="),
            GeneralComp::NotEqual => write!(f, "!="),
            GeneralComp::LessThan => write!(f, "<"),
            GeneralComp::LessThanEqualTo => write!(f, "<="),
            GeneralComp::GreaterThan => write!(f, ">"),
            GeneralComp::GreaterThanEqualTo => write!(f, ">="),
        }
    }
}

impl GeneralComp {
    pub(crate) fn is_match(&self, first: &AnyAtomicType, second: &AnyAtomicType) -> bool {
        let (eq, lt, gt) = match self {
            GeneralComp::Equal => (true, false, false),
            GeneralComp::NotEqual => (false, true, true),
            GeneralComp::LessThan => (false, true, false),
            GeneralComp::LessThanEqualTo => (true, true, false),
            GeneralComp::GreaterThan => (false, false, true),
            GeneralComp::GreaterThanEqualTo => (true, false, true),
        };
        compare_atomic(first, second, eq, lt, gt)
    }
}

/// Compare two atomic values, applying type coercion when needed.
///
/// The `eq`, `lt`, `gt` flags indicate which orderings are accepted — e.g.
/// `(true, true, false)` means "less-than-or-equal".  Coercion is applied
/// first via [`coerce_for_comparison`]; then `partial_cmp` (or `PartialEq`
/// for pure equality/inequality) determines the result.  Returns `false`
/// for incompatible types that cannot be ordered.
fn compare_atomic(
    first: &AnyAtomicType,
    second: &AnyAtomicType,
    eq: bool,
    lt: bool,
    gt: bool,
) -> bool {
    // Ordering comparisons (<, >, <=, >=) cast string operands to double per
    // XPath 3.1 §3.7.  Equality (=) and inequality (!=) compare strings as strings.
    let is_ordering = (lt || gt) && !(lt && gt && !eq);

    let (a, b);
    let (lhs, rhs) = if let Some(coerced) = coerce_for_comparison(first, second, is_ordering) {
        a = coerced.0;
        b = coerced.1;
        (&a, &b)
    } else {
        (first, second)
    };

    // Per IEEE 754 / XPath spec: any comparison involving NaN returns false,
    // except `ne` (not-equal) which returns true. OrderedFloat violates this
    // by making NaN == NaN, so we must guard explicitly.
    let either_nan = matches!(
        lhs,
        AnyAtomicType::Float(f) if f.is_nan()
    ) || matches!(
        lhs,
        AnyAtomicType::Double(d) if d.is_nan()
    ) || matches!(
        rhs,
        AnyAtomicType::Float(f) if f.is_nan()
    ) || matches!(
        rhs,
        AnyAtomicType::Double(d) if d.is_nan()
    );

    if either_nan {
        // `ne` is encoded as (!eq, lt, gt); for NaN ne anything, result is true.
        // All other comparisons involving NaN return false.
        return !eq && lt && gt;
    }

    // Pure equality / inequality can use PartialEq directly, which works
    // across all variant combinations (different variants → not equal).
    if eq && !lt && !gt {
        return lhs == rhs;
    }
    if !eq && lt && gt {
        return lhs != rhs;
    }

    // Ordering comparisons use partial_cmp. Returns false for incompatible
    // types (partial_cmp returns None).
    match lhs.partial_cmp(rhs) {
        Some(std::cmp::Ordering::Equal) => eq,
        Some(std::cmp::Ordering::Less) => lt,
        Some(std::cmp::Ordering::Greater) => gt,
        None => false,
    }
}

/// Coerce a pair of atomic values for comparison per XPath 3.1 §3.7.
///
/// - String (xs:untypedAtomic) vs numeric → cast string to xs:double, promote
///   numeric operand to xs:double.
/// - Mixed numeric types (e.g. Integer vs Double) → promote both to xs:double.
/// - Otherwise, return `None` (no coercion needed, compare directly).
fn coerce_for_comparison(
    first: &AnyAtomicType,
    second: &AnyAtomicType,
    _is_ordering: bool,
) -> Option<(AnyAtomicType, AnyAtomicType)> {
    fn is_numeric(v: &AnyAtomicType) -> bool {
        matches!(
            v,
            AnyAtomicType::Integer(_) | AnyAtomicType::Float(_) | AnyAtomicType::Double(_)
        )
    }

    fn to_double(v: &AnyAtomicType) -> AnyAtomicType {
        match v {
            AnyAtomicType::Integer(i) => {
                AnyAtomicType::Double(OrderedFloat(*i as f64))
            }
            AnyAtomicType::Float(f) => {
                AnyAtomicType::Double(OrderedFloat(f.0 as f64))
            }
            AnyAtomicType::Double(_) => v.clone(),
            _ => AnyAtomicType::Double(OrderedFloat(f64::NAN)),
        }
    }

    // Per XPath 3.1, general comparisons cast untypedAtomic to the type of the
    // other operand. A failed cast to xs:double produces NaN (matching fn:number
    // semantics for implicit casts), unlike explicit xs:double() which raises
    // FORG0001. This is intentionally different from the arithmetic error path.
    fn string_to_double(s: &str) -> AnyAtomicType {
        let d = s.trim().parse::<f64>().unwrap_or(f64::NAN);
        AnyAtomicType::Double(OrderedFloat(d))
    }

    fn to_boolean(v: &AnyAtomicType) -> AnyAtomicType {
        let b = match v {
            AnyAtomicType::Boolean(b) => *b,
            AnyAtomicType::Integer(n) => *n != 0,
            AnyAtomicType::Float(f) => !f.is_nan() && f.0 != 0.0,
            AnyAtomicType::Double(d) => !d.is_nan() && d.0 != 0.0,
            AnyAtomicType::String(s) => !s.is_empty(),
            AnyAtomicType::QName { .. } => true,
        };
        AnyAtomicType::Boolean(b)
    }

    match (first, second) {
        // Boolean coercion: when one operand is Boolean, cast the other to Boolean.
        (AnyAtomicType::Boolean(_), other) if !matches!(other, AnyAtomicType::Boolean(_)) => {
            Some((first.clone(), to_boolean(other)))
        }
        (other, AnyAtomicType::Boolean(_)) if !matches!(other, AnyAtomicType::Boolean(_)) => {
            Some((to_boolean(other), second.clone()))
        }
        // String (untyped) vs numeric: cast string to double, promote numeric to double.
        (AnyAtomicType::String(s), other) if is_numeric(other) => {
            Some((string_to_double(s), to_double(other)))
        }
        (other, AnyAtomicType::String(s)) if is_numeric(other) => {
            Some((to_double(other), string_to_double(s)))
        }
        // Mixed numeric types: promote both to double.
        (a, b)
            if is_numeric(a)
                && is_numeric(b)
                && std::mem::discriminant(a) != std::mem::discriminant(b) =>
        {
            Some((to_double(a), to_double(b)))
        }
        // Same type or non-numeric: no coercion needed.
        _ => None,
    }
}

fn node_comp(input: &str) -> Res<&str, NodeComp> {
    // https://www.w3.org/TR/2017/REC-xpath-31-20170321/#prod-xpath31-NodeComp

    fn is(input: &str) -> Res<&str, NodeComp> {
        tuple((symbol_separator, tag("is"), symbol_separator))(input)
            .map(|(next_input, _res)| (next_input, NodeComp::Is))
    }

    fn precedes(input: &str) -> Res<&str, NodeComp> {
        tuple((multispace0, tag("<<"), multispace0))(input)
            .map(|(next_input, _res)| (next_input, NodeComp::Precedes))
    }

    fn follows(input: &str) -> Res<&str, NodeComp> {
        tuple((multispace0, tag(">>"), multispace0))(input)
            .map(|(next_input, _res)| (next_input, NodeComp::Follows))
    }

    context("node_comp", alt((is, precedes, follows)))(input)
}

#[derive(PartialEq, Debug, Clone, Copy)]
pub enum NodeComp {
    Is,
    Precedes,
    Follows,
}

impl Display for NodeComp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NodeComp::Is => write!(f, " is "),
            NodeComp::Precedes => write!(f, "<<"),
            NodeComp::Follows => write!(f, ">>"),
        }
    }
}

impl NodeComp {
    pub(crate) fn is_match(
        &self,
        first: &XpathItemTreeNode,
        second: &XpathItemTreeNode,
    ) -> bool {
        match (first.node_id(), second.node_id()) {
            (Some(id1), Some(id2)) => match self {
                NodeComp::Is => id1 == id2,
                NodeComp::Precedes => id1 < id2,
                NodeComp::Follows => id1 > id2,
            },
            // If either operand has no node_id, the comparison is false.
            _ => false,
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn comparison_expr_should_parse() {
        // arrange
        let input = r#"$book1/author eq "Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author eq "Kennedy""#);
    }

    #[test]
    fn comparison_expr_should_parse_node_comp_precedes() {
        // arrange
        let input = r#"$book1/author<<"Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author<<"Kennedy""#);
    }

    #[test]
    fn comparison_expr_should_parse_node_comp_precedes_whitespace() {
        // arrange
        let input = r#"$book1/author << "Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author<<"Kennedy""#);
    }

    #[test]
    fn comparison_expr_should_parse_node_comp_is() {
        // arrange
        let input = r#"$book1/author is "Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author is "Kennedy""#);
    }

    #[test]
    fn comparison_expr_should_parse_general_comp() {
        // arrange
        let input = r#"$book1/author="Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author="Kennedy""#);
    }

    #[test]
    fn comparison_expr_should_parse_general_comp_whitespace() {
        // arrange
        let input = r#"$book1/author = "Kennedy""#;

        // act
        let (next_input, res) = comparison_expr(input).unwrap();

        // assert
        assert_eq!(next_input, "");
        assert_eq!(res.to_string(), r#"$book1/author="Kennedy""#);
    }
}