📄 codecompletiontestsbase.java
字号:
}
/**
* @return the default interpreter info for the current manager
*/
protected InterpreterInfo getDefaultInterpreterInfo() {
IInterpreterManager iMan = getInterpreterManager();
InterpreterInfo info = (InterpreterInfo) iMan.getDefaultInterpreterInfo(getProgressMonitor());
return info;
}
/**
* @return a progress monitor
*/
protected IProgressMonitor getProgressMonitor() {
if (DEBUG_TESTS_BASE){
return new PrintProgressMonitor();
}
return new NullProgressMonitor();
}
/**
* Sets the interpreter manager we should use
*/
protected void setInterpreterManager() {
PydevPlugin.setPythonInterpreterManager(new PythonInterpreterManagerStub(preferences));
}
/**
* @param info the information for the system manager that we just restored
*/
protected void afterRestorSystemPythonPath(InterpreterInfo info) {
nature = null; //has to be restored for the project, as we just restored the system pythonpath
//ok, the system manager must be there
assertTrue(info.getModulesManager().getSize() > 0);
//and it must be registered as the pydev interpreter manager
IInterpreterManager iMan2 = getInterpreterManager();
InterpreterInfo info2 = (InterpreterInfo) iMan2.getDefaultInterpreterInfo(getProgressMonitor());
assertTrue(info2 == info);
//does it have the loaded modules?
assertTrue(info2.getModulesManager().getSize() > 0);
assertTrue(info2.getModulesManager().getBuiltins().length > 0);
}
/**
* @see #restorePythonPath(boolean)
*
* same as the restorePythonPath function but also includes the site packages in the distribution
*/
public void restorePythonPath(String path, boolean force){
restoreSystemPythonPath(force, path);
restoreProjectPythonPath(force, TestDependent.TEST_PYSRC_LOC);
restoreProjectPythonPath2(force, TestDependent.TEST_PYSRC_LOC2);
checkSize();
}
/**
* @see #restorePythonPath(boolean)
*
* same as the restorePythonPath function but also includes the site packages in the distribution
*/
public void restorePythonPathWithSitePackages(boolean force){
restoreSystemPythonPath(force, TestDependent.GetCompletePythonLib(true));
restoreProjectPythonPath(force, TestDependent.TEST_PYSRC_LOC);
restoreProjectPythonPath2(force, TestDependent.TEST_PYSRC_LOC2);
checkSize();
}
/**
* restores the pythonpath with the source library (system manager) and the source location for the tests (project manager)
*
* @param force whether this should be forced, even if it was previously created for this class
*/
public void restorePythonPath(boolean force){
if(DEBUG_TESTS_BASE){
System.out.println("-------------- Restoring system pythonpath");
}
restoreSystemPythonPath(force, TestDependent.GetCompletePythonLib(false));
if(DEBUG_TESTS_BASE){
System.out.println("-------------- Restoring project pythonpath");
}
restoreProjectPythonPath(force, TestDependent.TEST_PYSRC_LOC);
restoreProjectPythonPath2(force, TestDependent.TEST_PYSRC_LOC2);
if(DEBUG_TESTS_BASE){
System.out.println("-------------- Checking size (for proj1 and proj2)");
}
checkSize();
}
/**
* checks if the size of the system modules manager and the project moule manager are coherent
* (we must have more modules in the system than in the project)
*/
protected void checkSize() {
IInterpreterManager iMan = getInterpreterManager();
InterpreterInfo info = (InterpreterInfo) iMan.getDefaultInterpreterInfo(getProgressMonitor());
assertTrue(info.getModulesManager().getSize() > 0);
int size = ((ASTManager)nature.getAstManager()).getSize();
assertTrue("Interpreter size:"+info.getModulesManager().getSize()+" should be smaller than project size:"+size+" " +
"(because it contains system+project info)" , info.getModulesManager().getSize() < size );
size = ((ASTManager)nature2.getAstManager()).getSize();
assertTrue("Interpreter size:"+info.getModulesManager().getSize()+" should be smaller than project size:"+size+" " +
"(because it contains system+project info)" , info.getModulesManager().getSize() < size );
}
public void testEmpty() {
//just so that we don't get 'no tests found' warning
}
// ================================================================= helpers for doing code completion requests
protected IPyCodeCompletion codeCompletion;
public ICompletionProposal[] requestCompl(String strDoc, int documentOffset, int returned, String []retCompl) throws CoreException, BadLocationException{
return requestCompl(strDoc, documentOffset, returned, retCompl, nature);
}
public ICompletionProposal[] requestCompl(String strDoc, int documentOffset, int returned, String []retCompl, PythonNature nature) throws CoreException, BadLocationException{
return requestCompl(null, strDoc, documentOffset, returned, retCompl, nature);
}
public ICompletionProposal[] requestCompl(File file, int documentOffset, int returned, String []retCompl) throws CoreException, BadLocationException{
String strDoc = REF.getFileContents(file);
return requestCompl(file, strDoc, documentOffset, returned, retCompl);
}
public ICompletionProposal[] requestCompl(File file, String strDoc, int documentOffset, int returned, String []retCompl) throws CoreException, BadLocationException{
return requestCompl(file, strDoc, documentOffset, returned, retCompl, nature);
}
/**
* make a request for a code completion
*
* @param the file where we are doing the completion
* @param strDoc the document requesting the code completion
* @param documentOffset the offset of the document (if -1, the doc length is used)
* @param returned the number of completions expected (if -1 not tested)
* @param retCompl a string array specifying the expected completions that should be contained (may only be a
* subset of all completions.
* @return
*
* @throws CoreException
* @throws BadLocationException
*/
public ICompletionProposal[] requestCompl(File file, String strDoc, int documentOffset, int returned, String []retCompl, PythonNature nature) throws CoreException, BadLocationException{
if(documentOffset == -1)
documentOffset = strDoc.length();
IDocument doc = new Document(strDoc);
CompletionRequest request = new CompletionRequest(file, nature, doc, documentOffset, codeCompletion);
List<Object> props = codeCompletion.getCodeCompletionProposals(null, request);
ICompletionProposal[] codeCompletionProposals = PyCodeCompletionUtils.onlyValidSorted(props, request.qualifier, request.isInCalltip);
for (int i = 0; i < retCompl.length; i++) {
assertContains(retCompl[i], codeCompletionProposals);
}
if(returned > -1){
StringBuffer buffer = getAvailableAsStr(codeCompletionProposals);
assertEquals("Expected "+returned+" received: "+codeCompletionProposals.length+"\n"+buffer, returned, codeCompletionProposals.length);
}
return codeCompletionProposals;
}
/**
* If this method does not find the completion we're looking for, it throws
* a failure exception.
*
* @param string the string we're looking for
* @param codeCompletionProposals the proposals found
*/
protected void assertContains(String string, ICompletionProposal[] codeCompletionProposals) {
for (int i = 0; i < codeCompletionProposals.length; i++) {
ICompletionProposal completionProposal = codeCompletionProposals[i];
if(checkIfEquals(string, completionProposal)){
return ;
}
}
StringBuffer buffer = getAvailableAsStr(codeCompletionProposals);
fail("The string "+string+" was not found in the returned completions.\nAvailable:\n"+buffer);
}
/**
* If this method does not find the completion we're looking for, it throws
* a failure exception.
*
* @param string the string we're looking for
* @param codeCompletionProposals the proposals found
*/
protected void assertNotContains(String string, ICompletionProposal[] codeCompletionProposals) {
for (int i = 0; i < codeCompletionProposals.length; i++) {
ICompletionProposal completionProposal = codeCompletionProposals[i];
if(checkIfEquals(string, completionProposal)){
fail("The string "+string+" was found in the returned completions (was not expected to be found).");
}
}
}
/**
* Checks if the completion we're looking for is the same completion we're analyzing.
*
* @param lookingFor this is the completion we are looking for
* @param completionProposal this is the completion proposal
* @return if the completion we're looking for is the same completion we're checking
*/
protected boolean checkIfEquals(String lookingFor, ICompletionProposal completionProposal) {
return completionProposal.getDisplayString().equals(lookingFor);
}
/**
* @return StringBuffer with a string representing the array of proposals found.
*/
protected StringBuffer getAvailableAsStr(ICompletionProposal[] codeCompletionProposals) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < codeCompletionProposals.length; i++) {
buffer.append(codeCompletionProposals[i].getDisplayString());
buffer.append("\n");
}
return buffer;
}
public ICompletionProposal[] requestCompl(String strDoc, String []retCompl) throws CoreException, BadLocationException{
return requestCompl(strDoc, -1, retCompl.length, retCompl);
}
public ICompletionProposal[] requestCompl(String strDoc, String retCompl) throws CoreException, BadLocationException{
return requestCompl(strDoc, new String[]{retCompl});
}
public static void assertContains(List<String> found, String toFind) {
for (String str : found) {
if (str.equals(toFind)){
return;
}
}
fail("The string "+toFind+" was not found amongst the passed strings.");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -