Skip to content

Commit

Permalink
Merge #182
Browse files Browse the repository at this point in the history
182: move test Uuid creation functions into new module r=Dylan-DPC a=kinggoesgaming

**I'm submitting a ...**
  - [ ] bug fix
  - [ ] feature enhancement
  - [ ] deprecation or removal
  - [x] refactor

# Description
Refactor the `tests::new` and `tests::new2` functions into `test_util` module.

# Motivation
Part of the great refactor. Allows different tests in different modules access the test constructors for `Uuid`

# Tests
The tests are passing

# Related Issue(s)
#124
  • Loading branch information
bors[bot] committed Mar 25, 2018
2 parents b7ce899 + e53b922 commit 80bf568
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
4 changes: 2 additions & 2 deletions benches/parse_str.rs
Expand Up @@ -4,10 +4,10 @@
extern crate slog;
extern crate test;
extern crate uuid;
use test::Bencher;
use uuid::Uuid;
#[cfg(feature = "slog")]
use slog::Drain;
use test::Bencher;
use uuid::Uuid;

#[bench]
fn bench_parse(b: &mut Bencher) {
Expand Down
107 changes: 52 additions & 55 deletions src/lib.rs
Expand Up @@ -180,6 +180,12 @@ cfg_if! {
}
}

cfg_if! {
if #[cfg(test)] {
mod test_util;
}
}

/// A 128-bit (16 byte) buffer containing the ID.
pub type UuidBytes = [u8; 16];

Expand Down Expand Up @@ -1104,32 +1110,29 @@ impl<'a> fmt::Display for Hyphenated<'a> {
}

macro_rules! hyphenated_write {
($f:expr, $format:expr, $bytes:expr) => {{

let data1 = u32::from($bytes[0]) << 24 |
u32::from($bytes[1]) << 16 |
u32::from($bytes[2]) << 8 |
u32::from($bytes[3]);

let data2 = u16::from($bytes[4]) << 8 |
u16::from($bytes[5]);

let data3 = u16::from($bytes[6]) << 8 |
u16::from($bytes[7]);

write!($f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15])
($f: expr, $format: expr, $bytes: expr) => {{
let data1 = u32::from($bytes[0]) << 24 | u32::from($bytes[1]) << 16
| u32::from($bytes[2]) << 8 | u32::from($bytes[3]);

let data2 = u16::from($bytes[4]) << 8 | u16::from($bytes[5]);

let data3 = u16::from($bytes[6]) << 8 | u16::from($bytes[7]);

write!(
$f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15]
)
}};
}

