tests.rs (3124B)
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 | use super::*;
fn expand(source: String) -> Result<String, String> {
let expander = tree_builder(Some("<test>"), source);
let tree = match expander.expand() {
Ok(tree) => tree,
Err(err) => return Err(format!("{}", err)),
};
let mut out = String::new();
for node in tree {
out += &format!("{}", node);
}
Ok(out)
}
fn assert_output_eq(input: &str, expect: &str) {
let source = String::from(input);
let output = match expand(source) {
Ok(o) => o,
Err(o) => o,
};
assert_eq!(output, expect);
}
mod tests {
use super::*;
#[test]
fn test_sort_macro_alphanumeric() {
// sort all signed, unsigened, floats and text.
assert_output_eq(
"(%sort (13 d 8 e 14 f 9 10 g 11 12 a b c 0 0.1 1.5 0.2 1 2 -1 -10 -2 -1.4 -0.3) :order ascending)",
"(-10 -2 -1.4 -1 -0.3 0 0.1 0.2 1 1.5 2 8 9 10 11 12 13 14 a b c d e f g)"
);
}
#[test]
fn test_sort_macro_key() {
// sort by the second element in 3-tuples.
assert_output_eq(
"(%sort :key (%lambda ((x y z)) %y) ((x 3 b) (z 1 a) (y 2 c)))",
"((z 1 a) (y 2 c) (x 3 b))"
);
}
#[test]
fn test_reverse_macro() {
// reverse a list while preserving whitespace-ordering.
assert_output_eq(
"(%reverse (a 3 2 1 2 s))",
"(s 2 1 2 3 a)"
);
}
#[test]
fn test_date_timestamps() {
// check %date works with i64 unix timestamps.
assert_output_eq(
"(%date \"%Y-%m-%d\" 0)",
"\"1970-01-01\""
);
assert_output_eq(
"(%date \"%Y-%m-%d %H:%M:%S\" 1733776966)",
"\"2024-12-09 20:42:46\""
);
}
#[test]
fn test_date_timezones() {
assert_output_eq(
"(%date \"%Y-%m-%d %H:%M:%S%Z\" 999999999 :timezone +1)",
"\"2001-09-09 02:46:39+01:00\""
);
assert_output_eq(
"(%date \"%Y-%m-%d %H:%M:%S%Z\" 999999999 :timezone +3.5)",
"\"2001-09-09 05:16:39+03:30\""
);
assert_output_eq(
"(%date \"%Y-%m-%d %H:%M:%S%Z\" 999999999 :timezone -2.25)",
"\"2001-09-08 23:31:39-02:15\""
);
}
#[test]
fn test_timestamp_macro() {
let timestamp = "419083754"; // 13 Apr 1983.
assert_output_eq(
"(%timestamp \"%Y %b %d %H:%M:%S%.3f %z\" \"1983 Apr 13 12:09:14.274 +0000\")",
timestamp
);
assert_output_eq(
"(%timestamp \"%Y %b %d %H:%M:%S%.3f\" \"1983 Apr 13 12:09:14.274\" :timezone 0)",
timestamp
);
// Same time *locally* but in a timezone of -02:00.
let timestamp_minus_2h = "419090954";
assert_output_eq(
"(%timestamp \"%Y %b %d %H:%M:%S%.3f %z\" \"1983 Apr 13 12:09:14.274 -0200\")",
timestamp_minus_2h
);
assert_output_eq(
"(%timestamp \"%Y %b %d %H:%M:%S%.3f\" \"1983 Apr 13 12:09:14.274\" :timezone -2)",
timestamp_minus_2h
);
}
}
|