Skip to content

Commit

Permalink
add more unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shigedangao committed Jun 27, 2023
1 parent 6cccbbb commit 0750cce
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 15 deletions.
58 changes: 58 additions & 0 deletions lymui-c/src/binding/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl AnyColor {
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;

#[test]
fn expect_to_convert_rgb_to_color() {
Expand All @@ -164,7 +165,64 @@ mod tests {
},
RgbKind::Hex,
);
assert!(color.is_ok());

let hex = color.unwrap().hex;
let cstr = unsafe { CStr::from_ptr(hex) }.to_str().unwrap();

assert_eq!(cstr, "#1170be");
}

#[test]
fn expect_to_convert_rgb_to_slice() {
let color = AnyColor::from_rgb(
Rgb {
r: 50,
g: 10,
b: 29,
},
RgbKind::YCbCr,
);
assert!(color.is_ok());

let slice = color.unwrap().slice;

// Retrieve the vector
unsafe {
let slice = Box::from_raw(slice);
let vec = Vec::from_raw_parts(slice.ptr as *mut f64, slice.len, slice.len);

assert!(vec.len() == 3);
assert!(vec[0].is_normal());
assert!(vec[1].is_normal());
assert!(vec[2].is_normal());
};
}

#[test]
fn expect_to_convert_xyz_to_slice() {
let lab = AnyColor::from_xyz(
Xyz {
x: 0.037632178651154785,
y: 0.01735228547582024,
z: 0.11380345919261227,
},
XyzKind::Lab,
LightKind::D65,
);
assert!(lab.is_ok());

let slice = lab.unwrap().slice;

// Retrieve the vector
unsafe {
let slice = Box::from_raw(slice);
let vec = Vec::from_raw_parts(slice.ptr as *mut f64, slice.len, slice.len);

assert!(vec.len() == 3);
assert!(vec[0].is_normal());
assert!(vec[1].is_normal());
assert!(vec[2].is_normal());
};
}
}
35 changes: 35 additions & 0 deletions lymui-c/src/binding/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,38 @@ impl ColorMixture {
Ok(ColorMixture { len, ptr })
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn expect_to_generate_shade() {
let shade = ColorMixture::new(
Rgb {
r: 10,
g: 10,
b: 255,
},
0.1,
true,
);

assert!(shade.is_ok());
}

#[test]
fn expect_to_generate_tint() {
let tint = ColorMixture::new(
Rgb {
r: 10,
g: 10,
b: 255,
},
0.1,
false,
);

assert!(tint.is_ok());
}
}
19 changes: 19 additions & 0 deletions lymui-c/src/binding/kind/grayscale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,22 @@ pub(crate) fn get_grayscale_from_rgb(rgb: Rgb, kind: GrayscaleKind) -> GrayScale

GrayScale::from_rgb(rgb, g_kind)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn expect_to_convert_rgb_to_grayscale() {
let gscale = get_grayscale_from_rgb(
Rgb {
r: 10,
g: 10,
b: 10,
},
GrayscaleKind::BT709,
);

assert!(gscale.0 > 0);
}
}
15 changes: 0 additions & 15 deletions lymui-c/src/binding/kind/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,6 @@ mod tests {

let res = tgt.as_rgb(hex_ptr);
assert!(res.is_ok());

let rgb = res.unwrap();
assert_eq!(rgb.r, 255);
assert_eq!(rgb.g, 255);
assert_eq!(rgb.b, 255);
}

#[test]
Expand All @@ -113,11 +108,6 @@ mod tests {

let res = tgt.as_rgb(hsl_ptr);
assert!(res.is_ok());

let rgb = res.unwrap();
assert_eq!(rgb.r, 5);
assert_eq!(rgb.g, 9);
assert_eq!(rgb.b, 95);
}

#[test]
Expand All @@ -128,11 +118,6 @@ mod tests {

let res = tgt.as_rgb(ansi_ptr);
assert!(res.is_ok());

let rgb = res.unwrap();
assert_eq!(rgb.r, 92);
assert_eq!(rgb.g, 191);
assert_eq!(rgb.b, 84);
}

#[test]
Expand Down

0 comments on commit 0750cce

Please sign in to comment.