com.jetbrains.python.testing.doctest.PythonDocTestUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of python-community Show documentation
Show all versions of python-community Show documentation
A packaging of the IntelliJ Community Edition python-community library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2014 JetBrains s.r.o.
*
* 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 com.jetbrains.python.testing.doctest;
import com.google.common.collect.Lists;
import com.jetbrains.python.psi.*;
import java.util.List;
import java.util.StringTokenizer;
/**
* User: catherine
*/
public class PythonDocTestUtil {
private PythonDocTestUtil() {
}
public static List getDocTestCasesFromFile(PyFile file) {
List result = Lists.newArrayList();
for (PyClass cls : file.getTopLevelClasses()) {
if (isDocTestClass(cls)) {
result.add(cls);
}
}
for (PyFunction cls : file.getTopLevelFunctions()) {
if (isDocTestFunction(cls)) {
result.add(cls);
}
}
if (file.getDocStringExpression() != null) {
PyStringLiteralExpression docString = file.getDocStringExpression();
if (docString != null && hasExample(docString.getStringValue())) {
result.add(file);
}
}
return result;
}
public static boolean isDocTestFunction(PyFunction pyFunction) {
if (pyFunction.getDocStringExpression() == null) return false;
PyStringLiteralExpression docString = pyFunction.getDocStringExpression();
if (docString != null && !hasExample(docString.getStringValue())) return false;
return true;
}
public static boolean isDocTestClass(PyClass pyClass) {
for (PyFunction cls : pyClass.getMethods(false)) {
if (isDocTestFunction(cls)) {
return true;
}
}
if (pyClass.getDocStringExpression() != null) {
PyStringLiteralExpression docString = pyClass.getDocStringExpression();
if (docString != null && hasExample(docString.getStringValue())) {
return true;
}
}
return false;
}
public static boolean hasExample(final String docString) {
boolean hasExample = false;
StringTokenizer tokenizer = new StringTokenizer(docString, "\n");
while (tokenizer.hasMoreTokens()) {
String str = tokenizer.nextToken().trim();
if (str.startsWith(">>>")) {
hasExample = true;
break;
}
}
return hasExample;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy