Skip to content

Commit

Permalink
Fix issues with Time, Positin, Location newtypes (#212)
Browse files Browse the repository at this point in the history
* Add Time, Position, Location to prelude.

* Closes #209

* Fix Display for f64-based newtypes

* Add tests of Time/Position/Location Display

* Closes #208
  • Loading branch information
molpopgen authored Dec 15, 2021
1 parent 99930e8 commit 5d6a289
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ macro_rules! impl_f64_newtypes {
($type: ty) => {
impl std::fmt::Display for $type {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}({})", stringify!($idtype), self.0)
write!(f, "{}({})", stringify!($type), self.0)
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,33 @@ pub fn c_api_version() -> String {
#[cfg(test)]
mod tests {
use super::c_api_version;
use super::Location;
use super::Position;
use super::Time;

#[test]
fn test_c_api_version() {
let _ = c_api_version();
}

#[test]
fn test_f64_newtype_Display() {
let x = Position::from(1.0);
let mut output = String::new();
std::fmt::write(&mut output, format_args!("{}", x))
.expect("Error occurred while trying to write in String");
assert_eq!(output, "Position(1)".to_string());
let x = Time::from(1.0);
let mut output = String::new();
std::fmt::write(&mut output, format_args!("{}", x))
.expect("Error occurred while trying to write in String");
assert_eq!(output, "Time(1)".to_string());
let x = Location::from(1.0);
let mut output = String::new();
std::fmt::write(&mut output, format_args!("{}", x))
.expect("Error occurred while trying to write in String");
assert_eq!(output, "Location(1)".to_string());
}
}

// Testing modules
Expand Down
5 changes: 3 additions & 2 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use crate::TSK_NODE_IS_SAMPLE;
pub use streaming_iterator::DoubleEndedStreamingIterator;
pub use streaming_iterator::StreamingIterator;
pub use {
crate::EdgeId, crate::IndividualId, crate::MigrationId, crate::MutationId, crate::NodeId,
crate::PopulationId, crate::SiteId, crate::SizeType,
crate::EdgeId, crate::IndividualId, crate::Location, crate::MigrationId, crate::MutationId,
crate::NodeId, crate::PopulationId, crate::Position, crate::SiteId, crate::SizeType,
crate::Time,
};

0 comments on commit 5d6a289

Please sign in to comment.