Skip to content

Commit

Permalink
create basic tests. update print methods. add as.character for alphabet.
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Jan 13, 2024
1 parent ffd268e commit 26549e3
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 23 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ Imports:
blob,
cli,
rlang
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
4 changes: 3 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion R/alphabet.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -59,3 +59,9 @@ print.alphabet <- function(x, ...) {
cat(get_alphabet_(x))
invisible(x)
}


#' @export
as.character.alphabet <- function(x, ...) {
get_alphabet_(x)
}
14 changes: 6 additions & 8 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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("<b64_config>\n")
cat(gsub(",", "", z), sep = "\n")
# z <- trimws(strsplit(y, "\n")[[1]][2:4])
cat("<engine_config>\n")
# cat(gsub(",", "", z), sep = "\n")
invisible(x)
}

Expand Down
1 change: 1 addition & 0 deletions R/encode.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ encode <- function(what, eng = engine()) {
}
}

#' @export
#' @rdname encode
decode <- function(what, eng = engine()) {
n <- length(what)
Expand Down
4 changes: 2 additions & 2 deletions R/engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)
)
Expand Down
11 changes: 1 addition & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ knitr::opts_chunk$set(
<!-- badges: start -->
<!-- badges: end -->

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

Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions man/alphabet.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/new_config.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -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")
62 changes: 62 additions & 0 deletions tests/testthat/test-roundtrip-alphabet.R
Original file line number Diff line number Diff line change
@@ -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)
)
})



41 changes: 41 additions & 0 deletions tests/testthat/test-roundtrip.R
Original file line number Diff line number Diff line change
@@ -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]])
)
})

0 comments on commit 26549e3

Please sign in to comment.