Skip to content

Commit

Permalink
🧪 test: #3 Enhance test for class decl
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 10, 2024
1 parent c1df33d commit a91f6f0
Showing 1 changed file with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,53 @@

import com.caoccao.javet.swc4j.ast.BaseTestSuiteSwc4jAst;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstClass;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstDecorator;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstType;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstCallExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.module.Swc4jAstExportDecl;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstModule;
import com.caoccao.javet.swc4j.ast.program.Swc4jAstScript;
import com.caoccao.javet.swc4j.exceptions.Swc4jCoreException;
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 TestSwc4jAstClassDecl extends BaseTestSuiteSwc4jAst {
@Test
public void testDecoratorsExportClass() throws Swc4jCoreException {
String code = "@x() export class A {}";
Swc4jParseOutput output = swc4j.parse(code, tsModuleParseOptions);
Swc4jAstModule module = output.getProgram().as(Swc4jAstModule.class);
Swc4jAstExportDecl exportDecl = assertAst(
module, module.getBody().get(0).as(Swc4jAstExportDecl.class), Swc4jAstType.ExportDecl, 5, 22);
Swc4jAstClassDecl classDecl = assertAst(
exportDecl, exportDecl.getDecl().as(Swc4jAstClassDecl.class), Swc4jAstType.ClassDecl, 12, 22);
assertFalse(classDecl.isDeclare());
Swc4jAstClass clazz = assertAst(
classDecl, classDecl.getClazz(), Swc4jAstType.Class, 12, 22);
assertTrue(clazz.getBody().isEmpty());
assertFalse(clazz.isAbstract());
assertFalse(clazz.getSuperClass().isPresent());
assertFalse(clazz.getSuperTypeParams().isPresent());
assertFalse(clazz.getTypeParams().isPresent());
assertEquals(1, clazz.getDecorators().size());
Swc4jAstDecorator decorator = assertAst(
clazz, clazz.getDecorators().get(0), Swc4jAstType.Decorator, 0, 4);
Swc4jAstCallExpr callExpr = assertAst(
decorator, decorator.getExpr().as(Swc4jAstCallExpr.class), Swc4jAstType.CallExpr, 1, 4);
assertTrue(callExpr.getArgs().isEmpty());
assertFalse(callExpr.getTypeArgs().isPresent());
Swc4jAstIdent ident = assertAst(
callExpr, callExpr.getCallee().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 1, 2);
assertEquals("x", ident.getSym());
ident = assertAst(
classDecl, classDecl.getIdent(), Swc4jAstType.Ident, 18, 19);
assertEquals("A", ident.getSym());
assertSpan(code, module);
}

@Test
public void testEmptyClass() throws Swc4jCoreException {
String code = "class A {}";
Expand All @@ -46,4 +82,37 @@ public void testEmptyClass() throws Swc4jCoreException {
assertEquals("A", ident.getSym());
assertSpan(code, script);
}

@Test
public void testExportDecoratorsClass() throws Swc4jCoreException {
String code = "export @x() class A {}";
Swc4jParseOutput output = swc4j.parse(code, tsModuleParseOptions);
Swc4jAstModule module = output.getProgram().as(Swc4jAstModule.class);
Swc4jAstExportDecl exportDecl = assertAst(
module, module.getBody().get(0).as(Swc4jAstExportDecl.class), Swc4jAstType.ExportDecl, 0, 22);
Swc4jAstClassDecl classDecl = assertAst(
exportDecl, exportDecl.getDecl().as(Swc4jAstClassDecl.class), Swc4jAstType.ClassDecl, 12, 22);
assertFalse(classDecl.isDeclare());
Swc4jAstClass clazz = assertAst(
classDecl, classDecl.getClazz(), Swc4jAstType.Class, 12, 22);
assertTrue(clazz.getBody().isEmpty());
assertFalse(clazz.isAbstract());
assertFalse(clazz.getSuperClass().isPresent());
assertFalse(clazz.getSuperTypeParams().isPresent());
assertFalse(clazz.getTypeParams().isPresent());
assertEquals(1, clazz.getDecorators().size());
Swc4jAstDecorator decorator = assertAst(
clazz, clazz.getDecorators().get(0), Swc4jAstType.Decorator, 7, 11);
Swc4jAstCallExpr callExpr = assertAst(
decorator, decorator.getExpr().as(Swc4jAstCallExpr.class), Swc4jAstType.CallExpr, 8, 11);
assertTrue(callExpr.getArgs().isEmpty());
assertFalse(callExpr.getTypeArgs().isPresent());
Swc4jAstIdent ident = assertAst(
callExpr, callExpr.getCallee().as(Swc4jAstIdent.class), Swc4jAstType.Ident, 8, 9);
assertEquals("x", ident.getSym());
ident = assertAst(
classDecl, classDecl.getIdent(), Swc4jAstType.Ident, 18, 19);
assertEquals("A", ident.getSym());
assertSpan(code, module);
}
}

0 comments on commit a91f6f0

Please sign in to comment.