
use ansi

class TestResults {
    entries: Array[TestEntry] (.{})
    asserts_total: uint (0)
    asserts_passed: uint (0)

    fn submit_entry(entry: TestEntry) {
        let passed = entry.assert_count == entry.assert_passed
        entry.passed = passed
        this.entries.append(entry)

        let row = "> " + entry.name + " "
        if row.bytes > 64 : row = row.part(0, 64)
        while row.bytes < 64 {
            let even = row.bytes % 2 == 0
            row += even ? " " : "."
        }
        row += " "
        if ansi:supported() {
            row += passed ? "✅" : "❌"
        } else {
            row += passed ? "OK" : "FAIL"
        }
        row += "\n"

        print(row)

        each entry.failures as msg {
            println(msg)
        }
    }

    fn finalize() {
        this.asserts_total = 0
        this.asserts_passed = 0
        each this.entries as entry {
            this.asserts_total += entry.assert_count
            this.asserts_passed += entry.assert_passed
        }
    }

    fn passed() bool {
        each this.entries as entry {
            if !entry.passed : return false
        }
        return true
    }

    fn print() {
        this.finalize()

        println("--------- TEST RESULTS ---------")
        println("Asserts: " + this.asserts_total)
        println("Passed: " + this.asserts_passed)
        println("Failed: " + (this.asserts_total - this.asserts_passed))
        println("--------------------------------")
    }
}
