Document test output JSON schema through '--help'

The help manual is getting a bit large, but fortunately, we can resort
  to a simpler/more compact version using `-h`.
This commit is contained in:
KtorZ
2024-11-13 14:59:20 +01:00
parent da982510dc
commit 51a8ddcc0b
5 changed files with 142 additions and 21 deletions

View File

@@ -45,16 +45,27 @@ impl EventListener for Json {
}
fn fmt_test_json(result: &TestResult<UntypedExpr, UntypedExpr>) -> serde_json::Value {
let on_test_failure = match result {
TestResult::UnitTestResult(UnitTestResult { ref test, .. }) => &test.on_test_failure,
TestResult::PropertyTestResult(PropertyTestResult { ref test, .. }) => {
&test.on_test_failure
}
};
let mut test = json!({
"title": result.title(),
"status": if result.is_success() { "pass" } else { "fail" },
"on_failure": match on_test_failure {
OnTestFailure::FailImmediately => "fail_immediately" ,
OnTestFailure::SucceedEventually => "succeed_eventually" ,
OnTestFailure::SucceedImmediately => "succeed_immediately",
}
});
match result {
TestResult::UnitTestResult(UnitTestResult {
spent_budget,
assertion,
test: unit_test,
..
}) => {
test["execution_units"] = json!({
@@ -63,14 +74,8 @@ fn fmt_test_json(result: &TestResult<UntypedExpr, UntypedExpr>) -> serde_json::V
});
if !result.is_success() {
if let Some(assertion) = assertion {
test["assertion"] = json!({
"message": assertion.to_string(false, &AssertionStyleOptions::new(None)),
"on_failure": match unit_test.on_test_failure {
OnTestFailure::FailImmediately => "fail_immediately" ,
OnTestFailure::SucceedEventually => "succeed_eventually" ,
OnTestFailure::SucceedImmediately => "succeed_immediately",
}
});
test["assertion"] =
json!(assertion.to_string(false, &AssertionStyleOptions::new(None)));
}
}
}
@@ -81,7 +86,9 @@ fn fmt_test_json(result: &TestResult<UntypedExpr, UntypedExpr>) -> serde_json::V
..
}) => {
test["iterations"] = json!(iterations);
test["labels"] = json!(labels);
if !labels.is_empty() {
test["labels"] = json!(labels);
}
test["counterexample"] = match counterexample {
Ok(Some(expr)) => json!(Formatter::new().expr(expr, false).to_pretty_string(60)),
Ok(None) => json!(null),

View File

@@ -30,6 +30,7 @@ clap = { version = "4.1.8", features = [
"string",
] }
clap_complete = "4.3.2"
color-print = "0.3.7"
hex = "0.4.3"
ignore = "0.4.20"
indoc = "2.0"

View File

@@ -12,7 +12,101 @@ use std::{
};
#[derive(clap::Args)]
/// Type-check an Aiken project
#[command(
verbatim_doc_comment,
about = color_print::cstr!(r#"
Type-check an Aiken project and run any tests found.
Test results are printed as stylized outputs when `stdout` is a TTY-capable terminal. If it
isn't, (e.g. because you are redirecting the output to a file), test results are printed as
a JSON structured object. Use `--help` to see the whole schema.
"#),
after_long_help = color_print::cstr!(r#"<bold><underline>Output JSON schema:</underline></bold>
<bold>type</bold>: object
<bold>properties</bold>:
<bold>seed</bold>: <cyan>&type_integer</cyan>
<bold>type</bold>: integer
<bold>summary</bold>:
<bold>type</bold>: object
<bold>properties</bold>: <cyan>&type_summary</cyan>
<bold>total</bold>: *type_integer
<bold>passed</bold>: *type_integer
<bold>failed</bold>: *type_integer
<bold>kind</bold>:
<bold>type</bold>: object
<bold>properties</bold>:
<bold>unit</bold>: *type_integer
<bold>property</bold>: *type_integer
<bold>modules</bold>:
<bold>type</bold>: array
<bold>items</bold>:
<bold>type</bold>: object
<bold>properties</bold>:
<bold>name</bold>: <cyan>&type_string</cyan>
<bold>type</bold>: string
<bold>summary</bold>: *type_summary
<bold>test</bold>:
<bold>type</bold>: array
<bold>items</bold>:
<bold>oneOf</bold>:
- <bold>type</bold>: object
<bold>required</bold>:
- kind
- title
- status
- on_failure
- execution_units
<bold>properties</bold>:
<bold>kind</bold>
<bold>type</bold>: string
<bold>enum</bold>: [ "unit" ]
<bold>title</bold>: *type_string
<bold>status</bold>: <cyan>&type_status</cyan>
<bold>type</bold>: string
<bold>enum</bold>: [ "pass", "fail" ]
<bold>on_failure</bold>: <cyan>&type_on_failure</cyan>
<bold>type</bold>: string
<bold>enum</bold>:
- fail_immediately
- succeed_immediately
- succeed_eventually
<bold>execution_units</bold>:
<bold>type</bold>: object
<bold>properties</bold>:
<bold>mem</bold>: *type_integer
<bold>cpu</bold>: *type_integer
<bold>assertion</bold>: *type_string
- <bold>type</bold>: object
<bold>required</bold>:
- kind
- title
- status
- on_failure
- iterations
- counterexample
<bold>properties</bold>:
<bold>kind</bold>
<bold>type</bold>: string
<bold>enum</bold>: [ "property" ]
<bold>title</bold>: *type_string
<bold>status</bold>: *type_status
<bold>on_failure</bold>: *type_on_failure
<bold>iterations</bold>: *type_integer
<bold>labels</bold>:
<bold>type</bold>: object
<bold>additionalProperties</bold>: *type_integer
<bold>counterexample</bold>:
<bold>oneOf</bold>:
- *type_string
- <bold>type</bold>: "null"
- <bold>type</bold>: object
<bold>properties</bold>:
<bold>error</bold>: *type_string
<bold><underline>Note:</underline></bold>
You are seeing the extended help. Use `-h` instead of `--help` for a more compact view.
"#
))]
pub struct Args {
/// Path to project
directory: Option<PathBuf>,
@@ -44,7 +138,7 @@ pub struct Args {
/// Only run tests if they match any of these strings.
/// You can match a module with `-m aiken/list` or `-m list`.
/// You can match a test with `-m "aiken/list.{map}"` or `-m "aiken/option.{flatten_1}"`
#[clap(short, long)]
#[clap(short, long, verbatim_doc_comment)]
match_tests: Option<Vec<String>>,
/// This is meant to be used with `--match-tests`.
@@ -76,14 +170,9 @@ pub struct Args {
/// Choose the verbosity level of traces:
///
/// - silent:
/// disable traces altogether
///
/// - compact:
/// only culprit line numbers are shown on failures
///
/// - verbose:
/// enable full verbose traces as provided by the user or the compiler
/// - silent: disable traces altogether
/// - compact: only culprit line numbers are shown on failures
/// - verbose: enable full verbose traces as provided by the user or the compiler
///
/// [optional]
#[clap(short, long, value_parser=trace_level_parser(), default_value_t=TraceLevel::Verbose, verbatim_doc_comment)]