From 5b1130debe9d35676aff972fbcf5270f28d8a2ff Mon Sep 17 00:00:00 2001 From: Rynco Maekawa Date: Tue, 19 Mar 2024 16:53:16 +0800 Subject: [PATCH] Add tests for escaping --- string/moon.pkg.json | 9 ++++++++- string/string.mbt | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/string/moon.pkg.json b/string/moon.pkg.json index 9e26dfeeb..a52055e50 100644 --- a/string/moon.pkg.json +++ b/string/moon.pkg.json @@ -1 +1,8 @@ -{} \ No newline at end of file +{ + "import": [ + { + "path": "moonbitlang/core/assertion", + "alias": "assertion" + } + ] +} diff --git a/string/string.mbt b/string/string.mbt index 1f2041e78..2cba163b7 100644 --- a/string/string.mbt +++ b/string/string.mbt @@ -65,3 +65,15 @@ pub fn escape(self : String) -> String { self.escape_to(buf) buf.to_string() } + +test "backslash escape" { + let s = "\n\r\t\b\"\'\\" + let expected = "\\n\\r\\t\\b\\\"\\\'\\\\" + @assertion.assert_eq(s.escape(), expected)? +} + +test "hex escape"{ + let s = "\x11\x12\x01\x02" + let expected = "\\x11\\x12\\x01\\x02" + @assertion.assert_eq(s.escape(), expected)? +}