Skip to content

Commit

Permalink
Re-enable README tests in a cleverer way
Browse files Browse the repository at this point in the history
This change borrows the core idea from the [doc-comment crate](https://github.com/GuillaumeGomez/doc-comment/) to run the README examples as doctests by wholesale including README.md via `include_str!`. The resulting docs are on an empty module marked as `#[doc(hidden)]` and I've also made them only run when the `derive` feature is enabled because AFAIK there isn't a good way to disable individual tests in a single item depending on features. This should still be useful since scroll's CI tests with that feature enabled so it'll ensure that the examples in the README continue to work!

Since the examples are being run as standard rusttests now I was able to do a little bit of cleanup on them as well.
  • Loading branch information
luser authored and m4b committed Nov 16, 2019
1 parent f7472de commit e94a84d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 58 deletions.
35 changes: 6 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ A simple example demonstrates its flexibility:
```rust
use scroll::{ctx, Pread, LE};

fn parse() -> Result<(), scroll::Error> {
fn main() -> Result<(), scroll::Error> {
let bytes: [u8; 4] = [0xde, 0xad, 0xbe, 0xef];

// reads a u32 out of `b` with the endianness of the host machine, at offset 0, turbofish-style
Expand Down Expand Up @@ -71,20 +71,13 @@ fn parse() -> Result<(), scroll::Error> {
assert_eq!("world", world);
Ok(())
}

fn main() {
parse().unwrap();
}
```

### Deriving `Pread` and `Pwrite`

Scroll implements a custom derive that can provide `Pread` and `Pwrite` implementations for your structs.

```no_test
#[macro_use]
extern crate scroll_derive;
```rust
use scroll::{Pread, Pwrite, BE};

#[derive(Pread, Pwrite)]
Expand All @@ -94,7 +87,7 @@ struct Data {
three: u8,
}

fn parse() -> Result<(), scroll::Error> {
fn main() -> Result<(), scroll::Error> {
let bytes: [u8; 7] = [0xde, 0xad, 0xbe, 0xef, 0xfa, 0xce, 0xff];
// Read a single `Data` at offset zero in big-endian byte order.
let data: Data = bytes.pread_with(0, BE)?;
Expand All @@ -108,10 +101,6 @@ fn parse() -> Result<(), scroll::Error> {
assert_eq!(bytes, out);
Ok(())
}
fn main() {
parse().unwrap();
}
```

This feature is **not** enabled by default, you must enable the `derive` feature in Cargo.toml to use it:
Expand All @@ -129,7 +118,7 @@ Scroll can also read/write simple types from a `std::io::Read` or `std::io::Writ
use std::io::Cursor;
use scroll::IOread;

fn parse_io() -> Result<(), scroll::Error> {
fn main() -> Result<(), scroll::Error> {
let bytes_ = [0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xef,0xbe,0x00,0x00,];
let mut bytes = Cursor::new(bytes_);

Expand All @@ -139,10 +128,6 @@ fn parse_io() -> Result<(), scroll::Error> {
let bar = bytes.ioread::<u32>()?;
Ok(())
}

fn main() {
parse_io().unwrap();
}
```

Similarly, we can write to anything that implements `std::io::Write` quite naturally:
Expand All @@ -151,18 +136,14 @@ Similarly, we can write to anything that implements `std::io::Write` quite natur
use scroll::{IOwrite, LE, BE};
use std::io::{Write, Cursor};

fn write_io() -> Result<(), scroll::Error> {
fn main() -> Result<(), scroll::Error> {
let mut bytes = [0x0u8; 10];
let mut cursor = Cursor::new(&mut bytes[..]);
cursor.write_all(b"hello")?;
cursor.iowrite_with(0xdeadbeef as u32, BE)?;
assert_eq!(cursor.into_inner(), [0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0x0]);
Ok(())
}

fn main() {
write_io().unwrap();
}
```

# Advanced Uses
Expand Down Expand Up @@ -196,17 +177,13 @@ impl<'a> ctx::TryFromCtx<'a, Endian> for Data<'a> {
}
}

fn parse_data() -> Result<(), scroll::Error> {
fn main() -> Result<(), scroll::Error> {
let bytes = b"UserName\x00\x01\x02\x03\x04";
let data = bytes.pread_with::<Data>(0, BE)?;
assert_eq!(data.id, 0x01020304);
assert_eq!(data.name.to_string(), "UserName".to_string());
Ok(())
}

fn main() {
parse_data().unwrap();
}
```

Please see the official documentation, or a simple [example](examples/data_ctx.rs) for more.
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ pub mod export {
pub use ::core::mem;
}


#[allow(unused)]
macro_rules! doc_comment {
($x:expr) => {
#[doc = $x]
#[doc(hidden)]
mod readme_tests {}
};
}

#[cfg(feature = "derive")]
doc_comment!(include_str!("../README.md"));

#[cfg(test)]
mod tests {
#[allow(overflowing_literals)]
Expand Down
29 changes: 0 additions & 29 deletions tests/readme.rs

This file was deleted.

0 comments on commit e94a84d

Please sign in to comment.