Skip to content

Commit

Permalink
feat: overwrite dot arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tin900 committed Mar 15, 2024
1 parent 13b4dc0 commit 20fddd5
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
^LICENSE\.md$
^\.github$
^vvprojector\.Rproj$
^\.Rproj\.user$
^helperfunctions$
^_pkgdown\.yml$
^docs$
^pkgdown$
^TODO\.md$
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated by roxygen2: do not edit by hand

export(overwrite_dot_arguments)
24 changes: 24 additions & 0 deletions R/overwrite_dot_arguments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#' Overwrite arguments with dot arguments
#'
#' This function takes a list of function arguments and additional arguments passed
#' with the dot (...). It overwrites the function arguments with the values from the
#' dot arguments if they have the same names.
#'
#' @param function_args A named list of function arguments.
#' @param ... Additional arguments passed with the dot (...). These arguments will
#' overwrite the corresponding ones in \code{function_args} if they have the same names.
#'
#' @return A list containing the original \code{function_args} with any overwritten values
#' from the dot arguments.
#'
#' @examples
#' # Example usage
#' original_args <- list(a = 1, b = 2)
#' result <- overwrite_dot_arguments(original_args, b = 3, c = 4)
#' result
#'
#' @export
overwrite_dot_arguments <- function(function_args, ...) {
dots <- list(...)
return(c(function_args[setdiff(names(function_args), names(dots))], dots))
}
30 changes: 30 additions & 0 deletions man/overwrite_dot_arguments.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(vvprojector)

test_check("vvprojector")
20 changes: 20 additions & 0 deletions tests/testthat/test-overwrite_dot_arguments.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("overwrite_dot_arguments correctly overwrites arguments", {
# Test case 1: Basic functionality
original_args <- list(a = 1, b = 2)
result <- overwrite_dot_arguments(original_args, b = 3, c = 4)
expect_equal(result$a, 1)
expect_equal(result$b, 3)
expect_equal(result$c, 4)

# Test case 2: Overwriting with the same value
result <- overwrite_dot_arguments(original_args, b = 2)
expect_equal(result$a, 1)
expect_equal(result$b, 2)
expect_equal(result$c, NULL)

# Test case 3: No overwriting when no matching names
result <- overwrite_dot_arguments(original_args, d = 5)
expect_equal(result$a, 1)
expect_equal(result$b, 2)
expect_equal(result$d, 5)
})

0 comments on commit 20fddd5

Please sign in to comment.