theextremeprogrammer / Succinct

UI tests at the speed of unit tests. Proper encapsulation. Architecture agnostic. Freedom to refactor.
MIT License
42 stars 7 forks source link

Searching for attributed text crashes when the text does not exist #23

Closed theextremeprogrammer closed 5 years ago

theextremeprogrammer commented 5 years ago

There's a forced unwrapped optional when looking for the search string in an attributed string:

https://github.com/derekleerock/Succinct/blob/413cda993cbbe6011feec8cdb7a6edf1939a9436/Succinct/NSAttributedString/NSAttributedString%2Battributes.swift#L13

Therefore when a search string that doesn't exist in the attributed string is searched for, the function crashes.

Here's a test that re-creates this issue:

context("when the search string does not exist in the attributed string") {
    it("cannot find the string") {
        let redForegroundColor = [NSAttributedString.Key.foregroundColor : UIColor.red]
        let mutableAttributedString = NSMutableAttributedStringBuilder(withText: "Foreground and Background")
            .withAttributes(
                redForegroundColor,
                range: NSRange(location: 0, length: 10)
            )
            .build()

        let attributedString = mutableAttributedString.copy() as! NSAttributedString
        expect(attributedString.containsExactString("Invalid", withAttributes: redForegroundColor)).to(beTrue())
    }
}

Can you add this test and adjust the code to make it pass? Thanks!