Skip to content

Commit

Permalink
Merge a278894 into 0093d17
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang committed Jun 28, 2024
2 parents 0093d17 + a278894 commit fdf920f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
4 changes: 0 additions & 4 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ fn panic[T]() -> T

fn physical_equal[T](T, T) -> Bool

fn print[T : Show](T) -> Unit

fn print_string(String) -> Unit

fn println[T : Show](T) -> Unit

// Types and methods
Expand Down
6 changes: 2 additions & 4 deletions builtin/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
// See the License for the specific language governing permissions and
// limitations under the License.

fn print_string(s : String) -> Unit = "%print_string"

pub fn println[T : Show](input : T) -> Unit {
print_string(input.to_string())
print_string("\n")
}

pub fn print[T : Show](input : T) -> Unit {
print_string(input.to_string())
}

pub fn to_string(self : Bool) -> String {
if self {
"true"
Expand Down
2 changes: 0 additions & 2 deletions builtin/intrinsics.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ pub fn ignore[T](t : T) -> Unit = "%ignore"

pub fn physical_equal[T](a : T, b : T) -> Bool = "%refeq"

pub fn print_string(s : String) -> Unit = "%print_string"

pub fn abort[T](msg : String) -> T {
ignore(msg)
panic()
Expand Down
55 changes: 38 additions & 17 deletions coverage/coverage.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@
// limitations under the License.

/// The `CoverageCounter` structure is used for keeping track of the number of
/// times each chunk of code is executed. It's not very useful outside of
/// times each chunk of code is executed. It's not very useful outside of
/// generated code.
struct CoverageCounter {
counter : FixedArray[Int]
} derive(Show)

// REMARK: Fow now `@coverage.skip` must have arguments, so we pass an empty string as an workaround.
/// Create a new coverage counter with the given size.
///
/// @coverage.skip
///
/// @coverage.skip ""
pub fn CoverageCounter::new(size : Int) -> CoverageCounter {
{ counter: FixedArray::make(size, 0) }
}

/// Increment the specified tracking index.
///
/// @coverage.skip
///
/// @coverage.skip ""
pub fn CoverageCounter::incr(self : CoverageCounter, idx : Int) -> Unit {
let counter = self.counter[idx]
if counter < 0x7fffffff { // prevent overflow
Expand Down Expand Up @@ -57,7 +58,7 @@ priv enum MList[T] {
let counters : Ref[MList[(String, CoverageCounter)]] = { val: MNil }

/// Add the given counter along its ID to the tracking list.
///
///
/// @coverage.skip
pub fn track(name : String, counter : CoverageCounter) -> Unit {
counters.val = MCons((name, counter), counters.val)
Expand All @@ -67,22 +68,21 @@ trait Output {
output(Self, String) -> Unit
}

type IO Int
priv type StringBuffer Array[String]

fn output(self : IO, content : String) -> Unit {
ignore(self)
print(content)
impl Output for StringBuffer with output(self : StringBuffer, content : String) -> Unit {
self.0.push(content)
}

fn Output::output(self : Buffer, content : String) -> Unit {
self.write_string(content)
}

// TODO: This escape function should belong to the String package, but is
// TODO: This escape function should belong to the String package, but is
// not mature enough, so it's left here temporarily.
/// Escape a string using standard JSON escape sequences to the given buffer,
/// usually for quoting uses.
///
///
/// This method only escapes characters below 0x20 and the following characters:
/// `"`, `'`, `\`.
fn escape_to(s : String, buf : Buffer) -> Unit {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn escape_to(s : String, buf : Buffer) -> Unit {

/// Escape a string using standard C escape sequences and return the escaped string,
/// usually for quoting uses.
///
///
/// This method only escapes characters below 0x20 and the following characters:
/// `"`, `'`, `\`.
pub fn escape(s : String) -> String {
Expand All @@ -134,9 +134,9 @@ pub fn escape(s : String) -> String {
/// 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 -----
/// {
Expand All @@ -147,7 +147,28 @@ pub fn escape(s : String) -> String {
/// ```
pub fn end() -> Unit {
println("----- BEGIN MOONBIT COVERAGE -----")
coverage_log(counters.val, IO::IO(0))
let sbuf = StringBuffer::StringBuffer([])
coverage_log(counters.val, sbuf)
let line_buf = Buffer::new()
for i = 0; i < sbuf.0.length(); i = i + 1 {
let str = sbuf.0[i]
let mut start = 0
for j = 0; j < str.length(); j = j + 1 {
if str[j] == '\n' {
line_buf.write_sub_string(str, start, j)
println(line_buf.to_string())
line_buf.reset()
start = j + 1
}
}
if start < str.length() {
line_buf.write_sub_string(str, start, str.length())
}
}
let trailing_line = line_buf.to_string()
if trailing_line.length() > 0 {
println(trailing_line)
}
println("----- END MOONBIT COVERAGE -----")
}

Expand Down Expand Up @@ -177,5 +198,5 @@ fn coverage_log(
}
MNil, _ => ()
}
println("}")
print("}")
}
2 changes: 0 additions & 2 deletions coverage/coverage.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ impl CoverageCounter {
to_string(Self) -> String
}

type IO

// Traits

// Extension Methods
Expand Down
19 changes: 8 additions & 11 deletions coverage/coverage_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ test "log" {
)
let buf = Buffer::new(size_hint=1024)
coverage_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)
}
inspect(
buf,
content=
#|{ "foo/foo": [1, 0]
#|, "foo/bar": [0, 1]
#|}
,
)?
}

test "escape_to" {
Expand Down

0 comments on commit fdf920f

Please sign in to comment.