Author: allain.lalonde
Date: Fri Jul 17 19:24:58 2009
New Revision: 500

Modified:
     
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PText.java
     
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PTextTest.java

Log:
Adding tests for PText and making it return empty string by default rather  
than null. More inline with JTextField

Modified:  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PText.java
==============================================================================
---  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PText.java     
 
(original)
+++  
piccolo2d.java/trunk/core/src/main/java/edu/umd/cs/piccolo/nodes/PText.java     
 
Fri Jul 17 19:24:58 2009
@@ -83,6 +83,7 @@
      public PText() {
          super();
          setTextPaint(Color.BLACK);
+        text = "";
      }

      public PText(String aText) {
@@ -185,7 +186,7 @@
       */
      public void setText(String aText) {
          String old = text;
-        text = aText;
+        text = (aText == null) ? "" : aText;
          lines = null;
          recomputeLayout();
          invalidatePaint();

Modified:  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PTextTest.java
==============================================================================
---  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PTextTest.java 
 
(original)
+++  
piccolo2d.java/trunk/core/src/test/java/edu/umd/cs/piccolo/nodes/PTextTest.java 
 
Fri Jul 17 19:24:58 2009
@@ -38,28 +38,48 @@
          super(name);
      }

-    public void testCopy() {
-        PText aNode = new PText("Boo");
-        aNode = (PText) aNode.clone();
-        assertNotNull(aNode.getText());
-        assertNotNull(aNode.getFont());
+    public void testClone() {
+        PText textNode = new PText("Boo");
+        PText clonedNode = (PText) textNode.clone();
+        assertEquals("Boo", clonedNode.getText());
+        assertEquals(textNode.getFont(), clonedNode.getFont());
      }

-    public void testEmptyString() {
-        PText t = new PText();
-        t.setText("hello world");
-        t.setText("");
-        t.setText(null);
+    public void testTextIsEmptyByDefault() {
+       PText textNode = new PText();
+       assertEquals("", textNode.getText());
      }

+    public void testTextMayBeAssignedEmptyString() {
+       PText textNode = new PText("Before");
+       textNode.setText("");
+       assertEquals("", textNode.getText());
+    }
+
+    public void testTextNullGetsInterpretedAsEmptyString() {
+       PText text = new PText("Before");
+       text.setText(null);
+       assertEquals("", text.getText());
+    }
+
+
+    public void testBoundsGrowWithTextByDefault() {
+       PText text123 = new PText("123");
+       double width123 = text123.getBounds().getWidth();
+       
+       PText text1234 = new PText("1234");
+       
+       double width1234 = text1234.getBounds().getWidth();
+       
+        assertTrue(width123 < width1234);
+    }
+
      public void testBoundsOfEmptyString() {
          PText t = new PText();
-        t.setText("hello world");
-        assertTrue(t.getBoundsReference().getWidth() > 0);
          t.setText("");
-        assertTrue(t.getBoundsReference().getWidth() == 0);
+        assertEquals(0, t.getBoundsReference().getWidth(), 0.000001);
          t.setText(null);
-        assertTrue(t.getBoundsReference().getWidth() == 0);
+        assertEquals(0, t.getBoundsReference().getWidth(), 0.000001);
      }

      public void testToString() {

--~--~---------~--~----~------------~-------~--~----~
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to