From e369ada35a5b021479d1eee7e14bd8f90edc0e77 Mon Sep 17 00:00:00 2001 From: Jake Goulding Date: Fri, 1 May 2020 12:52:00 -0400 Subject: [PATCH] refactor: extract a crate with our custom assertions There's probably an existing crate that we should use directly, but I haven't found an exact match yet. --- Cargo.lock | 5 +++++ Cargo.toml | 6 +++++- delorean_test_helpers/Cargo.toml | 9 +++++++++ delorean_test_helpers/src/lib.rs | 8 ++++++++ src/encoders/float.rs | 2 +- src/lib.rs | 11 ----------- src/line_parser.rs | 2 +- src/storage/block.rs | 2 +- 8 files changed, 30 insertions(+), 15 deletions(-) create mode 100644 delorean_test_helpers/Cargo.toml create mode 100644 delorean_test_helpers/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5d3a0b056e..f37a25cc09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -373,6 +373,7 @@ dependencies = [ "criterion 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "croaring 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", "csv 1.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "delorean_test_helpers 0.1.0", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -400,6 +401,10 @@ dependencies = [ "tonic-build 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "delorean_test_helpers" +version = "0.1.0" + [[package]] name = "delorean_utilities" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index e71d2b24e7..73bed27cfa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,10 @@ edition = "2018" default-run = "delorean" [workspace] -members = ["delorean_utilities"] +members = [ + "delorean_test_helpers", + "delorean_utilities", +] [profile.release] debug = true @@ -52,6 +55,7 @@ either = "1.5.3" smallvec = "1.2.0" [dev-dependencies] +delorean_test_helpers = { path = "delorean_test_helpers" } criterion = "0.3" assert_cmd = "1.0.0" tempfile = "3.1.0" diff --git a/delorean_test_helpers/Cargo.toml b/delorean_test_helpers/Cargo.toml new file mode 100644 index 0000000000..e1eb238a7d --- /dev/null +++ b/delorean_test_helpers/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "delorean_test_helpers" +version = "0.1.0" +authors = ["Paul Dix "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/delorean_test_helpers/src/lib.rs b/delorean_test_helpers/src/lib.rs new file mode 100644 index 0000000000..0cc55ef440 --- /dev/null +++ b/delorean_test_helpers/src/lib.rs @@ -0,0 +1,8 @@ +use std::f64; + +/// A test helper function for asserting floating point numbers are within the machine epsilon +/// because strict comparison of floating point numbers is incorrect +pub fn approximately_equal(f1: f64, f2: f64) -> bool { + (f1 - f2).abs() < f64::EPSILON +} + diff --git a/src/encoders/float.rs b/src/encoders/float.rs index df4e6ffadc..f74b95bf21 100644 --- a/src/encoders/float.rs +++ b/src/encoders/float.rs @@ -491,7 +491,7 @@ pub fn decode(src: &[u8], dst: &mut Vec) -> Result<(), Box> { #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] // TODO: Audit test values for truncation mod tests { - use crate::tests::approximately_equal; + use delorean_test_helpers::approximately_equal; #[test] fn encode_no_values() { diff --git a/src/lib.rs b/src/lib.rs index a59cc3303d..85a53ea9a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,14 +32,3 @@ impl error::Error for Error { None } } - -#[cfg(test)] -pub mod tests { - use std::f64; - - /// A test helper function for asserting floating point numbers are within the machine epsilon - /// because strict comparison of floating point numbers is incorrect - pub fn approximately_equal(f1: f64, f2: f64) -> bool { - (f1 - f2).abs() < f64::EPSILON - } -} diff --git a/src/line_parser.rs b/src/line_parser.rs index 82ec0d4855..0aac398959 100644 --- a/src/line_parser.rs +++ b/src/line_parser.rs @@ -791,7 +791,7 @@ fn map_fail<'a, R1, R2>( #[cfg(test)] mod test { use super::*; - use crate::tests::approximately_equal; + use delorean_test_helpers::approximately_equal; type Error = Box; type Result = std::result::Result; diff --git a/src/storage/block.rs b/src/storage/block.rs index 82f667f711..fab00ae954 100644 --- a/src/storage/block.rs +++ b/src/storage/block.rs @@ -948,7 +948,7 @@ where #[cfg(test)] mod test { use super::*; - use crate::tests::approximately_equal; + use delorean_test_helpers::approximately_equal; use std::io::Cursor; #[test]