diff --git a/exercises/rna-transcription/Sources/RnaTranscriptionExample.swift b/exercises/rna-transcription/Sources/RnaTranscriptionExample.swift index 78b88018f..3968390b0 100644 --- a/exercises/rna-transcription/Sources/RnaTranscriptionExample.swift +++ b/exercises/rna-transcription/Sources/RnaTranscriptionExample.swift @@ -1,5 +1,5 @@ enum TranscriptionError: Error { - case invalidNucleotide + case invalidNucleotide(String) } struct Nucleotide { @@ -19,7 +19,7 @@ struct Nucleotide { var tempText = "" for each in self.value.characters { if dict[each] == nil { - throw TranscriptionError.invalidNucleotide + throw TranscriptionError.invalidNucleotide("\(each) is not a valid Nucleotide") } tempText += dict[each] ?? "" } diff --git a/exercises/rna-transcription/Tests/RnaTranscriptionTests/RnaTranscriptionTests.swift b/exercises/rna-transcription/Tests/RnaTranscriptionTests/RnaTranscriptionTests.swift index 567927bc8..0e3d7fba7 100644 --- a/exercises/rna-transcription/Tests/RnaTranscriptionTests/RnaTranscriptionTests.swift +++ b/exercises/rna-transcription/Tests/RnaTranscriptionTests/RnaTranscriptionTests.swift @@ -23,30 +23,33 @@ class RnaTranscriptionTests: XCTestCase { } func testInvalidRnaComplementOfUracil() { - XCTAssertThrowsError(try Nucleotide("U").complementOfDNA()) - - // Uncomment to see more specific error handling -// XCTAssertThrowsError(try Nucleotide("U").complementOfDNA(), "This didn't work") { (error) in -// XCTAssertEqual(error as? RnaTranscription.TranscriptionError, RnaTranscription.TranscriptionError.invalidNucleotide) -// } + XCTAssertThrowsError(try Nucleotide("U").complementOfDNA()) { (error) in + if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error { + XCTAssertTrue(message == "U is not a valid Nucleotide") + } else { + XCTFail("Expected error not thrown") + } + } } func testInvalidRnaComplementOfXXX() { - XCTAssertThrowsError(try Nucleotide("XXX").complementOfDNA()) - - // Uncomment to see more specific error handling -// XCTAssertThrowsError(try Nucleotide("XXX").complementOfDNA(), "This didn't work") { (error) in -// XCTAssertEqual(error as? RnaTranscription.TranscriptionError, RnaTranscription.TranscriptionError.invalidNucleotide) -// } + XCTAssertThrowsError(try Nucleotide("XXX").complementOfDNA()) { (error) in + if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error { + XCTAssertTrue(message == "X is not a valid Nucleotide") + } else { + XCTFail("Expected error not thrown") + } + } } func testInvalidRnaComplementOfACGTXXXCTTAA() { - XCTAssertThrowsError(try Nucleotide("ACGTXXXCTTAA").complementOfDNA()) - - // Uncomment to see more specific error handling -// XCTAssertThrowsError(try Nucleotide("ACGTXXXCTTAA").complementOfDNA(), "This didn't work") { (error) in -// XCTAssertEqual(error as? RnaTranscription.TranscriptionError, RnaTranscription.TranscriptionError.invalidNucleotide) -// } + XCTAssertThrowsError(try Nucleotide("ACGTXXXCTTAA").complementOfDNA()) { error in + if case let RnaTranscription.TranscriptionError.invalidNucleotide(message) = error { + XCTAssertTrue(message == "X is not a valid Nucleotide") + } else { + XCTFail("Expected error not thrown") + } + } } static var allTests: [(String, (RnaTranscriptionTests) -> () throws -> Void)] {