001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model.definitions.reducedmodel;
038:
039: import junit.framework.*;
040: import edu.rice.cs.drjava.DrJavaTestCase;
041:
042: /**
043: * Tests the Brace class.
044: * @version $Id: BraceTest.java 4255 2007-08-28 19:17:37Z mgricken $
045: */
046: public final class BraceTest extends DrJavaTestCase implements
047: ReducedModelStates {
048: protected Brace rparen;
049: protected Brace lparen;
050:
051: /**
052: * Set up Braces for testing.
053: */
054: public void setUp() throws Exception {
055: super .setUp();
056: lparen = Brace.MakeBrace("(", FREE);
057: rparen = Brace.MakeBrace(")", FREE);
058: }
059:
060: /**
061: * Create the test suite.
062: * @return BraceTest test suite
063: */
064: public static Test suite() {
065: return new TestSuite(BraceTest.class);
066: }
067:
068: /**
069: * Tests the successful construction of a Brace using the MakeBrace method.
070: */
071: public void testMakeBraceSuccess() {
072: Brace brace = Brace.MakeBrace("{", FREE);
073: assertEquals("{", brace.getType());
074: assertEquals(1, brace.getSize());
075: }
076:
077: /**
078: * Tests the failure to make a Brace with a non-special character.
079: */
080: public void testMakeBraceFailure() {
081: try {
082: Brace.MakeBrace("k", FREE);
083: } catch (BraceException e) {
084: assertEquals("Invalid brace type \"k\"", e.getMessage());
085: }
086: }
087:
088: /**
089: * Test the getType function for Braces.
090: */
091: public void testGetType() {
092: assertEquals("(", lparen.getType());
093: assertEquals(")", rparen.getType());
094: }
095:
096: /**
097: * Test the isShadowed() function.
098: */
099: public void testIsShadowed() {
100: assertTrue("#0.0", !lparen.isShadowed());
101: lparen.setState(INSIDE_DOUBLE_QUOTE);
102: assertEquals("#0.0.1", INSIDE_DOUBLE_QUOTE, lparen.getState());
103: assertTrue("#0.1", lparen.isShadowed());
104: rparen.setState(INSIDE_BLOCK_COMMENT);
105: assertTrue("#0.2", rparen.isShadowed());
106: rparen.setState(FREE);
107: assertTrue("#0.3", !rparen.isShadowed());
108: }
109:
110: /**
111: * Test the isQuoted() function.
112: */
113: public void testIsQuoted() {
114: assertTrue("#0.0", !lparen.isQuoted());
115: lparen.setState(INSIDE_DOUBLE_QUOTE);
116: assertTrue("#0.1", lparen.isQuoted());
117: lparen.setState(INSIDE_BLOCK_COMMENT);
118: assertTrue("#0.2", !lparen.isQuoted());
119: }
120:
121: /**
122: * Test the isCommented() function.
123: */
124: public void testIsCommented() {
125: assertTrue("#0.0", !lparen.isCommented());
126: lparen.setState(INSIDE_BLOCK_COMMENT);
127: assertTrue("#0.1", lparen.isCommented());
128: lparen.setState(INSIDE_DOUBLE_QUOTE);
129: assertTrue("#0.2", !lparen.isCommented());
130: }
131:
132: /**
133: * Test the toString method.
134: */
135: public void testToString() {
136: assertEquals(" (", lparen.toString());
137: assertEquals(" )", rparen.toString());
138: }
139:
140: /**
141: * Test the flip() method.
142: */
143: public void testFlip() {
144: lparen.flip();
145: rparen.flip();
146: assertEquals("(", rparen.getType());
147: assertEquals(")", lparen.getType());
148: }
149:
150: /**
151: * Test isOpen() and isClosed().
152: */
153: public void testOpenClosed() {
154: assertTrue(lparen.isOpen());
155: assertTrue(rparen.isClosed());
156: }
157:
158: /**
159: * Test isMatch(Brace) method.
160: */
161: public void testIsMatch() {
162: Brace bracket = Brace.MakeBrace("]", FREE);
163: Brace dummy = Brace.MakeBrace("", FREE);
164: assertTrue(lparen.isMatch(rparen));
165: assertTrue(!lparen.isMatch(bracket));
166: assertTrue(!lparen.isMatch(dummy));
167: assertTrue(!dummy.isMatch(lparen));
168: }
169: }
|