@@ -431,5 +431,84 @@ class NetrcTests: XCTestCase {
431431 XCTAssertEqual ( netrc. machines [ 1 ] . login, " fred " )
432432 XCTAssertEqual ( netrc. machines [ 1 ] . password, " sunshine4ever " )
433433 }
434- }
435434
435+ func testComments( ) throws {
436+ let content = """
437+ # A comment at the beginning of the line
438+ machine example.com # Another comment
439+ login anonymous # Another comment
440+ password qw#erty # Another comment
441+ """
442+
443+ let netrc = try NetrcParser . parse ( content)
444+
445+ let machine = netrc. machines. first
446+ XCTAssertEqual ( machine? . name, " example.com " )
447+ XCTAssertEqual ( machine? . login, " anonymous " )
448+ XCTAssertEqual ( machine? . password, " qw#erty " )
449+ }
450+
451+ // TODO: These permutation tests would be excellent swift-testing parameterized tests.
452+ func testAllHashQuotingPermutations( ) throws {
453+ let cases = [
454+ ( " qwerty " , " qwerty " ) ,
455+ ( " qwe#rty " , " qwe#rty " ) ,
456+ ( " \" qwe#rty \" " , " qwe#rty " ) ,
457+ ( " \" qwe #rty \" " , " qwe #rty " ) ,
458+ ( " \" qwe# rty \" " , " qwe# rty " ) ,
459+ ]
460+
461+ for (testCase, expected) in cases {
462+ let content = """
463+ machine example.com
464+ login \( testCase)
465+ password \( testCase)
466+ """
467+ let netrc = try NetrcParser . parse ( content)
468+
469+ let machine = netrc. machines. first
470+ XCTAssertEqual ( machine? . name, " example.com " )
471+ XCTAssertEqual ( machine? . login, expected, " Expected login \( testCase) to parse as \( expected) " )
472+ XCTAssertEqual ( machine? . password, expected, " Expected \( testCase) to parse as \( expected) " )
473+ }
474+ }
475+
476+ func testAllCommentPermutations( ) throws {
477+ let cases = [
478+ ( " qwerty # a comment " , " qwerty " ) ,
479+ ( " qwe#rty # a comment " , " qwe#rty " ) ,
480+ ( " \" qwe#rty \" # a comment " , " qwe#rty " ) ,
481+ ( " \" qwe #rty \" # a comment " , " qwe #rty " ) ,
482+ ( " \" qwe# rty \" # a comment " , " qwe# rty " ) ,
483+ ]
484+
485+ for (testCase, expected) in cases {
486+ let content = """
487+ machine example.com
488+ login \( testCase)
489+ password \( testCase)
490+ """
491+ let netrc = try NetrcParser . parse ( content)
492+
493+ let machine = netrc. machines. first
494+ XCTAssertEqual ( machine? . name, " example.com " )
495+ XCTAssertEqual ( machine? . login, expected, " Expected login \( testCase) to parse as \( expected) " )
496+ XCTAssertEqual ( machine? . password, expected, " Expected password \( testCase) to parse as \( expected) " )
497+ }
498+ }
499+
500+ func testQuotedMachine( ) throws {
501+ let content = """
502+ machine " example.com "
503+ login anonymous
504+ password qwerty
505+ """
506+
507+ let netrc = try NetrcParser . parse ( content)
508+
509+ let machine = netrc. machines. first
510+ XCTAssertEqual ( machine? . name, " example.com " )
511+ XCTAssertEqual ( machine? . login, " anonymous " )
512+ XCTAssertEqual ( machine? . password, " qwerty " )
513+ }
514+ }
0 commit comments