<commit_before>// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import Benchmark

final class BenchmarkReporterTests: XCTestCase {
    func assertConsoleReported(_ results: [BenchmarkResult], _ expected: String) {
        let output = MockTextOutputStream()
        var reporter = ConsoleReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected)
    }

    func assertJSONReported(_ results: [BenchmarkResult], _ expected: String) {
        let output = MockTextOutputStream()
        var reporter = JSONReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected)
    }

    func assertCSVReported(
        _ results: [BenchmarkResult], _ expected: String
    ) {
        let output = MockTextOutputStream()
        var reporter = CSVReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected)
    }

    func assertReported(_ got: String, _ expected: String) {
        let lines = Array(got.split(separator: "\n").map { String($0) })
        let expectedLines = expected.split(separator: "\n").map { String($0) }
        let actual = lines.map { $0.trimmingCharacters(in: .newlines) }
            .filter { !$0.isEmpty }
        XCTAssertEqual(expectedLines.count, actual.count)
        for (expectedLine, actualLine) in zip(expectedLines, actual) {
            XCTAssertEqual(expectedLine, actualLine)
        }
    }

    func testConsoleBasic() throws {
        let results: [BenchmarkResult] = [
            BenchmarkResult(
                benchmarkName: "fast", suiteName: "MySuite",
                settings: BenchmarkSettings(),
                measurements: [1_000, 2_000],
                warmupMeasurements: [],
                counters: [:]),
            BenchmarkResult(
                benchmarkName: "slow", suiteName: "MySuite",
                settings: BenchmarkSettings(),
                measurements: [1_000_000, 2_000_000],
                warmupMeasurements: [],<commit_msg>Add `_:file:line:` arguments to assert functions (#80)<commit_after>// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import XCTest

@testable import Benchmark

final class BenchmarkReporterTests: XCTestCase {
    func assertConsoleReported(
        _ results: [BenchmarkResult],
        _ expected: String,
        _ message: @autoclosure () -> String = "",
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        let output = MockTextOutputStream()
        var reporter = ConsoleReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected, message(), file: file, line: line)
    }

    func assertJSONReported(
        _ results: [BenchmarkResult],
        _ expected: String,
        _ message: @autoclosure () -> String = "",
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        let output = MockTextOutputStream()
        var reporter = JSONReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected, message(), file: file, line: line)
    }

    func assertCSVReported(
        _ results: [BenchmarkResult],
        _ expected: String,
        _ message: @autoclosure () -> String = "",
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        let output = MockTextOutputStream()
        var reporter = CSVReporter(output: output)
        reporter.report(results: results)
        assertReported(output.result(), expected, message(), file: file, line: line)
    }

    func assertReported(
        _ got: String,
        _ expected: String,
        _ message: @autoclosure () -> String = "",
        file: StaticString = #filePath,
        line: UInt = #line
    ) {
        let lines = Array(got.split(separator: "\n").map { String($0) })
        let expectedLines = expected.split(separator: "\n").map { String($0) }
        let actual = lines.map { $0.trimmingCharacters(in: .newlines) }
            .filter { !$0.isEmpty }
        XCTAssertEqual(expectedLines.count, actual.count, message(), file: file, line: line)
        for (expectedLine, actualLine) in zip(expectedLines, actual) {
            XCTAssertEqual(expectedLine, actualLine, message(), file: file, line: line)
        }
    }

    func testConsoleBasic() throws {
        let results: [BenchmarkResult] = [
            BenchmarkResult(
                benchmarkName: "fast", suiteName: "MySuite",
                settings: BenchmarkSettings(),
                measurements: [1_000, 2_000],
                warmupMeasurements: [],
                counters: [:]),
            BenchmarkResult(
                benchmarkName: "slow", suiteName: "MySuite",
                settings: BenchmarkSettings(),
                measurements: [1_000_000, 2_000_000],
                warmupMeasurements: [],
{'fraction_non_alphanumeric': 0.105498509440212, 'fraction_numerical': 0.01159324279562769, 'mean_word_length': 2.5440140845070425, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6980196', 'n_tokens_mistral': 1668, 'n_tokens_neox': 1585, 'n_words': 477}