001: // Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
002: // Copyright (c) 2002 Cunningham & Cunningham, Inc.
003: // Released under the terms of the GNU General Public License version 2 or later.
004: package fit;
005:
006: import fitnesse.testutil.RegexTest;
007: import fit.exception.*;
008:
009: public class BindingTest extends RegexTest {
010: private TestFixture fixture;
011: private Parse cell1;
012: private Parse cell2;
013: private Parse cell3;
014: private Parse cell4;
015:
016: protected void setUp() throws Exception {
017: fixture = new TestFixture();
018: Parse table = new Parse(
019: "<table><tr><td>123</td><td>321</td><td>abc</td><td></td></tr></table>");
020: cell1 = table.parts.parts;
021: cell2 = table.parts.parts.more;
022: cell3 = table.parts.parts.more.more;
023: cell4 = table.parts.parts.more.more.more;
024: }
025:
026: public void testConstruction() throws Throwable {
027: assertEquals(Binding.QueryBinding.class, Binding.create(
028: fixture, "intMethod()").getClass());
029: assertEquals(Binding.QueryBinding.class, Binding.create(
030: fixture, "intMethod?").getClass());
031: assertEquals(Binding.QueryBinding.class, Binding.create(
032: fixture, "intMethod!").getClass());
033: assertEquals(Binding.SetBinding.class, Binding.create(fixture,
034: "intField").getClass());
035: assertEquals(Binding.RecallBinding.class, Binding.create(
036: fixture, "intField=").getClass());
037: assertEquals(Binding.SaveBinding.class, Binding.create(fixture,
038: "=intMethod()").getClass());
039: assertEquals(Binding.SaveBinding.class, Binding.create(fixture,
040: "=intField").getClass());
041: }
042:
043: public static class TestFixture extends Fixture {
044: public int intField = 0;
045:
046: public int intMethod() {
047: return intField;
048: }
049: }
050:
051: public void testQueryBinding() throws Throwable {
052: Binding binding = Binding.create(fixture, "intMethod()");
053: binding.doCell(fixture, cell1);
054: assertEquals(1, fixture.counts.wrong);
055:
056: fixture.intField = 321;
057: binding.doCell(fixture, cell2);
058: assertEquals(1, fixture.counts.right);
059: }
060:
061: public void testSetBinding() throws Throwable {
062: Binding binding = Binding.create(fixture, "intField");
063: binding.doCell(fixture, cell1);
064: assertEquals(123, fixture.intField);
065:
066: binding.doCell(fixture, cell2);
067: assertEquals(321, fixture.intField);
068: }
069:
070: public void testQueryBindingWithBlackCell() throws Throwable {
071: Binding binding = Binding.create(fixture, "intField");
072: binding.doCell(fixture, cell4);
073: assertSubString("0", cell4.text());
074: }
075:
076: public void testSaveBinding() throws Throwable {
077: Binding binding = Binding.create(fixture, "=intMethod()");
078: binding.doCell(fixture, cell1);
079: assertEquals("0", Fixture.getSymbol("123"));
080: assertSubString("123 = 0", cell1.text());
081:
082: fixture.intField = 999;
083: binding.doCell(fixture, cell2);
084: assertEquals("999", Fixture.getSymbol("321"));
085: }
086:
087: public void testRecallBinding() throws Throwable {
088: Binding binding = Binding.create(fixture, "intField=");
089: Fixture.setSymbol("123", "999");
090: binding.doCell(fixture, cell1);
091: assertEquals(999, fixture.intField);
092:
093: binding.doCell(fixture, cell3);
094: assertSubString("No such symbol: abc", cell3.text());
095: }
096:
097: public void testRecallBindingSymbolTableText() throws Throwable {
098: Binding binding = Binding.create(fixture, "intField=");
099: Fixture.setSymbol("123", "999");
100: binding.doCell(fixture, cell1);
101: assertEquals("123 = 999", cell1.text());
102: }
103:
104: public void testUseOfGracefulNamingForMethods() throws Throwable {
105: checkForMethodBinding("intMethod()", true);
106: checkForMethodBinding("int Method?", true);
107: checkForMethodBinding("int method?", true);
108: checkForMethodBinding("intmethod?", false);
109: checkForMethodBinding("Intmethod?", false);
110: checkForMethodBinding("IntMethod?", false);
111: }
112:
113: public void testUseOfGracefulNamingForFields() throws Throwable {
114: checkForFieldBinding("intField", true);
115: checkForFieldBinding("int Field", true);
116: checkForFieldBinding("int field", true);
117: checkForFieldBinding("intfield", false);
118: checkForFieldBinding("Intfield", false);
119: checkForFieldBinding("IntField", false);
120: }
121:
122: private void checkForMethodBinding(String name, boolean expected)
123: throws Throwable {
124: Binding binding = null;
125: try {
126: binding = Binding.create(fixture, name);
127: } catch (NoSuchMethodFitFailureException e) {
128: assertFalse("method not found", expected);
129: return;
130: }
131: assertTrue("method was found", expected);
132: assertTrue(binding instanceof Binding.QueryBinding);
133: assertEquals("intMethod", binding.adapter.method.getName());
134: }
135:
136: private void checkForFieldBinding(String name, boolean expected)
137: throws Throwable {
138: Binding binding = null;
139: try {
140: binding = Binding.create(fixture, name);
141: } catch (NoSuchFieldFitFailureException e) {
142: assertFalse("field not found", expected);
143: return;
144: }
145: assertTrue("field was found", expected);
146: assertTrue(binding instanceof Binding.SetBinding);
147: assertEquals("intField", binding.adapter.field.getName());
148: }
149: }
|