diff --git a/.gitignore b/.gitignore index 3ca43ae..36a69c8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb +cowgen/tests/out diff --git a/cowgen/src/lib.rs b/cowgen/src/lib.rs index eb56ff6..01f05cc 100644 --- a/cowgen/src/lib.rs +++ b/cowgen/src/lib.rs @@ -85,7 +85,7 @@ impl, V: AsRef> Element { ) .with( &TextSegment::new( - &Font::from_reader(File::open(text.font).unwrap(), 12.0).unwrap(), + &Font::open(text.font, 12.0).unwrap(), text.text, Dynamic::Rgba(Rgba::new( text.fill.0, @@ -140,12 +140,3 @@ impl From for CowError { CowError::Magick(m.0.to_owned()) } } - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); - } -} diff --git a/cowgen/tests/assets/OpenSans-ExtraBold.ttf b/cowgen/tests/assets/OpenSans-ExtraBold.ttf new file mode 100644 index 0000000..3660681 Binary files /dev/null and b/cowgen/tests/assets/OpenSans-ExtraBold.ttf differ diff --git a/cowmic/src/cow.png b/cowgen/tests/assets/cow.png similarity index 100% rename from cowmic/src/cow.png rename to cowgen/tests/assets/cow.png diff --git a/cowgen/tests/assets/mariah.jpg b/cowgen/tests/assets/mariah.jpg new file mode 100644 index 0000000..2da5e9a Binary files /dev/null and b/cowgen/tests/assets/mariah.jpg differ diff --git a/cowgen/tests/expected/one.png b/cowgen/tests/expected/one.png new file mode 100644 index 0000000..859ee45 Binary files /dev/null and b/cowgen/tests/expected/one.png differ diff --git a/cowgen/tests/overlay_text_and_image.rs b/cowgen/tests/overlay_text_and_image.rs new file mode 100644 index 0000000..a9004f2 --- /dev/null +++ b/cowgen/tests/overlay_text_and_image.rs @@ -0,0 +1,40 @@ +use cowgen::*; +use std::{ + fs::{self, File}, + io::Write, +}; + +#[test] +fn test_overlay_text_and_image() { + println!("{}", std::env::current_dir().unwrap().to_str().unwrap()); + let out = Template::new( + include_bytes!("assets/cow.png").to_vec(), + vec![ + Element::new( + Media::Text(Text::new( + "Hello", + "tests/assets/OpenSans-ExtraBold.ttf", + 20.0, + (0, 0, 0, 128), + )), + (0, 0), + (64, 64), + ), + Element::new( + Media::Image(include_bytes!("assets/mariah.jpg").to_vec()), + (400, 400), + (800, 200), + ), + ], + ) + .unwrap() + .produce() + .unwrap(); + let mut f = File::create("tests/out/one.png").unwrap(); + f.write_all(&out).unwrap(); + f.flush().unwrap(); + + let actual = fs::read("tests/out/one.png").unwrap(); + let expected = include_bytes!("expected/one.png").to_vec(); + assert_eq!(expected, actual); +} diff --git a/cowmic/src/cow2.png b/cowmic/src/cow2.png deleted file mode 100644 index 8096e53..0000000 Binary files a/cowmic/src/cow2.png and /dev/null differ diff --git a/cowmic/src/main.rs b/cowmic/src/main.rs index ec49f0a..62bdf70 100644 --- a/cowmic/src/main.rs +++ b/cowmic/src/main.rs @@ -3,29 +3,5 @@ use std::{fs::File, io::Write}; use cowgen::{CowError, Element, Text}; fn main() -> Result<(), CowError> { - let out = cowgen::Template::new( - include_bytes!("cow.png").to_vec(), - vec![ - Element::new( - cowgen::Media::Text(Text::new( - "Hello", - "/usr/share/fonts/TTF/OpenSans-ExtraBold.ttf", - 20.0, - (0, 0, 0, 128), - )), - (0, 0), - (64, 64), - ), - Element::new( - cowgen::Media::Image(include_bytes!("mariah.jpg").to_vec()), - (200, 400), - (800, 200), - ), - ], - )? - .produce()?; - let mut f = File::create("out2.png").unwrap(); - f.write_all(&out).unwrap(); - f.flush().unwrap(); Ok(()) }