Skip to content

Commit

Permalink
🧪 test: Enhance test for if stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 9, 2024
1 parent d154893 commit c1df33d
Showing 1 changed file with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,49 @@
import com.caoccao.javet.swc4j.outputs.Swc4jParseOutput;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

public class TestSwc4jAstIfStmt extends BaseTestSuiteSwc4jAst {
@Test
public void testNoAltWithoutParenthesis() throws Swc4jCoreException {
String code = "if (a) b;";
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstIfStmt ifStmt = assertAst(
script, script.getBody().get(0).as(Swc4jAstIfStmt.class), Swc4jAstType.IfStmt, 0, 9);
Swc4jAstIdent ident = assertAst(
ifStmt, ifStmt.getTest().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 4, 5);
assertEquals("a", ident.getSym());
Swc4jAstExprStmt exprStmt = assertAst(
ifStmt, ifStmt.getCons().as(Swc4jAstExprStmt.class), Swc4jAstType.ExprStmt, 7, 9);
ident = assertAst(
exprStmt, exprStmt.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 7, 8);
assertEquals("b", ident.getSym());
assertFalse(ifStmt.getAlt().isPresent());
assertSpan(code, script);
}

@Test
public void testNoAltWithParenthesis() throws Swc4jCoreException {
String code = "if (a) { b; }";
Swc4jParseOutput output = swc4j.parse(code, tsScriptParseOptions);
Swc4jAstScript script = output.getProgram().as(Swc4jAstScript.class);
Swc4jAstIfStmt ifStmt = assertAst(
script, script.getBody().get(0).as(Swc4jAstIfStmt.class), Swc4jAstType.IfStmt, 0, 13);
Swc4jAstIdent ident = assertAst(
ifStmt, ifStmt.getTest().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 4, 5);
assertEquals("a", ident.getSym());
Swc4jAstBlockStmt blockStmt = assertAst(
ifStmt, ifStmt.getCons().as(Swc4jAstBlockStmt.class), Swc4jAstType.BlockStmt, 7, 13);
Swc4jAstExprStmt exprStmt = assertAst(
blockStmt, blockStmt.getStmts().get(0).as(Swc4jAstExprStmt.class), Swc4jAstType.ExprStmt, 9, 11);
ident = assertAst(
exprStmt, exprStmt.getExpr().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 9, 10);
assertEquals("b", ident.getSym());
assertFalse(ifStmt.getAlt().isPresent());
assertSpan(code, script);
}

@Test
public void testWithParenthesis() throws Swc4jCoreException {
String code = "if (a) { b; } else { c; }";
Expand Down

0 comments on commit c1df33d

Please sign in to comment.