KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > indent > QuestionLineContainsTest


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.indent;
35
36 import javax.swing.text.BadLocationException JavaDoc;
37
38 /**
39  * Tests the question rule which determines if the current line
40  * in the document contains the given character.
41  * <p>
42  * All tests check for the ':' character on the current line.
43  *
44  * @version $Id: QuestionLineContainsTest.java 3901 2006-06-30 05:28:11Z rcartwright $
45  */

46 public final class QuestionLineContainsTest extends IndentRulesTestCase {
47
48   /**
49    * Ensures that a line containing a colon is detected.
50    * Tests that a line of text containing a colon is detected.
51    */

52   public void testLineContainsColon() throws BadLocationException JavaDoc {
53     IndentRuleQuestion rule = new QuestionLineContains(':', null, null);
54
55     // Colon in text
56
_setDocText("return test ? x : y;\n}\n");
57     _doc.setCurrentLocation(0);
58     assertTrue("colon in text (after startdoc)",
59         rule.applyRule(_doc, Indenter.OTHER));
60     _setDocText("foo();\nreturn test ? x : y;\n}\n");
61     _doc.setCurrentLocation(10);
62     assertTrue("colon in text (after newline)",
63         rule.applyRule(_doc, Indenter.OTHER));
64     _doc.setCurrentLocation(25);
65     assertTrue("colon in text (after colon on line)",
66         rule.applyRule(_doc, Indenter.OTHER));
67   }
68   
69   /**
70    * Ensures that a line containing a colon is detected.
71    * Tests that a line does not contain a colon.
72    */

73   public void testLineDoesNotContainColon() throws BadLocationException JavaDoc {
74     IndentRuleQuestion rule = new QuestionLineContains(':', null, null);
75     
76     // No colon in text
77
_setDocText("foo();\nreturn test ? x : y;\n}\n");
78     _doc.setCurrentLocation(6);
79     assertTrue("no colon", !rule.applyRule(_doc, Indenter.OTHER));
80     _doc.setCurrentLocation(28);
81     assertTrue("line of close brace (no colon in text)", !rule.applyRule(_doc, Indenter.OTHER));
82   }
83
84   /**
85    * Ensures that a line containing a colon is detected.
86    * Tests that a line containing a commented out colon is identified as a
87    * line that does not contain a colon.
88    */

89   public void testLineDoesNotContainColonDueToComments() throws BadLocationException JavaDoc {
90     IndentRuleQuestion rule = new QuestionLineContains(':', null, null);
91
92     // No colon, single line comment
93
_setDocText("//case 1:\nreturn test; //? x : y\n}\n");
94     _doc.setCurrentLocation(0);
95     assertTrue("entire line with colon in comment (no colon, single line comment)",
96         !rule.applyRule(_doc, Indenter.OTHER));
97     _doc.setCurrentLocation(10);
98     assertTrue("part of line with colon in comment (no colon, single line comment)",
99         !rule.applyRule(_doc, Indenter.OTHER));
100
101     // No colon, multi-line comment
102
_setDocText("foo();\nreturn test; /*? x : y*/\n}\n");
103     _doc.setCurrentLocation(7);
104     assertTrue("no colon, colon in multi-line comment", !rule.applyRule(_doc, Indenter.OTHER));
105   }
106
107   /**
108    * Ensures that a line containing a colon is detected.
109    * Tests that a line containing a colon in quotes is identified as a
110    * line that does not contain a colon.
111    */

112   public void testLineDoesNotContainColonDueToQuotes() throws BadLocationException JavaDoc {
113     IndentRuleQuestion rule = new QuestionLineContains(':', null, null);
114   
115     // No colon, quotes
116
_setDocText("foo();\nreturn \"total: \" + sum\n}\n");
117     _doc.setCurrentLocation(7);
118     assertTrue("no colon, colon in quotes", !rule.applyRule(_doc, Indenter.OTHER));
119   }
120 }
121
Popular Tags