From 88b4b2c1f9c4215715951654356d19395a86295f Mon Sep 17 00:00:00 2001 From: parmsam Date: Thu, 11 Apr 2024 19:30:05 -0400 Subject: [PATCH] add more unit tests --- tests/testthat/test-lzstringr.R | 42 ++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-lzstringr.R b/tests/testthat/test-lzstringr.R index 8160222..3062920 100644 --- a/tests/testthat/test-lzstringr.R +++ b/tests/testthat/test-lzstringr.R @@ -131,10 +131,50 @@ test_that("Compress and Decompress for different encodings", { latin1_str <- rawToChar(as.raw(0xFF)) Encoding(latin1_str) <- "latin1" compare_compress_decompress(latin1_str) + + japanese_text <- "こんにちは" # Hello in Japanese + encoded_text <- iconv(japanese_text, from = "UTF-8", to = "Shift-JIS") + decoded_text <- iconv(encoded_text, from = "Shift-JIS", to = "UTF-8") + compressed <- compressToBase64(decoded_text) + decompressed <- decompressFromBase64(compressed) + expect_equal(decompressed, japanese_text) +}) + +test_that("Compression handles special characters and symbols", { + text <- "𐐷𐑌 – Mathematical symbols: ∑ ∫, Emoji: 😊, Arabic: العربية, Hebrew: עברית" + expect_no_error(compressToBase64(text)) + compare_compress_decompress(text) + text <- "漢字 – Kanji, Cyrillic: Цирилица, Thai: ภาษาไทย" + expect_no_error(compressToBase64(text)) + compare_compress_decompress(text) +}) + +test_that("Decompression handles malformed input gracefully", { + malformed_base64 <- "This isn't base64 at all!" + # Decompression should handle this without crashing + expect_no_error(decompressFromBase64(malformed_base64)) + expect_equal(decompressFromBase64(malformed_base64), "") }) + ## Test cases for specific operating systems ---- -test_that("Compress and Decompress for specific operating systems", { +test_that("Compression handles OS-specific encodings", { skip() + input_windows <- iconv("This is a test – with a dash", from = "UTF-8", to = "Windows-1252") + input_mac <- iconv("This is a test – with a dash", from = "UTF-8", to = "macintosh") + compressToBase64(input_windows) + compare_compress_decompress(input_windows) + compare_compress_decompress(input_mac) }) +# Test cases for to and from URI component encoding ---- +test_that("Compress and Decompress for URI encoding", { + text <- "[{\"name\":\"app.py\",\"content\":\"from shiny.express import input, render, ui\\n\\nui.input_slider(\\\"n\\\", \\\"N\\\", 0, 100, 20)\\n\\n\\n@render.text\\ndef txt():\\n return f\\\"n*2 is {input.n() * 2}\\\"\\n\"}]" + hash <- "NobwRAdghgtgpmAXGKAHVA6VBPMAaMAYwHsIAXOcpMAMwCdiYACAZwAsBLCbDOAD1R04LFkw4xUxOmTERUAVzJ4mQiABM4dZfI4AdCPp0YuCsgH0WAGw4a6ACl2RHyxwDlnTAAzKAjJ+9MAEyeAJT64RAAAqq2GBR8ZPoaNExkCXYhiPpMOSpwZPJ0EEw0jhAAVIFioiAmihgQGUzlQQC+jvpgrQC6QA" + + compressed <- compressToEncodedURIComponent(text) + expect_equal(compressed, hash) + + decompressed <- decompressFromEncodedURIComponent(compressed) + expect_equal(decompressed, text) +})