1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package groovy.lang;
36
37 import org.codehaus.groovy.runtime.InvokerHelper;
38
39 import groovy.util.GroovyTestCase;
40
41 /***
42 * Tests the use of the structured Attribute type
43 *
44 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
45 * @version $Revision: 1.5 $
46 */
47 public class GStringTest extends GroovyTestCase {
48
49 public void testIterateOverText() {
50 DummyGString compString = new DummyGString(new Object[] { "James" });
51
52 assertArrayEquals(new String[] { "Hello ", "!" }, compString.getStrings());
53 assertArrayEquals(new Object[] { "James" }, compString.getValues());
54
55 assertEquals("Hello James!", compString.toString());
56 }
57
58 public void testAppendString() {
59 DummyGString a = new DummyGString(new Object[] { "James" });
60
61 GString result = a.plus(" how are you?");
62
63 assertEquals("Hello James! how are you?", result.toString());
64 }
65
66 public void testAppendString2() {
67 DummyGString a = new DummyGString(new Object[] { "James" }, new String[] { "Hello " });
68
69 GString result = a.plus(" how are you?");
70
71 System.out.println("Found: " + result);
72 System.out.println("Strings: " + InvokerHelper.toString(result.getStrings()));
73 System.out.println("Values: " + InvokerHelper.toString(result.getValues()));
74
75 assertEquals("Hello James how are you?", result.toString());
76 }
77
78 public void testAppendGString() {
79 DummyGString a = new DummyGString(new Object[] { "James" });
80 DummyGString b = new DummyGString(new Object[] { "Bob" });
81
82 GString result = a.plus(b);
83
84
85
86
87
88
89
90 assertEquals("Hello James!Hello Bob!", result.toString());
91 }
92
93 public void testAppendGString2() {
94 DummyGString a = new DummyGString(new Object[] { "James" }, new String[] { "Hello " });
95 DummyGString b = new DummyGString(new Object[] { "Bob" }, new String[] { "Hello " });
96
97 GString result = a.plus(b);
98
99
100
101
102
103
104
105 assertEquals("Hello JamesHello Bob", result.toString());
106 }
107
108 public void testEqualsAndHashCode() {
109 DummyGString a = new DummyGString(new Object[] { new Integer(1)});
110 DummyGString b = new DummyGString(new Object[] { new Long(1)});
111 DummyGString c = new DummyGString(new Object[] { new Double(2.3)});
112
113 assertTrue("a == b", a.equals(b));
114 assertEquals("hashcode a == b", a.hashCode(), b.hashCode());
115
116 assertFalse("a != c", a.equals(c));
117 assertTrue("hashcode a != c", a.hashCode() != c.hashCode());
118
119 assertEquals("a <=> b", 0, a.compareTo(b));
120 assertEquals("a <=> b", -1, a.compareTo(c));
121 }
122 }