forked from rust-bitcoin/rust-bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge rust-bitcoin#3834: Introduce an API surface test file
a18d287 api: Run just check-api (Tobin C. Harding) 1175fac io: Introduce api test file (Tobin C. Harding) 01f3eef io: Move bridge type constructors (Tobin C. Harding) a8fbd5e units: Remove api docs (Tobin C. Harding) aaa20fc units: Move code comment to correct imports (Tobin C. Harding) Pull request description: As we did for `units` add an integration test for the API surface of the `io` crate. The first two patches are cleanup to the `units` test. Patch 3 is trivial refactor. ACKs for top commit: apoelstra: ACK a18d287; successfully ran local tests Tree-SHA512: e5a39f97f7d6e34cb63613f337101f13fcc5d66f85277b6f0026d339b5677ff60f7e3050ae23807c3d20d0ae4e343a10983f43dab757a66fe4d33afd09a96f62
- Loading branch information
Showing
7 changed files
with
180 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//! Test the API surface of `io`. | ||
//! | ||
//! The point of these tests are to check the API surface as opposed to test the API functionality. | ||
//! | ||
//! ref: <https://rust-lang.github.io/api-guidelines/about.html> | ||
#![allow(dead_code)] | ||
#![allow(unused_imports)] | ||
|
||
use core::cell::Cell; | ||
use core::convert::Infallible; | ||
|
||
// These imports test "typical" usage by user code. | ||
use bitcoin_io::{self as io, BufRead, Cursor, ErrorKind, Read, Sink, Take, Write}; | ||
#[cfg(feature = "std")] | ||
use bitcoin_io::{FromStd, ToStd}; | ||
|
||
/// An arbitrary error kind. | ||
const ERROR_KIND: ErrorKind = ErrorKind::TimedOut; | ||
|
||
/// A struct that includes all public non-error enums. | ||
#[derive(Debug)] // All public types implement Debug (C-DEBUG). | ||
struct Enums { | ||
a: ErrorKind, | ||
} | ||
|
||
impl Enums { | ||
/// Creates an arbitrary `Enums` instance. | ||
fn new() -> Self { Self { a: ERROR_KIND } } | ||
} | ||
|
||
/// A struct that includes all public non-error structs except `Take`. | ||
#[derive(Debug)] // All public types implement Debug (C-DEBUG). | ||
struct Structs { | ||
#[cfg(feature = "std")] | ||
a: FromStd<u32>, | ||
#[cfg(feature = "std")] | ||
b: ToStd<Dummy>, | ||
c: Cursor<Dummy>, | ||
d: Sink, | ||
} | ||
|
||
impl Structs { | ||
fn new() -> Self { | ||
Self { | ||
#[cfg(feature = "std")] | ||
a: FromStd::new(0), | ||
#[cfg(feature = "std")] | ||
b: ToStd::new(DUMMY), | ||
c: Cursor::new(DUMMY), | ||
d: Sink, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] // `Take` implements Debug (C-DEBUG). | ||
struct Taker<'a> { | ||
a: Take<'a, Dummy>, | ||
} | ||
|
||
/// An arbitrary `Dummy` instance. | ||
static DUMMY: Dummy = Dummy(0); | ||
|
||
/// Dummy struct to implement all the traits we provide. | ||
#[derive(Debug, Copy, Clone)] | ||
struct Dummy(u64); | ||
|
||
impl Read for Dummy { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { | ||
if buf.is_empty() { | ||
Ok(0) | ||
} else { | ||
buf[0] = (self.0 & 0xFF) as u8; | ||
Ok(1) | ||
} | ||
} | ||
} | ||
|
||
impl BufRead for Dummy { | ||
fn fill_buf(&mut self) -> Result<&[u8], io::Error> { Ok(&[]) } | ||
fn consume(&mut self, _: usize) {} | ||
} | ||
|
||
impl Write for Dummy { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> { Ok(buf.len()) } | ||
fn write_all(&mut self, _: &[u8]) -> Result<(), io::Error> { Ok(()) } | ||
fn flush(&mut self) -> Result<(), io::Error> { Ok(()) } | ||
} | ||
|
||
impl AsRef<[u8]> for Dummy { | ||
fn as_ref(&self) -> &[u8] { &[] } | ||
} | ||
|
||
/// A struct that includes all public non-error types. | ||
#[derive(Debug)] // All public types implement Debug (C-DEBUG). | ||
struct Types { | ||
a: Enums, | ||
b: Structs, | ||
} | ||
|
||
impl Types { | ||
fn new() -> Self { Self { a: Enums::new(), b: Structs::new() } } | ||
} | ||
|
||
/// A struct that includes all public error types. | ||
#[derive(Debug)] // `io::Error` only implements `Debug`. | ||
struct Errors { | ||
a: io::Error, | ||
} | ||
|
||
// `Debug` representation is never empty (C-DEBUG-NONEMPTY). | ||
#[test] | ||
fn api_all_non_error_types_have_non_empty_debug() { | ||
let t = Types::new(); | ||
|
||
let debug = format!("{:?}", t.a.a); | ||
assert!(!debug.is_empty()); | ||
|
||
#[cfg(feature = "std")] | ||
{ | ||
let debug = format!("{:?}", t.b.a); | ||
assert!(!debug.is_empty()); | ||
let debug = format!("{:?}", t.b.b); | ||
assert!(!debug.is_empty()); | ||
} | ||
let debug = format!("{:?}", t.b.c); | ||
assert!(!debug.is_empty()); | ||
let debug = format!("{:?}", t.b.d); | ||
assert!(!debug.is_empty()); | ||
} | ||
|
||
// Types are `Send` and `Sync` where possible (C-SEND-SYNC). | ||
#[test] | ||
fn all_non_error_tyes_implement_send_sync() { | ||
fn assert_send<T: Send>() {} | ||
assert_send::<Types>(); | ||
|
||
fn assert_sync<T: Sync>() {} | ||
assert_sync::<Types>(); | ||
} |
Oops, something went wrong.