Skip to content

Commit

Permalink
feat(testkit): provide proptest 's arbitraies to generate samples
Browse files Browse the repository at this point in the history
Signed-off-by: David Bernard <david.bernard.31@gmail.com>
  • Loading branch information
davidB committed Jan 28, 2024
1 parent c7da42c commit 6beae16
Show file tree
Hide file tree
Showing 48 changed files with 979 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build_%:
cargo build --package $*

check:
cargo check
cargo hack check --each-feature
check_no_std:
cargo --version
cargo check --target thumbv7em-none-eabihf -p ockam --no-default-features --features 'no_std alloc software_vault'
Expand Down Expand Up @@ -57,7 +57,7 @@ generate:
cargo run -p generator -- --templates-dir "generator/templates" --jsonschema-dir "cdevents-spec/schemas" --dest "cdevents-sdk/src/generated"

test:
cargo nextest run
cargo nextest run --all-features
cargo test --doc

.PHONY:
Expand Down
6 changes: 6 additions & 0 deletions cdevents-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ description = "A Rust SDK for CDEvents"
[dependencies]
cloudevents-sdk = { version = "0.7", optional = true, default-features = false }
fluent-uri = "0.1"
proptest = { version = "1.4", optional = true }
proptest-derive = { version = "0.4", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0"
time = { version = "0.3", features = ["serde-human-readable"] }

[dev-dependencies]
assert-json-diff = "2.0"
proptest = "1"
rstest = "0.18"

[features]
default = ["cloudevents"]
# provide cloudevents helpers and extensions
cloudevents = ["dep:cloudevents-sdk"]
# provide test helpers (proptest'strategies and arbitraries for struct generation)
testkit = ["dep:proptest", "dep:proptest-derive"]
37 changes: 37 additions & 0 deletions cdevents-sdk/src/cdevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,40 @@ impl<'de> Deserialize<'de> for CDEvent {
deserializer.deserialize_struct("CDEvent", FIELDS, CDEventVisitor)
}
}

#[cfg(feature = "testkit")]
impl<> proptest::arbitrary::Arbitrary for CDEvent {
type Parameters = ();
type Strategy = proptest::strategy::BoxedStrategy<Self>;

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
(
any::<Subject>(),
"\\PC*",
any::<Option<UriReference>>(),
).prop_map(|(subject, id, source)| {
let mut cdevent = CDEvent::from(subject).with_id(id);
if let Some(source) = source {
cdevent = cdevent.with_source(source);
}
cdevent
}).boxed()
}
}

#[cfg(test)]
mod tests {
use crate::CDEvent;
use proptest::prelude::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<CDEvent>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<CDEvent>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
19 changes: 19 additions & 0 deletions cdevents-sdk/src/generated/artifact_packaged.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "change",)]
pub change: ContentChange,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct ContentChange {
#[serde(rename = "id",)]
pub id: String,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
18 changes: 18 additions & 0 deletions cdevents-sdk/src/generated/artifact_published.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
18 changes: 18 additions & 0 deletions cdevents-sdk/src/generated/artifact_signed.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "signature",)]
pub signature: String,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
19 changes: 19 additions & 0 deletions cdevents-sdk/src/generated/branch_created.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)]
pub repository: Option<ContentRepository>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
19 changes: 19 additions & 0 deletions cdevents-sdk/src/generated/branch_deleted.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)]
pub repository: Option<ContentRepository>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
18 changes: 18 additions & 0 deletions cdevents-sdk/src/generated/build_finished.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)]
pub artifact_id: Option<String>,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
18 changes: 18 additions & 0 deletions cdevents-sdk/src/generated/build_queued.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
18 changes: 18 additions & 0 deletions cdevents-sdk/src/generated/build_started.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
19 changes: 19 additions & 0 deletions cdevents-sdk/src/generated/change_abandoned.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
// @generated
// by cdevents/sdk-rust/generator (subject.hbs)

#[cfg(feature = "testkit")] use proptest_derive::Arbitrary;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)]
pub repository: Option<ContentRepository>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "testkit", derive(Arbitrary))]
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}

#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;

proptest! {
#[test]
#[cfg(feature = "testkit")]
fn arbitraries_are_json_valid(s in any::<Content>()) {
let json_str = serde_json::to_string(&s).unwrap();
let actual = serde_json::from_str::<Content>(&json_str).unwrap();
assert_eq!(s, actual);
}
}
}
Loading

0 comments on commit 6beae16

Please sign in to comment.