1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#![allow(dead_code)]

use crate::error::TinifyResult;
use crate::client::{Method, Client};
use std::path::Path;

#[macro_export]
macro_rules! create_file {
  () => {
    use image;
    use image::RgbImage;
    use num_complex;
    use std::path::Path;

    let img_x = 400;
    let img_y = 400;
    let scale_x = 3.0 / img_x as f32;
    let scale_y = 3.0 / img_y as f32;
    let mut img_buf: RgbImage = image::ImageBuffer::new(img_x, img_y);

    for (x, y, pixel) in img_buf.enumerate_pixels_mut() {
      let r = (1.3 * x as f32) as u8;
      let b = (1.3 * y as f32) as u8;
      *pixel = image::Rgb([r, 0, b]);
    }

    for x in 0..img_x {
      for y in 0..img_y {
        let cx = y as f32 * scale_x - 1.5;
        let cy = x as f32 * scale_y - 1.5;
        let c = num_complex::Complex::new(-0.4, 0.6);
        let mut z = num_complex::Complex::new(cx, cy);
        let mut i = 0;

        while i < 255 && z.norm() <= 2.0 {
          z = z * z + c;
          i += 1;
        }

        let pixel = img_buf.get_pixel_mut(x, y);
        let image::Rgb(data) = *pixel;
        *pixel = image::Rgb([data[0], i as u8, data[2]]);
      }
    }
  
    let tmp = Path::new("./tmp_test_image.png");
    img_buf.save(tmp).unwrap();
  }
}

pub struct MockClient<'a> {
  pub key: &'a str,
}

impl<'a> MockClient<'a> {
  pub fn new() -> Self {
    Self {
      key: "yjb7YwqyRZwzkGtCfDt6qmXs3QRQTJz3",
    }
  }

  pub fn request(
    &self, 
    method: Method, 
    path: &Path, 
    buffer: Option<&Vec<u8>>
  ) -> TinifyResult {
    let client = Client {
      key: self.key.to_owned(),
    };
    client.request(method, path, buffer)
  }
}

#[cfg(test)]
mod tests {
  use std::fs;
  use std::path::Path;
  
  #[test]
  fn test_tmp_file_was_created() {
    create_file!();
    let path = Path::new("./tmp_test_image.png");
    
    assert!(path.exists());
  }

  #[test]
  fn test_tmp_file_was_deleted() {
    let path = Path::new("./tmp_test_image.png");
    if path.exists() {
      fs::remove_file(path).unwrap();
    }

    assert!(!path.exists());
  }
}