1 // Copyright 2012-2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 pub(crate) struct JsonFormatter<T> {
14 out: OutputLocation<T>,
17 impl<T: Write> JsonFormatter<T> {
18 pub fn new(out: OutputLocation<T>) -> Self {
22 fn write_message(&mut self, s: &str) -> io::Result<()> {
23 assert!(!s.contains('\n'));
25 self.out.write_all(s.as_ref())?;
26 self.out.write_all(b"\n")
34 extra: Option<String>,
36 if let Some(extras) = extra {
37 self.write_message(&*format!(
38 r#"{{ "type": "{}", "name": "{}", "event": "{}", {} }}"#,
45 self.write_message(&*format!(
46 r#"{{ "type": "{}", "name": "{}", "event": "{}" }}"#,
55 impl<T: Write> OutputFormatter for JsonFormatter<T> {
56 fn write_run_start(&mut self, test_count: usize) -> io::Result<()> {
57 self.write_message(&*format!(
58 r#"{{ "type": "suite", "event": "started", "test_count": "{}" }}"#,
63 fn write_test_start(&mut self, desc: &TestDesc) -> io::Result<()> {
64 self.write_message(&*format!(
65 r#"{{ "type": "test", "event": "started", "name": "{}" }}"#,
77 TrOk => self.write_event("test", desc.name.as_slice(), "ok", None),
80 let extra_data = if stdout.len() > 0 {
83 EscapedString(String::from_utf8_lossy(stdout))
89 self.write_event("test", desc.name.as_slice(), "failed", extra_data)
92 TrFailedMsg(ref m) => {
97 Some(format!(r#""message": "{}""#, EscapedString(m))),
101 TrIgnored => self.write_event("test", desc.name.as_slice(), "ignored", None),
104 self.write_event("test", desc.name.as_slice(), "allowed_failure", None)
108 let median = bs.ns_iter_summ.median as usize;
109 let deviation = (bs.ns_iter_summ.max - bs.ns_iter_summ.min) as usize;
111 let mbps = if bs.mb_s == 0 {
114 format!(r#", "mib_per_second": {}"#, bs.mb_s)
118 "{{ \"type\": \"bench\", \
121 \"deviation\": {}{} }}",
128 self.write_message(&*line)
133 fn write_timeout(&mut self, desc: &TestDesc) -> io::Result<()> {
134 self.write_message(&*format!(
135 r#"{{ "type": "test", "event": "timeout", "name": "{}" }}"#,
140 fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool> {
142 self.write_message(&*format!(
143 "{{ \"type\": \"suite\", \
147 \"allowed_fail\": {}, \
150 \"filtered_out\": \"{}\" }}",
151 if state.failed == 0 { "ok" } else { "failed" },
153 state.failed + state.allowed_fail,
160 Ok(state.failed == 0)
164 /// A formatting utility used to print strings with characters in need of escaping.
165 /// Base code taken form `libserialize::json::escape_str`
166 struct EscapedString<S: AsRef<str>>(S);
168 impl<S: AsRef<str>> ::std::fmt::Display for EscapedString<S> {
169 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
172 for (i, byte) in self.0.as_ref().bytes().enumerate() {
173 let escaped = match byte {
176 b'\x00' => "\\u0000",
177 b'\x01' => "\\u0001",
178 b'\x02' => "\\u0002",
179 b'\x03' => "\\u0003",
180 b'\x04' => "\\u0004",
181 b'\x05' => "\\u0005",
182 b'\x06' => "\\u0006",
183 b'\x07' => "\\u0007",
187 b'\x0b' => "\\u000b",
190 b'\x0e' => "\\u000e",
191 b'\x0f' => "\\u000f",
192 b'\x10' => "\\u0010",
193 b'\x11' => "\\u0011",
194 b'\x12' => "\\u0012",
195 b'\x13' => "\\u0013",
196 b'\x14' => "\\u0014",
197 b'\x15' => "\\u0015",
198 b'\x16' => "\\u0016",
199 b'\x17' => "\\u0017",
200 b'\x18' => "\\u0018",
201 b'\x19' => "\\u0019",
202 b'\x1a' => "\\u001a",
203 b'\x1b' => "\\u001b",
204 b'\x1c' => "\\u001c",
205 b'\x1d' => "\\u001d",
206 b'\x1e' => "\\u001e",
207 b'\x1f' => "\\u001f",
208 b'\x7f' => "\\u007f",
215 f.write_str(&self.0.as_ref()[start..i])?;
218 f.write_str(escaped)?;
223 if start != self.0.as_ref().len() {
224 f.write_str(&self.0.as_ref()[start..])?;