<commit_before>//
//  YBSwiftLogTests.swift
//  YouboraLib
//
//  Created by <NAME> on 18/11/2019.
//  Copyright © 2019 NPAW. All rights reserved.
//

import XCTest

@objcMembers open class YBSwiftLogTests : XCTestCase, YBSwiftLogger {

    var delegateCallback: ((String,YBSwiftLogLevel) -> ())? = nil

    func testLogLevel() {
        YBSwiftLog.debugLevel = YBSwiftLogLevel.YBLogLevelError

        XCTAssertEqual(YBSwiftLogLevel.YBLogLevelError, YBSwiftLog.debugLevel)

        YBSwiftLog.debugLevel = YBSwiftLogLevel.YBLogLevelWarning

        XCTAssertEqual(YBSwiftLogLevel.YBLogLevelWarning, YBSwiftLog.debugLevel)
    }

    func testLogCallbacks() {
        var pendingCallbacks = 6

        self.delegateCallback = { message, level in
            pendingCallbacks -= 1
            switch pendingCallbacks {
            case 5:
                XCTAssertTrue(message.contains("requestLog"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelVerbose, level)
            case 4:
                XCTAssertTrue(message.contains("debug"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelDebug, level)
            case 3:
                XCTAssertTrue(message.contains("notice"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelNotice, level)
            case 2:
                XCTAssertTrue(message.contains("warn"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelWarning, level)
            case 1:
                XCTAssertTrue(message.contains("error"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelError, level)
            case 0:
                XCTAssertTrue(message.contains("Exception"))
                XCTAssertEqual(YBSwiftLogLevel.YBLogLevelError, level)
            default:
                XCTFail("unexpected pendingCallbacks")
            }
        }

        YBSwiftLog.addLoggerDelegate(self)
        YBSwiftLog.requestLog("requestLog")
        YBSwiftLog.debug("debug")
        YBSwiftLog.notice("notice")
        YBSwiftLog.warn("warn")
        YBSwiftLog.error("error")
        YBSwiftLog.logException(NSException.init(name: NSExceptionName.init("SampleException"), reason: "SampleReason", userInfo: nil))
    }

    func testAddRemoveLogger() {
        var callbackCount = 0
        self.delegateCallback = { message, level in
            callbackCount += 1
        }

        YBSwiftLog.addLoggerDelegate(self)

        YBSwiftLog.debug("debug") // 1<commit_msg>Improve test code<commit_after>//
//  YBSwiftLogTests.swift
//  YouboraLib
//
//  Created by <NAME> on 18/11/2019.
//  Copyright © 2019 NPAW. All rights reserved.
//

import XCTest

@objcMembers open class YBSwiftLogTests : XCTestCase, YBSwiftLogger {

    var delegateCallback: ((String, YBSwiftLogLevel) -> Void)?

    func testLogLevel() {
        YBSwiftLog.debugLevel = YBSwiftLogLevel.error

        XCTAssertEqual(YBSwiftLogLevel.error, YBSwiftLog.debugLevel)

        YBSwiftLog.debugLevel = YBSwiftLogLevel.warning

        XCTAssertEqual(YBSwiftLogLevel.warning, YBSwiftLog.debugLevel)
    }

    func testLogCallbacks() {
        var pendingCallbacks = 6

        self.delegateCallback = { message, level in
            pendingCallbacks -= 1
            switch pendingCallbacks {
            case 5:
                XCTAssertTrue(message.contains("requestLog"))
                XCTAssertEqual(YBSwiftLogLevel.verbose, level)
            case 4:
                XCTAssertTrue(message.contains("debug"))
                XCTAssertEqual(YBSwiftLogLevel.debug, level)
            case 3:
                XCTAssertTrue(message.contains("notice"))
                XCTAssertEqual(YBSwiftLogLevel.notice, level)
            case 2:
                XCTAssertTrue(message.contains("warn"))
                XCTAssertEqual(YBSwiftLogLevel.warning, level)
            case 1:
                XCTAssertTrue(message.contains("error"))
                XCTAssertEqual(YBSwiftLogLevel.error, level)
            case 0:
                XCTAssertTrue(message.contains("Exception"))
                XCTAssertEqual(YBSwiftLogLevel.error, level)
            default:
                XCTFail("unexpected pendingCallbacks")
            }
        }

        YBSwiftLog.addLoggerDelegate(self)
        YBSwiftLog.requestLog("requestLog")
        YBSwiftLog.debug("debug")
        YBSwiftLog.notice("notice")
        YBSwiftLog.warn("warn")
        YBSwiftLog.error("error")
        YBSwiftLog.logException(NSException.init(name: NSExceptionName.init("SampleException"), reason: "SampleReason", userInfo: nil))
    }

    func testAddRemoveLogger() {
        var callbackCount = 0
        self.delegateCallback = { message, level in
            callbackCount += 1
        }

        YBSwiftLog.addLoggerDelegate(self)

        YBSwiftLog.debug("debug") // 1
{'fraction_non_alphanumeric': 0.09148000837345614, 'fraction_numerical': 0.009629474565626963, 'mean_word_length': 2.2726027397260276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7042321', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1395, 'n_words': 220}