Skip to content

Commit

Permalink
Fix name resolution errors related to cfg(test)
Browse files Browse the repository at this point in the history
This commit is for fixing the below error. `cfg(test)` does not seem
to be passed to the imported library. To sidestep this issue, the
duplicate `MockDevice` is maintained (one for `io`'s tests and another
for `macro`'s tests).

```
$ ./scripts/tests/crates.sh
error[E0432]: unresolved import `io::test`
  --> rmm/src/macro.rs:77:13
   |
77 |     use io::test::MockDevice;
   |             ^^^^ could not find `test` in `io`
```

Signed-off-by: Changho Choi <[email protected]>
  • Loading branch information
zpzigi754 committed Jun 10, 2024
1 parent e8d4385 commit f625996
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn stdout() -> SpinlockGuard<'static, Stdout> {
#[cfg(test)]
pub mod test {
extern crate alloc;
use crate::io::{ConsoleWriter, Device, Result, Stdout, Write};
use super::{ConsoleWriter, Device, Result, Stdout, Write};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
Expand Down
51 changes: 48 additions & 3 deletions rmm/src/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,57 @@ macro_rules! const_assert_size {

#[cfg(test)]
mod test {
use crate::io::test::MockDevice;
use crate::io::{stdout, Write as IoWrite};
extern crate alloc;

use crate::{eprintln, println};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;
use io::{stdout, Write as IoWrite};
use io::{ConsoleWriter, Device, Result, Write};

pub struct MockDevice {
buffer: RefCell<Vec<u8>>,
ready: bool,
}

extern crate alloc;
impl MockDevice {
pub const fn new() -> Self {
MockDevice {
buffer: RefCell::new(Vec::new()),
ready: false,
}
}

pub fn output(&self) -> String {
String::from_utf8(self.buffer.borrow().to_vec()).unwrap()
}

pub fn clear(&mut self) {
self.buffer.borrow_mut().clear()
}
}

impl Device for MockDevice {
fn initialize(&mut self) -> Result<()> {
self.ready = true;
Ok(())
}

fn initialized(&self) -> bool {
self.ready
}
}

impl Write for MockDevice {
fn write_all(&mut self, buf: &[u8]) -> Result<()> {
self.buffer.borrow_mut().extend_from_slice(buf);
Ok(())
}
}

impl ConsoleWriter for MockDevice {}

#[test]
fn println_without_arg() {
Expand Down

0 comments on commit f625996

Please sign in to comment.