@@ -162,3 +162,120 @@ fn validate_overlap(val: &str) -> Result<u8, String> {
162162 Err ( _) => Err ( String :: from ( "overlap must be a number" ) ) ,
163163 }
164164}
165+
166+ #[ cfg( test) ]
167+ mod tests {
168+ use super :: * ;
169+ use std:: fs:: File ;
170+
171+ #[ test]
172+ fn test_must_not_exist_with_nonexistent_file ( ) {
173+ let path = "target/tmp_nonexistent_file.txt" ;
174+ if std:: path:: Path :: new ( path) . exists ( ) {
175+ std:: fs:: remove_file ( path) . unwrap ( ) ;
176+ }
177+ let result = must_not_exist ( path) ;
178+ assert ! ( result. is_ok( ) ) ;
179+ assert_eq ! ( result. unwrap( ) , std:: path:: PathBuf :: from( path) ) ;
180+ }
181+
182+ #[ test]
183+ fn test_must_not_exist_with_existing_file ( ) {
184+ let path = "target/tmp_existing_file.txt" ;
185+ File :: create ( path) . unwrap ( ) ;
186+ let result = must_not_exist ( path) ;
187+ assert ! ( result. is_err( ) ) ;
188+ assert ! ( result. unwrap_err( ) . contains( "should not already exist" ) ) ;
189+ std:: fs:: remove_file ( path) . unwrap ( ) ;
190+ }
191+
192+ #[ test]
193+ fn test_validate_image_output_with_valid_png_path ( ) {
194+ let path = "target/tmp_image.png" ;
195+ if std:: path:: Path :: new ( path) . exists ( ) {
196+ std:: fs:: remove_file ( path) . unwrap ( ) ;
197+ }
198+ let result = validate_image_output ( path) ;
199+ assert ! ( result. is_ok( ) ) ;
200+ assert_eq ! ( result. unwrap( ) , std:: path:: PathBuf :: from( path) ) ;
201+ }
202+
203+ #[ test]
204+ fn test_validate_image_output_with_wrong_extension ( ) {
205+ let result = validate_image_output ( "target/image.jpg" ) ;
206+ assert ! ( result. is_err( ) ) ;
207+ assert ! ( result. unwrap_err( ) . contains( "should have png extension" ) ) ;
208+ }
209+
210+ #[ test]
211+ fn test_validate_image_output_with_missing_extension ( ) {
212+ let result = validate_image_output ( "target/image" ) ;
213+ assert ! ( result. is_err( ) ) ;
214+ assert_eq ! (
215+ result. unwrap_err( ) ,
216+ "Output file must have png extension (.png)"
217+ ) ;
218+ }
219+
220+ #[ test]
221+ fn test_validate_image_output_with_existing_file ( ) {
222+ let path = "target/existing.png" ;
223+ File :: create ( path) . unwrap ( ) ;
224+ let result = validate_image_output ( path) ;
225+ assert ! ( result. is_err( ) ) ;
226+ assert ! ( result. unwrap_err( ) . contains( "should not already exist" ) ) ;
227+ std:: fs:: remove_file ( path) . unwrap ( ) ;
228+ }
229+
230+ #[ test]
231+ fn test_validate_block_width_valid ( ) {
232+ let result = validate_block_width ( "50" ) ;
233+ assert ! ( result. is_ok( ) ) ;
234+ assert_eq ! ( result. unwrap( ) , 50 ) ;
235+ }
236+
237+ #[ test]
238+ fn test_validate_block_width_non_number ( ) {
239+ let result = validate_block_width ( "abc" ) ;
240+ assert ! ( result. is_err( ) ) ;
241+ assert_eq ! ( result. unwrap_err( ) , "block_width must be a number" ) ;
242+ }
243+
244+ #[ test]
245+ fn test_validate_block_width_too_large ( ) {
246+ let result = validate_block_width ( "101" ) ;
247+ assert ! ( result. is_err( ) ) ;
248+ assert_eq ! (
249+ result. unwrap_err( ) ,
250+ "block_widht must be less or equal to 100"
251+ ) ;
252+ }
253+
254+ #[ test]
255+ fn test_validate_overlap_valid ( ) {
256+ let result = validate_overlap ( "10" ) ;
257+ assert ! ( result. is_ok( ) ) ;
258+ assert_eq ! ( result. unwrap( ) , 10 ) ;
259+ }
260+
261+ #[ test]
262+ fn test_validate_overlap_non_number ( ) {
263+ let result = validate_overlap ( "xyz" ) ;
264+ assert ! ( result. is_err( ) ) ;
265+ assert_eq ! ( result. unwrap_err( ) , "overlap must be a number" ) ;
266+ }
267+
268+ #[ test]
269+ fn test_validate_overlap_zero ( ) {
270+ let result = validate_overlap ( "0" ) ;
271+ assert ! ( result. is_err( ) ) ;
272+ assert_eq ! ( result. unwrap_err( ) , "overlap must be between 1 and 20" ) ;
273+ }
274+
275+ #[ test]
276+ fn test_validate_overlap_above_limit ( ) {
277+ let result = validate_overlap ( "21" ) ;
278+ assert ! ( result. is_err( ) ) ;
279+ assert_eq ! ( result. unwrap_err( ) , "overlap must be between 1 and 20" ) ;
280+ }
281+ }
0 commit comments