-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
238 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,6 +357,8 @@ pub trait ReadFixed { | |
} | ||
} | ||
|
||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use fixcol_derive::ReadFixed; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use fixcol::ReadFixed; | ||
#[cfg(feature = "experimental-write")] | ||
use fixcol::WriteFixed; | ||
|
||
#[cfg_attr(feature = "experimental-write", derive(WriteFixed))] | ||
#[derive(Debug, PartialEq, ReadFixed)] | ||
struct Thing1 { | ||
#[fixcol(width = 5)] | ||
name: String, | ||
#[fixcol(width = 5, align = "right")] | ||
x: Option::<f32>, | ||
#[fixcol(width = 5, align = "right")] | ||
y: Option<f32>, | ||
} | ||
|
||
#[derive(Debug, PartialEq, ReadFixed)] | ||
struct Thing2 { | ||
#[fixcol(width = 5)] | ||
name: String, | ||
#[fixcol(width = 5, align = "right")] | ||
x: Option<f32>, | ||
#[fixcol(width = 5, align = "right")] | ||
y: f32, | ||
} | ||
|
||
#[derive(Debug, PartialEq, ReadFixed)] | ||
struct Thing3 { | ||
#[fixcol(width = 5)] | ||
name: Option<String>, | ||
#[fixcol(width = 5, align = "right")] | ||
x: f32, | ||
#[fixcol(width = 5, align = "right")] | ||
y: f32, | ||
} | ||
|
||
#[derive(Debug, PartialEq, ReadFixed)] | ||
struct Thing4 { | ||
#[fixcol(width = 5)] | ||
name: String, | ||
#[fixcol(width = 5, align = "right")] | ||
x: f32, | ||
#[fixcol(width = 5, align = "right")] | ||
y: f32, | ||
} | ||
|
||
#[test] | ||
fn parse_option() { | ||
let actual = Thing1::read_fixed_str("foo 3.14 42").unwrap(); | ||
let expected = Thing1 { name: String::from("foo"), x: Some(3.14), y: Some(42.0)}; | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
#[test] | ||
fn parse_none() { | ||
let actual = Thing1::read_fixed_str("foo 3.14 ").unwrap(); | ||
let expected = Thing1 { name: String::from("foo"), x: Some(3.14), y: None}; | ||
assert_eq!(actual, expected); | ||
|
||
let actual = Thing1::read_fixed_str("foo 42").unwrap(); | ||
let expected = Thing1 { name: String::from("foo"), x: None, y: Some(42.0)}; | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
#[test] | ||
fn err_on_non_option_empty() { | ||
let actual = Thing2::read_fixed_str("foo 42").unwrap(); | ||
let expected = Thing2 { name: String::from("foo"), x: None, y: 42.0}; | ||
assert_eq!(actual, expected); | ||
|
||
let actual = Thing2::read_fixed_str("foo 3.14 "); | ||
assert!(actual.is_err()); | ||
assert_eq!( | ||
actual.unwrap_err().to_string(), | ||
"Error handling data from \"\": cannot parse float from empty string\n" | ||
); | ||
} | ||
|
||
#[test] | ||
fn option_vs_empty_string() { | ||
let actual = Thing3::read_fixed_str("foo 3.14 42").unwrap(); | ||
let expected = Thing3 { name: Some(String::from("foo")), x: 3.14, y: 42.0}; | ||
assert_eq!(actual, expected); | ||
|
||
let actual = Thing3::read_fixed_str(" 3.14 42").unwrap(); | ||
let expected = Thing3 { name: None, x: 3.14, y: 42.0}; | ||
assert_eq!(actual, expected); | ||
|
||
let actual = Thing4::read_fixed_str("foo 3.14 42").unwrap(); | ||
let expected = Thing4 { name: String::from("foo"), x: 3.14, y: 42.0}; | ||
assert_eq!(actual, expected); | ||
|
||
let actual = Thing4::read_fixed_str(" 3.14 42").unwrap(); | ||
let expected = Thing4 { name: String::from(""), x: 3.14, y: 42.0}; | ||
assert_eq!(actual, expected); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "experimental-write")] | ||
fn write_option_some() { | ||
let thing = Thing1 { name: String::from("foo"), x: Some(3.14), y: Some(42.0)}; | ||
|
||
let mut v = Vec::new(); | ||
let res = thing.write_fixed(&mut v); | ||
|
||
assert!(res.is_ok()); | ||
|
||
let text = std::str::from_utf8(v.as_slice()).unwrap(); | ||
assert_eq!(text, "foo 3.14 42"); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "experimental-write")] | ||
fn write_option_none() { | ||
let thing = Thing1 { name: String::from("foo"), x: None, y: Some(42.0)}; | ||
|
||
let mut v = Vec::new(); | ||
let res = thing.write_fixed(&mut v); | ||
|
||
assert!(res.is_ok()); | ||
|
||
let text = std::str::from_utf8(v.as_slice()).unwrap(); | ||
assert_eq!(text, "foo 42"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters