|
| 1 | +use crate::ifn::IFn; |
| 2 | +use crate::value::{ToValue, Value}; |
| 3 | +use std::rc::Rc; |
| 4 | + |
| 5 | +use crate::error_message; |
| 6 | +use crate::type_tag::TypeTag; |
| 7 | + |
| 8 | +/// clojure.string/blank? ; returns true if nil, empty or whitespace |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct BlankFn {} |
| 11 | +impl ToValue for BlankFn { |
| 12 | + fn to_value(&self) -> Value { |
| 13 | + Value::IFn(Rc::new(self.clone())) |
| 14 | + } |
| 15 | +} |
| 16 | +impl IFn for BlankFn { |
| 17 | + fn invoke(&self, args: Vec<Rc<Value>>) -> Value { |
| 18 | + if args.len() != 1 { |
| 19 | + return error_message::wrong_arg_count(1, args.len()); |
| 20 | + } else { |
| 21 | + match args.get(0).unwrap().to_value() { |
| 22 | + Value::Nil => Value::Boolean(true), |
| 23 | + Value::String(s) => { |
| 24 | + if s.len() == 0 { |
| 25 | + Value::Boolean(true) |
| 26 | + } else { |
| 27 | + return Value::Boolean( |
| 28 | + s.chars() |
| 29 | + .filter(|c| !c.is_whitespace()) |
| 30 | + .collect::<Vec<char>>() |
| 31 | + .len() |
| 32 | + == 0, |
| 33 | + ); |
| 34 | + } |
| 35 | + } |
| 36 | + Value::String(s) => Value::String(s.chars().rev().collect()), |
| 37 | + _a => error_message::type_mismatch(TypeTag::String, &_a.to_value()), |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[cfg(test)] |
| 44 | +mod tests { |
| 45 | + mod reverse_tests { |
| 46 | + use crate::clojure_string::blank_qmark_::BlankFn; |
| 47 | + use crate::ifn::IFn; |
| 48 | + use crate::value::Value; |
| 49 | + use std::rc::Rc; |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn is_non_empty_string_blank() { |
| 53 | + let blank = BlankFn {}; |
| 54 | + let s = "hello"; |
| 55 | + let args = vec![Rc::new(Value::String(String::from(s)))]; |
| 56 | + assert_eq!(Value::Boolean(false), blank.invoke(args)); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn is_empty_string_blank() { |
| 61 | + let blank = BlankFn {}; |
| 62 | + let s = ""; |
| 63 | + let args = vec![Rc::new(Value::String(String::from(s)))]; |
| 64 | + assert_eq!(Value::Boolean(true), blank.invoke(args)); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn is_string_with_whitespace_only_blank() { |
| 69 | + let blank = BlankFn {}; |
| 70 | + let s = " \t \n \r "; |
| 71 | + let args = vec![Rc::new(Value::String(String::from(s)))]; |
| 72 | + assert_eq!(Value::Boolean(true), blank.invoke(args)); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn is_string_with_whitespace_and_text_blank() { |
| 77 | + let blank = BlankFn {}; |
| 78 | + let s = " \thello \n \r "; |
| 79 | + let args = vec![Rc::new(Value::String(String::from(s)))]; |
| 80 | + assert_eq!(Value::Boolean(false), blank.invoke(args)); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn is_nil_blank() { |
| 85 | + let blank = BlankFn {}; |
| 86 | + let args = vec![Rc::new(Value::Nil)]; |
| 87 | + assert_eq!(Value::Boolean(true), blank.invoke(args)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments