<commit_before>//
//  Copyright RevenueCat Inc. All Rights Reserved.
//
//  Licensed under the MIT License (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      https://opensource.org/licenses/MIT
//
//  ErrorUtilsTests.swift
//
//  Created by <NAME> on 7/28/22.

import Nimble
import XCTest

@testable import RevenueCat

import StoreKit

class ErrorUtilsTests: TestCase {

    private var testLogHandler: TestLogHandler!

    override func setUp() {
        super.setUp()

        self.testLogHandler = TestLogHandler()
    }

    override func tearDown() {
        self.testLogHandler = nil

        super.tearDown()
    }

    func testPublicErrorsCanBeConvertedToErrorCode() throws {
        let error = ErrorUtils.customerInfoError().asPublicError
        let errorCode = try XCTUnwrap(error as? ErrorCode, "Error couldn't be converted to ErrorCode")

        expect(errorCode).to(matchError(error as Error))
    }

    func testPurchaseErrorsAreLoggedAsApppleErrors() {
        let underlyingError = NSError(domain: SKErrorDomain, code: SKError.Code.paymentInvalid.rawValue)
        let error = ErrorUtils.purchaseNotAllowedError(error: underlyingError)

        self.expectLoggedError(error, .appleError)
    }

    func testNetworkErrorsAreLogged() {
        let error = ErrorUtils.networkError(message: Strings.network.could_not_find_cached_response.description)

        self.expectLoggedError(error, .rcError, .networkError)
    }
<commit_msg>`ErrorUtils`: added test to verify that `PublicError`s can be `catch`'d as `ErrorCode` (#1924)<commit_after>//
//  Copyright RevenueCat Inc. All Rights Reserved.
//
//  Licensed under the MIT License (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      https://opensource.org/licenses/MIT
//
//  ErrorUtilsTests.swift
//
//  Created by <NAME> on 7/28/22.

import Nimble
import XCTest

@testable import RevenueCat

import StoreKit

class ErrorUtilsTests: TestCase {

    private var testLogHandler: TestLogHandler!

    override func setUp() {
        super.setUp()

        self.testLogHandler = TestLogHandler()
    }

    override func tearDown() {
        self.testLogHandler = nil

        super.tearDown()
    }

    func testPublicErrorsCanBeConvertedToErrorCode() throws {
        let error = ErrorUtils.customerInfoError().asPublicError
        let errorCode = try XCTUnwrap(error as? ErrorCode, "Error couldn't be converted to ErrorCode")

        expect(errorCode).to(matchError(error as Error))
    }

    func testPublicErrorsCanBeCaughtAsErrorCode() throws {
        func throwing() throws {
            throw ErrorUtils.customerInfoError().asPublicError
        }

        do {
            try throwing()
            fail("Expected error")
        } catch let error as ErrorCode {
            expect(error).to(matchError(ErrorCode.customerInfoError))
        } catch let error {
            fail("Invalid error: \(error)")
        }
    }

    func testPublicErrorsContainUnderlyingError() throws {
        let underlyingError = ErrorUtils.offlineConnectionError().asPublicError

        func throwing() throws {
            throw ErrorUtils.customerInfoError(error: underlyingError).asPublicError
        }

        do {
            try throwing()
            fail("Expected error")
        } catch let error as NSError {
            expect(error).to(matchError(ErrorCode.customerInfoError))
            expect(error.userInfo[NSUnderlyingErrorKey] as? NSError) == underlyingError
        }
    }

    func testPurchaseErrorsAreLoggedAsApppleErrors() {
        let underlyingError = NSError(domain: SKErrorDomain, code: SKError.Code.paymentInvalid.rawValue)
        let error = ErrorUtils.purchaseNotAllowedError(error: underlyingError)

        self.expectLoggedError(error, .appleError)
    }

    func testNetworkErrorsAreLogged() {
        let error = ErrorUtils.networkError(message: Strings.network.could_not_find_cached_response.description)

        self.expectLoggedError(error, .rcError, .networkError)
    }

{'fraction_non_alphanumeric': 0.08962037481979818, 'fraction_numerical': 0.003363767419509851, 'mean_word_length': 2.912593984962406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '4547723', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1138, 'n_words': 292}