Skip to content

Commit

Permalink
Merge pull request #11 from sophof/fix-write-fwf-escaped-string
Browse files Browse the repository at this point in the history
Fix issue with escape characters in strings
  • Loading branch information
sophof authored Dec 8, 2023
2 parents b0bd273 + 600a50c commit 1dcb4cf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
2 changes: 1 addition & 1 deletion R/get_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ get_width = function(col, digits){
}

string = function(col){
max(normal(col), 1)
max(nchar(col), 1)
}

switch(
Expand Down
4 changes: 2 additions & 2 deletions R/write_data.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
write_data = function(df, model, file, decimal.mark = '.', justify = 'right'){
uit = create_fixed_width_column(df, model, decimal.mark, justify)
if (format.info(uit)[1] != sum(model_widths(model))){
if (max(nchar(uit)) != sum(model_widths(model))) {
stop('total output width is not the sum of the individual column widths')
}

Expand Down Expand Up @@ -79,7 +79,7 @@ create_fixed_width_column = function(df, model, decimal.mark, justify){
scientific = FALSE)

col = replace_NA(col, width(var))
nmax = format.info(col)[1]
nmax = if (is.character(col)) max(nchar(col)) else format.info(col)[1]
if(width(var) < nmax){
stop('width in datamodel smaller than number of characters of largest element for variable: ',
name(var))
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test_read_fwf.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ test_that("DATETYPE can be used", {
df = read_fwf_blaise(datafile,
blafile,
locale = readr::locale(date_format = '%Y%m%d'))
expect_equal(weekdays(df[[1]]), c("Thursday", "Monday", "Sunday"))
expect_equal(months(df[[1]]), c("April", "May", "June"))
expect_equal(format(df[[1]], "%Y"), c("2010", "2011", "2012"))
expected_dates = c(
as.Date("2010-04-01"),
as.Date("2011-05-02"),
as.Date("2012-06-03")
)
expect_equal(df[[1]], expected_dates)
unlink(blafile)
unlink(datafile)
})
Expand Down
37 changes: 25 additions & 12 deletions tests/testthat/test_write_datamodel.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ test_that("all types are accepted", {
df$string = as.character(df$factor)

datafile = tempfile(fileext = '.bla')
dir = tempdir()

expect_silent(write_datamodel(get_model(df), datafile))
file = readr::read_file(datafile)
model ='
DATAMODEL
FIELDS
Expand Down Expand Up @@ -53,10 +50,7 @@ test_that("boolean is converted to INTEGER", {
df[5,] = NA

datafile = tempfile(fileext = '.bla')
dir = tempdir()

expect_silent(write_datamodel(get_model(df), datafile))
file = readr::read_file(datafile)
model ='
DATAMODEL
FIELDS
Expand All @@ -77,10 +71,7 @@ test_that("Name can be given to datamodel", {
)

datafile = tempfile(fileext = '.bla')
dir = tempdir()

expect_silent(write_datamodel(get_model(df), datafile, name = 'test'))
file = readr::read_file(datafile)
model ='
DATAMODEL test
FIELDS
Expand All @@ -101,10 +92,7 @@ test_that("small REALs are accepted but padded", {
)

datafile = tempfile(fileext = '.bla')
dir = tempdir()

expect_silent(write_datamodel(get_model(df), datafile, name = 'test'))
file = readr::read_file(datafile)
model ='
DATAMODEL test
FIELDS
Expand All @@ -119,3 +107,28 @@ test_that("small REALs are accepted but padded", {
expect_equal(model_types(m1), model_types(m2))
expect_equal(model_widths(m1), model_widths(m2))
})

test_that("strings with escape characters have the correct width", {
df = data.frame(
A = "0_escapes",
B = "1_\\escape",
C = "\\2esc\\"
)

datafile = tempfile(fileext = '.bla')
expect_silent(write_datamodel(get_model(df), datafile, name = 'test'))
model ='
DATAMODEL test
FIELDS
A : STRING[9]
B : STRING[9]
C : STRING[6]
ENDMODEL
'
blafile = makeblafile(model)
m1 = read_model(datafile)
m2 = read_model(blafile)
expect_equal(model_names(m1), model_names(m2))
expect_equal(model_types(m1), model_types(m2))
expect_equal(model_widths(m1), model_widths(m2))
})
16 changes: 16 additions & 0 deletions tests/testthat/test_write_fwf_blaise.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,19 @@ test_that("dataframe can be written with small reals", {

expect_silent(write_fwf_blaise(df, datafile, blafile, model_name = 'test'))
})

test_that("dataframe can be written with escaped strings", {
df = data.frame(
A = "0_escapes",
B = "1_\\escape",
C = "\\2esc\\"
)

datafile = tempfile(fileext = '.asc')

write_fwf_blaise(df, datafile, write_model = FALSE)

data_str = readLines(datafile)
expected_data_str = paste0(df, collapse = "")
expect_equal(data_str, expected_data_str)
})

0 comments on commit 1dcb4cf

Please sign in to comment.