<commit_before>import XCTest
import BonBon

final class ResultTests: XCTestCase {
	// MARK: Setup

	private var value: Int = 1
	private var error: TestError = .init(code: 42)
	private lazy var successResult: Result<Int> = .success(self.value)
	private lazy var failureResult: Result<Int> = .failure(self.error)
	
	// MARK: Unit tests

	func test_whenInitializingWithSomeValueAndNilError_thenItsASuccessWrappingTheValue() {
		let result = Result(value: value, error: nil)
		assertIsSuccessWithSetupValue(result)
	}

	func test_whenInitializingWithNilValueAndSomeError_thenItsAFailureWrappingTheError() {
		let result = Result<Int>(value: nil, error: error)
		assertIsFailureWithSetupError(result)
	}

	func test_whenInitializingWithSomeValueAndSomeError_thenItsASuccessWrappingTheValue() {
		let result = Result(value: value, error: error, allowInconsistentArguments: true)
		assertIsSuccessWithSetupValue(result)
	}

	func test_whenInitializingWithNilValueAndNilError_thenItsAFailure() {
		let result = Result<Int>(value: nil, error: nil, allowInconsistentArguments: true)
		if case .success = result {
			XCTFail("It should be a `failure`.")
		}
	}

	func test_whenHavingASuccess_thenValueIsTheWrappedOne() {
		assertIsSetupValue(successResult.value)
	}

	func test_whenHavingAFailure_thenValueIsNil() {
		XCTAssertEqual(failureResult.value, nil, "The wrapped value should be nil.")
	}

	func test_whenHavingASuccess_thenErrorIsNil() {
		XCTAssert(successResult.error == nil, "The wrapped error should be nil.")
	}

	func test_whenHavingAFailure_thenErrorIsTheWrappedOne() {
		assertIsSetupError(failureResult.error)
	}

	// MARK: - Private utilities

	private struct TestError: Error, Equatable {
		var code: Int = 0
		static func == (lhs: TestError, rhs: TestError) -> Bool {
			return lhs.code == rhs.code<commit_msg>Add error handling interop `Result` tests<commit_after>import XCTest
import BonBon

final class ResultTests: XCTestCase {
	// MARK: Setup

	private var value: Int = 1
	private var error: TestError = .init(code: 42)
	private lazy var successResult: Result<Int> = .success(self.value)
	private lazy var failureResult: Result<Int> = .failure(self.error)
	
	// MARK: Unit tests

	func test_whenInitializingWithSomeValueAndNilError_thenItsASuccessWrappingTheValue() {
		let result = Result(value: value, error: nil)
		assertIsSuccessWithSetupValue(result)
	}

	func test_whenInitializingWithNilValueAndSomeError_thenItsAFailureWrappingTheError() {
		let result = Result<Int>(value: nil, error: error)
		assertIsFailureWithSetupError(result)
	}

	func test_whenInitializingWithSomeValueAndSomeError_thenItsASuccessWrappingTheValue() {
		let result = Result(value: value, error: error, allowInconsistentArguments: true)
		assertIsSuccessWithSetupValue(result)
	}

	func test_whenInitializingWithNilValueAndNilError_thenItsAFailure() {
		let result = Result<Int>(value: nil, error: nil, allowInconsistentArguments: true)
		if case .success = result {
			XCTFail("It should be a `failure`.")
		}
	}

	func test_whenHavingASuccess_thenValueIsTheWrappedOne() {
		assertIsSetupValue(successResult.value)
	}

	func test_whenHavingAFailure_thenValueIsNil() {
		XCTAssertEqual(failureResult.value, nil, "The wrapped value should be nil.")
	}

	func test_whenHavingASuccess_thenErrorIsNil() {
		XCTAssert(successResult.error == nil, "The wrapped error should be nil.")
	}

	func test_whenHavingAFailure_thenErrorIsTheWrappedOne() {
		assertIsSetupError(failureResult.error)
	}

	func test_whenInitializingWithSuccessfulOperation_thenItsASuccess() {
		let operation = { self.value }
		let result = Result(operation)
		assertIsSuccessWithSetupValue(result)
	}

	func test_whenInitializingWithFailingOperation_thenItsAFailure() {
		let operation = { () -> Int in throw self.error }
		let result = Result(operation)
		assertIsFailureWithSetupError(result)
	}

	func test_whenUnwrappingASuccess_thenItReturnsTheWrappedValue() {
		do {
			assertIsSetupValue(try successResult.unwrap())
		} catch {
			XCTFail("The unwrap shouldn't throw.")
		}
	}

	func test_whenUnwrappingAFailure_thenItThrowsTheWrappedError() {
		do {
			_ = try failureResult.unwrap()
			XCTFail("The unwrap should throw.")
		} catch let error {
			assertIsSetupError(error)
		}
	}

	// MARK: - Private utilities

	private struct TestError: Error, Equatable {
		var code: Int = 0
		static func == (lhs: TestError, rhs: TestError) -> Bool {
			return lhs.code == rhs.code
{'fraction_non_alphanumeric': 0.08875872944356837, 'fraction_numerical': 0.0018022077044379365, 'mean_word_length': 5.626865671641791, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990291', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1387, 'n_words': 312}