| 1 | use colored_json::*; |
| 2 | use serde_json::json; |
| 3 | use std::error::Error; |
| 4 | use std::io::Write; |
| 5 | use std::result::Result; |
| 6 | |
| 7 | #[test] |
| 8 | fn test_display_json_value() -> Result<(), Box<dyn Error>> { |
| 9 | let data = json!({ |
| 10 | "name": "John Doe", |
| 11 | "age": 43, |
| 12 | "phones": [ |
| 13 | "+44 1234567", |
| 14 | "+44 2345678" |
| 15 | ] |
| 16 | }); |
| 17 | |
| 18 | let s = to_colored_json_auto(&data)?; |
| 19 | println!("\n{}", s); |
| 20 | |
| 21 | return Ok(()); |
| 22 | } |
| 23 | |
| 24 | #[test] |
| 25 | fn test_trait() -> Result<(), Box<dyn Error>> { |
| 26 | println!( |
| 27 | "\n{}", |
| 28 | r#"{ |
| 29 | "array": [ |
| 30 | "ele1", |
| 31 | "ele2" |
| 32 | ], |
| 33 | "float": 3.1415926, |
| 34 | "integer": 4398798674962568, |
| 35 | "string": "string" |
| 36 | } |
| 37 | "# |
| 38 | .to_colored_json_auto()? |
| 39 | ); |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn test_trait_err() -> Result<(), Box<dyn Error>> { |
| 45 | eprintln!( |
| 46 | "\n{}", |
| 47 | r#"{ |
| 48 | "array": [ |
| 49 | "ele1", |
| 50 | "ele2" |
| 51 | ], |
| 52 | "float": 3.1415926, |
| 53 | "integer": 4398798674962568, |
| 54 | "string": "string" |
| 55 | } |
| 56 | "# |
| 57 | .to_colored_json(ColorMode::Auto(Output::StdErr))? |
| 58 | ); |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn test_trait_color_off() -> Result<(), Box<dyn Error>> { |
| 64 | println!( |
| 65 | "\n{}", |
| 66 | r#"{ |
| 67 | "array": [ |
| 68 | "ele1", |
| 69 | "ele2" |
| 70 | ], |
| 71 | "float": 3.1415926, |
| 72 | "integer": 4398798674962568, |
| 73 | "string": "string" |
| 74 | } |
| 75 | "# |
| 76 | .to_colored_json(ColorMode::Off)? |
| 77 | ); |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn test_trait_styler() -> Result<(), Box<dyn Error>> { |
| 83 | println!( |
| 84 | "\n{}", |
| 85 | r#"{ |
| 86 | "array": [ |
| 87 | "ele1", |
| 88 | "ele2" |
| 89 | ], |
| 90 | "float": 3.1415926, |
| 91 | "integer": 4398798674962568, |
| 92 | "string": "string" |
| 93 | } |
| 94 | "# |
| 95 | .to_colored_json_with_styler( |
| 96 | ColorMode::default().eval(), |
| 97 | Styler { |
| 98 | key: Color::Green.foreground(), |
| 99 | string_value: Color::Blue.bold(), |
| 100 | integer_value: Color::Magenta.bold(), |
| 101 | float_value: Color::Magenta.italic(), |
| 102 | object_brackets: Color::Yellow.bold(), |
| 103 | array_brackets: Color::Cyan.bold(), |
| 104 | ..Default::default() |
| 105 | }, |
| 106 | )? |
| 107 | ); |
| 108 | Ok(()) |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_trait_styler_color_off() -> Result<(), Box<dyn Error>> { |
| 113 | println!( |
| 114 | "\n{}", |
| 115 | r#"{ |
| 116 | "array": [ |
| 117 | "ele1", |
| 118 | "ele2" |
| 119 | ], |
| 120 | "float": 3.1415926, |
| 121 | "integer": 4398798674962568, |
| 122 | "string": "string" |
| 123 | } |
| 124 | "# |
| 125 | .to_colored_json_with_styler( |
| 126 | ColorMode::Off, |
| 127 | Styler { |
| 128 | key: Color::Green.foreground(), |
| 129 | string_value: Color::Blue.bold(), |
| 130 | integer_value: Color::Magenta.bold(), |
| 131 | float_value: Color::Magenta.italic(), |
| 132 | object_brackets: Color::Yellow.bold(), |
| 133 | array_brackets: Color::Cyan.bold(), |
| 134 | ..Default::default() |
| 135 | }, |
| 136 | )? |
| 137 | ); |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn test_writer() -> Result<(), Box<dyn Error>> { |
| 143 | let data = json!({ |
| 144 | "name": "John Doe", |
| 145 | "age": 43, |
| 146 | "phones": [ |
| 147 | "+44 1234567", |
| 148 | "+44 2345678" |
| 149 | ] |
| 150 | }); |
| 151 | |
| 152 | let mut writer: Vec<u8> = Vec::with_capacity(128); |
| 153 | writer.write_all(b"\n")?; |
| 154 | write_colored_json(&data, &mut writer)?; |
| 155 | writer.write_all(b"\n")?; |
| 156 | let s = unsafe { String::from_utf8_unchecked(writer) }; |
| 157 | println!("{}", s); |
| 158 | return Ok(()); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn test_styler() -> Result<(), Box<dyn Error>> { |
| 163 | let f = ColoredFormatter::with_styler( |
| 164 | PrettyFormatter::new(), |
| 165 | Styler { |
| 166 | key: Color::Green.foreground(), |
| 167 | string_value: Color::Blue.bold(), |
| 168 | integer_value: Color::Magenta.bold(), |
| 169 | float_value: Color::Magenta.italic(), |
| 170 | object_brackets: Color::Yellow.bold(), |
| 171 | array_brackets: Color::Cyan.bold(), |
| 172 | ..Default::default() |
| 173 | }, |
| 174 | ); |
| 175 | |
| 176 | println!( |
| 177 | "\n{}", |
| 178 | f.clone().to_colored_json_auto(&json!({ |
| 179 | "string": "string", |
| 180 | "integer": 4398798674962568u64, |
| 181 | "float": 3.1415926, |
| 182 | "array": [ |
| 183 | "ele1", |
| 184 | "ele2" |
| 185 | ] |
| 186 | }))? |
| 187 | ); |
| 188 | |
| 189 | println!( |
| 190 | "{}", |
| 191 | f.to_colored_json_auto(&json!({ |
| 192 | "name":"John", "age":31, "city":"New York" |
| 193 | }))? |
| 194 | ); |
| 195 | |
| 196 | return Ok(()); |
| 197 | } |
| 198 | |
| 199 | #[test] |
| 200 | fn test_styler_no_color() -> Result<(), Box<dyn Error>> { |
| 201 | let f = ColoredFormatter::with_styler( |
| 202 | PrettyFormatter::new(), |
| 203 | Styler { |
| 204 | key: Color::Green.foreground(), |
| 205 | string_value: Color::Blue.bold(), |
| 206 | integer_value: Color::Magenta.bold(), |
| 207 | float_value: Color::Magenta.italic(), |
| 208 | object_brackets: Color::Yellow.bold(), |
| 209 | array_brackets: Color::Cyan.bold(), |
| 210 | ..Default::default() |
| 211 | }, |
| 212 | ); |
| 213 | |
| 214 | println!( |
| 215 | "\n{}", |
| 216 | f.clone().to_colored_json( |
| 217 | &json!({ |
| 218 | "string": "string", |
| 219 | "integer": 4398798674962568u64, |
| 220 | "float": 3.1415926, |
| 221 | "array": [ |
| 222 | "ele1", |
| 223 | "ele2" |
| 224 | ] |
| 225 | }), |
| 226 | ColorMode::Off, |
| 227 | )? |
| 228 | ); |
| 229 | |
| 230 | println!( |
| 231 | "{}", |
| 232 | f.to_colored_json_auto(&json!({ |
| 233 | "name":"John", "age":31, "city":"New York" |
| 234 | }))? |
| 235 | ); |
| 236 | |
| 237 | return Ok(()); |
| 238 | } |
| 239 | |
| 240 | #[test] |
| 241 | fn test_styler_compact() -> Result<(), Box<dyn Error>> { |
| 242 | let f = ColoredFormatter::with_styler( |
| 243 | CompactFormatter {}, |
| 244 | Styler { |
| 245 | key: Color::Green.foreground(), |
| 246 | string_value: Color::Blue.bold(), |
| 247 | integer_value: Color::Blue.bold(), |
| 248 | ..Default::default() |
| 249 | }, |
| 250 | ); |
| 251 | |
| 252 | println!( |
| 253 | "\n{}", |
| 254 | f.clone().to_colored_json_auto(&json!({ |
| 255 | "name": "John Doe", |
| 256 | "age": 43, |
| 257 | "phones": [ |
| 258 | "+44 1234567", |
| 259 | "+44 2345678" |
| 260 | ] |
| 261 | }))? |
| 262 | ); |
| 263 | |
| 264 | println!( |
| 265 | "{}", |
| 266 | f.to_colored_json_auto(&json!({ |
| 267 | "name":"John", "age":31, "city":"New York" |
| 268 | }))? |
| 269 | ); |
| 270 | |
| 271 | return Ok(()); |
| 272 | } |
| 273 | |
| 274 | #[test] |
| 275 | fn test_serializes() -> Result<(), Box<dyn Error>> { |
| 276 | #[derive(serde::Serialize)] |
| 277 | struct Data<'a> { |
| 278 | name: &'a str, |
| 279 | age: usize, |
| 280 | phones: &'a [&'a str], |
| 281 | } |
| 282 | |
| 283 | let data = Data { |
| 284 | name: "John Doe", |
| 285 | age: 43, |
| 286 | phones: &["+44 1234567", "+44 2345678"], |
| 287 | }; |
| 288 | |
| 289 | let s = to_colored_json_auto(&data)?; |
| 290 | println!("\n{}", s); |
| 291 | |
| 292 | return Ok(()); |
| 293 | } |