private static List<TestedObject> parseExpectedTestedObject(String testDescription) { String[] testObjectData = testDescription.substring(testDescription.indexOf("// TESTED_OBJECT_KIND")).split("\n\n"); ArrayList<TestedObject> objects = new ArrayList<TestedObject>(); for (String testData : testObjectData) { if (testData.isEmpty()) continue; TestedObject testObject = new TestedObject(); testObject.textData = testData; List<String> testedObjects = findListWithPrefixes(testData, "// TESTED_OBJECTS: "); assertTrue("Cannot find TESTED_OBJECTS instruction", !testedObjects.isEmpty()); testObject.containingClass = testedObjects.get(0); if (testedObjects.size() == 1) { testObject.name = testedObjects.get(0); } else if (testedObjects.size() == 2) { testObject.name = testedObjects.get(1); } else { throw new IllegalArgumentException( "TESTED_OBJECTS instruction must contain one (for class) or two (for function and property) values"); } testObject.kind = findStringWithPrefixes(testData, "// TESTED_OBJECT_KIND: "); List<String> isFullName = findListWithPrefixes(testData, "// IS_FULL_CONTAINING_CLASS_NAME: "); if (isFullName.size() == 1) { testObject.isFullContainingClassName = Boolean.parseBoolean(isFullName.get(0)); } objects.add(testObject); } assertTrue("Test description not present!", !objects.isEmpty()); return objects; }
protected void doTest(String path) throws Exception { File ktFile = new File(path); assertTrue("Cannot find a file " + ktFile.getAbsolutePath(), ktFile.exists()); String fileText = FileUtil.loadFile(ktFile, true); KtFile psiFile = KotlinTestUtils.createFile(ktFile.getName(), fileText, jetCoreEnvironment.getProject()); OutputFileCollection outputFiles = GenerationUtils.compileFileGetClassFileFactoryForTest(psiFile, jetCoreEnvironment); List<TestedObject> testedObjects = parseExpectedTestedObject(fileText); for (TestedObject testedObject : testedObjects) { String className = null; for (OutputFile outputFile : outputFiles.asList()) { String filePath = outputFile.getRelativePath(); if (testedObject.isFullContainingClassName && filePath.equals(testedObject.containingClass + ".class")) { className = filePath; } else if (!testedObject.isFullContainingClassName && filePath.startsWith(testedObject.containingClass)) { className = filePath; } } assertNotNull( "Couldn't find a class file with name " + testedObject.containingClass, className); OutputFile outputFile = outputFiles.get(className); assertNotNull(outputFile); ClassReader cr = new ClassReader(outputFile.asByteArray()); TestClassVisitor classVisitor = getClassVisitor(testedObject.kind, testedObject.name, false); cr.accept(classVisitor, ClassReader.SKIP_CODE); if (!classVisitor.isExists()) { classVisitor = getClassVisitor(testedObject.kind, testedObject.name, true); cr.accept(classVisitor, ClassReader.SKIP_CODE); } boolean isObjectExists = !Boolean.valueOf(findStringWithPrefixes(testedObject.textData, "// ABSENT: ")); assertEquals( "Wrong object existence state: " + testedObject, isObjectExists, classVisitor.isExists()); if (isObjectExists) { assertEquals( "Wrong access flag for " + testedObject + " \n" + outputFile.asText(), getExpectedFlags(testedObject.textData), classVisitor.getAccess()); } } }