Skip to content

Commit faafdf1

Browse files
committed
adds assert
1 parent 9d92d69 commit faafdf1

File tree

3 files changed

+35
-49
lines changed

3 files changed

+35
-49
lines changed

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTKeywordVisitor.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,6 @@ public boolean visit(ArrayInitializer node) {
226226
return false;
227227
}
228228

229-
public boolean visit(AssertStatement node) {
230-
/*
231-
* TODO: should be implemented
232-
*/
233-
// return super.visit(node);
234-
/*
235-
* The assert statement should be passed when debugging in native Java
236-
* application mode. No need for JavaScript to throws errors.
237-
*/
238-
return false;
239-
}
240-
241229
@SuppressWarnings("null")
242230
public boolean visit(Assignment node) {
243231
Expression left = node.getLeftHandSide();

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTScriptVisitor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
1919
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
2020
import org.eclipse.jdt.core.dom.ArrayType;
21+
import org.eclipse.jdt.core.dom.AssertStatement;
2122
import org.eclipse.jdt.core.dom.BodyDeclaration;
2223
import org.eclipse.jdt.core.dom.CastExpression;
2324
import org.eclipse.jdt.core.dom.CharacterLiteral;
@@ -390,6 +391,18 @@ private void addInitMethod(List<?> bodyDeclarations) {
390391
buffer.append("}, 1);\r\n");
391392
}
392393

394+
public boolean visit(AssertStatement node) {
395+
Expression msg = node.getMessage();
396+
buffer.append("Clazz.assert(C$, function(){return ");
397+
node.getExpression().accept(this);
398+
if (msg != null) {
399+
buffer.append("}, function(){return ");
400+
msg.accept(this);
401+
}
402+
buffer.append("});\r\n");
403+
return false;
404+
}
405+
393406
public boolean visit(CastExpression node) {
394407
Expression expression = node.getExpression();
395408
ITypeBinding expBinding = expression.resolveTypeBinding();

sources/net.sf.j2s.core/test/src/test/Test_Char.java

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,38 @@ public static void main(String[] args) {
99
char c = (char) 0xdf84;
1010
int i = (int) c;
1111
c = (char) 0xd800df84;
12-
System.out.println(i == 57220);
12+
assert(i == 57220);
1313
System.out.println(s.length() + " " + Character.charCount(s.codePointAt(0)) + " " + Character.getName(c));
1414

15-
System.out.println(getIntFromChar1('c') == 99);
16-
System.out.println(getIntFromChar2('c') == 99);
17-
// System.out.println$Z(test.Test_Char.getIntFromChar1$C('c') == 99);
18-
// System.out.println$Z(test.Test_Char.getIntFromChar2$C('c') == 99);
15+
assert(getIntFromChar1('c') == 99);
16+
assert(getIntFromChar2('c') == 99);
1917

20-
System.out.println(getCharacterFromChar1('c') == 'c');
21-
System.out.println(getCharacterFromChar2('c') == 'c');
22-
// System.out.println$Z(((test.Test_Char.getCharacterFromChar1$C('c'))).charValue () === 99);
23-
// System.out.println$Z(((test.Test_Char.getCharacterFromChar2$C('c'))).charValue () === 99);
18+
assert(getCharacterFromChar1('c') == 'c');
19+
assert(getCharacterFromChar2('c') == 'c');
2420

25-
System.out.println(getCharFromCharacter1('c') == new Character('c'));
26-
System.out.println(getCharFromCharacter2('c') == 'c');
27-
// System.out.println$Z((test.Test_Char.getCharFromCharacter1$Character(99)).charCodeAt (0) == ((Clazz.$new(Character.construct,['c']))).charValue ());
28-
// System.out.println$Z(test.Test_Char.getCharFromCharacter2$Character(99) == 'c');
29-
30-
System.out.println(getIntFromInteger(99) == 'c');
31-
// System.out.println$Z(test.Test_Char.getIntFromInteger$Integer(new Integer ((99))) == 99);
32-
System.out.println(getIntegerFromInt(Integer.valueOf(99)) == 'c');
33-
// System.out.println$Z(test.Test_Char.getIntegerFromInt$I(((Integer.$valueOf(99))).intValue ()) == 99);
21+
assert(getCharFromCharacter1('c') == new Character('c'));
22+
assert(getCharFromCharacter2('c') == 'c');
23+
assert(getIntFromInteger(99) == 'c');
24+
assert(getIntegerFromInt(Integer.valueOf(99)) == 'c');
3425

35-
System.out.println(new Double(3) > new Double(1));
36-
System.out.println(new Double(3) <= new Float(5));
37-
// System.out.println$Z((( new Double(3))).doubleValue () > (( new Double(1))).doubleValue ());
38-
// System.out.println$Z((( new Double(3))).doubleValue () <= (( new Float(5))).floatValue ());
26+
assert(new Double(3) > new Double(1));
27+
assert(new Double(3) <= new Float(5));
3928

4029
char cc = 'c';
4130
cc = new Character('d');
4231
cc += new Character('\1');
4332
cc += 3 + 6 + new Character('\1');
44-
System.out.println(cc == 'o');
33+
assert(cc == 'o');
4534
int ii = 3 + 6 + new Character('\1');
46-
System.out.println(ii == 10);
35+
assert(ii == 10);
4736
double d = 3;
4837
int di = 'c';
4938
d /= 'c';
5039
di /= 'c';
51-
System.out.println(d == 3.0/99);
52-
System.out.println(di == 1);
40+
assert(d == 3.0/99);
41+
assert(di == 1);
5342
di = 100;
54-
System.out.println(di / 7 / 3 == 4);
55-
56-
// TODO check for String + Double/Float --> String + ... .toString()
57-
// and change to String + double/float --> String + new
58-
// Double/Float(...).toString()
43+
assert(di / 7 / 3 == 4);
5944

6045
s = "testing" + 3;
6146
s = "testing" + 3 + cc + 5;
@@ -78,15 +63,15 @@ public static void main(String[] args) {
7863
s += new Double(3);
7964
s += new Double(3) + cc + 5;
8065
System.out.println(s);
81-
System.out.println(s.equals("33.53o53o5.03.03.0o5null33.0119119.03.0119.0"));
66+
assert(s.equals("33.53o53o5.03.03.0o5null33.0119119.03.0119.0"));
8267
d = 333.5;
83-
System.out.println((int)d == 333);
84-
System.out.println((int)-d == -333);
85-
System.out.println((byte)d == 77);
68+
assert((int)d == 333);
69+
assert((int)-d == -333);
70+
assert((byte)d == 77);
8671
int b = 255;
87-
System.out.println((byte) b == -1);
72+
assert((byte) b == -1);
8873
b = 99;
89-
System.out.println((char) b == 'c');
74+
assert((char) b == 'c');
9075
}
9176

9277
private static int getIntFromInteger(Integer i) {

0 commit comments

Comments
 (0)