tests/testthat/test-processYaml.r

##------------------------------------------------------------------------------
context("processYaml/inline")
##------------------------------------------------------------------------------

test_that("processYaml/from inline/single", {
  
  yaml <- processYaml(
    from = function() {
      "object-ref: {id: x_1, where: .GlobalEnv, as: ref_1}"
      ref_1 * 2
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_equal(body(yaml$src)[[2]], yaml$expr$object_1$get_assign) 
  expect_equal(body(yaml$src)[[3]], quote(ref_1 * 2)) 
      
})

test_that("processYaml/from inline/multiple", {
  
  yaml <- processYaml(
    from = function() {
      "object-ref: {id: x_1, where: .GlobalEnv, as: ref_1}"
      "object-ref: {id: x_2, where: .GlobalEnv, as: ref_1}"
      ref_1 + ref_2 * 2
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_equal(body(yaml$src)[[2]], yaml$expr$object_1$get_assign) 
  expect_equal(body(yaml$src)[[3]], yaml$expr$object_2$get_assign) 
  expect_equal(body(yaml$src)[[4]], quote(ref_1 + ref_2 * 2)) 
      
})

test_that("processYaml/from inline/strict", {
  
  yaml <- processYaml(
    from = function() {
      "Invalid YAML markup"
      ref_1 + ref_2 * 2
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_equal(yaml$original, character()) 
  expect_is(yaml$src, "function") 
  
  expect_error(processYaml(
    from = function() {
      "Invalid YAML markup"
      ref_1 + ref_2 * 2
    },
    ctx = YamlContext.ObjectReference.S3(),
    strict = TRUE
  ))
      
})

##------------------------------------------------------------------------------
context("processYaml/inline/expression")
##------------------------------------------------------------------------------

test_that("processYaml/inline/expression", {
  
  yaml <- processYaml(
    from = captureExpression({
      "object-ref: {id: x_1, where: .GlobalEnv, as: ref_1}"
      ref_1 * 2
    }),
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "{") 
  expect_equal(yaml$src[[2]], yaml$expr$object_1$get_assign) 
  expect_equal(yaml$src[[3]], quote(ref_1 * 2)) 
      
})


##------------------------------------------------------------------------------
context("processYaml/comments")
##------------------------------------------------------------------------------

test_that("processYaml/from comment/single", {
    
  ## With curly brackets //
  yaml <- processYaml(
    from = function() {
      ## object-ref: {id: x_1}
      x_1
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 
  
  ## With curly brackets at end of line //
  yaml <- processYaml(
    from = function() {
      ## object-ref: {id: x_1}
      x_1},
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 

  ## W/o curly brackets //
  yaml <- processYaml(
    from = function()
      ## object-ref: {id: x_1}
      x_1,
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 
      
})

test_that("processYaml/from comments/multiple", {
  
#   skip("manual only due to environment issues")
  
  ## With curly brackets //
  yaml <- processYaml(
    from = function() {
      ## object-ref: {id: x_1, where: where, as: ref_1}
      ## object-ref: {id: x_2, as: ref_2}
      ref_1 + ref_2
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_is(body(yaml$src)[[3]], "<-") 
  expect_equal(body(yaml$src)[[4]], quote(ref_1 + ref_2)) 
  
  ## W/o curly brackets //
  yaml <- processYaml(
    from = function()
      ## object-ref: {id: x_1, where: where, as: ref_1}
      ## object-ref: {id: x_2, as: ref_2}
      ref_1 + ref_2,
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_is(body(yaml$src)[[3]], "<-") 
  expect_equal(body(yaml$src)[[4]], quote(ref_1 + ref_2)) 
      
})

test_that("processYaml/from comments/empty lines)", {
  
  ## With curly brackets //
  yaml <- processYaml(
    from = function() {
      ## object-ref: {id: x_1, where: where}
      
      ## Do something // 
      x_1 * 2
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_is(body(yaml$src)[[3]], "call") 
  expect_equal(body(yaml$src)[[3]], quote(x_1 * 2)) 
  
  ## W/o curly brackets //
  yaml <- processYaml(
    from = function()
      ## object-ref: {id: x_1, where: where, as: ref_1}
      ## object-ref: {id: x_2, as: ref_2}
      
      ## Do something //
      ref_1 + ref_2,
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_is(body(yaml$src)[[3]], "<-") 
  expect_equal(body(yaml$src)[[4]], quote(ref_1 + ref_2)) 
      
})

##------------------------------------------------------------------------------
context("processYaml/function with arguments")
##------------------------------------------------------------------------------

test_that("function with arguments", {
  
  ## With curly brackets //
  yaml <- processYaml(
    from = function(strict = FALSE) {
      ## object-ref: {id: x_1}
      x_1
    },
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 
  expect_equal(names(formals(yaml$src)), "strict") 
  
  ## With curly brackets at end of line //
  yaml <- processYaml(
    from = function(strict = FALSE) {
      ## object-ref: {id: x_1}
      x_1},
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 
  expect_equal(names(formals(yaml$src)), "strict") 
  
  ## W/o curly brackets //
  yaml <- processYaml(
    from = function(strict = FALSE)
      ## object-ref: {id: x_1}
      x_1,
    ctx = YamlContext.ObjectReference.S3()
  )
  expect_is(yaml$src, "function") 
  expect_is(body(yaml$src)[[2]], "<-") 
  expect_equal(body(yaml$src)[[3]], quote(x_1)) 
  expect_equal(names(formals(yaml$src)), "strict") 
      
})
rappster/yamlr documentation built on May 27, 2019, 2:02 a.m.