From 26549e383a43fe00357d1c20d4c8fd82bf7cd28e Mon Sep 17 00:00:00 2001 From: Josiah Parry Date: Sat, 13 Jan 2024 08:33:00 -0500 Subject: [PATCH] create basic tests. update print methods. add as.character for alphabet. --- DESCRIPTION | 3 ++ NAMESPACE | 4 +- R/alphabet.R | 8 ++- R/config.R | 14 +++--- R/encode.R | 1 + R/engine.R | 4 +- README.Rmd | 11 +---- man/alphabet.Rd | 3 ++ man/new_config.Rd | 2 +- tests/testthat.R | 12 +++++ tests/testthat/test-roundtrip-alphabet.R | 62 ++++++++++++++++++++++++ tests/testthat/test-roundtrip.R | 41 ++++++++++++++++ 12 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-roundtrip-alphabet.R create mode 100644 tests/testthat/test-roundtrip.R diff --git a/DESCRIPTION b/DESCRIPTION index 78f083a..9397296 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,3 +16,6 @@ Imports: blob, cli, rlang +Suggests: + testthat (>= 3.0.0) +Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index 6d19436..79fcb03 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,9 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(as.character,alphabet) S3method(print,alphabet) -S3method(print,b64_config) S3method(print,engine) +S3method(print,engine_config) export(alphabet) +export(decode) export(decode_file) export(encode) export(encode_file) diff --git a/R/alphabet.R b/R/alphabet.R index 348f3bd..34a624e 100644 --- a/R/alphabet.R +++ b/R/alphabet.R @@ -37,7 +37,7 @@ alphabet <- function(which = "standard") { structure(alphabet_(which), class = "alphabet") } - +#' @rdname alphabet new_alphabet <- function(chars) { n <- nchar(chars) if (nchar(chars) != 64) { @@ -59,3 +59,9 @@ print.alphabet <- function(x, ...) { cat(get_alphabet_(x)) invisible(x) } + + +#' @export +as.character.alphabet <- function(x, ...) { + get_alphabet_(x) +} diff --git a/R/config.R b/R/config.R index 00cdd42..a2725ee 100644 --- a/R/config.R +++ b/R/config.R @@ -17,7 +17,7 @@ #' @param decode_padding_trailing_bits default `FALSE`. "If invalid trailing bits are present and this is true, those bits will be silently ignored." (See details for reference). #' @param decode_padding_mode default `"canonical"`. Other values are `"indifferent"` and `"none"`. See details for more. #' @export -#' @return an object of class `b64_config` +#' @return an object of class `engine_config` new_config <- function( encode_padding = TRUE, decode_padding_trailing_bits = FALSE, @@ -35,19 +35,17 @@ new_config <- function( padding_mode ) - structure(res, class = "b64_config") + structure(res, class = "engine_config") } # shoddy print method for the time being #' @export -print.b64_config <- function(x, ...) { +print.engine_config <- function(x, ...) { y <- print_config_(x) - - z <- trimws(strsplit(y, "\n")[[1]][2:4]) - - cat("\n") - cat(gsub(",", "", z), sep = "\n") + # z <- trimws(strsplit(y, "\n")[[1]][2:4]) + cat("\n") + # cat(gsub(",", "", z), sep = "\n") invisible(x) } diff --git a/R/encode.R b/R/encode.R index ca89cd4..8a49bae 100644 --- a/R/encode.R +++ b/R/encode.R @@ -18,6 +18,7 @@ encode <- function(what, eng = engine()) { } } +#' @export #' @rdname encode decode <- function(what, eng = engine()) { n <- length(what) diff --git a/R/engine.R b/R/engine.R index 01a6725..383b35a 100644 --- a/R/engine.R +++ b/R/engine.R @@ -39,10 +39,10 @@ new_engine <- function(.alphabet = alphabet(), .config = new_config()) { "*" = "use {.fn alphabet} for a standard base64 alphabet" ) ) - } else if (!rlang::inherits_only(.config, "b64_config")) { + } else if (!rlang::inherits_only(.config, "engine_config")) { cli::cli_abort( c( - "{.arg config} is not a {.cls b64_config} object", + "{.arg config} is not a {.cls engine_config} object", "*" = "create one with {.fn new_config}" ) ) diff --git a/README.Rmd b/README.Rmd index d08d3cf..359bec1 100644 --- a/README.Rmd +++ b/README.Rmd @@ -18,7 +18,7 @@ knitr::opts_chunk$set( -The goal of b64 is to provide a very fast and lightweight base64 encoder and decoder and truly open sourced. +The goal of b64 is to provide a very fast, lightweight, and vectorized base64 encoder and decoder. ## Installation @@ -154,12 +154,3 @@ Compare this to the standard encoder: ```{r} encode(txt) ``` - - -## TODO - -- [ ] provide interface to create custom encoder and decoders - - custom alphabets - - padding - - url safe alphabets - - streaming encoding and decoding diff --git a/man/alphabet.Rd b/man/alphabet.Rd index f5ecfd7..f5e6681 100644 --- a/man/alphabet.Rd +++ b/man/alphabet.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/alphabet.R \name{alphabet} \alias{alphabet} +\alias{new_alphabet} \title{Standard base64 alphabets} \usage{ alphabet(which = "standard") + +new_alphabet(chars) } \arguments{ \item{which}{default \code{"standard"}. Which base64 alphabet to use. diff --git a/man/new_config.Rd b/man/new_config.Rd index 080f9cd..a1b980a 100644 --- a/man/new_config.Rd +++ b/man/new_config.Rd @@ -18,7 +18,7 @@ new_config( \item{decode_padding_mode}{default \code{"canonical"}. Other values are \code{"indifferent"} and \code{"none"}. See details for more.} } \value{ -an object of class \code{b64_config} +an object of class \code{engine_config} } \description{ Create a custom encoding engine diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..cc78905 --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview +# * https://testthat.r-lib.org/articles/special-files.html + +library(testthat) +library(b64) + +test_check("b64") diff --git a/tests/testthat/test-roundtrip-alphabet.R b/tests/testthat/test-roundtrip-alphabet.R new file mode 100644 index 0000000..86c7b69 --- /dev/null +++ b/tests/testthat/test-roundtrip-alphabet.R @@ -0,0 +1,62 @@ +test_that("alphabet: standard", { + a <- alphabet("standard") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + +test_that("alphabet: bcrypt", { + a <- alphabet("bcrypt") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + +test_that("alphabet: bin_hex", { + a <- alphabet("bin_hex") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + +test_that("alphabet: crypt", { + a <- alphabet("crypt") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + +test_that("alphabet: imap_mutf7", { + a <- alphabet("imap_mutf7") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + +test_that("alphabet: url_safe", { + a <- alphabet("url_safe") + a2 <- new_alphabet(as.character(a)) + + expect_equal( + as.character(a), + as.character(a2) + ) +}) + + + diff --git a/tests/testthat/test-roundtrip.R b/tests/testthat/test-roundtrip.R new file mode 100644 index 0000000..13b3cf7 --- /dev/null +++ b/tests/testthat/test-roundtrip.R @@ -0,0 +1,41 @@ +test_that("engine: standard", { + txt <- "hello world, it is me!" + expect_equal( + txt, + rawToChar(decode(encode(txt))[[1]]) + ) +}) + +test_that("engine: standard_no_pad", { + eng <- engine("standard_no_pad") + txt <- "hello world, it is me!" + encoded <- encode(txt, eng) + decoded <- decode(encoded, eng) + expect_equal( + txt, + rawToChar(decoded[[1]]) + ) +}) + +test_that("engine: url_safe", { + eng <- engine("url_safe") + txt <- "\xfa\xec U" + encoded <- encode(txt, eng) + decoded <- decode(encoded, eng) + expect_equal( + txt, + rawToChar(decoded[[1]]) + ) +}) + + +test_that("engine: url_safe_no_pad", { + eng <- engine("url_safe_no_pad") + txt <- "\xfa\xec U" + encoded <- encode(txt, eng) + decoded <- decode(encoded, eng) + expect_equal( + txt, + rawToChar(decoded[[1]]) + ) +})