Skip to content

Commit

Permalink
Arbitrary testutils (#1134)
Browse files Browse the repository at this point in the history
### What

Move the arbitrary module under the testutils module.

FIxes #1131

### Why

All other testutils features are under the testutils module.

### Known limitations

This does not leave a deprecated module of reexports in the old
location, so fuzz users will experience breakage. The soroban-examples
repo and the fuzzing tutorial will need to be updated.

There is other possible restructuring of the arbitrary module that could
also be done, but is not, e.g. the contents of `arbitrary` could be
reexported directly under `testutils`.

This leaves the private `arbitrary_extra` module in its current
location.

`soroban-env-common` has a corresponding `arbitrary` module, but it is
private. Now these two crates will have their corresponding modules
located in different places.

---

I also updated the lockfile for tests/fuzz/fuzz, as Cargo seems to want
it updated.

---------

Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
  • Loading branch information
brson and leighmcculloch authored Nov 7, 2023
1 parent 5d55da6 commit d31628c
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 259 deletions.
16 changes: 8 additions & 8 deletions soroban-sdk-macros/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ fn derive_arbitrary_struct_common(
match &field.ident {
Some(ident) => {
quote! {
#ident: <#field_type as #path::arbitrary::SorobanArbitrary>::Prototype
#ident: <#field_type as #path::testutils::arbitrary::SorobanArbitrary>::Prototype
}
}
None => {
quote! {
<#field_type as #path::arbitrary::SorobanArbitrary>::Prototype
<#field_type as #path::testutils::arbitrary::SorobanArbitrary>::Prototype
}
}
}
Expand Down Expand Up @@ -132,13 +132,13 @@ pub fn derive_arbitrary_enum(
Some(ident) => {
field_types = Some(FieldType::Named);
quote! {
#ident: <#field_type as #path::arbitrary::SorobanArbitrary>::Prototype
#ident: <#field_type as #path::testutils::arbitrary::SorobanArbitrary>::Prototype
}
}
None => {
field_types = Some(FieldType::Unnamed);
quote! {
<#field_type as #path::arbitrary::SorobanArbitrary>::Prototype
<#field_type as #path::testutils::arbitrary::SorobanArbitrary>::Prototype
}
}
}
Expand Down Expand Up @@ -323,14 +323,14 @@ fn quote_arbitrary(
#[cfg(any(test, feature = "testutils"))]
const _: () = {
// derive(Arbitrary) expects these two to be in scope
use #path::arbitrary::std;
use #path::arbitrary::arbitrary;
use #path::testutils::arbitrary::std;
use #path::testutils::arbitrary::arbitrary;

#[derive(#path::arbitrary::arbitrary::Arbitrary)]
#[derive(#path::testutils::arbitrary::arbitrary::Arbitrary)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#vis #arbitrary_type_decl

impl #path::arbitrary::SorobanArbitrary for #ident {
impl #path::testutils::arbitrary::SorobanArbitrary for #ident {
type Prototype = #arbitrary_type_ident;
}

Expand Down
1 change: 0 additions & 1 deletion soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,6 @@ pub mod xdr;

pub mod testutils;

pub mod arbitrary;
mod arbitrary_extra;

mod tests;
2 changes: 1 addition & 1 deletion soroban-sdk/src/tests/proptest_val_cmp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Check that Val and ScVal can be converted between each other,
//! and that their comparison functions are equivalent.
use crate::arbitrary::SorobanArbitrary;
use crate::testutils::arbitrary::SorobanArbitrary;
use crate::xdr::ScVal;
use crate::Env;
use crate::Val;
Expand Down
2 changes: 2 additions & 0 deletions soroban-sdk/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

//! Utilities intended for use when testing.
pub mod arbitrary;

mod sign;
pub use sign::ed25519;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
//! [`SorobanArbitrary::Prototype`]:
//!
//! ```
//! # use soroban_sdk::arbitrary::Arbitrary;
//! # use soroban_sdk::testutils::arbitrary::Arbitrary;
//! # use soroban_sdk::{TryFromVal, IntoVal, Val, Env};
//! pub trait SorobanArbitrary:
//! TryFromVal<Env, Self::Prototype> + IntoVal<Env, Val> + TryFromVal<Env, Val>
Expand Down Expand Up @@ -119,7 +119,7 @@
//! # (|$data:ident: $dty: ty| $body:block) => { };
//! # }
//! use soroban_sdk::{Address, Env, Vec};
//! use soroban_sdk::arbitrary::SorobanArbitrary;
//! use soroban_sdk::testutils::arbitrary::SorobanArbitrary;
//!
//! fuzz_target!(|input: <Vec<Address> as SorobanArbitrary>::Prototype| {
//! let env = Env::default();
Expand All @@ -137,7 +137,7 @@
//! # }
//! use soroban_sdk::{Address, Env, Vec};
//! use soroban_sdk::contracttype;
//! use soroban_sdk::arbitrary::{Arbitrary, SorobanArbitrary};
//! use soroban_sdk::testutils::arbitrary::{Arbitrary, SorobanArbitrary};
//! use std::vec::Vec as RustVec;
//!
//! #[derive(Arbitrary, Debug)]
Expand Down Expand Up @@ -167,9 +167,6 @@
//! });
//! ```
#![cfg(any(test, feature = "testutils"))]
#![cfg_attr(feature = "docs", doc(cfg(feature = "testutils")))]

/// A reexport of the `arbitrary` crate.
///
/// Used by the `contracttype` macro to derive `Arbitrary`.
Expand Down Expand Up @@ -210,7 +207,7 @@ mod api {
/// # (|$data:ident: $dty: ty| $body:block) => { };
/// # }
/// # use soroban_sdk::{Address, Env, Vec, Bytes};
/// # use soroban_sdk::arbitrary::SorobanArbitrary;
/// # use soroban_sdk::testutils::arbitrary::SorobanArbitrary;
/// fuzz_target!(|input: <Bytes as SorobanArbitrary>::Prototype| {
/// // ...
/// });
Expand All @@ -232,7 +229,7 @@ mod api {
}
}

/// Implementations of `soroban_sdk::arbitrary::api` for Rust scalar types.
/// Implementations of `soroban_sdk::testutils::arbitrary::api` for Rust scalar types.
///
/// These types
///
Expand All @@ -245,7 +242,7 @@ mod api {
///
/// - `u32`
mod scalars {
use crate::arbitrary::api::*;
use super::api::*;

impl SorobanArbitrary for () {
type Prototype = ();
Expand Down Expand Up @@ -280,7 +277,7 @@ mod scalars {
}
}

/// Implementations of `soroban_sdk::arbitrary::api` for Soroban types that do not
/// Implementations of `soroban_sdk::testutils::arbitrary::api` for Soroban types that do not
/// need access to the Soroban host environment.
///
/// These types
Expand All @@ -294,15 +291,15 @@ mod scalars {
///
/// - `Error`
mod simple {
use crate::arbitrary::api::*;
use super::api::*;
pub use crate::Error;

impl SorobanArbitrary for Error {
type Prototype = Error;
}
}

/// Implementations of `soroban_sdk::arbitrary::api` for Soroban types that do
/// Implementations of `soroban_sdk::testutils::arbitrary::api` for Soroban types that do
/// need access to the Soroban host environment.
///
/// These types
Expand All @@ -316,8 +313,8 @@ mod simple {
mod objects {
use arbitrary::{Arbitrary, Result as ArbitraryResult, Unstructured};

use crate::arbitrary::api::*;
use crate::arbitrary::composite::ArbitraryVal;
use super::api::*;
use super::composite::ArbitraryVal;
use crate::env::FromVal;
use crate::ConversionError;
use crate::{Env, IntoVal, TryFromVal};
Expand Down Expand Up @@ -658,11 +655,11 @@ mod objects {
}
}

/// Implementations of `soroban_sdk::arbitrary::api` for tuples of Soroban types.
/// Implementations of `soroban_sdk::testutils::arbitrary::api` for tuples of Soroban types.
///
/// The implementation is similar to objects, but macroized.
mod tuples {
use crate::arbitrary::api::*;
use super::api::*;
use crate::ConversionError;
use crate::{Env, IntoVal, TryFromVal, TryIntoVal, Val};
use arbitrary::Arbitrary;
Expand Down Expand Up @@ -735,11 +732,11 @@ mod tuples {
);
}

/// Implementations of `soroban_sdk::arbitrary::api` for `Val`.
/// Implementations of `soroban_sdk::testutils::arbitrary::api` for `Val`.
mod composite {
use arbitrary::Arbitrary;

use crate::arbitrary::api::*;
use super::api::*;
use crate::ConversionError;
use crate::{Env, IntoVal, TryFromVal};

Expand Down Expand Up @@ -1086,7 +1083,7 @@ mod fuzz_test_helpers {
/// # fn deposit(&self, a: soroban_sdk::Address, n: i128) { }
/// # }
/// use soroban_sdk::{Address, Env};
/// use soroban_sdk::arbitrary::{Arbitrary, SorobanArbitrary};
/// use soroban_sdk::testutils::arbitrary::{Arbitrary, SorobanArbitrary};
///
/// #[derive(Arbitrary, Debug)]
/// struct FuzzDeposit {
Expand Down Expand Up @@ -1115,7 +1112,7 @@ mod fuzz_test_helpers {

#[cfg(test)]
mod tests {
use crate::arbitrary::*;
use super::*;
use crate::{
Address, Bytes, BytesN, Duration, Error, Map, String, Symbol, Timepoint, Val, Vec, I256,
U256,
Expand Down Expand Up @@ -1564,8 +1561,8 @@ mod tests {
}

mod user_defined_types {
use super::run_test;
use crate as soroban_sdk;
use crate::arbitrary::tests::run_test;
use crate::{
Address, Bytes, BytesN, Duration, Error, Map, Symbol, Timepoint, Vec, I256, U256,
};
Expand Down
Loading

0 comments on commit d31628c

Please sign in to comment.