Skip to content

Commit b98a448

Browse files
committed
Fix clippy errors and convert tests to use proper assertions
1 parent b8ad90c commit b98a448

File tree

4 files changed

+141
-110
lines changed

4 files changed

+141
-110
lines changed

tests/identifier_tests.rs

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,10 @@ fn test_utf8_to_utf32_length() {
8888

8989
for (input, expected_len) in test_cases {
9090
let actual_len = unicode::utf8_to_utf32_length(input.as_bytes());
91-
println!("UTF-8 '{}' has {} UTF-32 code points", input, actual_len);
92-
// Note: Expected values may need adjustment based on actual Unicode handling
93-
assert!(
94-
actual_len > 0 || input.is_empty(),
95-
"Length should be positive for non-empty strings"
91+
assert_eq!(
92+
actual_len, expected_len,
93+
"UTF-32 length mismatch for '{}'",
94+
input
9695
);
9796
}
9897
}
@@ -120,26 +119,24 @@ fn test_utf8_to_utf32_conversion() {
120119

121120
for input in test_cases {
122121
let utf32_chars = unicode::utf8_to_utf32(input.as_bytes());
123-
println!(
124-
"UTF-8 to UTF-32: '{}' -> {} chars",
125-
input,
126-
utf32_chars.len()
127-
);
128122

129123
// Verify the conversion produces valid UTF-32
130124
assert!(
131125
!utf32_chars.is_empty() || input.is_empty(),
132126
"Non-empty input should produce non-empty output"
133127
);
134128

135-
// Try to convert back to UTF-8
129+
// Try to convert back to UTF-8 for roundtrip test
136130
let utf8_bytes = unicode::utf32_to_utf8(&utf32_chars);
137131
if !utf8_bytes.is_empty() {
138132
let roundtrip = String::from_utf8(utf8_bytes);
139-
if let Ok(roundtrip_str) = roundtrip {
140-
println!("Roundtrip: '{}' -> '{}'", input, roundtrip_str);
141-
// Note: Some normalization might occur during roundtrip
142-
}
133+
assert!(
134+
roundtrip.is_ok(),
135+
"Failed to roundtrip UTF-8 conversion for '{}'",
136+
input
137+
);
138+
let roundtrip_str = roundtrip.unwrap();
139+
assert_eq!(roundtrip_str, input, "Roundtrip mismatch for '{}'", input);
143140
}
144141
}
145142
}
@@ -167,26 +164,26 @@ fn test_utf32_to_utf8_conversion() {
167164
for utf32_seq in test_utf32_sequences {
168165
let utf8_bytes = unicode::utf32_to_utf8(&utf32_seq);
169166

170-
if !utf8_bytes.is_empty() {
171-
if let Ok(utf8_str) = String::from_utf8(utf8_bytes.clone()) {
172-
println!("UTF-32 {:X?} -> UTF-8 '{}'", utf32_seq, utf8_str);
167+
assert!(
168+
!utf8_bytes.is_empty(),
169+
"UTF-32 to UTF-8 conversion should not produce empty result for {:X?}",
170+
utf32_seq
171+
);
173172

174-
// Test roundtrip conversion
175-
let back_to_utf32 = unicode::utf8_to_utf32(&utf8_bytes);
176-
if back_to_utf32 == utf32_seq {
177-
println!(" Roundtrip successful");
178-
} else {
179-
println!(
180-
" Roundtrip mismatch: {:X?} != {:X?}",
181-
back_to_utf32, utf32_seq
182-
);
183-
}
184-
} else {
185-
println!("UTF-32 {:X?} -> invalid UTF-8 bytes", utf32_seq);
186-
}
187-
} else {
188-
println!("UTF-32 {:X?} -> empty UTF-8", utf32_seq);
189-
}
173+
let utf8_str = String::from_utf8(utf8_bytes.clone());
174+
assert!(
175+
utf8_str.is_ok(),
176+
"UTF-32 {:X?} should produce valid UTF-8",
177+
utf32_seq
178+
);
179+
180+
// Test roundtrip conversion
181+
let back_to_utf32 = unicode::utf8_to_utf32(&utf8_bytes);
182+
assert_eq!(
183+
back_to_utf32, utf32_seq,
184+
"Roundtrip conversion failed for UTF-32 {:X?}",
185+
utf32_seq
186+
);
190187
}
191188
}
192189

@@ -218,11 +215,6 @@ fn test_identifier_validation_edge_cases() {
218215
let first_pos = validation::valid_name_code_point_first_position(ch as u32);
219216
let other_pos = validation::valid_name_code_point_other_position(ch as u32);
220217

221-
println!(
222-
"Character '{}' (U+{:04X}): first={}, other={}",
223-
ch, ch as u32, first_pos, other_pos
224-
);
225-
226218
// Control and format characters should generally be invalid
227219
if ch.is_control() || ch as u32 == 0x00AD || ch as u32 == 0x200C || ch as u32 == 0x200D {
228220
assert!(
@@ -231,6 +223,17 @@ fn test_identifier_validation_edge_cases() {
231223
ch as u32
232224
);
233225
}
226+
227+
assert_eq!(
228+
first_pos, expected_valid,
229+
"First position validation mismatch for U+{:04X}",
230+
ch as u32
231+
);
232+
assert_eq!(
233+
other_pos, expected_valid,
234+
"Other position validation mismatch for U+{:04X}",
235+
ch as u32
236+
);
234237
}
235238
}
236239

@@ -259,15 +262,10 @@ fn test_unicode_categories() {
259262
('-', "Dash Punctuation", false), // Valid in middle positions only
260263
];
261264

262-
for (ch, category, expected_first) in category_tests {
265+
for (ch, _category, expected_first) in category_tests {
263266
let first_pos = validation::valid_name_code_point_first_position(ch as u32);
264267
let other_pos = validation::valid_name_code_point_other_position(ch as u32);
265268

266-
println!(
267-
"'{}' ({}): first={}, other={}",
268-
ch, category, first_pos, other_pos
269-
);
270-
271269
if ch.is_alphabetic() {
272270
assert!(
273271
first_pos,
@@ -280,5 +278,11 @@ fn test_unicode_categories() {
280278
ch
281279
);
282280
}
281+
282+
assert_eq!(
283+
first_pos, expected_first,
284+
"First position validation mismatch for '{}' ({})",
285+
ch, _category
286+
);
283287
}
284288
}

tests/to_ascii_tests.rs

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn test_to_ascii_basic() {
1313
("bücher.example", "xn--bcher-kva.example"),
1414
];
1515

16-
for (input, expected) in test_cases {
16+
for (input, _expected) in test_cases {
1717
let result = to_ascii(input);
1818
assert!(
1919
result.is_ok(),
@@ -32,7 +32,7 @@ fn test_to_ascii_special_characters() {
3232
assert!(result.is_ok());
3333

3434
// Test soft hyphen removal
35-
let result = to_ascii("ex­ample.com"); // Contains U+00AD soft hyphen
35+
let result = to_ascii("ex\u{AD}ample.com"); // Contains U+00AD soft hyphen
3636
assert!(result.is_ok());
3737

3838
// Test replacement character
@@ -82,7 +82,7 @@ fn test_to_ascii_edge_cases() {
8282
("simple.café.com", "simple.xn--caf-dma.com"),
8383
];
8484

85-
for (input, expected) in test_cases {
85+
for (input, _expected) in test_cases {
8686
let result = to_ascii(input);
8787
assert!(
8888
result.is_ok(),
@@ -113,14 +113,13 @@ fn test_to_ascii_unicode_scripts() {
113113

114114
for (input, expected) in test_cases {
115115
let result = to_ascii(input);
116-
if result.is_ok() {
117-
let actual = result.unwrap();
118-
println!("Unicode script test: '{}' -> '{}'", input, actual);
119-
// Note: Expected values may need adjustment based on actual implementation
120-
// For now, just verify the conversion doesn't fail
121-
} else {
122-
println!("Failed to convert '{}': {:?}", input, result);
123-
}
116+
assert!(
117+
result.is_ok(),
118+
"Failed to convert Unicode script '{}'",
119+
input
120+
);
121+
let actual = result.unwrap();
122+
assert_eq!(actual, expected, "Unicode script mismatch for '{}'", input);
124123
}
125124
}
126125

@@ -149,16 +148,24 @@ fn test_to_ascii_punycode_expansion() {
149148

150149
for (input, expected) in test_cases {
151150
let result = to_ascii(input);
152-
if result.is_ok() {
153-
let actual = result.unwrap();
154-
println!("Punycode expansion: '{}' -> '{}'", input, actual);
155-
// Verify it starts with xn-- for Unicode labels
156-
if input.chars().any(|c| !c.is_ascii()) {
157-
assert!(
158-
actual.contains("xn--"),
159-
"Unicode input should produce punycode output"
160-
);
161-
}
151+
assert!(
152+
result.is_ok(),
153+
"Failed to convert punycode expansion '{}'",
154+
input
155+
);
156+
let actual = result.unwrap();
157+
assert_eq!(
158+
actual, expected,
159+
"Punycode expansion mismatch for '{}'",
160+
input
161+
);
162+
163+
// Verify it starts with xn-- for Unicode labels
164+
if !input.is_ascii() {
165+
assert!(
166+
actual.contains("xn--"),
167+
"Unicode input should produce punycode output"
168+
);
162169
}
163170
}
164171
}

tests/to_unicode_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ada_idna::domain::{IdnaError, to_unicode};
1+
use ada_idna::domain::to_unicode;
22

33
#[test]
44
fn test_to_unicode_basic() {

tests/wpt_tests.rs

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn test_wpt_basic_ascii_domains() {
1212
("a.b.c.d.e", "a.b.c.d.e"),
1313
];
1414

15-
for (input, expected) in test_cases {
15+
for (input, _expected) in test_cases {
1616
let result = to_ascii(input);
1717
assert!(
1818
result.is_ok(),
@@ -49,13 +49,17 @@ fn test_wpt_unicode_to_ascii() {
4949

5050
for (input, expected) in test_cases {
5151
let result = to_ascii(input);
52-
if result.is_ok() {
53-
let actual = result.unwrap();
54-
println!("WPT Unicode->ASCII: '{}' -> '{}'", input, actual);
55-
// Note: Exact expected values may need adjustment based on implementation
56-
} else {
57-
println!("WPT Unicode->ASCII failed for '{}': {:?}", input, result);
58-
}
52+
assert!(
53+
result.is_ok(),
54+
"WPT: Failed to convert Unicode->ASCII for '{}'",
55+
input
56+
);
57+
let actual = result.unwrap();
58+
assert_eq!(
59+
actual, expected,
60+
"WPT: Unicode->ASCII mismatch for '{}'",
61+
input
62+
);
5963
}
6064
}
6165

@@ -79,12 +83,17 @@ fn test_wpt_ascii_to_unicode() {
7983

8084
for (input, expected) in test_cases {
8185
let result = to_unicode(input);
82-
if result.is_ok() {
83-
let actual = result.unwrap();
84-
println!("WPT ASCII->Unicode: '{}' -> '{}'", input, actual);
85-
} else {
86-
println!("WPT ASCII->Unicode failed for '{}': {:?}", input, result);
87-
}
86+
assert!(
87+
result.is_ok(),
88+
"WPT: Failed to convert ASCII->Unicode for '{}'",
89+
input
90+
);
91+
let actual = result.unwrap();
92+
assert_eq!(
93+
actual, expected,
94+
"WPT: ASCII->Unicode mismatch for '{}'",
95+
input
96+
);
8897
}
8998
}
9099

@@ -194,25 +203,31 @@ fn test_wpt_international_domains() {
194203

195204
for (unicode_input, expected_ascii) in international_cases {
196205
let ascii_result = to_ascii(unicode_input);
197-
if ascii_result.is_ok() {
198-
let actual_ascii = ascii_result.unwrap();
199-
println!(
200-
"WPT International: '{}' -> '{}'",
201-
unicode_input, actual_ascii
202-
);
206+
assert!(
207+
ascii_result.is_ok(),
208+
"WPT: Failed to convert international domain '{}'",
209+
unicode_input
210+
);
211+
let actual_ascii = ascii_result.unwrap();
212+
assert_eq!(
213+
actual_ascii, expected_ascii,
214+
"WPT: International domain mismatch for '{}'",
215+
unicode_input
216+
);
203217

204-
// Test roundtrip
205-
let unicode_result = to_unicode(&actual_ascii);
206-
if unicode_result.is_ok() {
207-
let roundtrip_unicode = unicode_result.unwrap();
208-
println!(" Roundtrip: '{}' -> '{}'", actual_ascii, roundtrip_unicode);
209-
}
210-
} else {
211-
println!(
212-
"WPT International conversion failed for '{}': {:?}",
213-
unicode_input, ascii_result
214-
);
215-
}
218+
// Test roundtrip
219+
let unicode_result = to_unicode(&actual_ascii);
220+
assert!(
221+
unicode_result.is_ok(),
222+
"WPT: Failed to roundtrip convert '{}'",
223+
actual_ascii
224+
);
225+
let roundtrip_unicode = unicode_result.unwrap();
226+
assert_eq!(
227+
roundtrip_unicode, unicode_input,
228+
"WPT: Roundtrip mismatch for '{}'",
229+
unicode_input
230+
);
216231
}
217232
}
218233

@@ -236,12 +251,17 @@ fn test_wpt_normalization_cases() {
236251

237252
for (input, expected) in normalization_cases {
238253
let result = to_ascii(input);
239-
if result.is_ok() {
240-
let actual = result.unwrap();
241-
println!("WPT Normalization: '{}' -> '{}'", input, actual);
242-
} else {
243-
println!("WPT Normalization failed for '{}': {:?}", input, result);
244-
}
254+
assert!(
255+
result.is_ok(),
256+
"WPT: Failed to convert normalization case '{}'",
257+
input
258+
);
259+
let actual = result.unwrap();
260+
assert_eq!(
261+
actual, expected,
262+
"WPT: Normalization mismatch for '{}'",
263+
input
264+
);
245265
}
246266
}
247267

@@ -260,11 +280,11 @@ fn test_wpt_mixed_script_validation() {
260280
for (input, should_succeed) in mixed_script_cases {
261281
let result = to_ascii(input);
262282
if should_succeed {
263-
if result.is_ok() {
264-
println!("WPT Mixed script OK: '{}' -> '{}'", input, result.unwrap());
265-
} else {
266-
println!("WPT Mixed script failed: '{}' -> {:?}", input, result);
267-
}
283+
assert!(
284+
result.is_ok(),
285+
"WPT: Mixed script should succeed for '{}'",
286+
input
287+
);
268288
} else {
269289
assert!(
270290
result.is_err(),

0 commit comments

Comments
 (0)