use labelize::barcodes::{
aztec, code128, code39, datamatrix, ean13, maxicode, pdf417, qrcode, twooffive,
};
use labelize::elements::barcode_qr::QrErrorCorrectionLevel;
#[test]
fn code128_encodes_ascii() {
let img = code128::encode_auto("Hello123", 100, 2).expect("encode_auto failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn code128_encodes_digits_only() {
let img = code128::encode_auto("1234567890", 80, 2).expect("encode_auto failed");
assert!(img.width() > 0);
}
#[test]
fn code128_empty_input_handled() {
let _result = code128::encode_auto("", 100, 2);
}
#[test]
fn code128_no_mode_strips_prefix_from_display() {
let (img, text) = code128::encode_no_mode(">;382436>6CODE128>752375152", 100, 2)
.expect("encode_no_mode failed");
assert!(img.width() > 0);
assert!(
!text.contains('>'),
"display text should not contain '>' prefix codes: {}",
text
);
assert!(
text.contains("382436"),
"display text should contain '382436': {}",
text
);
assert!(
text.contains("CODE128"),
"display text should contain 'CODE128': {}",
text
);
assert!(
text.contains("TEST"),
"display text should contain 'TEST' (Code A pair-mode decoding of 52,37,51,52): {}",
text
);
}
#[test]
fn code128_no_mode_default_code_b() {
let (img1, text1) = code128::encode_no_mode("12345678", 100, 2).expect("encode_no_mode failed");
let img2 = code128::encode_auto("12345678", 100, 2).expect("encode_auto failed");
assert!(
img1.width() > img2.width(),
"Mode N without prefix should use Code B (wider), not auto Code C"
);
assert_eq!(text1, "12345678");
}
#[test]
fn code128_auto_with_fnc1() {
let content = format!("{}1234567890", code128::ESCAPE_FNC_1);
let img = code128::encode_auto(&content, 100, 2).expect("encode_auto with FNC1 failed");
let img_no_fnc1 = code128::encode_auto("1234567890", 100, 2).expect("encode_auto failed");
assert!(
img.width() > img_no_fnc1.width(),
"FNC1 should add one symbol width"
);
}
#[test]
fn code39_encodes_alphanumeric() {
let img = code39::encode("ABC123", 100, 3, 2).expect("code39 failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn code39_empty_input_handled() {
let _result = code39::encode("", 100, 3, 2);
}
#[test]
fn ean13_encodes_12_digits() {
let img = ean13::encode("123456789012", 100, 2).expect("ean13 failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn ean13_empty_input_returns_error() {
let result = ean13::encode("", 100, 2);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn twooffive_encodes_digits() {
let img = twooffive::encode("12345678", 100, 3, 2, false).expect("2of5 failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn twooffive_empty_input_returns_error() {
let result = twooffive::encode("", 100, 3, 2, false);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn pdf417_encodes_text() {
let img = pdf417::encode("Hello World", 4, 0, 0, 0, false, 10).expect("pdf417 failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn pdf417_empty_input_returns_error() {
let result = pdf417::encode("", 4, 0, 0, 0, false, 10);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn pdf417_module_width_scales_output() {
let img1 = pdf417::encode("Test data", 0, 0, 5, 0, false, 40).expect("encode 1");
let img3 = pdf417::encode("Test data", 0, 0, 5, 0, false, 40).expect("encode 3");
assert_eq!(img1.width(), img3.width());
}
#[test]
fn pdf417_row_height_fallback_from_by() {
let img = pdf417::encode("Hello World", 0, 2, 5, 0, false, 40).expect("encode");
assert!(img.height() > 0);
}
#[test]
fn pdf417_explicit_row_height_overrides_by() {
let img = pdf417::encode("Hello World", 5, 2, 5, 0, false, 9999).expect("encode");
assert!(
img.height() < 500,
"height should use b7_h=5, not by_height"
);
}
#[test]
fn pdf417_default_aspect_ratio() {
let img = pdf417::encode(
"Some data to encode for aspect ratio test",
0,
0,
0,
0,
false,
10,
)
.expect("encode");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn pdf417_validation_rejects_over_928() {
let result = pdf417::encode("x", 0, 0, 30, 90, false, 10);
assert!(result.is_err(), "30×90=2700 should exceed 928 limit");
}
#[test]
fn pdf417_truncated_mode() {
let full = pdf417::encode("Truncated test", 0, 0, 0, 0, false, 10).expect("full");
let trunc = pdf417::encode("Truncated test", 0, 0, 0, 0, true, 10).expect("truncated");
assert!(
trunc.width() <= full.width(),
"truncated PDF417 should not be wider than full"
);
}
#[test]
fn pdf417_crlf_in_data() {
let img = pdf417::encode("Line1\nLine2", 0, 0, 0, 0, false, 10).expect("encode");
assert!(img.width() > 0);
}
#[test]
fn aztec_encodes_text() {
let img = aztec::encode("Hello", 4, 0).expect("aztec failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
assert_eq!(img.width(), img.height(), "Aztec code should be square");
}
#[test]
fn aztec_empty_input_returns_error() {
let result = aztec::encode("", 4, 0);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn datamatrix_encodes_text() {
let img = datamatrix::encode("Hello", 4, 0, 0).expect("datamatrix failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn datamatrix_empty_input_returns_error() {
let result = datamatrix::encode("", 4, 0, 0);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn qrcode_encodes_text() {
let img = qrcode::encode("Hello World", 5, QrErrorCorrectionLevel::M).expect("qrcode failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
assert_eq!(img.width(), img.height(), "QR code should be square");
}
#[test]
fn qrcode_empty_input_returns_error() {
let result = qrcode::encode("", 5, QrErrorCorrectionLevel::M);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn maxicode_encodes_text() {
let img = maxicode::encode("Hello World", 4).expect("maxicode failed");
assert!(img.width() > 0);
assert!(img.height() > 0);
}
#[test]
fn maxicode_empty_input_returns_error() {
let result = maxicode::encode("", 4);
assert!(result.is_err(), "expected error for empty input");
}
#[test]
fn code128_wider_bar_produces_wider_image() {
let narrow = code128::encode_auto("TEST", 100, 1).expect("narrow");
let wide = code128::encode_auto("TEST", 100, 3).expect("wide");
assert!(
wide.width() > narrow.width(),
"wider bar width should produce wider image"
);
}
#[test]
fn code128_taller_height_produces_taller_image() {
let short = code128::encode_auto("TEST", 50, 2).expect("short");
let tall = code128::encode_auto("TEST", 200, 2).expect("tall");
assert!(
tall.height() > short.height(),
"taller height should produce taller image"
);
}