-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathconsole.mbt
238 lines (227 loc) · 7.49 KB
/
console.mbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2025 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.
///|
fn println_mono(s : String) -> Unit = "%println"
///|
fn any_to_string[T](any : T) -> String = "%any.to_string"
///|
/// Prints any value that implements the `Show` trait to the standard output,
/// followed by a newline.
///
/// Parameters:
///
/// * `value` : The value to be printed. Must implement the `Show` trait.
///
/// Example:
///
/// ```moonbit skip
/// test "println" {
/// println(42)
/// println("Hello, World!")
/// println([1, 2, 3])
/// }
/// ```
pub fn println[T : Show](input : T) -> Unit {
println_mono(input.to_string())
}
///|
/// Prints and returns the value of a given expression for quick and dirty debugging.
#deprecated("This function is for debugging only and should not be used in production")
pub fn dump[T](t : T, name? : String, loc~ : SourceLoc = _) -> T {
let name = match name {
Some(name) => name
None => ""
}
println("dump(\{name}@\{loc}) = \{any_to_string(t)}")
t
}
///|
/// Represents an error type used by the `inspect!` function to indicate failures
/// in value inspection. Contains a string message describing the nature of the
/// inspection failure.
///
/// Returns a type constructor that creates an error type from a string message.
///
/// Example:
///
/// ```moonbit
/// test "inspect/failure" {
/// let x : Int = 42
/// inspect!(x, content="42") // Raises InspectError with detailed failure message
/// }
/// ```
pub(all) type! InspectError String
///|
fn base64_encode(data : FixedArray[Byte]) -> String {
let base64 = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let buf = StringBuilder::new()
let len = data.length()
let rem = len % 3
for i = 0; i < len - rem; i = i + 3 {
let b0 = data[i].to_int()
let b1 = data[i + 1].to_int()
let b2 = data[i + 2].to_int()
let x0 = base64[(b0 & 0xFC) >> 2]
let x1 = base64[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]
let x2 = base64[((b1 & 0x0F) << 2) | ((b2 & 0xC0) >> 6)]
let x3 = base64[b2 & 0x3F]
buf.write_char(Char::from_int(x0.to_int()))
buf.write_char(Char::from_int(x1.to_int()))
buf.write_char(Char::from_int(x2.to_int()))
buf.write_char(Char::from_int(x3.to_int()))
}
if rem == 1 {
let b0 = data[len - 1].to_int()
let x0 = base64[(b0 & 0xFC) >> 2]
let x1 = base64[(b0 & 0x03) << 4]
buf.write_char(Char::from_int(x0.to_int()))
buf.write_char(Char::from_int(x1.to_int()))
buf.write_char('=')
buf.write_char('=')
} else if rem == 2 {
let b0 = data[len - 2].to_int()
let b1 = data[len - 1].to_int()
let x0 = base64[(b0 & 0xFC) >> 2]
let x1 = base64[((b0 & 0x03) << 4) | ((b1 & 0xF0) >> 4)]
let x2 = base64[(b1 & 0x0F) << 2]
buf.write_char(Char::from_int(x0.to_int()))
buf.write_char(Char::from_int(x1.to_int()))
buf.write_char(Char::from_int(x2.to_int()))
buf.write_char('=')
}
buf.to_string()
}
///|
fn base64_encode_string_codepoint(s : String) -> String {
// the input string is expected to be valid utf-16 string
let codepoint_length = s.char_length()
let data : FixedArray[Byte] = FixedArray::make(codepoint_length * 4, 0)
for i = 0, utf16_index = 0
i < codepoint_length
i = i + 1, utf16_index = utf16_index + 1 {
let c = s.unsafe_char_at(utf16_index).to_int()
if c > 0xFFFF {
data[i * 4] = (c & 0xFF).to_byte()
data[i * 4 + 1] = ((c >> 8) & 0xFF).to_byte()
data[i * 4 + 2] = ((c >> 16) & 0xFF).to_byte()
data[i * 4 + 3] = ((c >> 24) & 0xFF).to_byte()
continue i + 1, utf16_index + 2
} else {
data[i * 4] = (c & 0xFF).to_byte()
data[i * 4 + 1] = ((c >> 8) & 0xFF).to_byte()
data[i * 4 + 2] = 0
data[i * 4 + 3] = 0
}
}
base64_encode(data)
}
///|
test {
inspect!(base64_encode_string_codepoint(""))
inspect!(base64_encode_string_codepoint("a"), content="YQAAAA==")
inspect!(base64_encode_string_codepoint("ab"), content="YQAAAGIAAAA=")
inspect!(base64_encode_string_codepoint("abc"), content="YQAAAGIAAABjAAAA")
inspect!(
base64_encode_string_codepoint("abcd"),
content="YQAAAGIAAABjAAAAZAAAAA==",
)
inspect!(
base64_encode_string_codepoint("abcde"),
content="YQAAAGIAAABjAAAAZAAAAGUAAAA=",
)
inspect!(base64_encode_string_codepoint("a中"), content="YQAAAC1OAAA=")
inspect!(
base64_encode_string_codepoint("a中🤣"),
content="YQAAAC1OAAAj+QEA",
)
inspect!(
base64_encode_string_codepoint("a中🤣a"),
content="YQAAAC1OAAAj+QEAYQAAAA==",
)
inspect!(
base64_encode_string_codepoint("a中🤣中"),
content="YQAAAC1OAAAj+QEALU4AAA==",
)
}
///|
/// Tests if the string representation of an object matches the expected content.
/// Used primarily in test cases to verify the correctness of `Show`
/// implementations and program outputs.
///
/// Parameters:
///
/// * `object` : The object to be inspected. Must implement the `Show` trait.
/// * `content` : The expected string representation of the object. Defaults to
/// an empty string.
/// * `location` : Source code location information for error reporting.
/// Automatically provided by the compiler.
/// * `arguments_location` : Location information for function arguments in
/// source code. Automatically provided by the compiler.
///
/// Throws an `InspectError` if the actual string representation of the object
/// does not match the expected content. The error message includes detailed
/// information about the mismatch, including source location and both expected
/// and actual values.
///
/// Example:
///
/// ```moonbit skip
/// test "inspect/basic" {
/// inspect!(42, content="42")
/// inspect!("hello", content="hello")
/// inspect!([1, 2, 3], content="[1, 2, 3]")
/// }
/// ```
pub fn inspect(
obj : &Show,
content~ : String = "",
loc~ : SourceLoc = _,
args_loc~ : ArgsLoc = _
) -> Unit!InspectError {
let actual = obj.to_string()
if actual != content {
let loc = loc.to_string().escape()
let args_loc = args_loc.to_json().escape()
let expect = content.escape()
let actual = obj.to_string().escape()
let expect_base64 = "\"\{base64_encode_string_codepoint(content)}\""
let actual_base64 = "\"\{base64_encode_string_codepoint(obj.to_string())}\""
raise InspectError(
"@EXPECT_FAILED {\"loc\": \{loc}, \"args_loc\": \{args_loc}, \"expect\": \{expect}, \"actual\": \{actual}, \"expect_base64\": \{expect_base64}, \"actual_base64\": \{actual_base64}}",
)
}
}
///|
/// Represents an error that occurs during snapshot testing. Contains a string
/// message describing the error.
///
/// Used internally by the test driver to handle snapshot-related errors. Not
/// intended for direct use by end users.
///
/// Example:
///
/// ```moonbit
/// test "SnapshotError" {
/// let err : SnapshotError = SnapshotError("failed to load snapshot")
/// match err {
/// SnapshotError(msg) => assert_eq!(msg, "failed to load snapshot")
/// }
/// }
/// ```
pub(all) type! SnapshotError String
///|
test "panic error case of inspect" {
let x : Int = 42
inspect!(x, content="100")
}