@@ -368,3 +368,67 @@ fn scan_coding_comment(content: &[u8]) -> std::option::Option<Cow<str>> {
368
368
}
369
369
None
370
370
}
371
+
372
+ #[ test]
373
+ fn test_scan_coding_comment ( ) {
374
+ let text = "# encoding: utf-8" ;
375
+ let result = scan_coding_comment ( text. as_bytes ( ) ) ;
376
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
377
+
378
+ let text = "#coding:utf-8" ;
379
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
380
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
381
+
382
+ let text = "# foo\n # encoding: utf-8" ;
383
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
384
+ assert_eq ! ( result, None ) ;
385
+
386
+ let text = "# encoding: latin1 encoding: utf-8" ;
387
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
388
+ assert_eq ! ( result, Some ( "latin1" . into( ) ) ) ;
389
+
390
+ let text = "# encoding: nonsense" ;
391
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
392
+ assert_eq ! ( result, Some ( "nonsense" . into( ) ) ) ;
393
+
394
+ let text = "# coding = utf-8" ;
395
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
396
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
397
+
398
+ let text = "# CODING = utf-8" ;
399
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
400
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
401
+
402
+ let text = "# CoDiNg = utf-8" ;
403
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
404
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
405
+
406
+ let text = "# blah blahblahcoding = utf-8" ;
407
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
408
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
409
+
410
+ // unicode BOM is ignored
411
+ let text = "\u{FEFF} # encoding: utf-8" ;
412
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
413
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
414
+
415
+ let text = "\u{FEFF} # encoding: utf-8" ;
416
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
417
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
418
+
419
+ let text = "#! /usr/bin/env ruby\n # encoding: utf-8" ;
420
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
421
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
422
+
423
+ let text = "\u{FEFF} #! /usr/bin/env ruby\n # encoding: utf-8" ;
424
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
425
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
426
+
427
+ // A #! must be the first thing on a line, otherwise it's a normal comment
428
+ let text = " #! /usr/bin/env ruby encoding = utf-8" ;
429
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
430
+ assert_eq ! ( result, Some ( "utf-8" . into( ) ) ) ;
431
+ let text = " #! /usr/bin/env ruby \n # encoding = utf-8" ;
432
+ let result = scan_coding_comment ( & text. as_bytes ( ) ) ;
433
+ assert_eq ! ( result, None ) ;
434
+ }
0 commit comments