Skip to content

Commit

Permalink
[#2] Refactor tests in expand to setup/teardown through LibModules
Browse files Browse the repository at this point in the history
  • Loading branch information
kodemartin committed Jul 16, 2021
1 parent c6078b6 commit 9913654
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
//!
//! ```
//! use std::ffi::NulError;
//!
//! use rustpostal::{expand, LibModules};
//! use rustpostal::error::RuntimeError;
//!
//! fn main() -> Result<(), NulError> {
//! unsafe { rustpostal::setup(LibModules::Expand) }
//! fn main() -> Result<(), RuntimeError> {
//! let postal_module = LibModules::Expand;
//! postal_module.setup()?;
//!
//! let address = "St Johns Centre, Rope Walk, Bedford, Bedfordshire, MK42 0XE, United Kingdom";
//!
//! let languages = ["en", "gb"].iter().map(|&s| s);
//! let expanded = expand::expand_address_with_options(address, Some(languages))?;
//! let languages = ["en", "gb"];
//! let expanded = expand::expand_address_with_options(address, Some(languages.iter()))?;
//!
//! for variation in &expanded {
//! println!("{}", variation);
//! }
//!
//! unsafe { rustpostal::teardown(LibModules::Expand) }
//! Ok(())
//! }
//! ```
Expand Down Expand Up @@ -247,7 +249,7 @@ impl<'a> NormalizeOptions<'a> {
pub fn new<'b, T>(languages: Option<T>) -> NormalizeOptions<'a>
where
'a: 'b,
T: Iterator<Item = &'b&'a str>,
T: Iterator<Item = &'b &'a str>,
{
let mut options = NormalizeOptions::default();
if let Some(languages) = languages {
Expand Down Expand Up @@ -345,11 +347,13 @@ impl<'a> NormalizeOptions<'a> {
/// ```
/// use std::ffi::NulError;
///
/// use rustpostal::{LibModules, setup, teardown};
/// use rustpostal::LibModules;
/// use rustpostal::expand::NormalizeOptions;
/// use rustpostal::error::RuntimeError;
///
/// fn main() -> Result<(), NulError> {
/// unsafe { setup(LibModules::Expand) };
/// fn main() -> Result<(), RuntimeError> {
/// let postal_module = LibModules::Expand;
/// postal_module.setup()?;
///
/// let mut options = NormalizeOptions::default();
/// let address = "St Johns Centre, Rope Walk, Bedford, Bedfordshire, MK42 0XE, United Kingdom";
Expand All @@ -359,7 +363,6 @@ impl<'a> NormalizeOptions<'a> {
/// assert!(variation.ends_with("kingdom"))
/// }
///
/// unsafe { teardown(LibModules::Expand) };
/// Ok(())
/// }
/// ```
Expand Down Expand Up @@ -442,7 +445,7 @@ pub fn expand_address_with_options<'a, 'b, T>(
) -> Result<NormalizedAddress, NulError>
where
'a: 'b,
T: Iterator<Item = &'b&'a str>,
T: Iterator<Item = &'b &'a str>,
{
let mut options = NormalizeOptions::new(languages);
options.expand(address)
Expand All @@ -451,7 +454,8 @@ where
#[cfg(test)]
mod test {
use super::*;
use crate::{setup, teardown, LibModules};
use crate::error::RuntimeError;
use crate::LibModules;

#[test]
fn default_libpostal_normalize_options() {
Expand Down Expand Up @@ -497,11 +501,12 @@ mod test {
}

#[test]
fn libpostal_normalize_options_expand() {
unsafe { setup(LibModules::Expand) }
fn libpostal_normalize_options_expand() -> Result<(), RuntimeError> {
let postal_module = LibModules::Expand;
postal_module.setup()?;

let address = "St Johns Centre, Rope Walk, Bedford, Bedfordshire, MK42 0XE, United Kingdom";
let c_address = CString::new(address).unwrap();
let c_address = CString::new(address)?;

let mut libpostal_options: LibpostalNormalizeOptions = Default::default();

Expand All @@ -515,8 +520,7 @@ mod test {
for variation in &expanded {
assert!(variation.ends_with("kingdom"));
}

unsafe { teardown(LibModules::Expand) }
Ok(())
}

#[test]
Expand Down

0 comments on commit 9913654

Please sign in to comment.