diff options
| author | Lakelezz <[email protected]> | 2017-12-18 22:23:07 +0100 |
|---|---|---|
| committer | alex <[email protected]> | 2017-12-18 22:23:07 +0100 |
| commit | 143fddd83f1fc93c070e36bf31906d2631c68f97 (patch) | |
| tree | 7d2f0ff78cab7c72e86d044d0022b9565dc44bf4 | |
| parent | Fix ifs (diff) | |
| download | serenity-143fddd83f1fc93c070e36bf31906d2631c68f97.tar.xz serenity-143fddd83f1fc93c070e36bf31906d2631c68f97.zip | |
Actually fix `Args`'s `parse` and add a few tests (#236)
| -rw-r--r-- | src/framework/standard/args.rs | 11 | ||||
| -rw-r--r-- | tests/test_args.rs | 50 |
2 files changed, 53 insertions, 8 deletions
diff --git a/src/framework/standard/args.rs b/src/framework/standard/args.rs index 1076507..e940f8c 100644 --- a/src/framework/standard/args.rs +++ b/src/framework/standard/args.rs @@ -79,15 +79,10 @@ fn parse<T: FromStr>(s: &mut String, delimiter: &str) -> Result<T, T::Err> where T::Err: StdError { let mut pos = s.find(delimiter).unwrap_or_else(|| s.len()); - let res = (&s[..pos]).parse::<T>().map_err(Error::Parse); - // First find out whether the delimiter is 2 chars or longer, - // if so add those extras to the position. - // Otherwise just add `1` for 1 char delimiters. - if delimiter.len() > 1 { + + if pos < s.len() { pos += delimiter.len(); - } else if pos < s.len() { - pos += 1; } s.drain(..pos); @@ -214,7 +209,7 @@ impl Args { pub fn full(&self) -> &str { &self.message } /// The amount of args. - /// + /// /// # Examples /// /// ```rust diff --git a/tests/test_args.rs b/tests/test_args.rs new file mode 100644 index 0000000..424bc76 --- /dev/null +++ b/tests/test_args.rs @@ -0,0 +1,50 @@ +extern crate serenity; + +use serenity::framework::standard::Args; + +#[test] +fn single_i32_with_2_bytes_long_delimiter() { + let mut args = Args::new("1, 2", &[", ".to_string()]); + + assert_eq!(args.single::<i32>().unwrap(), 1); + assert_eq!(args.single::<i32>().unwrap(), 2); +} + +#[test] +fn single_i32_with_1_byte_long_delimiter_i32() { + let mut args = Args::new("1,2", &[",".to_string()]); + + assert_eq!(args.single::<i32>().unwrap(), 1); + assert_eq!(args.single::<i32>().unwrap(), 2); +} + +#[test] +fn single_i32_with_wrong_char_after_first_arg() { + let mut args = Args::new("1, 2", &[",".to_string()]); + + assert_eq!(args.single::<i32>().unwrap(), 1); + assert!(args.single::<i32>().is_err()); +} + +#[test] +fn single_i32_with_one_character_being_3_bytes_long() { + let mut args = Args::new("1★2", &["★".to_string()]); + + assert_eq!(args.single::<i32>().unwrap(), 1); + assert_eq!(args.single::<i32>().unwrap(), 2); +} + +#[test] +fn single_i32_with_untrimmed_whitespaces() { + let mut args = Args::new(" 1, 2 ", &[",".to_string()]); + + assert!(args.single::<i32>().is_err()); +} + +#[test] +fn single_i32_n() { + let args = Args::new("1,2", &[",".to_string()]); + + assert_eq!(args.single_n::<i32>().unwrap(), 1); + assert_eq!(args.single_n::<i32>().unwrap(), 1); +} |