Skip to content

Commit d2030ad

Browse files
Add tests.
1 parent 3c95abd commit d2030ad

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

lib/tests/assert_guidance.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use antithesis_sdk::{antithesis_init, assert_always_greater_than, LOCAL_OUTPUT};
2+
use serde_json::json;
3+
4+
mod common;
5+
use common::SDKInput;
6+
7+
use crate::common::{AntithesisGuidance, GuidanceType};
8+
9+
#[test]
10+
fn assert_guidance() {
11+
let output_file = "/tmp/antithesis-assert-guidance.json";
12+
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
13+
antithesis_init();
14+
15+
for i in 0..10 {
16+
let x = if i % 2 == 0 { i } else { -i };
17+
assert_always_greater_than!(x, 0, "Positive x", &json!({"x": x}));
18+
}
19+
20+
match common::read_jsonl_tags(output_file) {
21+
Ok(x) => {
22+
let mut did_register = false;
23+
let mut did_hit = false;
24+
for obj in x.iter() {
25+
if let SDKInput::AntithesisGuidance(AntithesisGuidance {
26+
guidance_type,
27+
hit,
28+
id,
29+
message,
30+
location,
31+
..
32+
}) = obj
33+
{
34+
if *hit {
35+
did_hit = true;
36+
} else {
37+
did_register = true;
38+
};
39+
assert_eq!(*guidance_type, GuidanceType::Numeric);
40+
assert_eq!(message, "Positive x");
41+
assert_eq!(id, message);
42+
assert!(location.begin_line > 0);
43+
assert!(location.begin_column >= 0);
44+
assert_eq!(location.class, "assert_guidance");
45+
assert!(location.function.ends_with("::assert_guidance"));
46+
assert!(location
47+
.file
48+
.ends_with("/tests/assert_guidance.rs"));
49+
}
50+
println!("{:?}", obj);
51+
}
52+
assert!(did_register);
53+
assert!(did_hit);
54+
}
55+
Err(e) => println!("{}", e),
56+
}
57+
common::env::restore_var(LOCAL_OUTPUT, prev_v);
58+
}

lib/tests/common/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,23 @@ pub struct AntithesisAssert {
4747
pub details: Value,
4848
}
4949

50+
#[derive(Deserialize, Debug)]
51+
pub struct AntithesisGuidance {
52+
pub guidance_type: GuidanceType,
53+
pub message: String,
54+
pub id: String,
55+
pub location: Location,
56+
pub maximize: bool,
57+
pub guidance_data: Value,
58+
pub hit: bool,
59+
}
60+
5061
#[derive(Deserialize, Debug)]
5162
#[serde(rename_all = "snake_case")]
5263
pub enum SDKInput {
5364
AntithesisSdk(AntithesisSdk),
5465
AntithesisAssert(AntithesisAssert),
66+
AntithesisGuidance(AntithesisGuidance),
5567
AntithesisSetup(AntithesisSetup),
5668
SendEvent { event_name: String, details: Value },
5769
}
@@ -64,6 +76,14 @@ pub enum AssertType {
6476
Reachability,
6577
}
6678

79+
#[derive(Deserialize, Debug, PartialEq)]
80+
#[serde(rename_all = "snake_case")]
81+
pub enum GuidanceType {
82+
Numeric,
83+
Boolean,
84+
JSON,
85+
}
86+
6787
fn parse_lines(lines: Vec<&str>) -> Result<Vec<SDKInput>, Box<dyn std::error::Error>> {
6888
let mut result = Vec::new();
6989

simple/src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ fn assert_demo() {
7979
// ant_unreachable
8080
let details = json!({"impossible!": {"name": "trouble", "weights": [100,200,300]}});
8181
assert_unreachable!("Impossible to get here", &details);
82+
83+
assert_always_greater_than!(3, 100, "not right", &json!({}));
84+
85+
assert_sometimes_all!({a: true, b: false}, "not all right", &json!({}));
8286
}
8387

8488
pub fn main() {

0 commit comments

Comments
 (0)