Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,9 @@ else if ('0' <= c && c <= '9') { // float literal
next = nextToken();
} while (next != null && AFTER_VAR_TOKENS.contains(next.id()));

varKeyword = next != null && next.id() == JavaTokenId.IDENTIFIER;
varKeyword = next != null
&& (next.id() == JavaTokenId.IDENTIFIER
|| next.id() == JavaTokenId.UNDERSCORE);
}

input.backup(input.readLengthEOF()- len);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,23 @@ public void testVarIdent() {
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.SEMICOLON, ";");
}

public void testVarUnnamed() {
String text = "var _ = 0;";
InputAttributes attr = new InputAttributes();
attr.setValue(JavaTokenId.language(), "version", Integer.valueOf(22), true);
TokenHierarchy<?> hi = TokenHierarchy.create(text, false, JavaTokenId.language(), EnumSet.noneOf(JavaTokenId.class), attr);
TokenSequence<?> ts = hi.tokenSequence();

LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.VAR, "var");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " ");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.UNDERSCORE, "_");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " ");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.EQ, "=");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.WHITESPACE, " ");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.INT_LITERAL, "0");
LexerTestUtilities.assertNextTokenEquals(ts, JavaTokenId.SEMICOLON, ";");
}

public void testVarWeird() {
String text = "var = 0; varu = 0; val = 0; if (a.var);";
InputAttributes attr = new InputAttributes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6274,13 +6274,12 @@ public void testForNoCondition() throws Exception {
}

public void testForVar1() throws Exception {
sourceLevel = "10";
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile, "");
FileObject testSourceFO = FileUtil.toFileObject(testFile);
DataObject testSourceDO = DataObject.find(testSourceFO);
EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class);
String oldLevel = JavaSourceTest.SourceLevelQueryImpl.sourceLevel;
JavaSourceTest.SourceLevelQueryImpl.sourceLevel = "1.10";
final Document doc = ec.openDocument();
doc.putProperty(Language.class, JavaTokenId.language());
String content
Expand All @@ -6299,17 +6298,15 @@ public void testForVar1() throws Exception {
+ " }\n"
+ "}\n";
reformat(doc, content, golden);
JavaSourceTest.SourceLevelQueryImpl.sourceLevel = oldLevel;
}

public void testForVar2() throws Exception {
sourceLevel = "10";
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile, "");
FileObject testSourceFO = FileUtil.toFileObject(testFile);
DataObject testSourceDO = DataObject.find(testSourceFO);
EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class);
String oldLevel = JavaSourceTest.SourceLevelQueryImpl.sourceLevel;
JavaSourceTest.SourceLevelQueryImpl.sourceLevel = "1.10";
final Document doc = ec.openDocument();
doc.putProperty(Language.class, JavaTokenId.language());
String content
Expand All @@ -6328,7 +6325,52 @@ public void testForVar2() throws Exception {
+ " }\n"
+ "}\n";
reformat(doc, content, golden);
JavaSourceTest.SourceLevelQueryImpl.sourceLevel = oldLevel;
}

public void testForVarUnnamed() throws Exception {
sourceLevel = "22";
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile, "");
FileObject testSourceFO = FileUtil.toFileObject(testFile);
DataObject testSourceDO = DataObject.find(testSourceFO);
EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class);
final Document doc = ec.openDocument();
doc.putProperty(Language.class, JavaTokenId.language());
String content
= """
package hierbas.del.litoral;

public class Test {

public static void main(String[] args) {
int[] orderIDs = {34, 45, 23, 27, 15};
int total = 0;
for ( var _:orderIDs) {
total++;
}
}
}
""";

String golden
= """
package hierbas.del.litoral;

public class Test {

public static void main(String[] args) {
int[] orderIDs = {34, 45, 23, 27, 15};
int total = 0;
for (var _ : orderIDs) {
total++;
}
}
}
""";

//check no change then formatted
reformat(doc, golden, golden);
reformat(doc, content, golden);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe reformat shoud reformat twice,because any reformat in this file should converge to same golden

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean? But the tests are isolated and don't (shouldn't) affect each other. The entire contents of doc is set to the second String parameter, reformatted, and tested against the third String parameter.

The first check is there to make sure golden does not change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly ensure golden is golden but for every use of reformat in the test.
More for the future of course (because it break 2 tests):

private void reformat(Document doc, String content, String golden) throws Exception {
        reformat(doc, golden, golden, 0, golden.length());
        reformat(doc, content, golden, 0, content.length());
    }

Tested for fun (only 2 golden not golden),
test135210 generate javadoc line
testJavadoc reformat is in 2 step

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, OK, that would make a lot of sense!

}

public void testTryBlockAfterIf() throws Exception {
Expand Down
Loading