Skip to content

Commit

Permalink
REMOVE ME: Show that failing schemars test passes in the crate that d…
Browse files Browse the repository at this point in the history
…efines the macro
  • Loading branch information
tcharding committed Apr 23, 2024
1 parent bc70f8a commit 45c093d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions hashes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ serde = { version = "1.0", default-features = false, optional = true }
[dev-dependencies]
serde_test = "1.0"
serde_json = "1.0"
jsonschema-valid = "0.4.0"
33 changes: 33 additions & 0 deletions hashes/src/sha256t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,37 @@ mod tests {
"cee837ec9192cfdb0e5762e9dd1881e9b4147fce76aea79f3b34cbc765935da6",
);
}

#[test]
#[cfg(feature = "schemars")]
fn newtype_implements_schemars() {
const TEST_MIDSTATE: [u8; 32] = [
156, 224, 228, 230, 124, 17, 108, 57, 56, 179, 202, 242, 195, 15, 80, 137, 211, 243,
147, 108, 71, 99, 110, 96, 125, 179, 62, 234, 221, 198, 240, 201,
];

sha256t_hash_newtype! {
/// A test hash.
#[hash_newtype(backward)]
struct TestHash(_);

struct TestHashEngine(_) = raw(TEST_MIDSTATE, 64);
}

static HASH_BYTES: [u8; 32] = [
0xef, 0x53, 0x7f, 0x25, 0xc8, 0x95, 0xbf, 0xa7, 0x82, 0x52, 0x65, 0x29, 0xa9, 0xb6,
0x3d, 0x97, 0xaa, 0x63, 0x15, 0x64, 0xd5, 0xd7, 0x89, 0xc2, 0xb7, 0x65, 0x44, 0x8c,
0x86, 0x35, 0xfb, 0x6c,
];

let hash = TestHash::from_slice(&HASH_BYTES).expect("right number of bytes");
let js = serde_json::from_str(&serde_json::to_string(&hash).unwrap()).unwrap();
let s = schemars::schema_for!(TestHash);
let schema = serde_json::from_str(&serde_json::to_string(&s).unwrap()).unwrap();
assert!(jsonschema_valid::Config::from_schema(&schema, None)
.unwrap()
.validate(&js)
.is_ok());

}
}

0 comments on commit 45c093d

Please sign in to comment.