pEpForiOSTests/StringExtensionsTest.swift
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 14 Jul 2016 14:25:00 +0200
changeset 430 f1d816d52f40
parent 426 32a6fd67ada0
child 437 4c7e1b466422
permissions -rw-r--r--
Compose: Email validation in UI
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@425
    34
        XCTAssertEqual("    abc".trimmedWhiteSpace(), "abc")
dirk@425
    35
        XCTAssertEqual("    abc\t".trimmedWhiteSpace(), "abc")
dirk@425
    36
        XCTAssertEqual("    abc \t ".trimmedWhiteSpace(), "abc")
dirk@425
    37
        XCTAssertEqual("abc   ".trimmedWhiteSpace(), "abc")
dirk@425
    38
        XCTAssertEqual(" finished2".trimmedWhiteSpace(), "finished2")
dirk@425
    39
    }
dirk@425
    40
dirk@425
    41
    func testFinishedRecipientPart() {
dirk@425
    42
        XCTAssertEqual("unfinis".finishedRecipientPart(), "")
dirk@425
    43
        XCTAssertEqual("finished1,".finishedRecipientPart(), "finished1")
dirk@425
    44
        XCTAssertEqual("finished, unfinis".finishedRecipientPart(), "finished")
dirk@425
    45
        XCTAssertEqual("finished1, finished2, unfinis".finishedRecipientPart(),
dirk@425
    46
                       "finished1, finished2")
dirk@425
    47
        XCTAssertEqual("finished1, finished2, finished3, non terminado".finishedRecipientPart(),
dirk@425
    48
                       "finished1, finished2, finished3")
dirk@425
    49
    }
dirk@426
    50
dirk@426
    51
    func testMatchesPattern() {
dirk@426
    52
        XCTAssertEqual("uiaeuiae, ".matchesPattern(", $"), true)
dirk@426
    53
        XCTAssertEqual("uiaeuiae, uiae".matchesPattern(", $"), false)
dirk@426
    54
        XCTAssertEqual("uiaeuiae, uiae".matchesPattern(",\\w*$"), false)
dirk@426
    55
        XCTAssertEqual("uiaeuiae,".matchesPattern(",\\s*$"), true)
dirk@426
    56
        XCTAssertEqual("uiaeuiae, ".matchesPattern(",\\s*$"), true)
dirk@426
    57
        XCTAssertEqual("uiaeuiae,  ".matchesPattern(",\\s*$"), true)
dirk@426
    58
        XCTAssertEqual("uiaeuiae,  .".matchesPattern(",\\s*$"), false)
dirk@426
    59
    }
dirk@430
    60
dirk@430
    61
    func testRemoveTrailingPattern() {
dirk@430
    62
        XCTAssertEqual("just@email1.com, ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    63
        XCTAssertEqual("just@email1.com,   ".removeTrailingPattern(",\\s*"), "just@email1.com")
dirk@430
    64
    }
dirk@430
    65
dirk@430
    66
    func testIsProbablyValidEmailList() {
dirk@430
    67
        XCTAssertFalse("email1, email2".isProbablyValidEmailListSeparatedBy(","))
dirk@430
    68
        XCTAssertTrue("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(","))
dirk@430
    69
        XCTAssertTrue("email1@test.com; email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
    70
        XCTAssertFalse("email1@test.com, email2@test.com".isProbablyValidEmailListSeparatedBy(";"))
dirk@430
    71
        XCTAssertFalse(
dirk@430
    72
            "email1@test.com, email2@test.com, email, test@com".isProbablyValidEmailListSeparatedBy(
dirk@430
    73
            ","))
dirk@430
    74
    }
dirk@425
    75
}