|
3 | 3 | # Set the precision to 16 digits: |
4 | 4 | options( digits = 16 ); |
5 | 5 |
|
6 | | -#' Get the script filepath. |
7 | | -#' |
8 | | -#' @return The absolute path of this script |
| 6 | +#' Generate test fixtures. |
9 | 7 | #' |
10 | 8 | #' @examples |
11 | | -#' filepath <- get_script_path() |
12 | | -get_script_path <- function() { |
13 | | - cmdArgs <- commandArgs( trailingOnly = FALSE ); |
14 | | - needle <- "--file="; |
15 | | - match <- grep( needle, cmdArgs ); |
16 | | - if ( length( match ) > 0 ) { |
17 | | - # Rscript: |
18 | | - filepath <- sub( needle, "", cmdArgs[match] ); |
19 | | - } else { |
20 | | - ls_vars <- ls( sys.frames()[[1]] ) |
21 | | - if ( "fileName" %in% ls_vars ) { |
22 | | - # Source'd via RStudio: |
23 | | - filepath <- sys.frames()[[1]]$fileName; |
| 9 | +#' main(); |
| 10 | +main <- function() { |
| 11 | + #' Get the script filepath. |
| 12 | + #' |
| 13 | + #' @return The absolute path of this script |
| 14 | + #' |
| 15 | + #' @examples |
| 16 | + #' filepath <- get_script_path(); |
| 17 | + get_script_path <- function() { |
| 18 | + args <- commandArgs( trailingOnly = FALSE ); |
| 19 | + needle <- "--file="; |
| 20 | + match <- grep( needle, args ); |
| 21 | + if ( length( match ) > 0 ) { |
| 22 | + # Rscript: |
| 23 | + filepath <- sub( needle, "", args[match] ); |
24 | 24 | } else { |
25 | | - # Source'd via R console: |
26 | | - filepath <- sys.frames()[[1]]$ofile; |
| 25 | + ls_vars <- ls( sys.frames()[[1]] ) |
| 26 | + if ( "fileName" %in% ls_vars ) { |
| 27 | + # Source'd via RStudio: |
| 28 | + filepath <- sys.frames()[[1]]$fileName; # nolint |
| 29 | + } else { |
| 30 | + # Source'd via R console: |
| 31 | + filepath <- sys.frames()[[1]]$ofile; |
| 32 | + } |
27 | 33 | } |
| 34 | + return( normalizePath( filepath ) ); |
28 | 35 | } |
29 | | - return( normalizePath( filepath ) ); |
30 | | -} |
31 | 36 |
|
32 | | -#' Convert a data structure to JSON. |
33 | | -#' |
34 | | -#' @param x A data structure to convert |
35 | | -#' @return JSON blob |
36 | | -#' |
37 | | -#' @examples |
38 | | -#' x <- seq( -6.5, 25, 0.5 ); |
39 | | -#' json <- to_json( x ); |
40 | | -to_json <- function( x ) { |
41 | | - return( jsonlite::toJSON( x, digits = 16, auto_unbox = TRUE ) ); |
42 | | -} |
| 37 | + #' Convert a data structure to JSON. |
| 38 | + #' |
| 39 | + #' @param x A data structure to convert |
| 40 | + #' @return JSON blob |
| 41 | + #' |
| 42 | + #' @examples |
| 43 | + #' x <- seq( -6.5, 25, 0.5 ); |
| 44 | + #' json <- to_json( x ); |
| 45 | + to_json <- function( x ) { |
| 46 | + return( jsonlite::toJSON( x, digits = 16, auto_unbox = TRUE ) ); |
| 47 | + } |
43 | 48 |
|
44 | | -#' Generate an output absolute filepath based on the script directory. |
45 | | -#' |
46 | | -#' @param name An output filename |
47 | | -#' @return An absolute filepath |
48 | | -#' |
49 | | -#' @examples |
50 | | -#' filepath <- get_filepath( "data.json" ); |
51 | | -get_filepath <- function( name ) { |
52 | | - return( paste( source_dir, "/", name, sep = "" ) ); |
53 | | -} |
| 49 | + #' Generate an output absolute filepath based on the script directory. |
| 50 | + #' |
| 51 | + #' @param name An output filename |
| 52 | + #' @return An absolute filepath |
| 53 | + #' |
| 54 | + #' @examples |
| 55 | + #' filepath <- get_filepath( "data.json" ); |
| 56 | + get_filepath <- function( name ) { |
| 57 | + return( paste( source_dir, "/", name, sep = "" ) ); |
| 58 | + } |
| 59 | + |
| 60 | + # Get the directory of this script: |
| 61 | + source_dir <- dirname( get_script_path() ); |
54 | 62 |
|
55 | | -# Get the directory of this script: |
56 | | -source_dir <- dirname( get_script_path() ); |
| 63 | + # TODO: generate test fixture data (`x` and `y`), making sure to handle NaNs |
57 | 64 |
|
58 | | -# TODO: generate test fixture data (`x` and `y`), making sure to handle NaNs |
59 | 65 |
|
| 66 | + # Convert fixture data to JSON: |
| 67 | + x <- to_json( x ); |
| 68 | + y <- to_json( y ); |
60 | 69 |
|
61 | | -# Convert fixture data to JSON: |
62 | | -x <- to_json( x ); |
63 | | -y <- to_json( y ); |
| 70 | + # Write the data to file... |
| 71 | + filepath <- get_filepath( "data.json" ); |
| 72 | + write( x, filepath ); |
64 | 73 |
|
65 | | -# Write the data to file... |
66 | | -filepath <- get_filepath( "data.json" ); |
67 | | -write( x, filepath ); |
| 74 | + filepath <- get_filepath( "expected.json" ); |
| 75 | + write( y, filepath ); |
| 76 | +} |
68 | 77 |
|
69 | | -filepath <- get_filepath( "expected.json" ); |
70 | | -write( y, filepath ); |
| 78 | +main(); |
0 commit comments