Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and processing.py

Friday, 7 November 2008

Netbeans and Unit Testing

Heres an PApplet, I created in NetBeans6.5β with a view to exploring JUnit applied to a processing project. It is in need of refactoring to make it more testable, and to incorporate the @pre and @post conditions. Although I read somewhere that minimum size for the display window is 10 x 10, this is not forced with linux, you can even use negative numbers.

   1:import processing.core.PApplet;
2:
3:public class GoldenWindow extends PApplet {
4:
5: float PHI = (1 + sqrt(5)) / 2;
6: int maxheight = screen.height;
7: int maxwidth = screen.width;
8: int ht = maxheight / 2;
9: int wdth = floor(PHI * ht); // comment this line uncomment next to test
10: // int wdth = 5; test does fail, although applet runs OK
11:
12: /**
13: * pre: ((width && height >= 10) &&
14: * ((width <= maxwidth) && (height <= maxheight)))
15: * post: instance != null
16: */
17: @Override
18: public void setup() {
19: size(wdth, ht);
20: background(03366);
21: }
22:
23: /**
24: * pre: (instance != null)
25: */
26: @Override
27: public void draw() {
28: }
29:
30: /**
31: * Is the main method required in netbeans
32: * @param args
33: */
34: public static void main(String[] args) {
35: PApplet.main(new String[]{"GoldenWindow"});
36: }
37:}


The output looked pretty tidy to me so I proceeded to set up some tests (I'm no expert at this so I would welcome any comments). Although its pretty clear I should refactor the code to render it more testable, and to impose the pre and post conditions.

   1:import org.junit.Test;
2:import static org.junit.Assert.*;
3:
4:/**
5: *
6: * @author Martin Prout
7: */
8:public class GoldenWindowTest {
9:
10: public GoldenWindowTest() {
11: }
12:
13: /**
14: * Test of setup method, of class GoldenWindow.
15: */
16: @Test
17: public void testSetup() {
18: System.out.println("setup");
19: GoldenWindow instance = new GoldenWindow();
20: assertTrue("Dimension < min", ((instance.wdth >= 10) && (instance.ht >= 10)));
21: assertTrue("Dimension > max", (instance.wdth <= instance.maxwidth) && (instance.ht <= instance.maxheight));
22: }
23:
24: /**
25: * Test of draw method, of class GoldenWindow.
26: */
27: @Test
28: public void testDraw() {
29: GoldenWindow instance = new GoldenWindow();
30: System.out.println("draw");
31: assertNotNull(instance);
32: instance.draw();
33: }
34:}


Update on templates for netbeans check this out http://code.google.com/p/netbeans-processing-template/NB: make sure add the core processing library to your project (revised Sunday 9:15 pm)

No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2