writing hamcrest tests

7 views
Skip to first unread message

Per Nyfelt

unread,
Dec 23, 2018, 12:22:16 PM12/23/18
to Renjin

Hi, I have created an extension that allows me to display images in a javafx application.

readImage <- function(url) {
import(se.alipsa.rideutils.ReadImage)
import(javafx.scene.image.Image)
img <- ReadImage$read(url)
return(img)
}
With this I can do stuff like this in the script engine:

library("se.alipsa:rideutils")


# read the file and return it as a javafx Image
img
<- readImage("image/logo.png")
# display it in my javafx application
inout$display
(img, "logo")

This works fine. I would like to write some hamcrest tests in the extension so that I can verify that what i did actually works during build time rather than have to test it in another project.

However this does not work:

library("se.alipsa:rideutils")
library("hamcrest")

test.readImage <- function() {
import(javafx.scene.image.Image)

img <- readImage("dino.jpg")
assertTrue(exists("img"))
assertTrue(img$height() > 0)

}

It will just fail with "readImage() in test.imageview.R failed" i.e. fail on img <- readImage("dino.jpg"). If i run the same test in the scriptEngine I get past that (org.renjin.eval.EvalException: 'function' of lang expression is of unsupported type 'double'). What can I do to be able to create meaningful hamcrest tests for my extension?

In case i did not express myself clearly, the code for the extension is at https://github.com/perNyfelt/rideutils

Any thoughts or suggestions would be most welcome!

Best regards,
Per

Bertram, Alexander

unread,
Jan 11, 2019, 4:03:11 AM1/11/19
to renji...@googlegroups.com
Hi Per,
I think you just have a syntax error in the test:

javafx.scene.image.Image has a method named getHeight(), so you should access it as a property, for example "img$height". If you want to call the method directly, then you can write "img$getHeight()", but as the error message says, "typeof(img$height)" is "double", and not "function"

If you evaluate this script with the RenjinScriptEngine, the test won't actually be invoked: the test runner takes the additional step of looking for all defined variables of type function that start with "test." and then executes them with no arguments. That would explain why you were getting different results.

Best,
Alex





--
You received this message because you are subscribed to the Google Groups "Renjin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to renjin-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Alexander Bertram
Technical Director
BeDataDriven BV

Web: http://bedatadriven.com
Email: al...@bedatadriven.com
Tel. Nederlands: +31(0)647205388
Skype: akbertram

Per Nyfelt

unread,
Jan 11, 2019, 2:24:07 PM1/11/19
to Renjin
Nice thanks!

I got this to work fine now. If i want to just run in the script engine i can just do something like:
1. "Load" the test first

library("se.alipsa:rideutils")
library("hamcrest")


greaterThan <- function(expected) {
function(actual) {
all(actual > expected)
}
}

test.readImage <- function() {
print("test.readImage")
import(javafx.scene.image.Image)


img <- readImage("image/logo.png")
assertTrue(exists("img"))
assertThat(img$getHeight(), greaterThan(0))
print("done")
}

Now verify the by invoking it (in the same session):

test.readImage()
[1] "test.readImage"
[1] "done"

Best regards,
Per
Reply all
Reply to author
Forward
0 new messages