@@ -87,3 +87,79 @@ test_that("get_dot_names() does not return empty strings for unnamed args passed
8787 expect_identical(test_fn(a = " 1" , " 2" , b = " 3" , c = " 4" ), c(" a" , " b" , " c" ))
8888 expect_length(test_fn(a = " 1" , " 2" , b = " 3" , c = " 4" ), 3 )
8989})
90+
91+ # append_with_attributes() ----
92+ test_that(" append_with_attributes() supports base::append() functionality" , {
93+ list_1 <- list (a = 1 , b = 2 )
94+ list_2 <- list (c = 3 , d = 4 )
95+ list_nested <- list (c = list (d = 3 ))
96+ expect_identical(
97+ append_with_attributes(1 : 5 , 0 : 1 , after = 3 ),
98+ append(1 : 5 , 0 : 1 , after = 3 )
99+ )
100+ expect_identical(
101+ append_with_attributes(list_1 , list_2 , after = 0 ),
102+ append(list_1 , list_2 , after = 0 )
103+ )
104+ expect_identical(
105+ append_with_attributes(list_1 , list_nested ),
106+ append(list_1 , list_nested )
107+ )
108+ })
109+
110+ test_that(" append_with_attributes() preserves attributes and classes" , {
111+ object <- head(letters , - 1L ) # a, b, ... y
112+ # Add custom attributes
113+ attr(object , " title" ) <- " Letters"
114+ attr(object , " description" ) <- " Lowercase letters in alphabetical order"
115+ # Add custom class
116+ class(object ) <- c(" letters" )
117+ class(object ) <- c(" series" , class(object ))
118+
119+ expect_identical(
120+ attributes(append_with_attributes(object , " z" )),
121+ attributes(object )
122+ )
123+ expect_s3_class(
124+ append_with_attributes(object , " z" ),
125+ c(" series" , " letters" )
126+ )
127+ })
128+
129+ test_that(" append_with_attributes() preserves names" , {
130+ object <- head(letters , - 1L ) # a, b, ... y
131+ # Add names
132+ object <- purrr :: set_names(object , toupper(object ))
133+ expect_named(
134+ append_with_attributes(object , c(" Z" = " z" )),
135+ toupper(letters )
136+ )
137+ })
138+
139+ test_that(" append_with_attributes() can return a valid package" , {
140+ p <- create_package()
141+ p_appended <- append_with_attributes(
142+ p ,
143+ list (custom_property = " custom_value" ),
144+ 0
145+ )
146+ expect_no_error(check_package(p_appended ))
147+ expect_identical(names(p_appended )[[1 ]], " custom_property" )
148+ })
149+
150+ test_that(" append_with_attributes() can return a valid resource" , {
151+ resource <- resource(example_package(), " deployments" )
152+ resource_appended <- append_with_attributes(
153+ resource ,
154+ list (custom_property = " custom_value" ),
155+ 0
156+ )
157+ # Resource attributes are kept
158+ expect_identical(
159+ attr(resource_appended , " data_location" ),
160+ attr(resource , " data_location" )
161+ )
162+ expect_identical(names(resource_appended )[[1 ]], " custom_property" )
163+ })
164+
165+
0 commit comments