@@ -246,24 +246,23 @@ apply_to_each <- function(.cstr, ..., .args = list()) {
246246# ' @param constraint A `<qf_constraint>` object
247247# ' @param table The table to which the constraint is applied
248248# '
249- # ' @returns A list; see `check_constraints` for details.
249+ # ' @returns A `<qf_check_constraint/qf_check>` object
250250# '
251251# ' @noRd
252252check_constraint <- function (constraint , table ) {
253- satisfied_rows <- check_constraint_strict(constraint , table )
254- excepted_rows <- attr(constraint , " exceptions" ) | >
253+ results <- data.frame (row.names = seq_len(nrow(table )))
254+ results $ satisfied <- check_constraint_strict(constraint , table )
255+ results $ excepted <- attr(constraint , " exceptions" ) | >
255256 purrr :: map(excepted_rows , table = table ) | >
256257 purrr :: reduce(`|` , .init = rep(FALSE , nrow(table )))
257- handled_rows <- satisfied_rows | excepted_rows
258- structure(list (
259- satisfied = all(satisfied_rows ),
260- handled = all(handled_rows ),
261- rows = list (
262- satisfied = satisfied_rows ,
263- excepted = excepted_rows ,
264- handled = handled_rows
265- )
266- ), class = " qf_report_check" , constraint = constraint )
258+ results $ handled <- results $ satisfied | results $ excepted
259+ new_qf_check(
260+ results ,
261+ satisfied = all(results $ satisfied ),
262+ handled = all(results $ handled ),
263+ constraint = constraint ,
264+ subclass = " qf_check_constraint"
265+ )
267266}
268267
269268# ' Check that a constraint is satisfied, ignoring exceptions
@@ -279,69 +278,6 @@ check_constraint_strict <- function(constraint, table) {
279278 UseMethod(" check_constraint_strict" )
280279}
281280
282- # ' @noRd
283- # ' @export
284- print.qf_report_check <- function (x , max_width = getOption(" width" ), ... ) {
285- print(attr(x , " constraint" ))
286- if (x $ satisfied ) {
287- cat(" All rows satisfied" )
288- } else if (x $ handled ) {
289- cat(" All rows satisfied or excepted" )
290- } else {
291- message <- " Violating rows: "
292- indices <- format_indices(
293- which(! x $ rows $ handled ),
294- max_width - nchar(message )
295- )
296- cat0(message , indices )
297- }
298- cat(" \n " )
299- invisible (x )
300- }
301-
302- # ' @noRd
303- # ' @export
304- print.qf_report_check_table <- function (x , ... ) {
305- report_check_header(x )
306- for (cstr in x ) print(cstr )
307- invisible (x )
308- }
309-
310- # ' @noRd
311- # ' @export
312- print.qf_report_check_dataset <- function (x , ... ) {
313- report_check_header(unlist(x , recursive = FALSE ))
314- indent <- " "
315- for (i in 1 : length(x )) {
316- if (any(vapply(x [[i ]], \(el ) ! el $ handled , logical (1 )))) {
317- cat0(" => In table '" , names(x )[[i ]], " ':\n " )
318- for (j in 1 : length(x [[i ]])) {
319- if (! x [[i ]][[j ]]$ handled ) {
320- report_check_constraint <- utils :: capture.output({
321- cat0(" [[" , j , " ]] " )
322- print(x [[i ]][[j ]], max_width = getOption(" width" ) - nchar(indent ))
323- })
324- cat(paste0(indent , report_check_constraint ), sep = " \n " )
325- }
326- }
327- }
328- }
329- }
330-
331- report_check_header <- function (x ) {
332- satisfied <- purrr :: map_lgl(x , " satisfied" )
333- handled <- purrr :: map_lgl(x , " handled" )
334- n_satisfied <- sum(satisfied )
335- n_excepted <- sum(handled ) - sum(satisfied )
336- n_violated <- length(x ) - sum(handled )
337- cat(" Constraint check report:" , n_satisfied , " satisfied" , " /" , n_excepted ,
338- " excepted" , " /" , n_violated , " violated:" , " \n " )
339- }
340-
341-
342-
343- # Exception handling ===========================================================
344-
345281# ' Get or set the exceptions of a constraint
346282# '
347283# ' @param x A constraint object
0 commit comments