summaryrefslogtreecommitdiff
path: root/python/testSrc/com/jetbrains/env/python/PythonConsoleTest.java
blob: 4134b1697851272ceb1d77a62b86846be611f516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.jetbrains.env.python;

import com.google.common.collect.Sets;
import com.jetbrains.env.PyEnvTestCase;
import com.jetbrains.env.python.console.PyConsoleTask;
import org.junit.Assert;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author traff
 */
public class PythonConsoleTest extends PyEnvTestCase {
  public void testConsolePrint() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("x = 96");
        exec("x += 1");
        exec("print(1)");
        exec("print(x)");
        waitForOutput("97");
      }
    });
  }

  public void testExecuteMultiline() throws Exception {   //PY-4329
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("if True:\n" +
             "        x=1\n" +
             "y=x+100\n" +
             "for i in range(1):\n" +
             "  print(y)\n");
        waitForOutput("101");
      }

      @Override
      public Set<String> getTags() {
        return Sets.newHashSet("-jython"); //jython doesn't support multiline execution: http://bugs.jython.org/issue2106
      }
    });
  }

  public void testInterruptAsync() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("import time");
        execNoWait("for i in range(10000):\n" +
                   "  print(i)\n" +
                   "  time.sleep(0.1)");
        waitForOutput("3\n4\n5");
        Assert.assertFalse(canExecuteNow());
        interrupt();
        waitForFinish();
        waitForReady();
      }

      @Override
      public Set<String> getTags() {
        return Sets.newHashSet("-iron", "-jython");
      }
    });
  }

  public void testLineByLineInput() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("x = 96");
        exec("x +=1");
        exec("if True:");
        exec("  print(x)");
        exec("");
        exec("");
        waitForOutput("97");
      }
    });
  }


  public void testVariablesView() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("x = 1");
        exec("print(x)");
        waitForOutput("1");
        
        assertTrue("Variable has wrong value", 
                   hasValue("x", "1"));
      }
    });
  }

  public void testCompoundVariable() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("x = [1, 2, 3]");
        exec("print(x)");
        waitForOutput("[1, 2, 3]");

        List<String> values = getCompoundValueChildren(getValue("x"));
        Collections.sort(values);
        assertContainsElements(values, "1", "2", "3", "3");
      }
    });
  }

  public void testChangeVariable() throws Exception {
    runPythonTest(new PyConsoleTask() {
      @Override
      public void testing() throws Exception {
        exec("x = 1");
        exec("print(x)");
        waitForOutput("1");
        
        setValue("x", "2");

        assertTrue("Variable has wrong value",
                   hasValue("x", "2"));
      }
    });
  }
}