Skip to content

Commit

Permalink
Finish integration of property tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
KtorZ committed Aug 30, 2024
1 parent 520261f commit 2e294fa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
22 changes: 13 additions & 9 deletions src/components/code_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ use aiken/fuzz
use cardano/assets
use cardano/transaction.{Transaction}
validator main() {
validator main {
mint(redeemer: List<Int>, policy_id: ByteArray, self: Transaction) {
trace @"minting": policy_id, @"with", redeemer
let quantities = self.mint
let quantities =
self.mint
|> assets.flatten
|> list.map(fn(t) { t.3rd })
quicksort(redeemer) == quantities
(quicksort(redeemer) == quantities)?
}
else(_) {
Expand All @@ -43,12 +44,14 @@ fn quicksort(xs: List<Int>) -> List<Int> {
[] ->
[]
[p, ..tail] -> {
let before = tail
|> list.filter(fn(x) { x < p })
|> quicksort
let after = tail
|> list.filter(fn(x) { x >= p })
|> quicksort
let before =
tail
|> list.filter(fn(x) { x < p })
|> quicksort
let after =
tail
|> list.filter(fn(x) { x >= p })
|> quicksort
list.concat(before, [p, ..after])
}
}
Expand All @@ -67,6 +70,7 @@ test quicksort_2() {
}
test quicksort_prop(xs via fuzz.list(fuzz.int())) {
fuzz.label_when(list.is_empty(xs), @"empty", @"non-empty")
quicksort(xs) == quicksort(quicksort(xs))
}"#;

Expand Down
20 changes: 20 additions & 0 deletions src/components/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ pub fn Output(
view! { cx, <div></div> }
}
}}
{ move || {
if !test_result.labels.is_empty() {
view! { cx,
<div class="flex flex-col items-left bg-gray-80 pr-2 pb-2 pl-3 space-y-2">
<div><span class="text-blue-40 font-semibold text-xs">"COVERAGE"</span></div>
{test_result.labels.iter().map(|(p, l)| {
view! { cx, <div
class="text-xs text-gray-70 font-mono flex flex-row gap-x-2"
class:success=test_result.success>
<span class="percentage font-semibold">{p}"%"</span>
<span class="label">{l}</span>
</div>
}
}).collect_view(cx)}
</div>
}
} else {
view! { cx, <div></div> }
}
}}
</li>
}
}
Expand Down
47 changes: 40 additions & 7 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use aiken_lang::{
TypedDataType, TypedFunction, TypedModule, TypedTest, TypedValidator, UntypedModule,
},
builtins,
format::Formatter,
gen_uplc::CodeGenerator,
line_numbers::LineNumbers,
parser,
Expand Down Expand Up @@ -43,6 +44,7 @@ pub struct TestResult {
pub name: String,
pub success: bool,
pub logs: Vec<String>,
pub labels: Vec<(String, String)>,
pub meta: TestResultMeta,
}

Expand Down Expand Up @@ -153,12 +155,9 @@ impl Project {
let program = generator.generate(validator, NAME);
let program: Program<DeBruijn> = program.try_into().unwrap();
let program = program.to_hex().unwrap();

for handler in validator.handlers.iter() {
let name =
TypedValidator::handler_name(&validator.name, &handler.name);
set_validators.update(|v| v.push((index, name, program.clone())))
}
set_validators.update(|v| {
v.push((index, validator.name.clone(), program.clone()))
});
}
}

Expand Down Expand Up @@ -280,16 +279,50 @@ impl Project {
name: unit_test.test.name,
success,
logs,
labels: Vec::new(),
meta: TestResultMeta::ExBudget(unit_test.spent_budget),
}
}
test_framework::TestResult::PropertyTestResult(prop_test) => {
let logs = Vec::new();
let prop_test = prop_test.reify(&data_types);

let mut logs = Vec::new();
if let Ok(Some(counterexample)) = prop_test.counterexample {
logs.push(format!(
"counterexample\n{}",
Formatter::new()
.expr(&counterexample, false)
.to_pretty_string(80)
));
}

let mut labels = Vec::new();
if success {
let mut total = 0;
let mut pad = 0;
for (k, v) in prop_test.labels.iter() {
total += v;
if k.len() > pad {
pad = k.len();
}
}

let mut xs = prop_test.labels.iter().collect::<Vec<_>>();
xs.sort_by(|a, b| b.1.cmp(a.1));

for (k, v) in xs {
labels.push((
format!("{}", 100.0 * (*v as f64) / (total as f64)),
k.clone(),
));
}
}

TestResult {
name: prop_test.test.name,
success,
meta: TestResultMeta::Iterations(prop_test.iterations),
labels,
logs,
}
}
Expand Down

0 comments on commit 2e294fa

Please sign in to comment.