From d816b0bd0e283dc132482282e42a4d8b371bcee7 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sun, 13 Oct 2024 09:51:50 +0000 Subject: [PATCH] test(codegen): add test for `CodeBuffer::print_byte_unchecked` (#6497) Add test, and rename the rest of the test functions for `CodeBuffer`. --- crates/oxc_codegen/src/code_buffer.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/oxc_codegen/src/code_buffer.rs b/crates/oxc_codegen/src/code_buffer.rs index dcc49b23a7af7..5a0c8a7f2be40 100644 --- a/crates/oxc_codegen/src/code_buffer.rs +++ b/crates/oxc_codegen/src/code_buffer.rs @@ -365,7 +365,7 @@ mod test { use super::CodeBuffer; #[test] - fn test_empty() { + fn empty() { let code = CodeBuffer::default(); assert!(code.is_empty()); assert_eq!(code.len(), 0); @@ -373,7 +373,7 @@ mod test { } #[test] - fn test_string_isomorphism() { + fn string_isomorphism() { let s = "Hello, world!"; let mut code = CodeBuffer::with_capacity(s.len()); code.print_str(s); @@ -382,7 +382,7 @@ mod test { } #[test] - fn test_into_source_string() { + fn into_source_string() { let s = "Hello, world!"; let mut code = CodeBuffer::with_capacity(s.len()); code.print_str(s); @@ -400,7 +400,7 @@ mod test { #[test] #[allow(clippy::byte_char_slices)] - fn test_print_byte_unchecked() { + fn print_ascii_byte() { let mut code = CodeBuffer::new(); code.print_ascii_byte(b'f'); code.print_ascii_byte(b'o'); @@ -412,7 +412,23 @@ mod test { } #[test] - fn test_peek() { + #[allow(clippy::byte_char_slices)] + fn print_byte_unchecked() { + let mut code = CodeBuffer::new(); + // SAFETY: These bytes are all ASCII + unsafe { + code.print_byte_unchecked(b'f'); + code.print_byte_unchecked(b'o'); + code.print_byte_unchecked(b'o'); + } + + assert_eq!(code.len(), 3); + assert_eq!(code.as_ref(), &[b'f', b'o', b'o']); + assert_eq!(String::from(code), "foo"); + } + + #[test] + fn peek_nth_back() { let mut code = CodeBuffer::new(); code.print_str("foo");