Files
Pictor-public/crates/qr/tests/errors.rs
T

85 lines
3.8 KiB
Rust

//! Colour checks and input errors: typed, with a plain message.
mod common;
use common::logo;
use pictor_qr::{Level, QrError, Style, contrast_ok, contrast_ratio, render_png, render_rgba, render_svg};
#[test]
fn contrast_ratio_matches_wcag() {
assert!((contrast_ratio([0, 0, 0, 255], [255, 255, 255, 255]) - 21.0).abs() < 1e-9);
assert!((contrast_ratio([255, 255, 255, 255], [255, 255, 255, 255]) - 1.0).abs() < 1e-9);
// #777777 on white is about 4.48:1.
assert!((contrast_ratio([0x77, 0x77, 0x77, 255], [255, 255, 255, 255]) - 4.48).abs() < 0.01);
}
#[test]
fn contrast_ok_cases() {
assert!(contrast_ok([0, 0, 0, 255], [255, 255, 255, 255]));
assert!(contrast_ok([16, 32, 96, 255], [255, 250, 225, 255]));
assert!(!contrast_ok([200, 200, 200, 255], [255, 255, 255, 255]), "too light");
assert!(!contrast_ok([255, 255, 255, 255], [0, 0, 0, 255]), "inverted");
assert!(!contrast_ok([0, 0, 0, 0], [255, 255, 255, 255]), "transparent dark");
assert!(contrast_ok([0, 0, 0, 255], [255, 255, 255, 0]), "a transparent background is allowed");
}
#[test]
fn low_contrast_is_refused() {
let style = Style { dark: [180, 180, 180, 255], light: [255, 255, 255, 255], ..Style::default() };
let err = render_rgba("hello", Level::M, &style, None).unwrap_err();
assert!(matches!(err, QrError::LowContrast { .. }), "{err:?}");
assert!(err.to_string().contains("contrast"), "{err}");
assert!(matches!(render_svg("hello", Level::M, &style, None), Err(QrError::LowContrast { .. })));
assert!(matches!(render_png("hello", Level::M, &style, None), Err(QrError::LowContrast { .. })));
}
#[test]
fn inverted_and_transparent_are_refused() {
let inverted = Style { dark: [255, 255, 255, 255], light: [0, 0, 0, 255], ..Style::default() };
assert_eq!(render_rgba("x", Level::M, &inverted, None).unwrap_err(), QrError::Inverted);
let invisible = Style { dark: [0, 0, 0, 0], ..Style::default() };
assert_eq!(render_rgba("x", Level::M, &invisible, None).unwrap_err(), QrError::TransparentDark);
}
#[test]
fn empty_input_is_refused() {
assert_eq!(render_rgba("", Level::M, &Style::default(), None).unwrap_err(), QrError::Empty);
assert_eq!(render_svg(" ", Level::M, &Style::default(), None).unwrap_err(), QrError::Empty);
assert!(!QrError::Empty.to_string().is_empty());
}
#[test]
fn data_too_long_is_refused() {
let text = "ž".repeat(700); // 1400 bytes: fits at L, not at H (max 1273 bytes)
let err = render_rgba(&text, Level::H, &Style::default(), None).unwrap_err();
assert_eq!(err, QrError::TooLong { level: Level::H, logo: false });
assert!(err.to_string().contains("too much"), "{err}");
let style = Style { size_px: 2000, ..Style::default() };
assert!(render_rgba(&text, Level::L, &style, None).is_ok());
// With a logo the level is H whatever was asked, and the message says why.
let err = render_rgba(&text, Level::L, &style, Some(&logo(32, 32))).unwrap_err();
assert_eq!(err, QrError::TooLong { level: Level::H, logo: true });
assert!(err.to_string().contains("logo"), "{err}");
let err = render_rgba(&"a".repeat(5000), Level::L, &Style::default(), None).unwrap_err();
assert_eq!(err, QrError::TooLong { level: Level::L, logo: false });
}
#[test]
fn size_too_small_is_refused() {
let style = Style { size_px: 20, ..Style::default() };
let err = render_rgba("hello", Level::M, &style, None).unwrap_err();
assert!(matches!(err, QrError::SizeTooSmall { min_px: 29 }), "{err:?}");
}
#[test]
fn size_too_large_is_refused() {
let style = Style { size_px: 100_000, ..Style::default() };
assert!(matches!(render_rgba("hello", Level::M, &style, None), Err(QrError::SizeTooLarge { .. })));
}
#[test]
fn empty_logo_is_refused() {
let empty = image::RgbaImage::new(0, 0);
assert!(matches!(render_rgba("x", Level::M, &Style::default(), Some(&empty)), Err(QrError::Logo(_))));
}