Expand Down Expand Up @@ -1173,20 +1176,14 @@ mod tests {

use self::std::prelude::v1::*;

use super::test_util;

use super::{NAMESPACE_X500, NAMESPACE_DNS, NAMESPACE_OID, NAMESPACE_URL};
use super::{Uuid, UuidVariant, UuidVersion};

#[cfg(feature = "slog")]
use slog::{self, Drain};

fn new() -> Uuid {
Uuid::parse_str("F9168C5E-CEB2-4FAA-B6BF-329BF39FA1E4").unwrap()
}

fn new2() -> Uuid {
Uuid::parse_str("F9168C5E-CEB2-4FAB-B6BF-329BF39FA1E4").unwrap()
}

#[cfg(feature = "v3")]
static FIXTURE_V3: &'static [(&'static Uuid, &'static str, &'static str)] = &[
(
Expand Down Expand Up @@ -1334,7 +1331,7 @@ mod tests {
#[test]
fn test_nil() {
let nil = Uuid::nil();
let not_nil = new();
let not_nil = test_util::new();

assert!(nil.is_nil());
assert!(!not_nil.is_nil());
Expand Down Expand Up @@ -1464,7 +1461,7 @@ mod tests {

#[test]
fn test_get_variant() {
let uuid1 = new();
let uuid1 = test_util::new();
let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
Expand Down Expand Up @@ -1567,7 +1564,7 @@ mod tests {
);

// Round-trip
let uuid_orig = new();
let uuid_orig = test_util::new();
let orig_str = uuid_orig.to_string();
let uuid_out = Uuid::parse_str(&orig_str).unwrap();
assert_eq!(uuid_orig, uuid_out);
Expand All @@ -1593,7 +1590,7 @@ mod tests {

#[test]
fn test_to_simple_string() {
let uuid1 = new();
let uuid1 = test_util::new();
let s = uuid1.simple().to_string();

assert_eq!(s.len(), 32);
Expand All @@ -1602,7 +1599,7 @@ mod tests {

#[test]
fn test_to_string() {
let uuid1 = new();
let uuid1 = test_util::new();
let s = uuid1.to_string();

assert_eq!(s.len(), 36);
Expand All @@ -1611,15 +1608,15 @@ mod tests {

#[test]
fn test_display() {
let uuid1 = new();
let uuid1 = test_util::new();
let s = uuid1.to_string();

assert_eq!(s, uuid1.hyphenated().to_string());
}

#[test]
fn test_to_hyphenated_string() {
let uuid1 = new();
let uuid1 = test_util::new();
let s = uuid1.hyphenated().to_string();

assert!(s.len() == 36);
Expand All @@ -1631,10 +1628,10 @@ mod tests {
use super::fmt::Write;

let mut buf = String::new();
let u = new();
let u = test_util::new();

macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
($buf: ident, $format: expr, $target: expr, $len: expr, $cond: expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!(buf.len() == $len);
Expand Down Expand Up @@ -1687,7 +1684,7 @@ mod tests {

#[test]
fn test_to_urn_string() {
let uuid1 = new();
let uuid1 = test_util::new();
let ss = uuid1.urn().to_string();
let s = &ss[9..];

Expand All @@ -1698,7 +1695,7 @@ mod tests {

#[test]
fn test_to_simple_string_matching() {
let uuid1 = new();
let uuid1 = test_util::new();

let hs = uuid1.hyphenated().to_string();
let ss = uuid1.simple().to_string();
Expand All @@ -1710,7 +1707,7 @@ mod tests {

#[test]
fn test_string_roundtrip() {
let uuid = new();
let uuid = test_util::new();

let hs = uuid.hyphenated().to_string();
let uuid_hs = Uuid::parse_str(&hs).unwrap();
Expand All @@ -1723,8 +1720,8 @@ mod tests {

#[test]
fn test_compare() {
let uuid1 = new();
let uuid2 = new2();
let uuid1 = test_util::new();
let uuid2 = test_util::new2();

assert_eq!(uuid1, uuid1);
assert_eq!(uuid2, uuid2);
Expand All @@ -1749,7 +1746,7 @@ mod tests {

#[test]
fn test_as_fields() {
let u = new();
let u = test_util::new();
let (d1, d2, d3, d4) = u.as_fields();

assert_ne!(d1, 0);
Expand Down Expand Up @@ -1803,7 +1800,7 @@ mod tests {

#[test]
fn test_as_bytes() {
let u = new();
let u = test_util::new();
let ub = u.as_bytes();

assert_eq!(ub.len(), 16);
Expand Down Expand Up @@ -1839,9 +1836,9 @@ mod tests {

#[test]
fn test_operator_eq() {
let u1 = new();
let u1 = test_util::new();
let u2 = u1.clone();
let u3 = new2();
let u3 = test_util::new2();

assert_eq!(u1, u1);
assert_eq!(u1, u2);
Expand All @@ -1856,8 +1853,8 @@ mod tests {
#[test]
fn test_iterbytes_impl_for_uuid() {
let mut set = std::collections::HashSet::new();
let id1 = new();
let id2 = new2();
let id1 = test_util::new();
let id2 = test_util::new2();
set.insert(id1.clone());

assert!(set.contains(&id1));
Expand All @@ -1868,7 +1865,7 @@ mod tests {
#[test]
fn test_slog_kv() {
let root = slog::Logger::root(slog::Discard.fuse(), o!());
let u1 = new();
let u1 = test_util::new();
crit!(root, "test"; "u1" => u1);
}
}
19 changes: 19 additions & 0 deletions src/test_util.rs
@@ -0,0 +1,19 @@
use Uuid;

pub fn new() -> Uuid {
Uuid {
bytes: [
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
0xA1, 0xE4,
],
}
}

pub fn new2() -> Uuid {
Uuid {
bytes: [
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
0xA1, 0xE4,
],
}
}

0 comments on commit 80bf568

Please sign in to comment.