pEpForiOSTests/StringExtensionsTest.swift
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 11 Aug 2016 14:51:26 +0200
changeset 563 c939990d4e4f
parent 527 0bf99c95d1c0
child 572 3e4f24f3cb48
permissions -rw-r--r--
IOS-121 HTMLConvertOperation testing
dirk@425
     1
//
dirk@425
     2
//  StringExtensionsTest.swift
dirk@425
     3
//  pEpForiOS
dirk@425
     4
//
dirk@425
     5
//  Created by Dirk Zimmermann on 13/07/16.
dirk@425
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@425
     7
//
dirk@425
     8
dirk@425
     9
import XCTest
dirk@425
    10
dirk@425
    11
import pEpForiOS
dirk@425
    12
dirk@425
    13
class StringExtensionsTest: XCTestCase {
dirk@430
    14
    func testValidEmail() {
dirk@430
    15
        XCTAssertFalse("".isProbablyValidEmail())
dirk@430
    16
        XCTAssertFalse("whe@@@uiae".isProbablyValidEmail())
dirk@430
    17
        XCTAssertTrue("whe@uiae".isProbablyValidEmail())
dirk@430
    18
        XCTAssertTrue("w@u".isProbablyValidEmail())
dirk@430
    19
        XCTAssertFalse("whe@uiae, whe@uiae".isProbablyValidEmail())
dirk@430
    20
        XCTAssertFalse("wh,e@uiae".isProbablyValidEmail())
dirk@430
    21
    }
dirk@430
    22
dirk@430
    23
    func testUnquote() {
dirk@430
    24
        let blah1 = "blah1"
dirk@430
    25
        XCTAssertEqual(blah1.unquote(), blah1)
dirk@430
    26
        XCTAssertEqual("\"uiaeuiae".unquote(), "\"uiaeuiae")
dirk@430
    27
        XCTAssertEqual("\"uiaeuiae\"".unquote(), "uiaeuiae")
dirk@430
    28
        XCTAssertEqual("\"\"".unquote(), "")
dirk@430
    29
        XCTAssertEqual("uiae\"uiaeuiae\"".unquote(), "uiae\"uiaeuiae\"")
dirk@430
    30
    }
dirk@430
    31
dirk@563
    32
    func testTrimmedWhiteSpace() {
dirk@425
    33
        XCTAssertEqual("".trimmedWhiteSpace(), "")
dirk@437
    34
        XCTAssertEqual(" ".trimmedWhiteSpace(), "")
dirk@437
    35
        XCTAssertEqual("   ".trimmedWhiteSpace(), "")
dirk@425
    36
        XCTAssertEqual("    abc".trimmedWhiteSpace(), "abc")
dirk@425
    37
        XCTAssertEqual("    abc\t".trimmedWhiteSpace(), "abc")
dirk@425
    38
        XCTAssertEqual("    abc \t ".trimmedWhiteSpace(), "abc")
dirk@425
    39
        XCTAssertEqual("abc   ".trimmedWhiteSpace(), "abc")
dirk@425
    40
        XCTAssertEqual(" finished2".trimmedWhiteSpace(), "finished2")
dirk@563
    41
dirk@563
    42
        XCTAssertEqual("".trimmedWhiteSpace(), "")
dirk@563
    43
        XCTAssertEqual(" ".trimmedWhiteSpace(), "")
dirk@563
    44
        XCTAssertEqual("uze".trimmedWhiteSpace(), "uze")
dirk@563
    45
        XCTAssertEqual("\nuze".trimmedWhiteSpace(), "uze")
dirk@563
    46
        XCTAssertEqual("\nuze\r\n".trimmedWhiteSpace(), "uze")
dirk@563
    47
        XCTAssertEqual("\r\n\nuze\r\n".trimmedWhiteSpace(), "uze")
dirk@563
    48
        XCTAssertEqual("\n\r\n\nuze\r\n".trimmedWhiteSpace(), "uze")
dirk@563
    49
dirk@563
    50
        XCTAssertEqual("\r\n\r\n\nuze\r\n".trimmedWhiteSpace(), "uze")
dirk@563
    51
        XCTAssertEqual("\r\n\r\n\r\n\nuze\r\n".trimmedWhiteSpace(), "uze")
dirk@563
    52
        XCTAssertEqual("\r\n\r\n\r\nuze".trimmedWhiteSpace(), "uze")
dirk@563
    53
        XCTAssertEqual("\r\n\r\n\r\n\nuze\r\n\r\r\r\n\r\n".trimmedWhiteSpace(), "uze")
dirk@425
    54
    }
dirk@425
    55
dirk@425
    56
    func testFinishedRecipientPart() {
dirk@425
    57
        XCTAssertEqual("unfinis".finishedRecipientPart(), "")
dirk@425
    58
        XCTAssertEqual("finished1,".finishedRecipientPart(), "finished1")
dirk@425
    59
        XCTAssertEqual("finished, unfinis".finishedRecipientPart(), "finished")
dirk@425
    60
        XCTAssertEqual("finished1, finished2, unfinis".finishedRecipientPart(),
dirk@425
    61
                       "finished1, finished2")
dirk@425
    62
        XCTAssertEqual("finished1, finished2, finished3, non terminado".finishedRecipientPart(),
dirk@425
    63
                       "finished1, finished2, finished3")
dirk@425
    64
    }
dirk@426
    65
dirk@426
    66
    func testMatchesPattern() {
dirk@506
    67
        XCTAssertTrue("uiaeuiae, ".matchesPattern(", $"))
dirk@506
    68
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(", $"))
dirk@506
    69
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(",\\w*$"))
dirk@506
    70
        XCTAssertTrue("uiaeuiae,".matchesPattern(",\\s*$"))
