Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoorkin committed May 13, 2024
1 parent f34d328 commit 7615186
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ pub fn blit(

/// Create a new String from byte sequence.
pub fn to_string(self : Bytes) -> String {
let buf = Buffer::make(self.length() * 4 + 2)
// the length of the result string is at most bytes.length() * 4 + 8
let buf = Buffer::make(self.length() * 4 + 8)
buf.write_string("Bytes::[")
for i = 0; i < self.length(); i = i + 1 {
if i != 0 {
Expand Down
2 changes: 1 addition & 1 deletion bytes/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

test "from_array" {
let b = Bytes::[65, 0, 66, 0, 67, 0]
inspect(b.to_string(), content="ABC")?
inspect(b, content="Bytes::[65,0,66,0,67,0]")?
}

test "hash" {
Expand Down
14 changes: 9 additions & 5 deletions string/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ pub fn to_bytes(self : String) -> Bytes {
}

test "to_bytes" {
@assertion.assert_eq("中文".to_bytes().to_string(), "中文")?
@assertion.assert_eq("asdf".to_bytes().to_string(), "asdf")?
@assertion.assert_eq("🤣🤣".to_bytes().to_string(), "🤣🤣")?
@assertion.assert_eq("🤔".to_bytes().to_string(), "🤔")?
fn f(x : String) {
x.to_bytes().to_unchecked_string()
}

inspect("中文" |> f, content="中文")?
inspect("asdf" |> f, content="asdf")?
inspect("🤣🤣" |> f, content="🤣🤣")?
inspect("🤔" |> f, content="🤔")?
}

pub fn hash(self : String) -> Int {
Expand Down Expand Up @@ -89,7 +93,7 @@ test "debug_write" {
test "Buffer::to_bytes" {
let buffer = Buffer::make(16)
buffer.write_string("中文")
@assertion.assert_eq(buffer.to_bytes().to_string(), "中文")?
inspect(buffer.to_bytes().to_unchecked_string(), content="中文")?
}

/// Converts the String into an array of Chars.
Expand Down
9 changes: 6 additions & 3 deletions uuid/uuid.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ test "bytes" {
let buf = Bytes::[
201, 130, 251, 104, 223, 80, 76, 161, 145, 248, 186, 162, 4, 9, 229, 185,
]
inspect(from_bytes(buf)?.to_bytes(), content="苉棻僟ꅌꊺऄ맥")? // XXX: this is a terrible Show
inspect(
from_bytes(buf)?.to_bytes(),
content="Bytes::[201,130,251,104,223,80,76,161,145,248,186,162,4,9,229,185]",
)?
inspect(from_bytes(Bytes::make(15, 0)), content="Err(requires 16 bytes)")?
inspect(from_bytes(Bytes::make(17, 0)), content="Err(requires 16 bytes)")?
}
Expand Down Expand Up @@ -167,14 +170,14 @@ pub fn to_string(self : UUID) -> String {
rv[i - 1] = dash
continue v, i - 2, i - 2, d - 1
} else {
break rv.to_string()
break rv.to_unchecked_string()
}
}
}

test "from_hex" {
let hex = "13cb501c-1234-5678-9034-abcdef937e20"
inspect(from_hex(hex)?, content=hex)?
inspect(from_hex(hex)?, content="13cb501c-1234-5678-9034-abcdef937e20")?
inspect(
from_hex("12345678-1234-1234-1234-ABCDEF098765")?,
content="12345678-1234-1234-1234-abcdef098765",
Expand Down

0 comments on commit 7615186

Please sign in to comment.