tests/test-gadgetfleet.R

library(Rgadget)
library(unittest, quietly = TRUE)
library(magrittr)

ver_string <- paste("; Generated by Rgadget", utils::packageVersion("Rgadget"))

cmp_dir <- function(path, expected) {
    # Read all files in a directory
    dir_list <- function (dir) {
        files <- sort(list.files(dir, full.names = FALSE, recursive = TRUE), method = 'radix')
        structure(
            lapply(files, function (f) readLines(file.path(dir, f), n = -1)),
            names = files
        )
    }

    dir_list_filter <- function (x) {
        invisible(lapply(names(x), function (n) { writeLines(paste0("\n=== ", n, " ===")); writeLines(x[[n]]) }))
    }

    ut_cmp_identical(dir_list(path), expected, filter = dir_list_filter)
}

ok_group("fleet", {
    path <- tempfile()

    gadgetfleet('fleet', path, missingOkay = TRUE) %>%
        gadget_update('totalfleet', name = 'comm', data = structure(data.frame(
            year = 1990,
            step = 1,
            area = 1,
            weight = 1,
            stringsAsFactors = FALSE),
            area_group = list('1'=1))) %>% # Add an totalfleet component
        write.gadget.file(path)

    ok(cmp_dir(path, list(
        "Data/fleet.comm.data" = c(
            ver_string,
            "; -- data --",
            "; year\tstep\tarea\tfleetname\tweight",
            "1990\t1\t1\tcomm\t1",
        NULL),
        fleet = c(
            ver_string,
            "; ",
            "[component]",
            "totalfleet\tcomm",
            "livesonareas\t1",
            "multiplicative\t1",
            "suitability",
            "amount\tData/fleet.comm.data",
        NULL),
        main = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "fleetfiles\tfleet",
            "[likelihood]",
        NULL)
    )), "Created fleet file with a fleet component")

    gadgetfleet('fleet', path, missingOkay = TRUE) %>%
        gadget_update('numberfleet', name = 'surv', data = structure(data.frame(
            year = 1990,
            step = 1,
            area = 1,
            number = 1,
            stringsAsFactors = FALSE),
            area_group = list('1' = 1))) %>% # Add an totalfleet component
        write.gadget.file(path)

    ok(cmp_dir(path, list(
        "Data/fleet.comm.data" = c(
            ver_string,
            "; -- data --",
            "; year\tstep\tarea\tfleetname\tweight",
            "1990\t1\t1\tcomm\t1",
        NULL),
        "Data/fleet.surv.data" = c(
            ver_string,
            "; -- data --",
            "; year\tstep\tarea\tfleetname\tnumber",
            "1990\t1\t1\tsurv\t1",
        NULL),
        fleet = c(
            ver_string,
            "; ",
            "[component]",
            "totalfleet\tcomm",
            "livesonareas\t1",
            "multiplicative\t1",
            "suitability",
            "amount\tData/fleet.comm.data",
            "; ",
            "[component]",
            "numberfleet\tsurv",
            "livesonareas\t1",
            "multiplicative\t1",
            "suitability",
            "amount\tData/fleet.surv.data",
        NULL),
        main = c(
            ver_string,
            "timefile\t",
            "areafile\t",
            "printfiles\t; Required comment",
            "[stock]",
            "[tagging]",
            "[otherfood]",
            "[fleet]",
            "fleetfiles\tfleet",
            "[likelihood]",
        NULL)
    )), "Added a second fleet")
})
gadget-framework/rgadget documentation built on July 20, 2022, 12:16 p.m.