/projects/drjava-stable-20100913-r5387/src/src/edu/rice/cs/drjava/model/definitions/indent/QuestionPrevLineStartsWithTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 171 lines · 81 code · 28 blank · 62 comment · 0 complexity · f3d203d499a6465e80a667e88d92c7f1 MD5 · raw file

  1. /*BEGIN_COPYRIGHT_BLOCK
  2. *
  3. * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  25. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * This software is Open Source Initiative approved Open Source Software.
  30. * Open Source Initative Approved is a trademark of the Open Source Initiative.
  31. *
  32. * This file is part of DrJava. Download the current version of this project
  33. * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
  34. *
  35. * END_COPYRIGHT_BLOCK*/
  36. package edu.rice.cs.drjava.model.definitions.indent;
  37. import javax.swing.text.BadLocationException;
  38. /**
  39. * Tests the indention rule which detects whether the immediately previous line
  40. * starts with a particular string.
  41. * @version $Id: QuestionPrevLineStartsWithTest.java 5175 2010-01-20 08:46:32Z mgricken $
  42. */
  43. public final class QuestionPrevLineStartsWithTest extends IndentRulesTestCase {
  44. /** Tests not having the prefix in the text. */
  45. public void testNoPrefix() throws BadLocationException {
  46. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{", null, null);
  47. // Open brace
  48. _setDocText("}\nfoo();\n}\n");
  49. assertTrue("line after close brace (no open brace)", !rule.testApplyRule(_doc, 2, Indenter.IndentReason.OTHER));
  50. assertTrue("line after text (no open brace)", !rule.testApplyRule(_doc, 9, Indenter.IndentReason.OTHER));
  51. assertTrue("line after text (no open brace)", !rule.testApplyRule(_doc, 10, Indenter.IndentReason.OTHER));
  52. // Star
  53. rule = new QuestionPrevLineStartsWith("*", null, null);
  54. _setDocText("{\nfoo();");
  55. assertTrue("no star", !rule.testApplyRule(_doc, 6, Indenter.IndentReason.OTHER));
  56. }
  57. /** Tests hitting start of document. */
  58. public void testStartOfDocument() throws BadLocationException {
  59. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("{", null, null);
  60. // Hits docstart
  61. _setDocText("\nfoo();");
  62. assertTrue("first line", !rule.testApplyRule(_doc, 0, Indenter.IndentReason.OTHER));
  63. assertTrue("second line", !rule.testApplyRule(_doc, 2, Indenter.IndentReason.OTHER));
  64. }
  65. /** Tests prefix on current line. */
  66. public void testPrefixOnCurrLine() throws BadLocationException {
  67. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}", null, null);
  68. // Prefix at start of current line
  69. _setDocText("} foo();");
  70. assertTrue("before brace", !rule.testApplyRule(_doc, 0, Indenter.IndentReason.OTHER));
  71. assertTrue("after brace", !rule.testApplyRule(_doc, 2, Indenter.IndentReason.OTHER));
  72. // Prefix in middle of current line
  73. _setDocText("foo();\n bar(); } foo();");
  74. assertTrue("before brace", !rule.testApplyRule(_doc, 7, Indenter.IndentReason.OTHER));
  75. assertTrue("after brace", !rule.testApplyRule(_doc, 18, Indenter.IndentReason.OTHER));
  76. }
  77. /** Tests having prev line start with prefix, with text following */
  78. public void testStartsWithPrefixWithText() throws BadLocationException {
  79. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("}", null, null);
  80. // Prefix plus text (no space)
  81. _setDocText("}bar();\nfoo();\nbar();");
  82. assertTrue("line of brace (no space)", !rule.testApplyRule(_doc, 2, Indenter.IndentReason.OTHER));
  83. assertTrue("line after brace (no space)", rule.testApplyRule(_doc, 8, Indenter.IndentReason.OTHER));
  84. assertTrue("two lines after brace (no space)", !rule.testApplyRule(_doc, 16, Indenter.IndentReason.OTHER));
  85. // Prefix plus text (with space)
  86. rule = new QuestionPrevLineStartsWith("*", null, null);
  87. _setDocText("foo\n * comment\nbar");
  88. assertTrue("just before star (with space)", !rule.testApplyRule(_doc, 4, Indenter.IndentReason.OTHER));
  89. assertTrue("just after star (with space)", !rule.testApplyRule(_doc, 6, Indenter.IndentReason.OTHER));
  90. assertTrue("line after star (with space)", rule.testApplyRule(_doc, 16, Indenter.IndentReason.OTHER));
  91. }
  92. /** Tests having prev line start with prefix, with no text following */
  93. public void testStartsWithPrefixNoText() throws BadLocationException {
  94. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
  95. // Prefix plus no text (no space)
  96. _setDocText("foo();\n*\nbar();\n}");
  97. assertTrue("line of star (no space)", !rule.testApplyRule(_doc, 8, Indenter.IndentReason.OTHER));
  98. assertTrue("line after star (no space)", rule.testApplyRule(_doc, 10, Indenter.IndentReason.OTHER));
  99. assertTrue("two lines after star (no space)", !rule.testApplyRule(_doc, 16, Indenter.IndentReason.OTHER));
  100. // Prefix plus no text (with space)
  101. _setDocText("foo();\n * \nbar();\n{");
  102. assertTrue("line of star (with space)", !rule.testApplyRule(_doc, 7, Indenter.IndentReason.OTHER));
  103. assertTrue("just after star (with space)", !rule.testApplyRule(_doc, 11, Indenter.IndentReason.OTHER));
  104. assertTrue("line after star (with space)", rule.testApplyRule(_doc, 13, Indenter.IndentReason.OTHER));
  105. }
  106. /** Tests having a multiple character prefix. */
  107. public void testMultipleCharPrefix() throws BadLocationException {
  108. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("* ", null, null);
  109. // Multi-char prefix
  110. _setDocText("*\n *\n * \n * foo\nbar");
  111. assertTrue("star", !rule.testApplyRule(_doc, 2, Indenter.IndentReason.OTHER));
  112. assertTrue("space star", !rule.testApplyRule(_doc, 5, Indenter.IndentReason.OTHER));
  113. assertTrue("space star space", rule.testApplyRule(_doc, 11, Indenter.IndentReason.OTHER));
  114. assertTrue("space star space text", rule.testApplyRule(_doc, 16, Indenter.IndentReason.OTHER));
  115. }
  116. /** Tests having a commented prefix. */
  117. public void testCommentedPrefix() throws BadLocationException {
  118. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
  119. // Star in comment
  120. _setDocText("/**\n* \ncomment\n*/");
  121. assertTrue("just before star", !rule.testApplyRule(_doc, 4, Indenter.IndentReason.OTHER));
  122. assertTrue("just after star", !rule.testApplyRule(_doc, 6, Indenter.IndentReason.OTHER));
  123. assertTrue("line after star", rule.testApplyRule(_doc, 7, Indenter.IndentReason.OTHER));
  124. assertTrue("line after star", !rule.testApplyRule(_doc, 15, Indenter.IndentReason.OTHER));
  125. }
  126. /** Tests a prefix that begins a comment. */
  127. public void testCommentPrefix() throws BadLocationException {
  128. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("/**", null, null);
  129. // Star in comment
  130. _setDocText("/**\n* \ncomment\n*/");
  131. assertTrue("just before star", rule.testApplyRule(_doc, 4, Indenter.IndentReason.OTHER));
  132. assertTrue("just after star", rule.testApplyRule(_doc, 6, Indenter.IndentReason.OTHER));
  133. assertTrue("line after star", !rule.testApplyRule(_doc, 7, Indenter.IndentReason.OTHER));
  134. assertTrue("line after star", !rule.testApplyRule(_doc, 15, Indenter.IndentReason.OTHER));
  135. }
  136. /** Tests having text on a line before the prefix. */
  137. public void testDoesNotStartWithPrefix() throws BadLocationException {
  138. IndentRuleQuestion rule = new QuestionPrevLineStartsWith("*", null, null);
  139. // Star in text, not starting line
  140. _setDocText("foo(); *\nbar();\n");
  141. assertTrue("line after star", !rule.testApplyRule(_doc, 10, Indenter.IndentReason.OTHER));
  142. }
  143. }