Tuesday 15 May 2012

Testing only individual functions in a R script -


I am trying to test a personal function in R script which has a function call as the last description of the script I code is:

  mul.R #! / Usr / bin / env rcriptcript mul & lt; - Function (n, m) {prod & lt; - n * m Returns (prod)} Mul (4,5)   

Here is a test script:

  test_simpleprogram.r #! / Usr / bin / env Raptor Library ('RUNIT') source ("./../ simpleProgram.R") test.mul & lt; - Function () {checkEqualsNumeric (mul (n = 2, m = 3), 4)}   

I execute the test script using the following command:

  runTestFile (absFileName = file.path ("test_simpleprogram.R"))   

Now, when I support R script i.e. I want to test the mul.r , because in the final statement calls the mul function, the whole script executes and then the test script runs again again. This is the first execution ( due to source (..) is not required for test purpose.

Is there a way to test such a script, without running a script, while sourcing it in script?

source instead of

use parse

  code & lt; - Parse ("c: / r / mul".) # The actual file path may be different   

This file loads the file as unavailable sense, seen in this form Can be:

  # & gt; As.list (code) # [[1]] #mult & lt; - function (n, m) {#product   

The first element of the list given above is actually ' function:

  # & gt; As.list (code [[1]] # [[1]] # `& lt; -` # # [[2]] # mul # # [[3]] # function (n, m) {# pruss and lieutenant; - n * m # return (prod) #}   

The second element, OTOH, is the call to the mul function:

  #> As.list (code [[2]] # [[1]] # MULL # # [[2]] # [1] 4 # # [[3]] # [1] 5   

Therefore, if your files are assignable and Unwanted functions are made of calls, so we can find out that by inspecting the first element of each entry in the list given above:

  filter & lt; - sapply (code, function ( X) as.character (x [[1]] == "& lt; We can only evaluate the assignment:  
  eval (code [ Filter], envir = globalenv ())   

Then you can call your test.mul function You can use this statement to give you the source can be normalized for other filters. For example, if you want to evaluate all the statements except for the last, you can only use it Tax tax Es:

  eval (code [-line (code)], envir = globalenv ())    

No comments:

Post a Comment