Skip to content

primitives: Add an API test module#4078

Merged
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:03-19-primitives-api
May 9, 2025
Merged

primitives: Add an API test module#4078
apoelstra merged 2 commits intorust-bitcoin:masterfrom
tcharding:03-19-primitives-api

Conversation

@tcharding
Copy link
Copy Markdown
Member

@tcharding tcharding commented Feb 19, 2025

In preparation for 1.0-ing primitives add an api test module that makes an effort to verify the API surface.

This is similar to what is in units and what is in development for hashes (in #4017).

Note, there is a WIP attempt at this in #3992.

Close: #3928

Copy link
Copy Markdown
Member Author

@tcharding tcharding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooph, this was pending from yesterday.

// If this builds then traits are dyn compatible.
struct Traits {
// a: Box<dyn block::Validation>,
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Kixunil I went digging for your simplified suggestion and I couldn't find it. Flagging that I wasn't ignoring you my memory is just failing me right now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just define an #[allow(unused)] function that takes _: &dyn Trait.

@tcharding tcharding force-pushed the 03-19-primitives-api branch from e576550 to 8341c3f Compare March 20, 2025 03:59
@tcharding tcharding marked this pull request as ready for review March 20, 2025 03:59
@tcharding tcharding force-pushed the 03-19-primitives-api branch from 8341c3f to b6bf31c Compare March 20, 2025 05:01
// `Debug` representation is never empty (C-DEBUG-NONEMPTY).
#[test]
fn api_all_non_error_types_have_non_empty_debug() {
macro_rules! check_debug {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would not_empty be an accurate name also?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or debug_not_empty anyway can also just ignore my comment here.

transaction.compute_wtxid();
transaction.version;
Witness::arbitrary(&mut u).unwrap();
// ad: witness::Iter<'a>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the ad comment here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is a hangover from when there were more than 26 and I used ad as a variable name. Your comment made me remember that I forgot to fix all the local var names after removing a bunch of stuff yesterday (during rebase). Thanks

// We abuse `Arbitrary` here to get a quick and dirty instance.
let ab: [u8; 32] = [0xab; 32];
let mut u = arbitrary::Unstructured::new(&ab);
let transaction = Transaction::arbitrary(&mut u).unwrap();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this belong in checked_debug!? Interesting idea to abuse Arbitrary like this to create random test data, although is this just testing Arbitrary? It doesn't appear to be testing that debug is not empty.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is used below.

//!
//! 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>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's more worthwhile to call out what section(s) of the api-guidelines this is addressing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are comments in the code eg C-DEBUG

@tcharding tcharding force-pushed the 03-19-primitives-api branch from b6bf31c to d845272 Compare March 20, 2025 21:38
@tcharding
Copy link
Copy Markdown
Member Author

Thanks for the review. Rebased and fixed single letter variable names.

a: absolute::LockTime,
b: relative::LockTime,
// block::Checked, // Empty enums are not constructable.
// block::Unchecked,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In d845272:

We should uncomment these. They don't need to be constructible for the API tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now resolved.

@tcharding tcharding force-pushed the 03-19-primitives-api branch 2 times, most recently from ccf93cf to c80cff3 Compare April 11, 2025 00:07
apoelstra
apoelstra previously approved these changes Apr 14, 2025
Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK c80cff3; successfully ran local tests

@tcharding
Copy link
Copy Markdown
Member Author

I had a few review suggestion.

tcharding added 2 commits May 9, 2025 08:59
There is no obvious reason why the witness iterator cannot be cloned.
In preparation for 1.0-ing `primitives` add an `api` test module that
makes an effort to verify the API surface.

This is similar to what is in `units` and what is in development for
`hashes` (in rust-bitcoin#4017).
@tcharding tcharding force-pushed the 03-19-primitives-api branch from 4759942 to e2d9a8a Compare May 8, 2025 23:10
@tcharding
Copy link
Copy Markdown
Member Author

Rebased and feature gated the whole test on hex and arbitrary (already has alloc).

Copy link
Copy Markdown
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK e2d9a8a; successfully ran local tests

@apoelstra
Copy link
Copy Markdown
Member

Part of me thinks we should have non-hex non-arbtrary API tests, but realistically that's a lot of boilerplate code for very little value -- these feature gates typically enable entire types at once and none of them gate things like Copy or Clone or constructibility, which is what these API tests are testing.

@apoelstra apoelstra merged commit 79a6840 into rust-bitcoin:master May 9, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

primitives: Add an API test module

4 participants