|
| 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 | +} |
0 commit comments