Skip to content

Commit

Permalink
Merge 616c058 into 0394cdd
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jun 13, 2024
2 parents 0394cdd + 616c058 commit 96fc8eb
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 79 deletions.
97 changes: 18 additions & 79 deletions coverage/coverage.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ fn CoverageCounter::debug_write(self : CoverageCounter, buf : Buffer) -> Unit {
buf.write_char(']')
}

test "new_counter" {
let counter = CoverageCounter::new(2)
let result = counter.to_string()
// @assertion.assert_eq(result, "[0, 0]")?
if result != "[0, 0]" {
return Err("Expected: [0, 0], got: " + result)
}
}

test "incr_counter" {
let counter = CoverageCounter::new(10)
counter.incr(0)
counter.incr(9)
let result = counter.to_string()
// @assertion.assert_eq(result, "[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]")?
if result != "[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]" {
return Err("Expected: [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], got: " + result)
}
}

priv enum MList[T] {
MNil
MCons(T, MList[T])
Expand Down Expand Up @@ -145,23 +125,26 @@ pub fn escape(s : String) -> String {
buf.to_string()
}

test "backslash escape" {
let s = "\n\r\t\b\"'\\"
let expected = "\\n\\r\\t\\b\\\"'\\\\"
// @assertion.assert_eq(escape(s), expected)?
let escape_s = escape(s)
if escape_s != expected {
return Err("Expected: " + expected + ", got: " + escape_s)
}
/// Output the counters to stdout for coverage report usages. The counter data
/// is printed as a map of `{ counter_id: counter_contents }`, enclosed between
/// a pair of delimiters.
///
/// An example output of this function is like the following:
///
/// ```plaintext
/// ----- BEGIN MOONBIT COVERAGE -----
/// {
/// "foo/foo": [1, 2, 3, 4]
/// , "foo/bar": [5, 6, 7, 8]
/// }
/// ----- END MOONBIT COVERAGE -----
/// ```
pub fn end() -> Unit {
println("----- BEGIN MOONBIT COVERAGE -----")
log(counters.val, IO::IO(0))
println("----- END MOONBIT COVERAGE -----")
}

// FIXME: formatting string does not work, this test will be destroyed by formatting
// test "hex escape" {
// let s = "\x11\x12\x01\x02"
// let expected = "\\x11\\x12\\x01\\x02"
// @assertion.assert_eq(s.escape(), expected)?
// }

fn log(counters : MList[(String, CoverageCounter)], io : Output) -> Unit {
let print = fn { x => io.output(x) }
let println = fn {
Expand All @@ -187,47 +170,3 @@ fn log(counters : MList[(String, CoverageCounter)], io : Output) -> Unit {
}
println("}")
}

test "log" {
let test_counter_a = CoverageCounter::new(2)
let test_counter_b = CoverageCounter::new(2)
test_counter_a.incr(0)
test_counter_b.incr(1)
let counters : MList[(String, CoverageCounter)] = MCons(
("foo/foo", test_counter_a),
MCons(("foo/bar", test_counter_b), MNil),
)
let buf = Buffer::make(1024)
log(counters, buf)
let result = buf.to_string()
let expected =
#|{ "foo/foo": [1, 0]
#|, "foo/bar": [0, 1]
#|}
#|

// @assertion.assert_eq(result, expected)?
if result != expected {
return Err("Expected: " + expected + ", got: " + result)
}
}

/// Output the counters to stdout for coverage report usages. The counter data
/// is printed as a map of `{ counter_id: counter_contents }`, enclosed between
/// a pair of delimiters.
///
/// An example output of this function is like the following:
///
/// ```plaintext
/// ----- BEGIN MOONBIT COVERAGE -----
/// {
/// "foo/foo": [1, 2, 3, 4]
/// , "foo/bar": [5, 6, 7, 8]
/// }
/// ----- END MOONBIT COVERAGE -----
/// ```
pub fn end() -> Unit {
println("----- BEGIN MOONBIT COVERAGE -----")
log(counters.val, IO::IO(0))
println("----- END MOONBIT COVERAGE -----")
}
74 changes: 74 additions & 0 deletions coverage/coverage_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 International Digital Economy Academy
//
// 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.

test "new_counter" {
let counter = CoverageCounter::new(2)
let result = counter.to_string()
// @assertion.assert_eq(result, "[0, 0]")?
if result != "[0, 0]" {
return Err("Expected: [0, 0], got: " + result)
}
}

test "incr_counter" {
let counter = CoverageCounter::new(10)
counter.incr(0)
counter.incr(9)
let result = counter.to_string()
// @assertion.assert_eq(result, "[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]")?
if result != "[1, 0, 0, 0, 0, 0, 0, 0, 0, 1]" {
return Err("Expected: [1, 0, 0, 0, 0, 0, 0, 0, 0, 1], got: " + result)
}
}

test "backslash escape" {
let s = "\n\r\t\b\"'\\"
let expected = "\\n\\r\\t\\b\\\"'\\\\"
// @assertion.assert_eq(escape(s), expected)?
let escape_s = escape(s)
if escape_s != expected {
return Err("Expected: " + expected + ", got: " + escape_s)
}
}

// FIXME: formatting string does not work, this test will be destroyed by formatting
// test "hex escape" {
// let s = "\x11\x12\x01\x02"
// let expected = "\\x11\\x12\\x01\\x02"
// @assertion.assert_eq(s.escape(), expected)?
// }

test "log" {
let test_counter_a = CoverageCounter::new(2)
let test_counter_b = CoverageCounter::new(2)
test_counter_a.incr(0)
test_counter_b.incr(1)
let counters : MList[(String, CoverageCounter)] = MCons(
("foo/foo", test_counter_a),
MCons(("foo/bar", test_counter_b), MNil),
)
let buf = Buffer::make(1024)
log(counters, buf)
let result = buf.to_string()
let expected =
#|{ "foo/foo": [1, 0]
#|, "foo/bar": [0, 1]
#|}
#|

// @assertion.assert_eq(result, expected)?
if result != expected {
return Err("Expected: " + expected + ", got: " + result)
}
}

0 comments on commit 96fc8eb

Please sign in to comment.