|
| 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 IncludesFn {} |
| 11 | +impl ToValue for IncludesFn { |
| 12 | + fn to_value(&self) -> Value { |
| 13 | + Value::IFn(Rc::new(self.clone())) |
| 14 | + } |
| 15 | +} |
| 16 | +impl IFn for IncludesFn { |
| 17 | + fn invoke(&self, args: Vec<Rc<Value>>) -> Value { |
| 18 | + if args.len() != 2 { |
| 19 | + return error_message::wrong_arg_count(2, args.len()); |
| 20 | + } else { |
| 21 | + match ( |
| 22 | + args.get(0).unwrap().to_value(), |
| 23 | + args.get(1).unwrap().to_value(), |
| 24 | + ) { |
| 25 | + (Value::String(s), Value::String(substring)) => { |
| 26 | + Value::Boolean(s.contains(&substring)) |
| 27 | + } |
| 28 | + _a => error_message::type_mismatch(TypeTag::String, &_a.1.to_value()), |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg(test)] |
| 35 | +mod tests { |
| 36 | + mod reverse_tests { |
| 37 | + use crate::clojure_string::includes_qmark_::IncludesFn; |
| 38 | + use crate::ifn::IFn; |
| 39 | + use crate::value::Value; |
| 40 | + use std::rc::Rc; |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn hello_includes_ell() { |
| 44 | + let blank = IncludesFn {}; |
| 45 | + let s = "hello"; |
| 46 | + let substring = "ell"; |
| 47 | + let args = vec![ |
| 48 | + Rc::new(Value::String(String::from(s))), |
| 49 | + Rc::new(Value::String(String::from(substring))), |
| 50 | + ]; |
| 51 | + assert_eq!(Value::Boolean(true), blank.invoke(args)); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn hello_does_not_include_leh() { |
| 56 | + let blank = IncludesFn {}; |
| 57 | + let s = "hello"; |
| 58 | + let substring = "leh"; |
| 59 | + let args = vec![ |
| 60 | + Rc::new(Value::String(String::from(s))), |
| 61 | + Rc::new(Value::String(String::from(substring))), |
| 62 | + ]; |
| 63 | + assert_eq!(Value::Boolean(false), blank.invoke(args)); |
| 64 | + } |
| 65 | + |
| 66 | + #[test] |
| 67 | + fn hello_includes_empty_string() { |
| 68 | + let blank = IncludesFn {}; |
| 69 | + let s = "hello"; |
| 70 | + let substring = ""; |
| 71 | + let args = vec![ |
| 72 | + Rc::new(Value::String(String::from(s))), |
| 73 | + Rc::new(Value::String(String::from(substring))), |
| 74 | + ]; |
| 75 | + assert_eq!(Value::Boolean(true), blank.invoke(args)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments