1 package org.incava.javadoc; 2 3 import java.awt.Point ; 4 import java.util.List ; 5 import junit.framework.TestCase; 6 7 8 public class TestJavadocParser extends TestCase 9 { 10 public TestJavadocParser(String name) 11 { 12 super(name); 13 } 14 15 public static List parse(String text) 16 { 17 JavadocParser jp = new JavadocParser(); 18 return jp.parse(text); 19 } 20 21 public void testBasic() 22 { 23 String text = ("/** This is a description.\n" + 24 " */\n" + 25 "class Test {\n" + 26 " /**\n" + 27 " * This is a description. \n" + 28 " * @@throws IOException \n" + 29 " */\n" + 30 " int f(int i) throws IOException { return 1; }\n" + 31 "}\n"); 32 List segments = parse(text); 33 34 assertNotNull("results", segments); 35 } 36 37 public void testNone() 38 { 39 String text = ""; 40 List segments = parse(text); 41 42 assertNull("results", segments); 43 } 44 45 public void testNotJavadoc() 46 { 47 String text = ("/* This is a description,\n" + 48 " * not in Javadoc format. */\n"); 49 List segments = parse(text); 50 51 assertNull("results", segments); 52 } 53 54 public void testEmpty() 55 { 56 List segments = null; 57 58 segments = parse("/**/"); 59 assertEquals("size of results", 0, segments.size()); 60 61 segments = parse("/** */"); 62 assertEquals("size of results", 0, segments.size()); 63 64 segments = parse("/** */"); 65 assertEquals("size of results", 0, segments.size()); 66 } 67 68 public void testDescribedSingleLine() 69 { 70 String text = "/** This is a test. */"; 71 List segments = parse(text); 72 73 assertEquals("size of results", 1, segments.size()); 74 assertEquals("description", (new Point (4, 19)), (Point )segments.get(0)); 75 assertEquals("description text", "This is a test.", text.substring(4, 19)); 76 } 77 78 public void testDescribedSeparateLine() 79 { 80 String text = "/** \n * This is a test. \n */"; 81 List segments = parse(text); 82 83 assertEquals("size of results", 1, segments.size()); 84 assertEquals("description", (new Point (8, 23)), (Point )segments.get(0)); 85 assertEquals("description text", "This is a test.", text.substring(8, 23)); 86 } 87 88 public void testDescribedMultiLineOnSeparateLine() 89 { 90 String text = "/** \n * This is a test.\n * There are many like it,\n but this one is mine. \n */"; 91 List segments = parse(text); 92 93 assertEquals("size of results", 1, segments.size()); 94 assertEquals("description", (new Point (8, 75)), (Point )segments.get(0)); 95 assertEquals("description text", "This is a test.\n * There are many like it,\n but this one is mine.", text.substring(8, 75)); 96 } 97 98 public void testDescribedOneTag() 99 { 100 String text = "/** \n * This is a test.\n * @tag description. \n */"; 101 List segments = parse(text); 102 103 assertEquals("size of results", 2, segments.size()); 104 assertEquals("description", (new Point (8, 23)), (Point )segments.get(0)); 105 assertEquals("description text", "This is a test.", text.substring(8, 23)); 106 assertEquals("tag text", "@tag description.", text.substring(27, 44)); 107 } 108 109 public void testUndescribedOneTag() 110 { 111 String text = "/** \n * @tag description. \n */"; 112 List segments = parse(text); 113 114 assertEquals("size of results", 2, segments.size()); 115 assertNull("no description", segments.get(0)); 116 assertEquals("tag", (new Point (8, 25)), (Point )segments.get(1)); 117 assertEquals("tag text", "@tag description.", text.substring(8, 25)); 118 } 119 120 public void testDescribedTwoTags() 121 { 122 String text = "/** \n * This is a test.\n * @tag0 description. \n * @tag1 Another description, \n * this one on multiple lines.\n */"; 123 List segments = parse(text); 124 125 assertEquals("size of results", 3, segments.size()); 126 assertEquals("description", (new Point (8, 23)), (Point )segments.get(0)); 127 assertEquals("description text", "This is a test.", text.substring(8, 23)); 128 assertEquals("tag", (new Point (27, 46)), (Point )segments.get(1)); 129 assertEquals("tag text", "@tag0 description. ", text.substring(27, 46)); 130 assertEquals("tag", (new Point (50, 108)), (Point )segments.get(2)); 131 assertEquals("tag text", "@tag1 Another description, \n * this one on multiple lines.", text.substring(50, 108)); 132 } 133 134 public void testDescribedTwoTagsEndCommentOnSameLine() 135 { 136 String text = "/** \n * This is a test.\n * @tag0 description. \n * @tag1 Another description, \n * this one on multiple lines. */"; 137 List segments = parse(text); 138 139 assertEquals("size of results", 3, segments.size()); 140 assertEquals("description", (new Point (8, 23)), (Point )segments.get(0)); 141 assertEquals("description text", "This is a test.", text.substring(8, 23)); 142 assertEquals("tag", (new Point (27, 46)), (Point )segments.get(1)); 143 assertEquals("tag text", "@tag0 description. ", text.substring(27, 46)); 144 assertEquals("tag", (new Point (50, 108)), (Point )segments.get(2)); 145 assertEquals("tag text", "@tag1 Another description, \n * this one on multiple lines.", text.substring(50, 108)); 146 } 147 148 public void testDescribedPreBlock() 149 { 150 String text = "/** \n" + 151 " * This is a test.\n" + 152 " * And here is its pre block: \n" + 153 " * <pre> \n" + 154 " * blah\n" + 155 " * blah\n" + 156 " * blah\n" + 157 " * </pre> \n" + 158 " */"; 159 List segments = parse(text); 160 161 assertEquals("size of results", 1, segments.size()); 162 assertEquals("description", (new Point (8, 110)), (Point )segments.get(0)); 163 assertEquals("description text", 164 "This is a test.\n" + 165 " * And here is its pre block: \n" + 166 " * <pre> \n" + 167 " * blah\n" + 168 " * blah\n" + 169 " * blah\n" + 170 " * </pre>", text.substring(8, 110)); 171 } 172 173 public void testLineStartsWithLink() 174 { 175 String text = ("/**\n" + 176 " * This is a description that makes a reference to \n" + 177 " * {@link SomeWhere#someThing(someType)} which should not\n" + 178 " * be treated as a comment/tag start.\n" + 179 " */\n"); 180 181 List segments = parse(text); 182 183 assertEquals("size of results", 1, segments.size()); 184 assertEquals("description", (new Point (7, 151)), (Point )segments.get(0)); 185 } 186 } 187 | Popular Tags |