Skip to content

Commit

Permalink
Added test for custom_cmp
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Feb 7, 2025
1 parent 37ec7fd commit c0256c5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions minijinja/tests/test_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};
use std::fmt;
use std::sync::Arc;
Expand Down Expand Up @@ -1248,3 +1249,37 @@ fn test_plain_object_compare() {
assert!(v1 != Value::from(vec![1, 2, 3]));
assert!(v1 > Value::from(vec![1, 2, 3]));
}

#[test]
fn test_custom_object_compare() {
#[derive(Debug)]
struct X(i32);

impl Object for X {
fn repr(self: &Arc<Self>) -> ObjectRepr {
ObjectRepr::Plain
}

fn custom_cmp(self: &Arc<Self>, other: &DynObject) -> Option<Ordering> {
let other = other.downcast_ref::<Self>()?;
Some(self.0.cmp(&other.0))
}

fn render(self: &Arc<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result
where
Self: Sized + 'static,
{
write!(f, "{}", self.0)
}
}

let nums = (0..5)
.map(X)
.map(Value::from_object)
.rev()
.collect::<Vec<_>>();
let seq = Value::from_object(nums);

let rv = render!("{{ seq|sort|join('|') }}", seq);
assert_eq!(rv, "0|1|2|3|4");
}

0 comments on commit c0256c5

Please sign in to comment.