@@ -3,7 +3,7 @@ import XCTest
33@testable import Assertions
44
55struct TestError : Error , Equatable {
6- let payload = Int . random ( in: 0 ..< 100 )
6+ var payload = Int . random ( in: 0 ..< 100 )
77}
88
99struct TestStruct : Equatable {
@@ -1803,8 +1803,11 @@ final class AssertionsTests: XCTestCase {
18031803 }
18041804
18051805 func testAssertNoThrow( ) throws {
1806+ let expectedResult = Int . random ( in: 0 ..< 100 )
1807+
18061808 do {
1807- try assertNoThrow { }
1809+ let result = try assertNoThrow { expectedResult }
1810+ XCTAssertEqual ( expectedResult, result)
18081811 } catch {
18091812 XCTFail ( " Did not expect a throw " )
18101813 return
@@ -1841,4 +1844,244 @@ final class AssertionsTests: XCTestCase {
18411844 XCTAssertEqual ( testMessage, error. debugDescription)
18421845 }
18431846 }
1847+
1848+ func testAssertThrowsError( ) throws {
1849+ let testError = TestError ( )
1850+
1851+ do {
1852+ var handledError : ( any Error ) ? = nil
1853+
1854+ try assertThrowsError {
1855+ throw testError
1856+ } errorHandler: { error in
1857+ handledError = error
1858+ }
1859+
1860+ XCTAssertEqual ( handledError as? TestError , testError)
1861+ } catch {
1862+ XCTFail ( " Did not expect a throw " )
1863+ return
1864+ }
1865+
1866+ do {
1867+ try assertThrowsError {
1868+ throw testError
1869+ }
1870+ } catch {
1871+ XCTFail ( " Did not expect a throw " )
1872+ return
1873+ }
1874+
1875+ do {
1876+ try assertThrowsError { 5 }
1877+ XCTFail ( " Expected a throw " )
1878+ } catch {
1879+ guard let error = error as? Fail else {
1880+ XCTFail ( " Expected a `Fail` error " )
1881+ return
1882+ }
1883+
1884+ XCTAssertEqual ( " Expression did not throw an error " , error. debugDescription)
1885+ }
1886+
1887+ let testMessage = " Test Message "
1888+
1889+ do {
1890+ try assertThrowsError (
1891+ { " " } ,
1892+ { testMessage } ( )
1893+ )
1894+ XCTFail ( " Expected a throw " )
1895+ } catch {
1896+ guard let error = error as? Fail else {
1897+ XCTFail ( " Expected a `Fail` error " )
1898+ return
1899+ }
1900+
1901+ XCTAssertEqual ( testMessage, error. debugDescription)
1902+ }
1903+ }
1904+
1905+ func testAssertThrowsExpectedError( ) throws {
1906+ let testError = TestError ( )
1907+ let otherError = TestError ( payload: testError. payload + 1 )
1908+
1909+ do {
1910+ try assertThrowsError ( expectedError: testError) {
1911+ throw testError
1912+ }
1913+ } catch {
1914+ XCTFail ( " Did not expect a throw " )
1915+ return
1916+ }
1917+
1918+ do {
1919+ try assertThrowsError ( expectedError: testError) {
1920+ throw otherError
1921+ }
1922+ XCTFail ( " Expected a throw " )
1923+ } catch {
1924+ guard let error = error as? Fail else {
1925+ XCTFail ( " Expected a `Fail` error " )
1926+ return
1927+ }
1928+
1929+ XCTAssertEqual ( """
1930+ Values are not equal
1931+
1932+ − TestError(payload: \( otherError. payload) )
1933+ + TestError(payload: \( testError. payload) )
1934+ """ , error. debugDescription)
1935+ }
1936+
1937+ do {
1938+ try assertThrowsError ( expectedError: testError) { 5 }
1939+ XCTFail ( " Expected a throw " )
1940+ } catch {
1941+ guard let error = error as? Fail else {
1942+ XCTFail ( " Expected a `Fail` error " )
1943+ return
1944+ }
1945+
1946+ XCTAssertEqual ( " Expression did not throw an error " , error. debugDescription)
1947+ }
1948+
1949+ let testMessage = " Test Message "
1950+
1951+ do {
1952+ try assertThrowsError (
1953+ expectedError: testError,
1954+ { " " } ,
1955+ { testMessage } ( )
1956+ )
1957+ XCTFail ( " Expected a throw " )
1958+ } catch {
1959+ guard let error = error as? Fail else {
1960+ XCTFail ( " Expected a `Fail` error " )
1961+ return
1962+ }
1963+
1964+ XCTAssertEqual ( testMessage, error. debugDescription)
1965+ }
1966+ }
1967+
1968+ func testAssertThrowsErrorAsync( ) async throws {
1969+ let testError = TestError ( )
1970+
1971+ do {
1972+ var handledError : ( any Error ) ? = nil
1973+
1974+ try await assertThrowsError { ( ) async throws in
1975+ throw testError
1976+ } errorHandler: { error async in
1977+ handledError = error
1978+ }
1979+
1980+ XCTAssertEqual ( handledError as? TestError , testError)
1981+ } catch {
1982+ XCTFail ( " Did not expect a throw " )
1983+ return
1984+ }
1985+
1986+ do {
1987+ try await assertThrowsError { ( ) async throws in
1988+ throw testError
1989+ }
1990+ } catch {
1991+ XCTFail ( " Did not expect a throw " )
1992+ return
1993+ }
1994+
1995+ do {
1996+ try await assertThrowsError { ( ) async throws in 5 }
1997+ XCTFail ( " Expected a throw " )
1998+ } catch {
1999+ guard let error = error as? Fail else {
2000+ XCTFail ( " Expected a `Fail` error " )
2001+ return
2002+ }
2003+
2004+ XCTAssertEqual ( " Expression did not throw an error " , error. debugDescription)
2005+ }
2006+
2007+ let testMessage = " Test Message "
2008+
2009+ do {
2010+ try await assertThrowsError (
2011+ { ( ) async throws in " " } ,
2012+ { testMessage } ( )
2013+ )
2014+ XCTFail ( " Expected a throw " )
2015+ } catch {
2016+ guard let error = error as? Fail else {
2017+ XCTFail ( " Expected a `Fail` error " )
2018+ return
2019+ }
2020+
2021+ XCTAssertEqual ( testMessage, error. debugDescription)
2022+ }
2023+ }
2024+
2025+ func testAssertThrowsExpectedErrorAsync( ) async throws {
2026+ let testError = TestError ( )
2027+ let otherError = TestError ( payload: testError. payload + 1 )
2028+
2029+ do {
2030+ try await assertThrowsError ( expectedError: testError) { ( ) async throws in
2031+ throw testError
2032+ }
2033+ } catch {
2034+ XCTFail ( " Did not expect a throw " )
2035+ return
2036+ }
2037+
2038+ do {
2039+ try await assertThrowsError ( expectedError: testError) { ( ) async throws in
2040+ throw otherError
2041+ }
2042+ XCTFail ( " Expected a throw " )
2043+ } catch {
2044+ guard let error = error as? Fail else {
2045+ XCTFail ( " Expected a `Fail` error " )
2046+ return
2047+ }
2048+
2049+ XCTAssertEqual ( """
2050+ Values are not equal
2051+
2052+ − TestError(payload: \( otherError. payload) )
2053+ + TestError(payload: \( testError. payload) )
2054+ """ , error. debugDescription)
2055+ }
2056+
2057+ do {
2058+ try await assertThrowsError ( expectedError: testError) { ( ) async throws in 5 }
2059+ XCTFail ( " Expected a throw " )
2060+ } catch {
2061+ guard let error = error as? Fail else {
2062+ XCTFail ( " Expected a `Fail` error " )
2063+ return
2064+ }
2065+
2066+ XCTAssertEqual ( " Expression did not throw an error " , error. debugDescription)
2067+ }
2068+
2069+ let testMessage = " Test Message "
2070+
2071+ do {
2072+ try await assertThrowsError (
2073+ expectedError: testError,
2074+ { ( ) async throws in " " } ,
2075+ { testMessage } ( )
2076+ )
2077+ XCTFail ( " Expected a throw " )
2078+ } catch {
2079+ guard let error = error as? Fail else {
2080+ XCTFail ( " Expected a `Fail` error " )
2081+ return
2082+ }
2083+
2084+ XCTAssertEqual ( testMessage, error. debugDescription)
2085+ }
2086+ }
18442087}
0 commit comments