Skip to content

Commit

Permalink
Refactor tests and improve verification
Browse files Browse the repository at this point in the history
Improve verification somewhat, but mostly refactor tests
to use test tree, and be nicer to read and write.

type: changed
  • Loading branch information
casey committed Apr 8, 2020
1 parent 2fb5bdb commit d71bdff
Show file tree
Hide file tree
Showing 13 changed files with 1,060 additions and 519 deletions.
96 changes: 48 additions & 48 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,53 @@
use crate::common::*;

impl Div<Bytes> for Bytes {
type Output = u64;

fn div(self, rhs: Bytes) -> u64 {
self.0 / rhs.0
}
}

impl Div<u64> for Bytes {
type Output = Bytes;

fn div(self, rhs: u64) -> Bytes {
Bytes::from(self.0 / rhs)
}
}

impl Mul<u64> for Bytes {
type Output = Bytes;

fn mul(self, rhs: u64) -> Self {
Bytes::from(self.0 * rhs)
}
}

impl DivAssign<u64> for Bytes {
fn div_assign(&mut self, rhs: u64) {
self.0 /= rhs;
}
}

impl MulAssign<u64> for Bytes {
fn mul_assign(&mut self, rhs: u64) {
self.0 *= rhs;
}
}

impl AddAssign<Bytes> for Bytes {
fn add_assign(&mut self, rhs: Bytes) {
self.0 += rhs.0;
}
}

impl SubAssign<u64> for Bytes {
fn sub_assign(&mut self, rhs: u64) {
self.0 -= rhs;
}
}

const KI: u64 = 1 << 10;
const MI: u64 = KI << 10;
const GI: u64 = MI << 10;
Expand Down Expand Up @@ -52,54 +100,6 @@ impl<I: Into<u64>> From<I> for Bytes {
}
}

impl Div<Bytes> for Bytes {
type Output = u64;

fn div(self, rhs: Bytes) -> u64 {
self.0 / rhs.0
}
}

impl Div<u64> for Bytes {
type Output = Bytes;

fn div(self, rhs: u64) -> Bytes {
Bytes::from(self.0 / rhs)
}
}

impl DivAssign<u64> for Bytes {
fn div_assign(&mut self, rhs: u64) {
self.0 /= rhs;
}
}

impl Mul<u64> for Bytes {
type Output = Bytes;

fn mul(self, rhs: u64) -> Self {
Bytes::from(self.0 * rhs)
}
}

impl MulAssign<u64> for Bytes {
fn mul_assign(&mut self, rhs: u64) {
self.0 *= rhs;
}
}

impl AddAssign<Bytes> for Bytes {
fn add_assign(&mut self, rhs: Bytes) {
self.0 += rhs.0;
}
}

impl SubAssign<u64> for Bytes {
fn sub_assign(&mut self, rhs: u64) {
self.0 -= rhs;
}
}

impl FromStr for Bytes {
type Err = Error;

Expand Down
3 changes: 0 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ mod test {
pub(crate) use tempfile::TempDir;
pub(crate) use temptree::temptree;

// test modules
pub(crate) use crate::testing;

// test structs and enums
pub(crate) use crate::{capture::Capture, test_env::TestEnv, test_env_builder::TestEnvBuilder};
}
Expand Down
18 changes: 9 additions & 9 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::*;

pub(crate) struct Env {
args: Vec<String>,
args: Vec<OsString>,
dir: Box<dyn AsRef<Path>>,
pub(crate) err: Box<dyn Write>,
pub(crate) out: Box<dyn Write>,
Expand Down Expand Up @@ -76,7 +76,7 @@ impl Env {
D: AsRef<Path> + 'static,
O: Write + 'static,
E: Write + 'static,
S: Into<String>,
S: Into<OsString>,
I: IntoIterator<Item = S>,
{
Self {
Expand Down Expand Up @@ -155,8 +155,8 @@ mod tests {

#[test]
fn error_message_on_stdout() {
let mut env = testing::env(
[
let mut env = test_env! {
args: [
"torrent",
"create",
"--input",
Expand All @@ -165,11 +165,11 @@ mod tests {
"udp:bar.com",
"--announce-tier",
"foo",
]
.iter()
.cloned(),
);
fs::write(env.resolve("foo"), "").unwrap();
],
tree: {
foo: "",
}
};
env.status().ok();
let err = env.err();
if !err.starts_with("error: Failed to parse announce URL:") {
Expand Down
33 changes: 0 additions & 33 deletions src/file_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,36 +63,6 @@ impl FileStatus {
Ok(())
}

pub(crate) fn icon(&self) -> char {
if self.error.is_some() {
return '!';
}

if !self.present {
return '?';
}

if !self.file {
return '¿';
}

if !self.md5() {
return 'x';
}

let length = self.length_actual.unwrap();

if length > self.length_expected {
return '+';
}

if length < self.length_expected {
return '-';
}

'♡'
}

fn md5(&self) -> bool {
match (self.md5_actual, self.md5_expected) {
(Some(actual), Some(expected)) => actual == expected,
Expand All @@ -108,7 +78,4 @@ impl FileStatus {
pub(crate) fn bad(&self) -> bool {
!self.good()
}
pub(crate) fn path(&self) -> &Path {
&self.path
}
}
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ mod err;
mod outln;

#[cfg(test)]
mod testing;

#[cfg(test)]
#[macro_use]
mod test_env;

#[cfg(test)]
Expand Down
21 changes: 11 additions & 10 deletions src/metainfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,27 @@ impl Metainfo {
Self::deserialize(path, &bytes)
}

#[cfg(test)]
pub(crate) fn dump(&self, path: impl AsRef<Path>) -> Result<(), Error> {
let path = path.as_ref();
let bencode = bendy::serde::ser::to_bytes(&self).context(error::MetainfoSerialize)?;
fs::write(path, &bencode).context(error::Filesystem { path })?;
Ok(())
}

pub(crate) fn deserialize(path: impl AsRef<Path>, bytes: &[u8]) -> Result<Metainfo, Error> {
let path = path.as_ref();
bendy::serde::de::from_bytes(&bytes).context(error::MetainfoLoad { path })
let metainfo = bendy::serde::de::from_bytes(&bytes).context(error::MetainfoLoad { path })?;
Ok(metainfo)
}

pub(crate) fn serialize(&self) -> Result<Vec<u8>, Error> {
bendy::serde::ser::to_bytes(&self).context(error::MetainfoSerialize)
}

#[cfg(test)]
pub(crate) fn dump(&self, path: impl AsRef<Path>) -> Result<(), Error> {
let path = path.as_ref();
let bencode = bendy::serde::ser::to_bytes(&self).context(error::MetainfoSerialize)?;
fs::write(path, &bencode).context(error::Filesystem { path })?;
Ok(())
}

#[cfg(test)]
pub(crate) fn from_bytes(bytes: &[u8]) -> Metainfo {
bendy::serde::de::from_bytes(bytes).unwrap()
Self::deserialize("<TEST>", bytes).unwrap()
}

pub(crate) fn files<'a>(
Expand Down
Loading

0 comments on commit d71bdff

Please sign in to comment.