Skip to content

Commit a6768e5

Browse files
committed
add tests for compressToEncodedURIComponent with complex and simple JSON
1 parent 0f9940d commit a6768e5

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tests/testthat/test-lzstringr.R

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,74 @@ A
249249
"
250250
compare_compress_decompress(x)
251251
})
252+
253+
# Test cases for complex JSON ----
254+
255+
test_that("compressToEncodedURIComponent round-trips complex JSON", {
256+
stable_full <- list(
257+
list(version = 1, class = "ellmer::Turn", props = list(
258+
role = "user",
259+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(text = "a"))),
260+
tokens = c(0, 0)
261+
)),
262+
list(version = 1, class = "ellmer::Turn", props = list(
263+
role = "assistant",
264+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(
265+
text = "There once was a lady by the river,\nWhose heart was full,\nWho spent her days looking up so,\nAnd day after day she grew.\n\nShe found delight in every turn of view,\nHer eyes wide open wide,\nSo that whenever time seemed long, dear friend,\nYou could trust she'd return."
266+
))),
267+
tokens = c(23L, 63L)
268+
)),
269+
list(version = 1, class = "ellmer::Turn", props = list(
270+
role = "user",
271+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(text = "b"))),
272+
tokens = c(0, 0)
273+
)),
274+
list(version = 1, class = "ellmer::Turn", props = list(
275+
role = "assistant",
276+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(
277+
text = "In the land where love does run wild,\nThere was a lover who didn't care,\nWho believed he would find,\nAnd all the while he searched.\n\nHe spent his days looking for just one girl,\nHis thoughts were pure and clear,\nSo that every year he dreamed,\nOf her presence in the air."
278+
))),
279+
tokens = c(96L, 63L)
280+
))
281+
)
282+
json <- jsonlite::serializeJSON(stable_full)
283+
compressed <- lzstring::compressToEncodedURIComponent(json)
284+
decompressed <- lzstring::decompressFromEncodedURIComponent(compressed)
285+
result <- jsonlite::unserializeJSON(decompressed)
286+
expect_equal(stable_full, result)
287+
})
288+
289+
test_that("compressToEncodedURIComponent round trips intermediate JSON (to_empty_string)", {
290+
stable <- list(
291+
list(version = 1, class = "ellmer::Turn", props = list(
292+
role = "user",
293+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(text = "a"))),
294+
tokens = c(0, 0)
295+
)),
296+
list(version = 1, class = "ellmer::Turn", props = list(
297+
role = "assistant",
298+
contents = list(list(version = 1, class = "ellmer::ContentText", props = "She found delight in every turn of view,Her eyes wide open wide,So that whenever time seemed long, dear friend,You could trust she'd return.")),
299+
tokens = c(23L, 63L)
300+
))
301+
)
302+
json <- jsonlite::serializeJSON(stable)
303+
compressed <- lzstring::compressToEncodedURIComponent(json)
304+
decompressed <- lzstring::decompressFromEncodedURIComponent(compressed)
305+
result <- jsonlite::unserializeJSON(decompressed)
306+
expect_equal(stable, result)
307+
})
308+
309+
test_that("compressToEncodedURIComponent round-trips simple JSON", {
310+
stable <- list(
311+
list(version = 1, class = "ellmer::Turn", props = list(
312+
role = "user",
313+
contents = list(list(version = 1, class = "ellmer::ContentText", props = list(text = "a"))),
314+
tokens = c(0, 0)
315+
))
316+
)
317+
json <- jsonlite::serializeJSON(stable)
318+
compressed <- lzstring::compressToEncodedURIComponent(json)
319+
decompressed <- lzstring::decompressFromEncodedURIComponent(compressed)
320+
result <- jsonlite::unserializeJSON(decompressed)
321+
expect_equal(stable, result)
322+
})

0 commit comments

Comments
 (0)