dirk@506
    71
        XCTAssertTrue("uiaeuiae, ".matchesPattern(",\\s*$"))
dirk@506
    72
        XCTAssertTrue("uiaeuiae,  ".matchesPattern(",\\s*$"))
dirk@506
    73
        XCTAssertFalse("uiaeuiae,  .".matchesPattern(",\\s*$"))
dirk@506
    74
dirk@506
    75
        let whiteSpacePattern = "^\\s*$"
dirk@506
    76
        XCTAssertTrue("".matchesPattern(whiteSpacePattern))
dirk@506
    77
        XCTAssertTrue("   ".matchesPattern(whiteSpacePattern))
dirk@506
    78
        XCTAssertFalse(" uiae  ".matchesPattern(whiteSpacePattern))
dirk@506
    79
    }
dirk@506
    80
dirk@506
    81
    func testIsOnlyWhitespace() {
dirk@506
    82
        XCTAssertTrue("   ".isOnlyWhiteSpace())
dirk@506
    83
        XCTAssertTrue("".isOnlyWhiteSpace())
dirk@506
    84
        XCTAssertFalse(" ui ".isOnlyWhiteSpace())
dirk@426
    85
    }
dirk@430
    86
dirk@430
    87
    func testRemoveTrailingPattern() {
dirk@430
    88
        XCTAssertEqual("just@email1.com, ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    89
        XCTAssertEqual("just@email1.com,   ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    90
    }
dirk@430
    91
dirk@506
    92
    func testRemoveLeadingPattern() {
dirk@506
    93
        XCTAssertEqual("To: test005@peptest.ch".removeLeadingPattern("\\w*:\\s*"),
dirk@506
    94
                       "test005@peptest.ch")
dirk@506
    95
    }
dirk@506
    96
dirk@430
    97
    func testIsProbablyValidEmailList() {
dirk@430
    98
        XCTAssertFalse("email1, email2".isProbablyValidEmailListSeparatedBy(","))
dirk@430
    99
        XCTAssertTrue("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(","))
dirk@430
   100
        XCTAssertTrue("email1@test.com; email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
   101
        XCTAssertFalse("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
   102
        XCTAssertFalse(
dirk@430
   103
            "email1@test.com, email2@test.com, email, test@com".isProbablyValidEmailListSeparatedBy(
dirk@430
   104
            ","))
dirk@430
   105
    }
dirk@437
   106
dirk@437
   107
    func testComponentsSeparatedBy() {
dirk@437
   108
        // This behavior is somewhat odd. Want to make sure it's documented.
dirk@437
   109
        XCTAssertEqual("".componentsSeparatedByString(","), [""])
dirk@437
   110
    }
dirk@527
   111
dirk@527
   112
    func testRemoveAngleBrackets() {
dirk@527
   113
        XCTAssertEqual("messageid".removeAngleBrackets(), "messageid")
dirk@527
   114
        XCTAssertEqual("<messageid@someserver>".removeAngleBrackets(), "messageid@someserver")
dirk@527
   115
        XCTAssertEqual("  <messageid@someserver>  ".removeAngleBrackets(),
dirk@527
   116
                       "messageid@someserver")
dirk@527
   117
    }
dirk@563
   118
dirk@563
   119
    func testExtractTextFromHTML() {
dirk@563
   120
        var html = "<html>\r\n  <head>\r\n\r\n"
dirk@563
   121
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\r\n"
dirk@563
   122
            + "</head>\r\n  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n"
dirk@563
   123
            + "<p>HTML! <b>Yes!</b><br>\r\n"
dirk@563
   124
            + "</p>\r\n  </body>\r\n</html>\r\n"
dirk@563
   125
        XCTAssertEqual(html.extractTextFromHTML(), "HTML! Yes!")
dirk@563
   126
dirk@563
   127
        html = "<html>\r\n  <head>\r\n\r\n"
dirk@563
   128
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\r\n"
dirk@563
   129
            + "</head>\r\n  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n"
dirk@563
   130
            + "<p>HTML! <b>Yes!</b><br>\r\n"
dirk@563
   131
            + "</p><p>Whatever. New paragraph.</p>\r\n  </body>\r\n</html>\r\n"
dirk@563
   132
        XCTAssertEqual(html.extractTextFromHTML(), "HTML! Yes! Whatever. New paragraph.")
dirk@563
   133
    }
dirk@527
   134
}