@Nullable private static JstdTestCaseStructure createTestCaseStructure( @NotNull JstdTestFileStructure jsTestFileStructure, @NotNull JSCallExpression testCaseCallExpression) { JSReferenceExpression referenceExpression = ObjectUtils.tryCast( testCaseCallExpression.getMethodExpression(), JSReferenceExpression.class); if (referenceExpression != null) { String referenceName = referenceExpression.getReferencedName(); if (TEST_CASE_NAME.equals(referenceName) || ASYNC_TEST_CASE_NAME.equals(referenceName)) { JSExpression[] arguments = JsPsiUtils.getArguments(testCaseCallExpression); if (arguments.length >= 1) { String testCaseName = JsPsiUtils.extractStringValue(arguments[0]); if (testCaseName != null) { JSObjectLiteralExpression testsObjectLiteral = null; if (arguments.length >= 2) { testsObjectLiteral = JsPsiUtils.extractObjectLiteralExpression(arguments[1]); } JstdTestCaseStructure testCaseStructure = new JstdTestCaseStructure( jsTestFileStructure, testCaseName, testCaseCallExpression, testsObjectLiteral); jsTestFileStructure.addTestCaseStructure(testCaseStructure); if (testsObjectLiteral != null) { fillTestCaseStructureByObjectLiteral(testCaseStructure, testsObjectLiteral); } return testCaseStructure; } } } } return null; }
@Nullable public JstdTestCaseStructure findEnclosingTestCaseByOffset(int documentOffset) { for (JstdTestCaseStructure testCaseStructure : myTestCaseStructures) { TextRange testCaseCallExpressionTextRange = testCaseStructure.getEnclosingCallExpression().getTextRange(); if (JsPsiUtils.containsOffsetStrictly(testCaseCallExpressionTextRange, documentOffset)) { return testCaseStructure; } } return null; }
@NotNull @Override public JstdTestFileStructure buildTestFileStructure(@NotNull JSFile jsFile) { JstdTestFileStructure jsTestFileStructure = new JstdTestFileStructure(jsFile); List<JSStatement> statements = JsPsiUtils.listStatementsInExecutionOrder(jsFile); for (JSStatement statement : statements) { fillJsTestFileStructure(jsTestFileStructure, statement); } jsTestFileStructure.postProcess(); return jsTestFileStructure; }
private static void fillTestCaseStructureByObjectLiteral( @NotNull JstdTestCaseStructure testCaseStructure, @NotNull JSObjectLiteralExpression testsObjectLiteral) { JSProperty[] properties = JsPsiUtils.getProperties(testsObjectLiteral); for (JSProperty property : properties) { JstdTestStructure testStructure = JstdTestStructure.newPropertyBasedTestStructure(property); if (testStructure != null) { testCaseStructure.addTestStructure(testStructure); } } }
private static void addPrototypeTests( @NotNull JstdTestCaseStructure testCaseStructure, @NotNull String referenceName, @NotNull JSStatement refStatement) { List<JSStatement> statements = JsPsiUtils.listStatementsInExecutionOrderNextTo(refStatement); for (JSStatement statement : statements) { JSExpressionStatement expressionStatement = ObjectUtils.tryCast(statement, JSExpressionStatement.class); if (expressionStatement != null) { JSAssignmentExpression assignmentExpr = ObjectUtils.tryCast(expressionStatement.getExpression(), JSAssignmentExpression.class); if (assignmentExpr != null) { JSDefinitionExpression wholeLeftDefExpr = ObjectUtils.tryCast(assignmentExpr.getLOperand(), JSDefinitionExpression.class); if (wholeLeftDefExpr != null) { JSReferenceExpression wholeLeftRefExpr = ObjectUtils.tryCast(wholeLeftDefExpr.getExpression(), JSReferenceExpression.class); if (wholeLeftRefExpr != null) { JSReferenceExpression testCaseAndPrototypeRefExpr = ObjectUtils.tryCast(wholeLeftRefExpr.getQualifier(), JSReferenceExpression.class); if (testCaseAndPrototypeRefExpr != null) { if ("prototype".equals(testCaseAndPrototypeRefExpr.getReferencedName())) { JSReferenceExpression testCaseRefExpr = ObjectUtils.tryCast( testCaseAndPrototypeRefExpr.getQualifier(), JSReferenceExpression.class); if (testCaseRefExpr != null && testCaseRefExpr.getQualifier() == null) { if (referenceName.equals(testCaseRefExpr.getReferencedName())) { addPrototypeTest( testCaseStructure, assignmentExpr.getROperand(), wholeLeftDefExpr); } } } } } } } } } }
private static void addPrototypeTest( @NotNull JstdTestCaseStructure testCaseStructure, @Nullable JSExpression rightAssignmentOperand, @NotNull JSDefinitionExpression wholeLeftDefExpr) { JSReferenceExpression wholeLeftRefExpr = ObjectUtils.tryCast(wholeLeftDefExpr.getExpression(), JSReferenceExpression.class); LeafPsiElement testMethodLeafPsiElement = null; if (wholeLeftRefExpr != null) { testMethodLeafPsiElement = ObjectUtils.tryCast(wholeLeftRefExpr.getReferenceNameElement(), LeafPsiElement.class); } if (testMethodLeafPsiElement != null && testMethodLeafPsiElement.getElementType() == JSTokenTypes.IDENTIFIER) { JSFunctionExpression jsFunctionExpression = JsPsiUtils.extractFunctionExpression(rightAssignmentOperand); JstdTestStructure jstdTestStructure = JstdTestStructure.newPrototypeBasedTestStructure( wholeLeftDefExpr, testMethodLeafPsiElement, jsFunctionExpression); if (jstdTestStructure != null) { testCaseStructure.addTestStructure(jstdTestStructure); } } }