Skip to content

Commit 4ea0cd7

Browse files
RagiGeorgesubyssurendran666
authored andcommitted
added new spec ASTConveter javadoc tests for the snippet tag #5035
1 parent c1013e8 commit 4ea0cd7

1 file changed

Lines changed: 311 additions & 0 deletions

File tree

org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/dom/ASTConverterJavadocTest_18.java

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,4 +1057,315 @@ public void testSnippetMultiLineTagsJavadoc4() throws JavaModelException {
10571057
assertEquals("Tag element fragment should be TextElement", true, tagElem.fragments().get(0) instanceof TextElement);
10581058
assertEquals("Fourth fragment should be TextElement", true, fragments.get(3) instanceof TextElement);
10591059
}
1060+
1061+
public void testSnippetMultilineOnlyJavadoc5() throws JavaModelException {
1062+
this.workingCopies = new ICompilationUnit[1];
1063+
this.workingCopies[0] = getWorkingCopy("/Converter_15_1/src/javadoc/X.java",
1064+
"""
1065+
package javadoc;
1066+
public class X {
1067+
/**
1068+
* {@snippet :
1069+
* int a = 1;
1070+
* int b = 2;
1071+
* }
1072+
*/
1073+
public static void foo(Object object) {}
1074+
}
1075+
"""
1076+
);
1077+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1078+
List unitComments = compilUnit.getCommentList();
1079+
assertEquals("Wrong number of comments", 1, unitComments.size());
1080+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1081+
TagElement snippetTag = getSnippetTag(javadoc);
1082+
List<TextElement> snippetFrags = snippetTag.fragments();
1083+
assertEquals("Invalid snippet elements", 3, snippetFrags.size());
1084+
assertEquals("Incorrect content for firstElemnt", " int a = 1;\n", snippetFrags.get(0).getText());
1085+
assertEquals("Incorrect content for secondElemnt", " int b = 2;\n", snippetFrags.get(1).getText());
1086+
1087+
}
1088+
1089+
public void testSnippetNestedBlocksJavadoc() throws JavaModelException {
1090+
this.workingCopies = new ICompilationUnit[1];
1091+
this.workingCopies[0] = getWorkingCopy(
1092+
"/Converter_15_1/src/javadoc/X.java",
1093+
"""
1094+
package javadoc;
1095+
public class X {
1096+
/**
1097+
* {@snippet:
1098+
* if (a) {
1099+
* while (b) {
1100+
* foo();
1101+
* }
1102+
* }
1103+
* }
1104+
*/
1105+
public static void foo(Object o) {}
1106+
}
1107+
"""
1108+
);
1109+
1110+
CompilationUnit cu = verifyComments(this.workingCopies[0]);
1111+
List unitComments = cu.getCommentList();
1112+
assertEquals("Wrong number of comments", 1, unitComments.size());
1113+
1114+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1115+
TagElement snippetTag = getSnippetTag(javadoc);
1116+
assertNotNull("Snippet tag should be present", snippetTag);
1117+
1118+
List fragments = snippetTag.fragments();
1119+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1120+
assertTrue( "Snippet should contain multiple text elements",fragments.size() >= 3);
1121+
assertTrue("Snippet should contain if block", ((TextElement) fragments.get(0)).getText().contains("if (a)"));
1122+
assertTrue("Snippet should contain while loop", ((TextElement) fragments.get(1)).getText().contains("while (b)"));
1123+
assertTrue("Snippet should contain method call", ((TextElement) fragments.get(2)).getText().contains("foo();"));
1124+
}
1125+
1126+
public void testSnippetEmptyBodyJavadoc() throws JavaModelException {
1127+
this.workingCopies = new ICompilationUnit[1];
1128+
this.workingCopies[0] = getWorkingCopy(
1129+
"/Converter_15_1/src/javadoc/X.java",
1130+
"""
1131+
package javadoc;
1132+
public class X {
1133+
/**
1134+
* {@snippet :
1135+
* }
1136+
*/
1137+
public static void foo(Object o) {}
1138+
}
1139+
"""
1140+
);
1141+
1142+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1143+
List unitComments = compilUnit.getCommentList();
1144+
assertEquals("Wrong number of comments", 1, unitComments.size());
1145+
1146+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1147+
TagElement snippetTag = getSnippetTag(javadoc);
1148+
assertNotNull("Snippet tag should be present", snippetTag);
1149+
1150+
List<?> fragments = snippetTag.fragments();
1151+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1152+
1153+
String empty_text = ((TextElement) fragments.get(0)).getText();
1154+
assertTrue("Empty snippet should not contain non-empty text",empty_text.trim().isEmpty());
1155+
}
1156+
1157+
public void testSnippetSingleElementJavadoc() throws JavaModelException {
1158+
this.workingCopies = new ICompilationUnit[1];
1159+
this.workingCopies[0] = getWorkingCopy(
1160+
"/Converter_15_1/src/javadoc/X.java",
1161+
"""
1162+
package javadoc;
1163+
public class X {
1164+
/**
1165+
* {@snippet :
1166+
* int a = 10;
1167+
* }
1168+
*/
1169+
public static void foo(Object o) {}
1170+
}
1171+
"""
1172+
);
1173+
1174+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1175+
List unitComments = compilUnit.getCommentList();
1176+
assertEquals("Wrong number of comments", 1, unitComments.size());
1177+
1178+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1179+
TagElement snippetTag = getSnippetTag(javadoc);
1180+
assertNotNull("Snippet tag should be present", snippetTag);
1181+
1182+
List<?> fragments = snippetTag.fragments();
1183+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1184+
assertTrue("Snippet should contain the statement", ((TextElement) fragments.get(0)).getText().contains("int a = 10;"));
1185+
}
1186+
1187+
public void testSnippetBlankLineInsideBodyJavadoc() throws JavaModelException {
1188+
this.workingCopies = new ICompilationUnit[1];
1189+
this.workingCopies[0] = getWorkingCopy(
1190+
"/Converter_15_1/src/javadoc/X.java",
1191+
"""
1192+
package javadoc;
1193+
public class X {
1194+
/**
1195+
* {@snippet :
1196+
*
1197+
* int a = 1;
1198+
*
1199+
* int b = 2;
1200+
* }
1201+
*/
1202+
public static void foo(Object o) {}
1203+
}
1204+
"""
1205+
);
1206+
1207+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1208+
List unitComments = compilUnit.getCommentList();
1209+
assertEquals("Wrong number of comments", 1, unitComments.size());
1210+
1211+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1212+
TagElement snippetTag = getSnippetTag(javadoc);
1213+
assertNotNull("Snippet tag should be present", snippetTag);
1214+
1215+
List<?> fragments = snippetTag.fragments();
1216+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1217+
1218+
assertTrue("Snippet should contain first statement", ((TextElement) fragments.get(1)).getText().contains("int a = 1;"));
1219+
assertTrue("Snippet should contain second statement", ((TextElement) fragments.get(3)).getText().contains("int b = 2;"));
1220+
}
1221+
1222+
public void testSnippetMultipleAttributesJavadoc() throws JavaModelException {
1223+
this.workingCopies = new ICompilationUnit[1];
1224+
this.workingCopies[0] = getWorkingCopy(
1225+
"/Converter_15_1/src/javadoc/X.java",
1226+
"""
1227+
package javadoc;
1228+
public class X {
1229+
/**
1230+
* {@snippet id="x1" lang=java region="main" :
1231+
* int a;
1232+
* }
1233+
*/
1234+
public static void foo(Object o) {}
1235+
}
1236+
"""
1237+
);
1238+
1239+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1240+
List unitComments = compilUnit.getCommentList();
1241+
assertEquals("Wrong number of comments", 1, unitComments.size());
1242+
1243+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1244+
TagElement snippetTag = getSnippetTag(javadoc);
1245+
assertNotNull("Snippet tag should be present", snippetTag);
1246+
1247+
List<?> fragments = snippetTag.fragments();
1248+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1249+
assertTrue("Snippet should contain the statement", ((TextElement) fragments.get(0)).getText().contains("int a;"));
1250+
}
1251+
1252+
public void testSnippetSingleQuoteAttributesJavadoc() throws JavaModelException {
1253+
this.workingCopies = new ICompilationUnit[1];
1254+
this.workingCopies[0] = getWorkingCopy(
1255+
"/Converter_15_1/src/javadoc/X.java",
1256+
"""
1257+
package javadoc;
1258+
public class X {
1259+
/**
1260+
* {@snippet id='demo' lang='java' :
1261+
* int a;
1262+
* }
1263+
*/
1264+
public static void foo(Object o) {}
1265+
}
1266+
"""
1267+
);
1268+
1269+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1270+
List unitComments = compilUnit.getCommentList();
1271+
assertEquals("Wrong number of comments", 1, unitComments.size());
1272+
1273+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1274+
TagElement snippetTag = getSnippetTag(javadoc);
1275+
assertNotNull("Snippet tag should be present", snippetTag);
1276+
1277+
List<?> fragments = snippetTag.fragments();
1278+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1279+
assertTrue("Snippet should contain the statement", ((TextElement) fragments.get(0)).getText().contains("int a;"));
1280+
}
1281+
1282+
1283+
public void testSnippetReplaceTagJavadoc() throws JavaModelException {
1284+
this.workingCopies = new ICompilationUnit[1];
1285+
this.workingCopies[0] = getWorkingCopy(
1286+
"/Converter_15_1/src/javadoc/X.java",
1287+
"""
1288+
package javadoc;
1289+
public class X {
1290+
/**
1291+
* {@snippet :
1292+
* System.out.println("abc"); // @replace substring="abc" replacement="xyz"
1293+
* }
1294+
*/
1295+
public static void foo(Object o) {}
1296+
}
1297+
"""
1298+
);
1299+
1300+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1301+
List unitComments = compilUnit.getCommentList();
1302+
assertEquals("Wrong number of comments", 1, unitComments.size());
1303+
1304+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1305+
TagElement snippetTag = getSnippetTag(javadoc);
1306+
assertNotNull("Snippet tag should be present", snippetTag);
1307+
1308+
List<?> fragments = snippetTag.fragments();
1309+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1310+
TagElement firstTag = (TagElement) fragments.get(0);
1311+
assertEquals(
1312+
"Unexpected tag inside snippet",
1313+
"@replace",
1314+
firstTag.getTagName()
1315+
);
1316+
List<?> tagFragments = firstTag.fragments();
1317+
assertFalse("Tag should have inner fragments", tagFragments.isEmpty());
1318+
TextElement textElement = (TextElement) tagFragments.get(0);
1319+
assertTrue(
1320+
"Snippet should contain System.out.println(\"abc\")",
1321+
textElement.getText().contains("System.out.println(\"abc\")")
1322+
);
1323+
assertFalse(
1324+
"Snippet should contain System.out.println(\"xyz\")",
1325+
textElement.getText().contains("System.out.println(\"xyz\")")
1326+
);
1327+
}
1328+
1329+
public void testSnippetLinkTagJavadoc() throws JavaModelException {
1330+
this.workingCopies = new ICompilationUnit[1];
1331+
this.workingCopies[0] = getWorkingCopy(
1332+
"/Converter_15_1/src/javadoc/X.java",
1333+
"""
1334+
package javadoc;
1335+
public class X {
1336+
/**
1337+
* {@snippet :
1338+
* System.out.println("abc"); // @link substring="System" target="System"
1339+
* }
1340+
*/
1341+
public static void foo(Object o) {}
1342+
}
1343+
"""
1344+
);
1345+
1346+
CompilationUnit compilUnit = verifyComments(this.workingCopies[0]);
1347+
List unitComments = compilUnit.getCommentList();
1348+
assertEquals("Wrong number of comments", 1, unitComments.size());
1349+
1350+
Javadoc javadoc = (Javadoc) unitComments.get(0);
1351+
TagElement snippetTag = getSnippetTag(javadoc);
1352+
assertNotNull("Snippet tag should be present", snippetTag);
1353+
1354+
List<?> fragments = snippetTag.fragments();
1355+
assertFalse("Snippet should have fragments", fragments.isEmpty());
1356+
1357+
TagElement firstTag = (TagElement) fragments.get(0);
1358+
assertEquals(
1359+
"Unexpected tag inside snippet",
1360+
"@link",
1361+
firstTag.getTagName()
1362+
);
1363+
List<?> tagFragments = firstTag.fragments();
1364+
assertFalse("Tag should have inner fragments", tagFragments.isEmpty());
1365+
TextElement textElement = (TextElement) tagFragments.get(0);
1366+
assertTrue(
1367+
"Original text should be preserved in AST",
1368+
textElement.getText().contains("System.out.println(\"abc\")")
1369+
);
1370+
}
10601371
}

0 commit comments

Comments
 (0)