pEpForiOSTests/StringExtensionsTest.swift
author Xavier Algarra <xavier@pep-project.org>
Tue, 06 Jun 2017 07:49:23 +0200
changeset 2248 75013a586bfd
parent 784 03ecceb48c71
child 2673 988c11a42a0f
permissions -rw-r--r--
IOS-137 add multiple accounts option with the new login
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@572
    54
        XCTAssertEqual("Whatever New ".trimmedWhiteSpace(), "Whatever New")
dirk@425
    55
    }
dirk@425
    56
dirk@425
    57
    func testFinishedRecipientPart() {
dirk@425
    58
        XCTAssertEqual("unfinis".finishedRecipientPart(), "")
dirk@425
    59
        XCTAssertEqual("finished1,".finishedRecipientPart(), "finished1")
dirk@425
    60
        XCTAssertEqual("finished, unfinis".finishedRecipientPart(), "finished")
dirk@425
    61
        XCTAssertEqual("finished1, finished2, unfinis".finishedRecipientPart(),
dirk@425
    62
                       "finished1, finished2")
dirk@425
    63
        XCTAssertEqual("finished1, finished2, finished3, non terminado".finishedRecipientPart(),
dirk@425
    64
                       "finished1, finished2, finished3")
dirk@425
    65
    }
dirk@426
    66
dirk@426
    67
    func testMatchesPattern() {
dirk@506
    68
        XCTAssertTrue("uiaeuiae, ".matchesPattern(", $"))
dirk@506
    69
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(", $"))
dirk@506
    70
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(",\\w*$"))
dirk@506
    71
        XCTAssertTrue("uiaeuiae,".matchesPattern(",\\s*$"))
dirk@506
    72
        XCTAssertTrue("uiaeuiae, ".matchesPattern(",\\s*$"))
dirk@506
    73
        XCTAssertTrue("uiaeuiae,  ".matchesPattern(",\\s*$"))
dirk@506
    74
        XCTAssertFalse("uiaeuiae,  .".matchesPattern(",\\s*$"))
dirk@506
    75
dirk@506
    76
        let whiteSpacePattern = "^\\s*$"
dirk@506
    77
        XCTAssertTrue("".matchesPattern(whiteSpacePattern))
dirk@506
    78
        XCTAssertTrue("   ".matchesPattern(whiteSpacePattern))
dirk@506
    79
        XCTAssertFalse(" uiae  ".matchesPattern(whiteSpacePattern))
dirk@506
    80
    }
dirk@506
    81
dirk@506
    82
    func testIsOnlyWhitespace() {
dirk@506
    83
        XCTAssertTrue("   ".isOnlyWhiteSpace())
dirk@506
    84
        XCTAssertTrue("".isOnlyWhiteSpace())
dirk@506
    85
        XCTAssertFalse(" ui ".isOnlyWhiteSpace())
dirk@426
    86
    }
dirk@430
    87
dirk@430
    88
    func testRemoveTrailingPattern() {
dirk@430
    89
        XCTAssertEqual("just@email1.com, ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    90
        XCTAssertEqual("just@email1.com,   ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    91
    }
dirk@430
    92
dirk@506
    93
    func testRemoveLeadingPattern() {
dirk@506
    94
        XCTAssertEqual("To: test005@peptest.ch".removeLeadingPattern("\\w*:\\s*"),
dirk@506
    95
                       "test005@peptest.ch")
dirk@506
    96
    }
dirk@506
    97
dirk@430
    98
    func testIsProbablyValidEmailList() {
dirk@430
    99
        XCTAssertFalse("email1, email2".isProbablyValidEmailListSeparatedBy(","))
dirk@430
   100
        XCTAssertTrue("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(","))
dirk@430
   101
        XCTAssertTrue("email1@test.com; email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
   102
        XCTAssertFalse("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
   103
        XCTAssertFalse(
dirk@430
   104
            "email1@test.com, email2@test.com, email, test@com".isProbablyValidEmailListSeparatedBy(
dirk@430
   105
            ","))
dirk@430
   106
    }
dirk@437
   107
dirk@437
   108
    func testComponentsSeparatedBy() {
dirk@437
   109
        // This behavior is somewhat odd. Want to make sure it's documented.
dirk@784
   110
        XCTAssertEqual("".components(separatedBy: ","), [""])
dirk@437
   111
    }
dirk@527
   112
dirk@527
   113
    func testRemoveAngleBrackets() {
dirk@527
   114
        XCTAssertEqual("messageid".removeAngleBrackets(), "messageid")
dirk@527
   115
        XCTAssertEqual("<messageid@someserver>".removeAngleBrackets(), "messageid@someserver")
dirk@527
   116
        XCTAssertEqual("  <messageid@someserver>  ".removeAngleBrackets(),
dirk@527
   117
                       "messageid@someserver")
dirk@527
   118
    }
dirk@563
   119
dirk@563
   120
    func testExtractTextFromHTML() {
dirk@563
   121
        var html = "<html>\r\n  <head>\r\n\r\n"
dirk@563
   122
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\r\n"
dirk@563
   123
            + "</head>\r\n  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n"
dirk@563
   124
            + "<p>HTML! <b>Yes!</b><br>\r\n"
dirk@563
   125
            + "</p>\r\n  </body>\r\n</html>\r\n"
dirk@563
   126
        XCTAssertEqual(html.extractTextFromHTML(), "HTML! Yes!")
dirk@563
   127
dirk@563
   128
        html = "<html>\r\n  <head>\r\n\r\n"
dirk@563
   129
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\r\n"
dirk@563
   130
            + "</head>\r\n  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n"
dirk@563
   131
            + "<p>HTML! <b>Yes!</b><br>\r\n"
dirk@563
   132
            + "</p><p>Whatever. New paragraph.</p>\r\n  </body>\r\n</html>\r\n"
dirk@563
   133
        XCTAssertEqual(html.extractTextFromHTML(), "HTML! Yes! Whatever. New paragraph.")
dirk@572
   134
dirk@572
   135
        html = "<html>\r\n  <head>\r\n\r\n"
dirk@572
   136
            + "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\r\n"
dirk@572
   137
            + "</head>\r\n  <body bgcolor=\"#FFFFFF\" text=\"#000000\">\r\n"
dirk@572
   138
            + "<p>HTML! <b>Yes!</b><br>\r\n"
dirk@572
   139
            + "</p><p>Whatever. New <b>bold</b> paragraph.</p>\r\n  </body>\r\n</html>\r\n"
dirk@572
   140
        XCTAssertEqual(
dirk@572
   141
            html.extractTextFromHTML(), "HTML! Yes! Whatever. New bold paragraph.")
dirk@563
   142
    }
dirk@784
   143
}