From cf08520c8a2d7b7c8daa5ab396dd6228873e3974 Mon Sep 17 00:00:00 2001 From: jrrom <77691121+jrrom@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:37:39 +0530 Subject: [PATCH 1/4] Added Console.mint --- core/source/Console.mint | 280 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 core/source/Console.mint diff --git a/core/source/Console.mint b/core/source/Console.mint new file mode 100644 index 000000000..5c1dadb9b --- /dev/null +++ b/core/source/Console.mint @@ -0,0 +1,280 @@ +store Console.Counter { + state count : Number = 0 + + fun increment { + next { count: count + 1 } + } + + fun clear { + next { count: 0 } + } +} + +/* Module to work with the [Console API](https://developer.mozilla.org/en-US/docs/Web/API/Console_API). */ +module Console { + /* + If the assertion is false, the message is written to the console. Supports string substitution. + + [assert()](https://developer.mozilla.org/en-US/docs/Web/API/console/assert) + */ + fun assert (assertion : Bool, value : a, values : Array(b) = []) : Tuple(Bool, a, Array(b)) { + `console.assert(#{assertion}, #{value}, ...#{values})` + + {assertion, value, values} + } + + /* + Clears the console. + + [clear()](https://developer.mozilla.org/en-US/docs/Web/API/console/clear) + */ + fun clear : Void { + `console.clear()` + } + + /* + Logs the number of times that this particular call to count() has been called. + Returns the label passed along with the current count. + + [count()](https://developer.mozilla.org/en-US/docs/Web/API/console/count) + */ + fun count (label : String = "Default") : Tuple(String, Number) { + `console.count(#{label})` + Console.Counter.increment() + + {label, Console.Counter.count} + } + + /* + Resets the counter used with Console.count(). + Returns the label passed along with the current count. + + [countReset()](https://developer.mozilla.org/en-US/docs/Web/API/console/countReset) + */ + fun countReset (label : String = "Default") : Tuple(String, Number) { + `console.countReset(#{label})` + Console.Counter.clear() + + {label, Console.Counter.count} + } + + /* + Outputs a message to the Web Console at the "debug" log level. + The message is only displayed to the user if the console is configured to display debug output. + Supports string substitution. + + [debug()](https://developer.mozilla.org/en-US/docs/Web/API/console/debug) + */ + fun debug (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { + `console.debug(#{value}, ...#{values})` + + {value, values} + } + + /* + Displays an interactive list of the properties of the generated JavaScript object. + + [dir()](https://developer.mozilla.org/en-US/docs/Web/API/console/dir) + */ + fun dir (value : a) : a { + `console.dir(#{value})` + + value + } + + /* + Displays an interactive tree of the descendant elements of the specified XML/HTML + element. + + [dirxml()](https://developer.mozilla.org/en-US/docs/Web/API/console/dirxml) + */ + fun dirxml (value : a) : a { + `console.dirxml(#{value})` + + value + } + + /* + Outputs an error message to the Web Console. Supports string substitution. + + [error()](https://developer.mozilla.org/en-US/docs/Web/API/console/error) + */ + fun error (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { + `console.error(#{value}, ...#{values})` + + {value, values} + } + + /* + Creates a new inline group in the Web Console log. + + [group()](https://developer.mozilla.org/en-US/docs/Web/API/console/group) + */ + fun group (label : String = "Default") : String { + `console.group(#{label})` + + label + } + + /* + Creates a new inline group in the Web Console log. The new group is created collapsed. + + [groupCollapsed()](https://developer.mozilla.org/en-US/docs/Web/API/console/groupCollapsed) + */ + fun groupCollapsed (label : String = "Default") : String { + `console.groupCollapsed(#{label})` + + label + } + + /* + Exits the current inline group in the Web Console. + + [groupEnd()](https://developer.mozilla.org/en-US/docs/Web/API/console/groupEnd) + */ + fun groupEnd (label : String = "Default") : String { + `console.groupEnd(#{label})` + + label + } + + /* + Outputs an informational message to the Web Console. Supports string substitution. + + [info()](https://developer.mozilla.org/en-US/docs/Web/API/console/info) + */ + fun info (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { + `console.info(#{value}, ...#{values})` + + {value, values} + } + + /* + Outputs a message to the Web Console. Supports string substitution. + + [log()](https://developer.mozilla.org/en-US/docs/Web/API/console/log) + */ + fun log (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { + `console.log(#{value}, ...#{values})` + + {value, values} + } + + /* + **NON-STANDARD**:\ + Starts recording a performance profile. + + [profile()](https://developer.mozilla.org/en-US/docs/Web/API/console/profile) + */ + fun profile (profileName : String = "Default") : String { + if `!console.profile` { + Debug.log("Your browser does not support console.profile") + } else { + `console.profile(#{profileName})` + } + + profileName + } + + /* + **NON-STANDARD**:\ + Stops recording a performance profile previously started. + + [profileEnd()](https://developer.mozilla.org/en-US/docs/Web/API/console/profileEnd) + */ + fun profileEnd (profileName : String = "Default") : String { + if `!console.profileEnd` { + Debug.log("Your browser does not support console.profileEnd") + } else { + `console.profileEnd(#{profileName})` + } + + profileName + } + + /* + Logs data as a table. + + [table()](https://developer.mozilla.org/en-US/docs/Web/API/console/table) + */ + fun table (data : a, columns : Array(b) = []) : Tuple(a, Array(b)) { + if columns != [] { + `console.table(#{data}, #{columns})` + } else { + `console.table(#{data})` + } + + {data, columns} + } + + /* + Starts a timer. + + [time()](https://developer.mozilla.org/en-US/docs/Web/API/console/time) + */ + fun time (label : String = "Default") : String { + `console.time(#{label})` + + label + } + + /* + Stops a timer that was previously started. + + [timeEnd()](https://developer.mozilla.org/en-US/docs/Web/API/console/timeEnd) + */ + fun timeEnd (label : String = "Default") : String { + `console.timeEnd(#{label})` + + label + } + + /* + Logs the current value of a timer that was previously started. + + [timeLog()](https://developer.mozilla.org/en-US/docs/Web/API/console/timeLog) + */ + fun timeLog (label : String = "Default", values : Array(a) = []) : Tuple(String, Array(a)) { + `console.timeLog(#{label}, ...#{values})` + + {label, values} + } + + /* + **NON-STANDARD**:\ + Adds a single marker to the browser's performance tool. + + [timestamp()](https://developer.mozilla.org/en-US/docs/Web/API/console/timestamp) + */ + fun timestamp (label : String = "Default") : String { + if `!console.timestamp` { + Debug.log("Your browser does not support console.profile") + } else { + `console.timestamp(#{label})` + } + + label + } + + /* + Outputs a stack trace to the Web Console. Does not support string substitution. + + [trace()](https://developer.mozilla.org/en-US/docs/Web/API/console/trace) + */ + fun trace (value : a, values : Array(b)) : Tuple(a, Array(b)) { + `console.trace(#{value}, ...#{values})` + + {value, values} + } + + /* + Outputs a warning message to the Web Console. + + [warn()](https://developer.mozilla.org/en-US/docs/Web/API/console/warn) + */ + fun warn (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { + `console.warn(#{value}, ...#{values})` + + {value, values} + } +} From b324fd8d6fc0167fa05a6861671c165038381440 Mon Sep 17 00:00:00 2001 From: jrrom <77691121+jrrom@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:42:48 +0530 Subject: [PATCH 2/4] Fixed timestamp error message. --- core/source/Console.mint | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/source/Console.mint b/core/source/Console.mint index 5c1dadb9b..f241094df 100644 --- a/core/source/Console.mint +++ b/core/source/Console.mint @@ -248,7 +248,7 @@ module Console { */ fun timestamp (label : String = "Default") : String { if `!console.timestamp` { - Debug.log("Your browser does not support console.profile") + Debug.log("Your browser does not support console.timestamp") } else { `console.timestamp(#{label})` } From daef254e1f89911fa8528c42a4679dcf6f02297a Mon Sep 17 00:00:00 2001 From: jrrom <77691121+jrrom@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:45:56 +0530 Subject: [PATCH 3/4] Small example added. --- core/source/Console.mint | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/source/Console.mint b/core/source/Console.mint index f241094df..e2880ea25 100644 --- a/core/source/Console.mint +++ b/core/source/Console.mint @@ -152,6 +152,8 @@ module Console { /* Outputs a message to the Web Console. Supports string substitution. + Console.log("Hello ", ["World", "!"]) => "Hello World!" + [log()](https://developer.mozilla.org/en-US/docs/Web/API/console/log) */ fun log (value : a, values : Array(b) = []) : Tuple(a, Array(b)) { From 68cb3d4468beed1b8f8ce253102f991ed433d1ae Mon Sep 17 00:00:00 2001 From: jrrom <77691121+jrrom@users.noreply.github.com> Date: Wed, 13 Sep 2023 21:51:13 +0530 Subject: [PATCH 4/4] Made requested changes and added test --- core/source/Console.mint | 40 ++++++++----- core/tests/tests/Console.mint | 105 ++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 core/tests/tests/Console.mint diff --git a/core/source/Console.mint b/core/source/Console.mint index e2880ea25..95617df88 100644 --- a/core/source/Console.mint +++ b/core/source/Console.mint @@ -1,12 +1,23 @@ store Console.Counter { - state count : Number = 0 + state counts : Map(String, Number) = Map.empty() + + fun increment (label : String = "Default") { + next + { + counts: + case Map.has(counts, label) { + false => Map.set(counts, label, 1) + => Map.set(counts, label, Map.getWithDefault(counts, label, 0) + 1) + } + } + } - fun increment { - next { count: count + 1 } + fun clear (label : String = "Default") { + next { counts: Map.set(counts, label, 0) } } - fun clear { - next { count: 0 } + fun get (label : String = "Default") : Number { + Map.getWithDefault(counts, label, 0) } } @@ -40,9 +51,9 @@ module Console { */ fun count (label : String = "Default") : Tuple(String, Number) { `console.count(#{label})` - Console.Counter.increment() + Console.Counter.increment(label) - {label, Console.Counter.count} + {label, Console.Counter.get(label)} } /* @@ -53,9 +64,9 @@ module Console { */ fun countReset (label : String = "Default") : Tuple(String, Number) { `console.countReset(#{label})` - Console.Counter.clear() + Console.Counter.clear(label) - {label, Console.Counter.count} + {label, Console.Counter.get(label)} } /* @@ -163,7 +174,8 @@ module Console { } /* - **NON-STANDARD**:\ + **NON-STANDARD**: + Starts recording a performance profile. [profile()](https://developer.mozilla.org/en-US/docs/Web/API/console/profile) @@ -179,7 +191,8 @@ module Console { } /* - **NON-STANDARD**:\ + **NON-STANDARD**: + Stops recording a performance profile previously started. [profileEnd()](https://developer.mozilla.org/en-US/docs/Web/API/console/profileEnd) @@ -199,7 +212,7 @@ module Console { [table()](https://developer.mozilla.org/en-US/docs/Web/API/console/table) */ - fun table (data : a, columns : Array(b) = []) : Tuple(a, Array(b)) { + fun table (data : a, columns : Array(String) = []) : Tuple(a, Array(String)) { if columns != [] { `console.table(#{data}, #{columns})` } else { @@ -243,7 +256,8 @@ module Console { } /* - **NON-STANDARD**:\ + **NON-STANDARD**: + Adds a single marker to the browser's performance tool. [timestamp()](https://developer.mozilla.org/en-US/docs/Web/API/console/timestamp) diff --git a/core/tests/tests/Console.mint b/core/tests/tests/Console.mint new file mode 100644 index 000000000..e3d5d7d1c --- /dev/null +++ b/core/tests/tests/Console.mint @@ -0,0 +1,105 @@ +suite "Console.Counter" { + test "it works once on 'Default' label using tuple" { + // "Default" label + let {a, b} = + Console.count() + + // reset "Default" label + Console.countReset() + + b == 1 + } + + test "it works once on 'Default' label using get" { + // "Default" label + Console.count() + + let a = + Console.Counter.get("Default") + + // reset "Default" label + Console.countReset() + + a == 1 + } + + test "it works multiple times on 'Default' label using tuple" { + // "Default" label + for item of [1, 2, 3, 4] { + Console.count() + } + + let {a, b} = + Console.count() + + // reset "Default" label + Console.countReset() + b == 5 + } + + test "it works multiple times on 'Default' label" { + // "Default" label + for item of [1, 2, 3, 4, 5] { + Console.count() + } + + let a = + Console.Counter.get("Default") + + // reset "Default" label + Console.countReset() + a == 5 + } + + test "it works multiple times on different values using tuple" { + // "Default" label + for item of [1, 2, 3] { + Console.count() + } + + // "Test" label + for item of [1, 2] { + Console.count("Test") + } + + let {a, b} = + Console.count() + + let {c, d} = + Console.count("Test") + + // reset "Default" label + Console.countReset() + + // reset "Test" label + Console.countReset("Test") + + b == 4 && d == 3 + } + + test "it works multiple times on different values using get" { + // "Default" label + for item of [1, 2, 3, 4] { + Console.count() + } + + // "Test" label + for item of [1, 2, 3] { + Console.count("Test") + } + + let a = + Console.Counter.get("Default") + + let b = + Console.Counter.get("Test") + + // reset "Default" label + Console.countReset() + + // reset "Test" label + Console.countReset("Test") + + a == 4 && b == 3 + } +}