|
| 1 | +#[cfg(test)] |
| 2 | +mod tests { |
| 3 | + use error_stack_macros2::Error; |
| 4 | + |
| 5 | + #[test] |
| 6 | + fn unit_variant_works() { |
| 7 | + #[derive(Debug, Error)] |
| 8 | + enum EnumType { |
| 9 | + #[display("unit variant")] |
| 10 | + UnitVariant, |
| 11 | + } |
| 12 | + |
| 13 | + assert_eq!(EnumType::UnitVariant.to_string(), "unit variant"); |
| 14 | + } |
| 15 | + |
| 16 | + #[test] |
| 17 | + fn named_field_variant_works_without_interpolation() { |
| 18 | + #[derive(Debug, Error)] |
| 19 | + enum EnumType { |
| 20 | + #[display("named field variant")] |
| 21 | + NamedFieldVariant { |
| 22 | + _length: usize, |
| 23 | + _is_ascii: bool, |
| 24 | + _inner: String, |
| 25 | + }, |
| 26 | + } |
| 27 | + |
| 28 | + let test_val = EnumType::NamedFieldVariant { |
| 29 | + _length: 5, |
| 30 | + _is_ascii: true, |
| 31 | + _inner: String::from("hello"), |
| 32 | + }; |
| 33 | + assert_eq!(test_val.to_string(), "named field variant"); |
| 34 | + } |
| 35 | + |
| 36 | + #[test] |
| 37 | + fn named_field_variant_works_with_interpolation_of_some_fields() { |
| 38 | + #[derive(Debug, Error)] |
| 39 | + enum EnumType { |
| 40 | + #[display("named field variant: {inner:?} has {length} characters")] |
| 41 | + NamedFieldVariant { |
| 42 | + length: usize, |
| 43 | + _is_ascii: bool, |
| 44 | + inner: String, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + let test_val = EnumType::NamedFieldVariant { |
| 49 | + length: 5, |
| 50 | + _is_ascii: true, |
| 51 | + inner: String::from("hello"), |
| 52 | + }; |
| 53 | + assert_eq!( |
| 54 | + test_val.to_string(), |
| 55 | + "named field variant: \"hello\" has 5 characters" |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn named_field_variant_works_with_interpolation_of_all_fields() { |
| 61 | + #[derive(Debug, Error)] |
| 62 | + enum EnumType { |
| 63 | + #[display( |
| 64 | + "named field variant: {inner:?} has {length} characters and is ascii={is_ascii}" |
| 65 | + )] |
| 66 | + NamedFieldVariant { |
| 67 | + length: usize, |
| 68 | + is_ascii: bool, |
| 69 | + inner: String, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + let test_val = EnumType::NamedFieldVariant { |
| 74 | + length: 5, |
| 75 | + is_ascii: true, |
| 76 | + inner: String::from("hello"), |
| 77 | + }; |
| 78 | + assert_eq!( |
| 79 | + test_val.to_string(), |
| 80 | + "named field variant: \"hello\" has 5 characters and is ascii=true" |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn tuple_variant_works_without_interpolation() { |
| 86 | + #[derive(Debug, Error)] |
| 87 | + enum EnumType { |
| 88 | + #[display("tuple variant")] |
| 89 | + TupleVariant(isize, isize, isize), |
| 90 | + } |
| 91 | + |
| 92 | + let test_val = EnumType::TupleVariant(5, 10, 15); |
| 93 | + assert_eq!(test_val.to_string(), "tuple variant"); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn tuple_variant_works_with_interpolation_of_some_fields() { |
| 98 | + #[derive(Debug, Error)] |
| 99 | + enum EnumType { |
| 100 | + #[display("tuple variant: point with y value {1}")] |
| 101 | + TupleVariant(isize, isize, isize), |
| 102 | + } |
| 103 | + |
| 104 | + let test_val = EnumType::TupleVariant(5, 10, 15); |
| 105 | + assert_eq!( |
| 106 | + test_val.to_string(), |
| 107 | + "tuple variant: point with y value 10" |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + #[allow(dead_code)] |
| 113 | + fn tuple_variant_works_with_interpolation_of_all_fields() { |
| 114 | + #[derive(Debug, Error)] |
| 115 | + enum EnumType { |
| 116 | + #[display( |
| 117 | + "tuple variant: point {2} units in front of the origin, and with x and y coords ({0}, {1})" |
| 118 | + )] |
| 119 | + TupleVariant(isize, isize, isize), |
| 120 | + } |
| 121 | + |
| 122 | + let test_val = EnumType::TupleVariant(5, 10, 15); |
| 123 | + assert_eq!( |
| 124 | + test_val.to_string(), |
| 125 | + "tuple variant: point 15 units in front of the origin, and with x and y coords (5, 10)" |
| 126 | + ); |
| 127 | + } |
| 128 | +} |
0 commit comments