Search in sources :

Example 1 with IpnbOutputCell

use of org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell in project intellij-community by JetBrains.

the class JsonParserTest method testOutputs.

public void testOutputs() throws IOException {
    final String fileName = "testData/outputs.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    assertNotNull(ipnbFile);
    final List<IpnbCell> cells = ipnbFile.getCells();
    assertEquals(1, cells.size());
    final IpnbCell cell = cells.get(0);
    assertTrue(cell instanceof IpnbCodeCell);
    final List<IpnbOutputCell> outputs = ((IpnbCodeCell) cell).getCellOutputs();
    assertEquals(1, outputs.size());
    final IpnbOutputCell output = outputs.get(0);
    final List<String> text = output.getText();
    assertNotNull(text);
    final String joined = StringUtil.join(text, "");
    assertEquals("\"Add(Symbol('x'), Mul(Integer(2), Symbol('y')))\"", joined);
}
Also used : IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Example 2 with IpnbOutputCell

use of org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell in project intellij-community by JetBrains.

the class JsonParserTest method testCodeCell.

public void testCodeCell() throws IOException {
    final String fileName = "testData/code.ipynb";
    final String fileText = IpnbTestCase.getFileText(fileName);
    final IpnbFile ipnbFile = IpnbParser.parseIpnbFile(fileText, new LightVirtualFile());
    assertNotNull(ipnbFile);
    final List<IpnbCell> cells = ipnbFile.getCells();
    assertEquals(1, cells.size());
    final IpnbCell cell = cells.get(0);
    assertTrue(cell instanceof IpnbCodeCell);
    final List<IpnbOutputCell> outputs = ((IpnbCodeCell) cell).getCellOutputs();
    assertEquals(0, outputs.size());
    final List<String> source = ((IpnbCodeCell) cell).getSource();
    final String joined = StringUtil.join(source, "");
    assertEquals("e = x + 2*y", joined);
    final String language = ((IpnbCodeCell) cell).getLanguage();
    assertEquals("python", language);
    final Integer number = ((IpnbCodeCell) cell).getPromptNumber();
    assertEquals(new Integer(4), number);
}
Also used : IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) IpnbCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCell) IpnbCodeCell(org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) IpnbFile(org.jetbrains.plugins.ipnb.format.IpnbFile)

Example 3 with IpnbOutputCell

use of org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell in project intellij-community by JetBrains.

the class WebSocketConnectionTest method testCompositeInput.

public void testCompositeInput() throws IOException, URISyntaxException, InterruptedException {
    final Ref<Boolean> evaluated = Ref.create(false);
    final IpnbConnection connection = new IpnbConnection(getTestServerURI(), new IpnbConnectionListenerBase() {

        private String myMessageId;

        @Override
        public void onOpen(@NotNull IpnbConnection connection) {
            myMessageId = connection.execute("def simple_crit_func(feat_sub):\n" + "\n" + "    \"\"\" Returns sum of numerical values of an input list. \"\"\" \n" + "\n" + "    return sum(feat_sub)\n" + "\n" + "simple_crit_func([1,2,4])");
        }

        @Override
        public void onOutput(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
            if (myMessageId.equals(parentMessageId)) {
                final IpnbOutputCell output = connection.getOutput();
                assertEquals(output.getClass(), IpnbOutOutputCell.class);
                final List<String> text = output.getText();
                assertNotNull(text);
                assertEquals("7", text.get(0));
                evaluated.set(true);
                connection.shutdown();
            }
        }
    }, null, DefaultProjectFactory.getInstance().getDefaultProject(), "");
    connection.close();
    assertTrue(evaluated.get());
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) List(java.util.List) IpnbOutOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutOutputCell) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)

Example 4 with IpnbOutputCell

use of org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell in project intellij-community by JetBrains.

the class WebSocketConnectionTest method testBasicWebSocket.

public void testBasicWebSocket() throws IOException, URISyntaxException, InterruptedException {
    final Ref<Boolean> evaluated = Ref.create(false);
    final IpnbConnection connection = new IpnbConnection(getTestServerURI(), new IpnbConnectionListenerBase() {

        private String myMessageId;

        @Override
        public void onOpen(@NotNull IpnbConnection connection) {
            myMessageId = connection.execute("2 + 2");
        }

        @Override
        public void onOutput(@NotNull IpnbConnection connection, @NotNull String parentMessageId) {
            if (myMessageId.equals(parentMessageId)) {
                final IpnbOutputCell output = connection.getOutput();
                assertEquals(output.getClass(), IpnbOutOutputCell.class);
                final List<String> text = output.getText();
                assertNotNull(text);
                assertEquals("4", text.get(0));
                evaluated.set(true);
                connection.shutdown();
            }
        }
    }, null, DefaultProjectFactory.getInstance().getDefaultProject(), "");
    connection.close();
    assertTrue(evaluated.get());
}
Also used : IpnbConnection(org.jetbrains.plugins.ipnb.protocol.IpnbConnection) IpnbOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell) List(java.util.List) IpnbOutOutputCell(org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutOutputCell) IpnbConnectionListenerBase(org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)

Aggregations

IpnbOutputCell (org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutputCell)4 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)2 List (java.util.List)2 IpnbFile (org.jetbrains.plugins.ipnb.format.IpnbFile)2 IpnbCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCell)2 IpnbCodeCell (org.jetbrains.plugins.ipnb.format.cells.IpnbCodeCell)2 IpnbOutOutputCell (org.jetbrains.plugins.ipnb.format.cells.output.IpnbOutOutputCell)2 IpnbConnection (org.jetbrains.plugins.ipnb.protocol.IpnbConnection)2 IpnbConnectionListenerBase (org.jetbrains.plugins.ipnb.protocol.IpnbConnectionListenerBase)2