pEpForiOSTests/StringExtensionsTest.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 01 Aug 2016 14:30:40 +0200
changeset 506 f4c6e78357de
parent 437 4c7e1b466422
child 527 0bf99c95d1c0
permissions -rw-r--r--
Using text view for recipients
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@425
    32
    func testTrimWhiteSpace() {
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@425
    41
    }
dirk@425
    42
dirk@425
    43
    func testFinishedRecipientPart() {
dirk@425
    44
        XCTAssertEqual("unfinis".finishedRecipientPart(), "")
dirk@425
    45
        XCTAssertEqual("finished1,".finishedRecipientPart(), "finished1")
dirk@425
    46
        XCTAssertEqual("finished, unfinis".finishedRecipientPart(), "finished")
dirk@425
    47
        XCTAssertEqual("finished1, finished2, unfinis".finishedRecipientPart(),
dirk@425
    48
                       "finished1, finished2")
dirk@425
    49
        XCTAssertEqual("finished1, finished2, finished3, non terminado".finishedRecipientPart(),
dirk@425
    50
                       "finished1, finished2, finished3")
dirk@425
    51
    }
dirk@426
    52
dirk@426
    53
    func testMatchesPattern() {
dirk@506
    54
        XCTAssertTrue("uiaeuiae, ".matchesPattern(", $"))
dirk@506
    55
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(", $"))
dirk@506
    56
        XCTAssertFalse("uiaeuiae, uiae".matchesPattern(",\\w*$"))
dirk@506
    57
        XCTAssertTrue("uiaeuiae,".matchesPattern(",\\s*$"))
dirk@506
    58
        XCTAssertTrue("uiaeuiae, ".matchesPattern(",\\s*$"))
dirk@506
    59
        XCTAssertTrue("uiaeuiae,  ".matchesPattern(",\\s*$"))
dirk@506
    60
        XCTAssertFalse("uiaeuiae,  .".matchesPattern(",\\s*$"))
dirk@506
    61
dirk@506
    62
        let whiteSpacePattern = "^\\s*$"
dirk@506
    63
        XCTAssertTrue("".matchesPattern(whiteSpacePattern))
dirk@506
    64
        XCTAssertTrue("   ".matchesPattern(whiteSpacePattern))
dirk@506
    65
        XCTAssertFalse(" uiae  ".matchesPattern(whiteSpacePattern))
dirk@506
    66
    }
dirk@506
    67
dirk@506
    68
    func testIsOnlyWhitespace() {
dirk@506
    69
        XCTAssertTrue("   ".isOnlyWhiteSpace())
dirk@506
    70
        XCTAssertTrue("".isOnlyWhiteSpace())
dirk@506
    71
        XCTAssertFalse(" ui ".isOnlyWhiteSpace())
dirk@426
    72
    }
dirk@430
    73
dirk@430
    74
    func testRemoveTrailingPattern() {
dirk@430
    75
        XCTAssertEqual("just@email1.com, ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    76
        XCTAssertEqual("just@email1.com,   ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    77
    }
dirk@430
    78
dirk@506
    79
    func testRemoveLeadingPattern() {
dirk@506
    80
        XCTAssertEqual("To: test005@peptest.ch".removeLeadingPattern("\\w*:\\s*"),
dirk@506
    81
                       "test005@peptest.ch")
dirk@506
    82
    }
dirk@506
    83
dirk@430
    84
    func testIsProbablyValidEmailList() {
dirk@430
    85
        XCTAssertFalse("email1, email2".isProbablyValidEmailListSeparatedBy(","))
dirk@430
    86
        XCTAssertTrue("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(","))
dirk@430
    87
        XCTAssertTrue("email1@test.com; email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
    88
        XCTAssertFalse("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
    89
        XCTAssertFalse(
dirk@430
    90
            "email1@test.com, email2@test.com, email, test@com".isProbablyValidEmailListSeparatedBy(
dirk@430
    91
            ","))
dirk@430
    92
    }
dirk@437
    93
dirk@437
    94
    func testComponentsSeparatedBy() {
dirk@437
    95
        // This behavior is somewhat odd. Want to make sure it's documented.
dirk@437
    96
        XCTAssertEqual("".componentsSeparatedByString(","), [""])
dirk@437
    97
    }
dirk@425
    98
}