@@ -129,6 +129,97 @@ test_that("ppc_violin_grouped returns a ggplot object", {
129129 expect_gg(ppc_violin_grouped(y , yrep , group , y_draw = " both" , y_jitter = 0.3 ))
130130})
131131
132+ # ppc_data / ppd_data tests -----------------------------------------------
133+
134+ test_that(" ppc_data returns the correct structure" , {
135+ y_small <- c(10 , 20 )
136+ yrep_small <- rbind(c(11 , 21 ), c(12 , 22 ))
137+
138+ d <- ppc_data(y_small , yrep_small )
139+
140+ expect_s3_class(d , " data.frame" )
141+ expect_named(d , c(" y_id" , " y_name" , " rep_id" , " rep_label" ,
142+ " is_y" , " is_y_label" , " value" ))
143+ expect_equal(nrow(d ), length(y_small ) * (nrow(yrep_small ) + 1 ))
144+ expect_equal(d $ y_id , c(1L , 1L , 2L , 2L , 1L , 2L ))
145+ expect_equal(as.character(d $ y_name ), c(" 1" , " 1" , " 2" , " 2" , " 1" , " 2" ))
146+ expect_equal(d $ rep_id , c(1L , 2L , 1L , 2L , NA , NA ))
147+ expect_equal(d $ is_y , c(FALSE , FALSE , FALSE , FALSE , TRUE , TRUE ))
148+ expect_equal(d $ value , c(11 , 12 , 21 , 22 , 10 , 20 ))
149+ expect_equal(d $ value [d $ is_y ], y_small )
150+
151+ first_level <- levels(d $ rep_label )[1 ]
152+ expect_true(all(as.character(d $ rep_label [d $ is_y ]) == first_level ))
153+ expect_true(all(as.character(d $ rep_label [! d $ is_y ]) != first_level ))
154+ })
155+
156+ test_that(" ppc_data carries group through correctly" , {
157+ y_small <- c(10 , 20 )
158+ yrep_small <- rbind(c(11 , 21 ), c(12 , 22 ))
159+ group_small <- factor (c(" a" , " b" ))
160+
161+ d <- ppc_data(y_small , yrep_small , group = group_small )
162+
163+ expect_named(d , c(" group" , " y_id" , " y_name" , " rep_id" , " rep_label" ,
164+ " is_y" , " is_y_label" , " value" ))
165+ expect_equal(as.character(d $ group ), c(" a" , " a" , " b" , " b" , " a" , " b" ))
166+ expect_equal(as.character(d $ group [d $ is_y ]), as.character(group_small ))
167+ })
168+
169+ test_that(" ppc_data handles a single replicate matrix" , {
170+ y_small <- c(10 , 20 )
171+ yrep_small <- matrix (c(11 , 21 ), nrow = 1 )
172+
173+ d <- ppc_data(y_small , yrep_small )
174+
175+ expect_equal(sum(! d $ is_y ), length(y_small ))
176+ expect_equal(d $ rep_id [! d $ is_y ], c(1L , 1L ))
177+ expect_equal(d $ value [! d $ is_y ], c(11 , 21 ))
178+ })
179+
180+ test_that(" ppd_data returns the correct structure" , {
181+ yrep_small <- rbind(c(11 , 21 ), c(12 , 22 ))
182+
183+ d <- ppd_data(yrep_small )
184+
185+ expect_s3_class(d , " data.frame" )
186+ expect_named(d , c(" y_id" , " y_name" , " rep_id" , " rep_label" , " value" ))
187+ expect_equal(nrow(d ), nrow(yrep_small ) * ncol(yrep_small ))
188+ expect_equal(d $ y_id , c(1L , 1L , 2L , 2L ))
189+ expect_equal(as.character(d $ y_name ), c(" 1" , " 1" , " 2" , " 2" ))
190+ expect_equal(d $ rep_id , c(1L , 2L , 1L , 2L ))
191+ expect_equal(d $ value , c(11 , 12 , 21 , 22 ))
192+ expect_true(all(grepl(" pred" , levels(d $ rep_label ), fixed = TRUE )))
193+ })
194+
195+ test_that(" ppd_data carries group through correctly" , {
196+ yrep_small <- rbind(c(11 , 21 ), c(12 , 22 ))
197+ group_small <- factor (c(" a" , " b" ))
198+
199+ d <- ppd_data(yrep_small , group = group_small )
200+
201+ expect_named(d , c(" group" , " y_id" , " y_name" , " rep_id" , " rep_label" , " value" ))
202+ expect_equal(as.character(d $ group ), c(" a" , " a" , " b" , " b" ))
203+ })
204+
205+ test_that(" ppd_data carries observation names through to y_name" , {
206+ yrep_named <- rbind(c(11 , 21 ), c(12 , 22 ))
207+ colnames(yrep_named ) <- c(" obs_a" , " obs_b" )
208+
209+ d <- ppd_data(yrep_named )
210+
211+ expect_equal(as.character(d $ y_name ), c(" obs_a" , " obs_a" , " obs_b" , " obs_b" ))
212+ })
213+
214+ test_that(" ppd_data handles a single replicate matrix" , {
215+ yrep_small <- matrix (c(11 , 21 ), nrow = 1 )
216+
217+ d <- ppd_data(yrep_small )
218+
219+ expect_equal(nrow(d ), ncol(yrep_small ))
220+ expect_equal(d $ rep_id , c(1L , 1L ))
221+ expect_equal(d $ value , c(11 , 21 ))
222+ })
132223
133224
134225# Visual tests -----------------------------------------------------------------
0 commit comments