#How to test this function?

4 messages · Page 1 of 1 (latest)

twilit yacht
#

Hi,

I have a function which parse a string to return a Time struct and I have a test which return an error on the assert_eq line : binary operation == cannot be applied to type std::result::Result<(&str, Time), nom::Err<nom::error::Error<&str>>>

parse.rs:

use crate::Time;
use nom::{
    bytes::complete::tag,
    character::complete::{u16, u8},
    sequence::{preceded, tuple},
    *,
};

pub fn srt_time(input: &str) -> IResult<&str, Time> {
    let (input, values) = tuple((
        u8,
        preceded(tag(":"), u8),
        preceded(tag(":"), u8),
        preceded(tag(","), u16),
    ))(input)?;
    dbg!(values);
    Ok((
        input,
        Time {
            hour: values.0,
            minute: values.1,
            second: values.2,
            millisecond: values.3,
        },
    ))
}

lib.rs:

#[cfg(test)]
mod tests {
    use crate::parser::srt_time;

    use super::*;

    #[test]
    fn parse_time() {
        let time = Time {
            hour: 0,
            minute: 0,
            second: 23,
            millisecond: 123,
        };

        assert_eq!(srt_time("00:00:23,123"), Ok(("00:00:23,123", time)));
    }
}
zealous obsidian
#

You can unwrap() the result and remove the Ok from the rhs.

twilit yacht
#

Like this? assert_eq!(srt_time("00:00:23,123").unwrap(), ("00:00:23,123", time));

I still have binary operation -- cannot be applied to type (&str, Time)

#

I've added this and it's ok.

impl PartialEq for Time {
    fn eq(&self, other: &Self) -> bool {
        self.hour == other.hour
            && self.minute == other.hour
            && self.second == other.second
            && self.millisecond == other.millisecond
    }
}