dwww Home | Show directory contents | Find package

test_that("bibConvert works ok", {
    ## TODO: on Windows tests containing
    ##       expect_known_value(readLines(tmp_bib2), ... )
    
    ## fail since a BOM mark is added in that case. Changed these to drop the first line (the
    ## one containing BOM:
    ##    expect_known_value(readLines(tmp_bib2)[-1], ...)
    ## This is not entirely satisfactory since the rds files created by 'expect_known_value'
    ## miss the first line.
     
    bibdir <- system.file("bib", package = "rbibutils")

    ## file is from
    ##    https://gist.github.com/low-decarie/3831049#file-endnote-xml
    ## downloaded on 2021-05-14
    ##
    ## This doesn't work under devtools since pkgload::system.file
    ## adds 'inst' again: 
    ##     endx_in <- system.file(bibdir, "endnote.xml", package = "rbibutils")
    ##
    ## file.path here is more logical anyway
    endx_in <- file.path(bibdir, "endnote.xml")
    med_in  <- file.path(bibdir, "easyPubMedvig.xml")
    bibacc  <- file.path(bibdir, "latin1accents_utf8.bib")
    end_in  <- file.path(bibdir, "Putnam1992.end")
    
    tmp_xml <- tempfile(fileext = ".xml")
    tmp_bib <- tempfile(fileext = ".bib")
    tmp_bib2 <- tempfile(fileext = ".bib")
    tmp_bib3 <- tempfile(fileext = ".bib")
    tmp_bbl <- tempfile(fileext = ".bbl")
    tmp_bbl2 <- tempfile(fileext = ".bbl")
    tmp_rds <- tempfile(fileext = ".rds")
    tmp_R   <- tempfile(fileext = ".R")

    tmp_ads   <- tempfile(fileext = ".ads")
    tmp_copac   <- tempfile(fileext = ".copac")
    tmp_ebi   <- tempfile(fileext = ".ebi")
    tmp_end   <- tempfile(fileext = ".end")
    tmp_endx   <- tempfile(fileext = ".endx")
    tmp_isi   <- tempfile(fileext = ".isi")
    tmp_med   <- tempfile(fileext = ".med")
    tmp_nbib  <- tempfile(fileext = ".nbib")
    tmp_ris   <- tempfile(fileext = ".ris")
    tmp_wordbib   <- tempfile(fileext = ".wordbib")

    ex0_xml <- file.path(bibdir, "ex0.xml")
    bibConvert(ex0_xml, tmp_bib)
    expect_known_value(readLines(tmp_bib)[-1], "xml2bib.rds", update = FALSE)

    
    bibConvert(tmp_bib, tmp_ads)
    expect_known_value(readLines(tmp_ads)[-1], "bib2ads.rds", update = FALSE)
    
    expect_error(bibConvert(tmp_ads, tmp_bib2),
                 "import from ADS abstracts format not implemented")
    expect_error(bibConvert(tmp_ads, tmp_bib2, informat = "ads"),
                 "import from ADS abstracts format not implemented")

    
    bibConvert(tmp_bib, tmp_bbl, outformat = "biblatex")
    expect_known_value(readLines(tmp_bbl)[-1], "bib2biblatex.rds", update = FALSE)

    bibConvert(tmp_bbl, tmp_bib2, informat = "biblatex")
    expect_known_value(readLines(tmp_bib2)[-1], "biblatex2bib.rds", update = FALSE)

    
    ## TODO: need copac file to test for input from copac
    expect_error(bibConvert(tmp_bib, tmp_copac),
                 "export to copac format not implemented")

    
    expect_error(bibConvert(tmp_bib, tmp_ebi, outformat = "ebi"),
                 "export to EBI XML format not implemented")
    ## TODO: need EBI file to test for input from EBI

    
    bibConvert(tmp_bib, tmp_end)
    bibConvert(tmp_bib, tmp_end, outformat = "end")
    expect_known_value(readLines(tmp_end)[-1], "bib2end.rds", update = FALSE)

    bibConvert(tmp_end, tmp_bib3)     
    bibConvert(tmp_end, tmp_bib2, informat = "end")
    ## github actions gives error on all platforms:
    ##       incomplete final line found on '.../Rtmp770EIj/fileb7c68793fe6.bib'
    ## expect_known_value(readLines(tmp_bib2), "end2bib1.rds", update = FALSE)
    
    bibConvert(end_in, tmp_bib3)
    ## see note above 
    ## expect_known_value(readLines(tmp_bib3), "end2bib2.rds", update = FALSE)
    

    expect_error(bibConvert(tmp_bib3, tmp_endx),
                 "export to Endnote XML format not implemented")
    expect_error(bibConvert(tmp_bib3, tmp_endx, outformat = "endx"),
                 "export to Endnote XML format not implemented")
    expect_message(bibConvert(endx_in, tmp_bib3), "no references to output")
    bibConvert(endx_in, tmp_bib3, informat = "endx")
    expect_known_value(readLines(tmp_bib3)[-1], "end2bib.rds", update = FALSE)

    
    bibConvert(tmp_bib, tmp_isi)
    expect_known_value(readLines(tmp_isi)[-1], "bib2isi.rds", update = FALSE)

    bibConvert(tmp_isi, tmp_bib2)
    expect_known_value(readLines(tmp_bib2)[-1], "isi2bib.rds", update = FALSE)

    
    expect_error(bibConvert(tmp_bib, tmp_med),
                 "export to Medline XML format not implemented")
    bibConvert(med_in, tmp_bib, informat = "med")
    bibConvert(med_in, tmp_bib, informat = "med", outformat = "biblatex")
    expect_known_value(readLines(tmp_bib)[-1], "med2bib.rds", update = FALSE)

    
    ## bibConvert(tmp_bib, tmp_nbib)   # TODO: segfaults!
    ## bibConvert(tmp_nbib, tmp_bib2)  # TODO: need nbib file for import!
    ## expect_known_value(readLines(tmp_bib2), "nbib2bib.rds", update = FALSE)

    
    bibConvert(tmp_bib, tmp_ris)
    expect_known_value(readLines(tmp_ris)[-1], "bib2ris.rds", update = FALSE)

    bibConvert(tmp_ris, tmp_bib)
    expect_known_value(readLines(tmp_bib)[-1], "ris2bib.rds", update = FALSE)

    
    bibConvert(tmp_bib, tmp_xml)
    expect_known_value(readLines(tmp_xml)[-1], "bib2xml2.rds", update = FALSE)

    bibConvert(tmp_bib, tmp_rds)
    expect_known_value(readRDS(tmp_rds)[-1], "bib2rds.rds", update = FALSE)

    bib2R <- bibConvert(tmp_bib, tmp_R)
    expect_known_value(bib2R$bib, "bib2R.rds", update = FALSE)

    
    bibConvert(tmp_rds, tmp_xml)  # rds to MODS XML intermediate
    expect_known_value(readLines(tmp_xml)[-1], "rds2xml.rds", update = FALSE)

    bibConvert(tmp_rds, tmp_bbl2, outformat = "biblatex")
    expect_known_value(readLines(tmp_bbl2)[-1], "rds2bbl2.rds", update = FALSE)

    
    bibConvert(tmp_bib, tmp_wordbib)
    bibConvert(tmp_bib, tmp_wordbib, outformat = "wordbib")
    expect_known_value(readLines(tmp_wordbib)[-1], "bib2wordbib.rds", update = FALSE)

    ## TODO: this currently misses the authors! Don't know if the culprit is
    ##       the above conversion to wordbib.
    bibConvert(tmp_wordbib, tmp_bib3)
    bibConvert(tmp_wordbib, tmp_bib3, informat = "wordbib")
    expect_known_value(readLines(tmp_bib3)[-1], "wordbib2bib.rds", update = FALSE)

    ## accept also 'word' since, due to a mixup, the C code was accepting that
    bibConvert(tmp_bib, tmp_wordbib, outformat = "word")
    bibConvert(tmp_wordbib, tmp_bib3, informat = "word")
    

    cyr_utf8 <- file.path(bibdir, "cyr_utf8.bib")
    bibConvert(cyr_utf8, tmp_bib, options = c(o = "cp1251"))
    bibConvert(tmp_bib, tmp_xml, options = c(i = "cp1251"))
    
    bibConvert(bibacc, tempfile(fileext = "bib"), outformat = "biblatex")
    
})

Generated by dwww version 1.15 on Thu Jun 20 13:56:36 CEST 2024.