Doctests not finding public functions

Hi I'm struggling to get the doc tests to work out that the function it is documenting actually exists.

it's in a public module, and implements a trait called Parser, which provides the required parse_s method.

/// A function for parsing integers
/// ```
/// use gobble::*;
/// let r = common_int.parse_s("32").unwrap();
/// assert_eq!(r,32);
/// ```
///
pub fn common_int<'a>(it: &LCChars<'a>) -> ParseRes<'a, isize> {
    //TODO add and mul without panic
    let mut it = it.clone();
    let (minus, mut res) = match it.next() {
        Some('-') => (-1, 0),
        Some(v) if v >= '0' && v <= '9' => (1, (v as isize - '0' as isize)),
        _ => return it.err_cr(ECode::SMess("Not an int")),
    };

    let mut it2 = it.clone();
    loop {
        match it.next() {
            Some(v) if v >= '0' && v <= '9' => {
                res = res
                    .checked_mul(10)
                    .ok_or(it.err_c(ECode::SMess("Num too big")))?
                    .checked_add(v as isize - '0' as isize)
                    .ok_or(it.err_c(ECode::SMess("Num too big")))?;
                it2 = it.clone();
            }
            _ => return Ok((it2, minus * res)),
        }
    }
}

When called from a local test this function works, called from the doctest it fails to find it and I get the following error:

---- src/common.rs - common::common_int (line 7) stdout ----
error[E0425]: cannot find value `common_int` in this scope
   --> src/common.rs:9:9
    |
5   | let r = common_int.parse_s("32").unwrap();
    |         ^^^^^^^^^^ help: a function with a similar name exists: `common_str`
    | 
   ::: /home/matthew/bdata/CargoTarget/package/gobble-0.1.3/src/reader.rs:201:1
    |


common_str is a different function in another module, I have made this module public in the lib.rs:

Any ideas?

Doc tests are using your library, like the any external crate will. So, as the common_int is inside module commons, and the crate is (if I understand correctly) called gobble, it should be either called as gobble::common::common_int or explicitly imported in the use statement.

thanks for getting back, but
my lib.rs already has:

pub mod common;
pub use common::*;

your doctest seems incomplete. It calls parse_s() on an object called common_int, but there is no let common_int = … anywhere to be seen. Do you maybe have to instantiate the parser first?

common_int is the name of the method I'm testing.

I'm not at my computer so I might oet this a bit off, but I impl parser for this function in a public exported module.

impl <F,R> Parser<R> for F
    where F:for <'a> Fn(&LCChars<'a>)->ParseRes<'a,R>{
    fn parse<'a>(self, it:&LCchars<'a>)->ParseRes<'a>{
    (self)(it)
}
}

parse_s(Str) is a provided method by the trait.

This exact same code as the doctest works in module tests.

Not sure why,but running cargo clean seemed to sort it out

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.