Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 516862b

Browse files
committed
update tests
1 parent 85f7f47 commit 516862b

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

src/decoder.rs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,33 @@ use Error;
1010
/// Parses ITM packets.
1111
pub struct Decoder<R: Read> {
1212
inner: R,
13-
follow: bool
13+
follow: bool,
1414
}
1515

1616
// Copy&Paste from std::io::Read::read_exact
17-
fn read_exact_gently<R: Read>(reader: &mut R, mut buf: &mut [u8], keep_reading: bool) -> ::std::io::Result<()> {
18-
use std::io::{ErrorKind, Error};
17+
fn read_exact_gently<R: Read>(
18+
reader: &mut R,
19+
mut buf: &mut [u8],
20+
keep_reading: bool,
21+
) -> ::std::io::Result<()> {
22+
use std::io::{Error, ErrorKind};
1923
while !buf.is_empty() {
2024
match reader.read(buf) {
2125
Ok(0) if !keep_reading => break,
2226
Ok(0) if keep_reading => continue,
23-
Ok(n) => { let tmp = buf; buf = &mut tmp[n..]; }
27+
Ok(n) => {
28+
let tmp = buf;
29+
buf = &mut tmp[n..];
30+
}
2431
Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
2532
Err(e) => return Err(e),
2633
}
2734
}
2835
if !buf.is_empty() {
29-
Err(Error::new(ErrorKind::UnexpectedEof,
30-
"failed to fill whole buffer"))
36+
Err(Error::new(
37+
ErrorKind::UnexpectedEof,
38+
"failed to fill whole buffer",
39+
))
3140
} else {
3241
Ok(())
3342
}
@@ -37,7 +46,10 @@ impl<R: Read> Decoder<R> {
3746
/// Construct a new `Decoder` that reads encoded packets from
3847
/// `inner`.
3948
pub fn new(inner: R, follow: bool) -> Decoder<R> {
40-
Decoder::<R> { inner: inner, follow: follow}
49+
Decoder::<R> {
50+
inner: inner,
51+
follow: follow,
52+
}
4153
}
4254

4355
// TODO: If we need config for the Decoder, my plan is to:
@@ -105,8 +117,11 @@ pub fn from_slice(s: &[u8]) -> Result<Packet, failure::Error> {
105117
#[cfg(test)]
106118
mod tests {
107119
use super::from_slice;
108-
use error::{Error, ErrorKind, Result};
120+
121+
use failure;
122+
109123
use packet::{Kind, Packet};
124+
use Error;
110125

111126
#[test]
112127
fn header() {
@@ -168,36 +183,45 @@ mod tests {
168183

169184
#[test]
170185
fn unknown_header() {
171-
let p = try_decode_one(&[0x00]);
172-
match p {
173-
Err(Error(ErrorKind::UnknownHeader(0x00), _)) => (),
174-
_ => panic!(),
186+
if let Err(e) = try_decode_one(&[0x00]) {
187+
match e.downcast_ref::<Error>() {
188+
Some(Error::UnknownHeader { byte: 0 }) => return (),
189+
_ => {}
190+
}
175191
}
192+
193+
panic!()
176194
}
177195

178196
#[test]
179197
fn eof_before_payload() {
180-
let p = try_decode_one(&[0x01 /* Missing payload bytes */]);
181-
match p {
182-
Err(Error(ErrorKind::EofDuringPacket, _)) => (),
183-
_ => panic!(),
198+
if let Err(e) = try_decode_one(&[0x01 /* Missing payload bytes */]) {
199+
match e.downcast_ref::<Error>() {
200+
Some(Error::EofDuringPacket) => return (),
201+
_ => {}
202+
}
184203
}
204+
205+
panic!()
185206
}
186207

187208
#[test]
188209
fn eof_before_packet() {
189-
let p = try_decode_one(&[/* Missing packet bytes */]);
190-
match p {
191-
Err(Error(ErrorKind::EofBeforePacket, _)) => (),
192-
_ => panic!(),
210+
if let Err(e) = try_decode_one(&[/* Missing packet bytes */]) {
211+
match e.downcast_ref::<Error>() {
212+
Some(Error::EofBeforePacket) => return (),
213+
_ => {}
214+
}
193215
}
216+
217+
panic!()
194218
}
195219

196220
fn decode_one(data: &[u8]) -> Packet {
197221
try_decode_one(data).unwrap()
198222
}
199223

200-
fn try_decode_one(data: &[u8]) -> Result<Packet> {
224+
fn try_decode_one(data: &[u8]) -> Result<Packet, failure::Error> {
201225
let p = from_slice(data);
202226
println!("{:#?}", p);
203227
p

0 commit comments

Comments
 (0)