Skip to content

Commit

Permalink
add scope for test step
Browse files Browse the repository at this point in the history
Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 8, 2024
1 parent 5ae6c80 commit 47c790f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 74 deletions.
81 changes: 44 additions & 37 deletions src/output/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use std::io;
use std::sync::atomic::{self, Ordering};
use std::sync::Arc;

#[cfg(feature = "boxed-scopes")]
use futures::future::BoxFuture;

use crate::output as tv;
use crate::spec::{self, TestStepArtifactImpl, TestStepStart};
use tv::measure::MeasurementSeries;
Expand Down Expand Up @@ -64,43 +67,47 @@ impl TestStep {
})
}

// /// Builds a scope in the [`TestStep`] object, taking care of starting and
// /// ending it. View [`TestStep::start`] and [`TestStep::end`] methods.
// /// After the scope is constructed, additional objects may be added to it.
// /// This is the preferred usage for the [`TestStep`], since it guarantees
// /// all the messages are emitted between the start and end messages, the order
// /// is respected and no messages is lost.
// ///
// /// # Examples
// ///
// /// ```rust
// /// # tokio_test::block_on(async {
// /// # use ocptv::output::*;
// ///
// /// let run = TestRun::new("diagnostic_name", "my_dut", "1.0").start().await?;
// ///
// /// let step = run.add_step("first step")?;
// /// step.scope(|s| async {
// /// s.add_log(
// /// LogSeverity::Info,
// /// "This is a log message with INFO severity",
// /// ).await?;
// /// Ok(TestStatus::Complete)
// /// }).await?;
// ///
// /// # Ok::<(), OcptvError>(())
// /// # });
// /// ```
// pub async fn scope<'a, F, R>(&'a self, func: F) -> Result<(), emitters::WriterError>
// where
// R: Future<Output = Result<models::TestStatus, emitters::WriterError>>,
// F: std::ops::FnOnce(&'a TestStep) -> R,
// {
// self.start().await?;
// let status = func(self).await?;
// self.end(status).await?;
// Ok(())
// }
/// Builds a scope in the [`TestStep`] object, taking care of starting and
/// ending it. View [`TestStep::start`] and [`TestStep::end`] methods.
/// After the scope is constructed, additional objects may be added to it.
/// This is the preferred usage for the [`TestStep`], since it guarantees
/// all the messages are emitted between the start and end messages, the order
/// is respected and no messages is lost.
///
/// # Examples
///
/// ```rust
/// # tokio_test::block_on(async {
/// # use futures::FutureExt;
/// # use ocptv::output::*;
///
/// let run = TestRun::new("diagnostic_name", "my_dut", "1.0").start().await?;
///
/// let step = run.add_step("first step");
/// step.scope(|s| {
/// async move {
/// s.add_log(
/// LogSeverity::Info,
/// "This is a log message with INFO severity",
/// ).await?;
/// Ok(TestStatus::Complete)
/// }.boxed()
/// }).await?;
///
/// # Ok::<(), OcptvError>(())
/// # });
/// ```
#[cfg(feature = "boxed-scopes")]
pub async fn scope<F>(self, func: F) -> Result<(), tv::OcptvError>
where
F: FnOnce(&StartedTestStep) -> BoxFuture<'_, Result<tv::TestStatus, tv::OcptvError>>,
{
let step = self.start().await?;
let status = func(&step).await?;
step.end(status).await?;

Ok(())
}
}

pub struct StartedTestStep {
Expand Down
80 changes: 43 additions & 37 deletions tests/output/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ async fn test_testrun_with_error_with_details() -> Result<()> {
.await
}

// #[cfg(any(test, feature = "boxed-scopes"))]
#[cfg(feature = "boxed-scopes")]
#[tokio::test]
async fn test_testrun_with_scope() -> Result<()> {
let expected = [
Expand Down Expand Up @@ -573,43 +573,49 @@ async fn test_testrun_step_error_with_details() -> Result<()> {
.await
}

// #[tokio::test]
// async fn test_testrun_step_scope_log() -> Result<()> {
// let expected = [
// json_schema_version(),
// json_run_default_start(),
// json_step_default_start(),
// json!({
// "sequenceNumber": 3,
// "testStepArtifact": {
// "log": {
// "message": "This is a log message with INFO severity",
// "severity": "INFO"
// }
// }
// }),
// json_step_complete(4),
// json_run_pass(5),
// ];
#[cfg(feature = "boxed-scopes")]
#[tokio::test]
async fn test_testrun_step_scope_log() -> Result<()> {
let expected = [
json_schema_version(),
json_run_default_start(),
json_step_default_start(),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"log": {
"message": "This is a log message with INFO severity",
"severity": "INFO"
}
},
"sequenceNumber": 3,
"timestamp": DATETIME_FORMATTED
}),
json_step_complete(4),
json_run_pass(5),
];

// check_output_run(&expected, |run| {
// async {
// run.step("first step")
// .start()
// .scope(|s| async {
// s.add_log(
// LogSeverity::Info,
// "This is a log message with INFO severity",
// )
// .await?;
// Ok(TestStatus::Complete)
// })
// .await
// }
// .boxed()
// })
// .await
// }
check_output_run(&expected, |run| {
async {
run.add_step("first step")
.scope(|s| {
async move {
s.add_log(
LogSeverity::Info,
"This is a log message with INFO severity",
)
.await?;

Ok(TestStatus::Complete)
}
.boxed()
})
.await
}
.boxed()
})
.await
}

#[tokio::test]
async fn test_step_with_measurement() -> Result<()> {
Expand Down

0 comments on commit 47c790f

Please sign in to comment.