Skip to content

Commit 4feb25d

Browse files
committed
Fix defect in emitting assertions without namespacing in and add integration test for this
1 parent f410a28 commit 4feb25d

File tree

4 files changed

+148
-18
lines changed

4 files changed

+148
-18
lines changed

lib/src/assert/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ impl AssertionInfo {
193193
}
194194

195195
fn emit(&self) {
196-
internal::dispatch_output(&self);
196+
let json_event = json!({ "antithesis_assert": &self });
197+
internal::dispatch_output(&json_event)
197198
}
198199
}
199200

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
use antithesis_sdk::{antithesis_init, assert_always};
2+
use serde_json::{json, Value};
3+
4+
mod common;
5+
use common::{AntithesisAssert, AssertType, SDKInput};
6+
7+
const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
8+
9+
/// Expected Output in /tmp/antithesis-assert-always-with-details.json
10+
/// Note: Actual version info in antithesis_sdk can vary
11+
///
12+
/// {
13+
/// "antithesis_sdk": {
14+
/// "language": {
15+
/// "name": "Rust",
16+
/// "version": "1.69.0"
17+
/// },
18+
/// "sdk_version": "0.1.2",
19+
/// "protocol_version": "1.0.0"
20+
/// }
21+
/// }
22+
/// {
23+
/// "antithesis_assert": {
24+
/// "assert_type": "always",
25+
/// "condition": false,
26+
/// "details": {},
27+
/// "display_type": "Always",
28+
/// "hit": false,
29+
/// "id": "Waterproof Red",
30+
/// "location": {
31+
/// "begin_column": 5,
32+
/// "begin_line": 23,
33+
/// "class": "assert_always_with_details",
34+
/// "file": "lib/tests/assert_always_with_details.rs",
35+
/// "function": "assert_always_with_details::assert_always_with_details"
36+
/// },
37+
/// "message": "Waterproof Red",
38+
/// "must_hit": true
39+
/// }
40+
/// }
41+
/// {
42+
/// "antithesis_assert": {
43+
/// "assert_type": "always",
44+
/// "condition": true,
45+
/// "details": {
46+
/// "color": "red",
47+
/// "labels": [
48+
/// "outdoor",
49+
/// "waterproof"
50+
/// ],
51+
/// "width": 4
52+
/// },
53+
/// "display_type": "Always",
54+
/// "hit": true,
55+
/// "id": "Waterproof Red",
56+
/// "location": {
57+
/// "begin_column": 5,
58+
/// "begin_line": 23,
59+
/// "class": "assert_always_with_details",
60+
/// "file": "lib/tests/assert_always_with_details.rs",
61+
/// "function": "assert_always_with_details::assert_always_with_details"
62+
/// },
63+
/// "message": "Waterproof Red",
64+
/// "must_hit": true
65+
/// }
66+
/// }
67+
#[test]
68+
fn assert_always_with_details() {
69+
let output_file = "/tmp/antithesis-assert-always-with-details.json";
70+
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
71+
antithesis_init();
72+
let clothing_details: Value = json!({
73+
"color": "red",
74+
"width": 4,
75+
"labels": [
76+
"outdoor",
77+
"waterproof"
78+
]
79+
});
80+
let is_waterproof = true;
81+
assert_always!(is_waterproof, "Waterproof Red", &clothing_details);
82+
83+
// verify the output has landed in the expected file
84+
match common::read_jsonl_tags(output_file) {
85+
Ok(x) => {
86+
let mut did_register = false;
87+
let mut did_hit = false;
88+
for obj in x.iter() {
89+
if let SDKInput::AntithesisAssert(AntithesisAssert {
90+
assert_type,
91+
condition,
92+
display_type,
93+
hit,
94+
must_hit,
95+
id,
96+
message,
97+
location,
98+
details,
99+
}) = obj
100+
{
101+
if *hit {
102+
did_hit = true;
103+
assert_eq!(*condition, is_waterproof);
104+
assert_eq!(details, &clothing_details);
105+
} else {
106+
did_register = true;
107+
};
108+
assert_eq!(*assert_type, AssertType::Always);
109+
assert_eq!(*display_type, "Always");
110+
assert!(*must_hit);
111+
assert_eq!(message, "Waterproof Red");
112+
assert_eq!(id, message);
113+
assert!(location.begin_line > 0);
114+
assert!(location.begin_column >= 0);
115+
assert_eq!(location.class, "assert_always_with_details");
116+
assert!(location.function.ends_with("::assert_always_with_details"));
117+
assert!(location
118+
.file
119+
.ends_with("/tests/assert_always_with_details.rs"));
120+
}
121+
println!("{:?}", obj);
122+
}
123+
assert!(did_register);
124+
assert!(did_hit);
125+
}
126+
Err(e) => println!("{}", e),
127+
}
128+
common::env::restore_var(LOCAL_OUTPUT, prev_v);
129+
}

lib/tests/common/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,24 @@ pub struct AntithesisSetup {
2727

2828
#[derive(Deserialize, Serialize, Debug)]
2929
pub struct Location {
30-
begin_column: i32,
31-
begin_line: i32,
32-
class: String,
33-
file: String,
34-
function: String,
30+
pub begin_column: i32,
31+
pub begin_line: i32,
32+
pub class: String,
33+
pub file: String,
34+
pub function: String,
3535
}
3636

3737
#[derive(Deserialize, Debug)]
3838
pub struct AntithesisAssert {
39-
assert_type: AssertType,
40-
condition: bool,
41-
display_type: String,
42-
hit: bool,
43-
must_hit: bool,
44-
id: String,
45-
message: String,
46-
location: Location,
47-
details: Value,
39+
pub assert_type: AssertType,
40+
pub condition: bool,
41+
pub display_type: String,
42+
pub hit: bool,
43+
pub must_hit: bool,
44+
pub id: String,
45+
pub message: String,
46+
pub location: Location,
47+
pub details: Value,
4848
}
4949

5050
#[derive(Deserialize, Debug)]
@@ -56,9 +56,9 @@ pub enum SDKInput {
5656
SendEvent { event_name: String, details: Value },
5757
}
5858

59-
#[derive(Deserialize, Debug)]
59+
#[derive(Deserialize, Debug, PartialEq)]
6060
#[serde(rename_all = "snake_case")]
61-
enum AssertType {
61+
pub enum AssertType {
6262
Always,
6363
Sometimes,
6464
Reachability,

lib/tests/setup_complete_without_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const LOCAL_OUTPUT: &str = "ANTITHESIS_SDK_LOCAL_OUTPUT";
1414
// ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1515
#[test]
1616
fn setup_complete_without_details() {
17-
let output_file = "/tmp/antithesis-lifecycle-withou-details.json";
17+
let output_file = "/tmp/antithesis-lifecycle-without-details.json";
1818
let prev_v = common::env::set_var(LOCAL_OUTPUT, output_file);
1919
let no_details = json!({});
2020

0 commit comments

Comments
 (0)