mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
doc tests + doc fixes
This commit is contained in:
parent
b7f242d031
commit
6acb7bcd51
@ -199,9 +199,9 @@ Examples of valid statements:
|
|||||||
// warning '==' behaves like the JavaScript one at runtime
|
// warning '==' behaves like the JavaScript one at runtime
|
||||||
Integer i = 2;
|
Integer i = 2;
|
||||||
assert i == 2;
|
assert i == 2;
|
||||||
Double d = i + 4;
|
Double d = i + 4d;
|
||||||
assert d.toString() == "6";
|
assert d.toString() == "6";
|
||||||
assert d == "6";
|
assert (Object) d == "6";
|
||||||
BiFunction<String, Integer, String> f = (s, i) -> { return s.substring(i); };
|
BiFunction<String, Integer, String> f = (s, i) -> { return s.substring(i); };
|
||||||
assert "bc" == f.apply("abc", 1);
|
assert "bc" == f.apply("abc", 1);
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
@ -214,9 +214,9 @@ Arrays can be used in JSweet and are transpiled to JavaScript arrays. Array init
|
|||||||
int[] arrayOfInts = { 1, 2, 3, 4};
|
int[] arrayOfInts = { 1, 2, 3, 4};
|
||||||
assert arrayOfInts.length == 4;
|
assert arrayOfInts.length == 4;
|
||||||
assert arrayOfInts[0] == 1;
|
assert arrayOfInts[0] == 1;
|
||||||
for(int i : arrayOfInts) {
|
int i = 0;
|
||||||
arrayOfInts[i] = arrayOfInts[i] - 1;
|
for (int intItem : arrayOfInts) {
|
||||||
assert arrayOfInts[i] == i;
|
assert arrayOfInts[i++] == intItem;
|
||||||
}
|
}
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
@ -240,7 +240,7 @@ Programmers should use this API most of the time, which is HTML5 compatible and
|
|||||||
As a consequence, programmers need to be able to switch to the JavaScript API when coming from a Java object. The \texttt{jsweet.util.Globals} class defines convenient static methods to cast back and forth core Java objects to their corresponding JSweet objects. For instance the \texttt{string(...)} method will allow the programmer to switch from the Java to the JSweet strings and conversely.
|
As a consequence, programmers need to be able to switch to the JavaScript API when coming from a Java object. The \texttt{jsweet.util.Globals} class defines convenient static methods to cast back and forth core Java objects to their corresponding JSweet objects. For instance the \texttt{string(...)} method will allow the programmer to switch from the Java to the JSweet strings and conversely.
|
||||||
|
|
||||||
\begin{lstlisting}[language=Java]
|
\begin{lstlisting}[language=Java]
|
||||||
import jsweet.util.Globals.string;
|
import static jsweet.util.Globals.string;
|
||||||
String str = "This is a test string";
|
String str = "This is a test string";
|
||||||
str.toLowerCase(); // valid: toLowerCase it defined both in Java and JavaScript
|
str.toLowerCase(); // valid: toLowerCase it defined both in Java and JavaScript
|
||||||
str.substr(1); // compile error: substr is not a Java method
|
str.substr(1); // compile error: substr is not a Java method
|
||||||
@ -251,7 +251,7 @@ string(str).substr(1); // valid: string(str) is a jsweet.lang.String.
|
|||||||
Here is another example that shows the use of the \texttt{array} method to access the \texttt{push} method available on JavaScript arrays.
|
Here is another example that shows the use of the \texttt{array} method to access the \texttt{push} method available on JavaScript arrays.
|
||||||
|
|
||||||
\begin{lstlisting}[language=Java]
|
\begin{lstlisting}[language=Java]
|
||||||
import jsweet.util.Globals.array;
|
import static jsweet.util.Globals.array;
|
||||||
String[] strings = { "a", "b", "c" };
|
String[] strings = { "a", "b", "c" };
|
||||||
array(strings).push("d");
|
array(strings).push("d");
|
||||||
assert strings[3] == "d";
|
assert strings[3] == "d";
|
||||||
@ -410,7 +410,7 @@ The following statements are valid statements in JSweet.
|
|||||||
\begin{lstlisting}[language=Java]
|
\begin{lstlisting}[language=Java]
|
||||||
MyEnum e = MyEnum.A;
|
MyEnum e = MyEnum.A;
|
||||||
assert MyEnum.A == e;
|
assert MyEnum.A == e;
|
||||||
assert e.name().equals("A");
|
assert e.name() == "A";
|
||||||
assert e.ordinal() == 0;
|
assert e.ordinal() == 0;
|
||||||
assert MyEnum.valueOf("A") == e;
|
assert MyEnum.valueOf("A") == e;
|
||||||
assert array(MyEnum.values()).indexOf(MyEnum.valueOf("C")) == 2;
|
assert array(MyEnum.values()).indexOf(MyEnum.valueOf("C")) == 2;
|
||||||
@ -631,7 +631,7 @@ Main methods are the program execution entry points and will be invoked globally
|
|||||||
public class C {
|
public class C {
|
||||||
private int n;
|
private int n;
|
||||||
public static C instance;
|
public static C instance;
|
||||||
public static main(String[] args) {
|
public static void main(String[] args) {
|
||||||
instance = new C();
|
instance = new C();
|
||||||
instance.n = 4;
|
instance.n = 4;
|
||||||
}
|
}
|
||||||
@ -680,7 +680,7 @@ public class C2 {
|
|||||||
n = 4;
|
n = 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert new C2.n == 4;
|
assert C2.n == 4;
|
||||||
\end{lstlisting}
|
\end{lstlisting}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
src/test/java/org/jsweet/test/transpiler/DocTests.java
Normal file
36
src/test/java/org/jsweet/test/transpiler/DocTests.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* JSweet - http://www.jsweet.org
|
||||||
|
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.jsweet.test.transpiler;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.jsweet.transpiler.util.EvaluationResult;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import source.doc.LanguageSpecifications;
|
||||||
|
|
||||||
|
public class DocTests extends AbstractTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLanguageSpecifications() {
|
||||||
|
EvaluationResult result = eval(getSourceFile(LanguageSpecifications.class));
|
||||||
|
|
||||||
|
assertEquals(4, result.<Number> get("main_n"));
|
||||||
|
assertEquals(true, result.<Boolean> get("finished"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
112
src/test/java/source/doc/LanguageSpecifications.java
Normal file
112
src/test/java/source/doc/LanguageSpecifications.java
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
package source.doc;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
import static jsweet.util.Globals.string;
|
||||||
|
import static jsweet.util.Globals.$export;
|
||||||
|
import static jsweet.util.Globals.array;
|
||||||
|
|
||||||
|
enum MyEnum {
|
||||||
|
A, B, C
|
||||||
|
}
|
||||||
|
|
||||||
|
class C1 {
|
||||||
|
int n;
|
||||||
|
|
||||||
|
{
|
||||||
|
n = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class C2 {
|
||||||
|
static int n;
|
||||||
|
|
||||||
|
static {
|
||||||
|
n = 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class LanguageSpecifications {
|
||||||
|
public static void basicConcepts_coreTypes_primitiveTypes() {
|
||||||
|
int i = 2;
|
||||||
|
assert i == 2;
|
||||||
|
double d = i + 4;
|
||||||
|
assert d == 6;
|
||||||
|
String s = "string" + '0' + i;
|
||||||
|
assert s == "string02";
|
||||||
|
boolean b = false;
|
||||||
|
assert!b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void basicConcepts_coreTypes_allowedJavaObjects() {
|
||||||
|
Integer i = 2;
|
||||||
|
assert i == 2;
|
||||||
|
|
||||||
|
Double d = i + 4d;
|
||||||
|
assert d.toString() == "6";
|
||||||
|
assert(Object) d == "6";
|
||||||
|
|
||||||
|
BiFunction<String, Integer, String> f = (str, integer) -> {
|
||||||
|
return str.substring(integer);
|
||||||
|
};
|
||||||
|
assert"bc" == f.apply("abc", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void basicConcepts_coreTypes_javaArrays() {
|
||||||
|
int[] arrayOfInts = { 1, 2, 3, 4 };
|
||||||
|
assert arrayOfInts.length == 4;
|
||||||
|
assert arrayOfInts[0] == 1;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
for (int intItem : arrayOfInts) {
|
||||||
|
assert arrayOfInts[i++] == intItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void basicConcepts_coreTypes_coreJavascript() {
|
||||||
|
String str = "This is a test string";
|
||||||
|
assert str.toLowerCase() == "this is a test string";
|
||||||
|
assert string(str).substr(1) == "his is a test string";
|
||||||
|
|
||||||
|
String[] strings = { "a", "b", "c" };
|
||||||
|
array(strings).push("d");
|
||||||
|
assert strings[3] == "d";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void basicConcepts_enums() {
|
||||||
|
|
||||||
|
MyEnum e = MyEnum.A;
|
||||||
|
assert MyEnum.A == e;
|
||||||
|
assert e.name() == "A";
|
||||||
|
assert e.ordinal() == 0;
|
||||||
|
assert MyEnum.valueOf("A") == e;
|
||||||
|
assert array(MyEnum.values()).indexOf(MyEnum.valueOf("C")) == 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void semantics_initializers() {
|
||||||
|
assert new C1().n == 4;
|
||||||
|
|
||||||
|
assert C2.n == 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int n;
|
||||||
|
public static LanguageSpecifications instance;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
instance = new LanguageSpecifications();
|
||||||
|
instance.n = 4;
|
||||||
|
|
||||||
|
$export("main_n", instance.n);
|
||||||
|
|
||||||
|
basicConcepts_coreTypes_primitiveTypes();
|
||||||
|
basicConcepts_coreTypes_allowedJavaObjects();
|
||||||
|
basicConcepts_coreTypes_javaArrays();
|
||||||
|
basicConcepts_coreTypes_coreJavascript();
|
||||||
|
basicConcepts_enums();
|
||||||
|
|
||||||
|
semantics_initializers();
|
||||||
|
|
||||||
|
$export("finished", true);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user