aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared/src
diff options
context:
space:
mode:
authorLibravatar D.Loh <lploh2016@gmail.com>2020-02-07 09:08:40 -0800
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2020-02-07 20:29:58 +0100
commit144ecc5ef9064b3e8e820329f00a84a50e6762eb (patch)
treef2c6db40031ad7eee914bc1ee455b1dc4f98ca91 /askama_shared/src
parent6a63520785e65d8bfe9fd9e4f1db39d7ffab045d (diff)
downloadaskama-144ecc5ef9064b3e8e820329f00a84a50e6762eb.tar.gz
askama-144ecc5ef9064b3e8e820329f00a84a50e6762eb.tar.bz2
askama-144ecc5ef9064b3e8e820329f00a84a50e6762eb.zip
add unit test, and do not truncate when len equal
Diffstat (limited to '')
-rw-r--r--askama_shared/src/filters/mod.rs24
1 files changed, 23 insertions, 1 deletions
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs
index 11216a5..eeb9e52 100644
--- a/askama_shared/src/filters/mod.rs
+++ b/askama_shared/src/filters/mod.rs
@@ -158,7 +158,7 @@ pub fn trim(s: &dyn fmt::Display) -> Result<String> {
/// Limit string length, appends '...' if truncated
pub fn truncate(s: &dyn fmt::Display, len: &usize) -> Result<String> {
let mut s = s.to_string();
- if s.len() < *len {
+ if s.len() <= *len {
Ok(s)
} else {
let mut real_len = *len;
@@ -348,6 +348,28 @@ mod tests {
fn test_trim() {
assert_eq!(trim(&" Hello\tworld\t").unwrap(), "Hello\tworld");
}
+
+ #[test]
+ fn test_truncate() {
+ assert_eq!(truncate(&"hello", &2).unwrap(), "he...");
+ let a = String::from("您好");
+ assert_eq!(a.len(), 6);
+ assert_eq!(String::from("您").len(), 3);
+ assert_eq!(truncate(&"您好", &1).unwrap(), "您...");
+ assert_eq!(truncate(&"您好", &2).unwrap(), "您...");
+ assert_eq!(truncate(&"您好", &3).unwrap(), "您...");
+ assert_eq!(truncate(&"您好", &6).unwrap(), "您好");
+ assert_eq!(truncate(&"您好", &7).unwrap(), "您好");
+ let s = String::from("🤚a🤚");
+ assert_eq!(s.len(), 9);
+ assert_eq!(String::from("🤚").len(), 4);
+ assert_eq!(truncate(&"🤚a🤚", &1).unwrap(), "🤚...");
+ assert_eq!(truncate(&"🤚a🤚", &2).unwrap(), "🤚...");
+ assert_eq!(truncate(&"🤚a🤚", &3).unwrap(), "🤚...");
+ assert_eq!(truncate(&"🤚a🤚", &4).unwrap(), "🤚...");
+ assert_eq!(truncate(&"🤚a🤚", &9).unwrap(), "🤚a🤚");
+ assert_eq!(truncate(&"🤚a🤚", &10).unwrap(), "🤚a🤚");
+ }
#[test]
fn test_indent() {