Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/oned/code_128_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,8 @@ impl OneDimensionalCodeWriter for Code128Writer {
}

fn check(contents: &str, hints: &crate::EncodeHints) -> Result<i32> {
let length = contents.chars().count();
// Check length
if !(1..=80).contains(&length) {
return Err(Exceptions::illegal_argument_with(format!(
"Contents length should be between 1 and 80 characters, but got {length}"
)));
if contents.is_empty() {
return Err(Exceptions::illegal_argument_with("Found empty contents"));
}

// Check for forced code set hint.
Expand Down
52 changes: 45 additions & 7 deletions src/oned/code_128_writer_test_tase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const LF: &str = "10000110010";
use once_cell::sync::Lazy;

use crate::{
BarcodeFormat, DecodeHints, EncodeHintValue, EncodeHints, Writer,
BarcodeFormat, DecodeHints, EncodeHintValue, EncodeHints, Exceptions, Writer,
common::{BitMatrix, Result, bit_matrix_test_case},
oned::{Code128Reader, OneDReader},
oned::{Code128Reader, OneDReader, OneDimensionalCodeWriter},
};

use super::Code128Writer;
Expand Down Expand Up @@ -142,11 +142,49 @@ fn testRoundtrip() {
}

#[test]
fn testLongCompact() {
//test longest possible input
let toEncode =
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
encode(toEncode, true, toEncode).expect("encode");
fn testLongContents() {
for length in [80, 81, 82, 256] {
for character in ["A", "a", "1"] {
let toEncode = character.repeat(length);
for compact in [false, true] {
encode(&toEncode, compact, &toEncode).expect("encode");
}
}
}
}

#[test]
fn testLongContentsWithForcedCodeSet() {
for (codeSet, content, startCode) in [
("A", "A\n", START_CODE_A),
("B", "ab", START_CODE_B),
("C", "12", START_CODE_C),
] {
let hints = EncodeHints::default().with(EncodeHintValue::ForceCodeSet(codeSet.to_string()));
for length in [80, 82, 256] {
let toEncode = content.repeat(length / 2);
let result = WRITER
.encode_with_hints(&toEncode, &BarcodeFormat::CODE_128, 0, 0, &hints)
.expect("encode");
let decoded = Code128Reader
.decode_row(0, &result.getRow(0), &DecodeHints::default())
.expect("decode");
assert_eq!(toEncode, decoded.getText());
let actual = bit_matrix_test_case::matrix_to_string(&result);
assert!(actual.starts_with(&format!("{QUIET_SPACE}{startCode}")));
}
}
}

#[test]
fn testEmptyContents() {
for compact in [false, true] {
let hints = EncodeHints::default().with(EncodeHintValue::Code128Compact(compact));
assert!(matches!(
WRITER.encode_oned_with_hints("", &hints),
Err(Exceptions::IllegalArgumentException(_))
));
}
}

#[test]
Expand Down
Loading