<commit_before>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import expect = require('expect.js');

import {
  Kernel
} from '@jupyterlab/services';

import {
  each
} from 'phosphor/lib/algorithm/iteration';

import {
  CodeCellWidget, MarkdownCellWidget, RawCellWidget
} from '../../../lib/cells/widget';

import {
 NotebookModel
} from '../../../lib/notebook/model';

import {
  NotebookActions
} from '../../../lib/notebook/actions';

import {
  Notebook, JUPYTER_CELL_MIME
} from '../../../lib/notebook/widget';

import {
  DEFAULT_CONTENT, createNotebookFactory, clipboard, rendermime,
  mimeTypeService
} from './utils';


const ERROR_INPUT = 'a = foo';
const kernelPromise = Kernel.startNew();


describe('notebook/notebook/actions', () => {

  describe('NotebookActions', () => {

    let widget: Notebook;
    let kernel: Kernel.IKernel;

    beforeEach((done) => {
      widget = new Notebook({
        rendermime,
        contentFactory: createNotebookFactory(),
        mimeTypeService
      });
      let model = new NotebookModel();
      model.fromJSON(DEFAULT_CONTENT);
      widget.model = model;

      kernelPromise.then(k => {
        kernel = k;
        done();
      });
      widget.activeCellIndex = 0;
    });

    afterEach(() => {
      widget.dispose();
      clipboard.clear();
    });

    after(() => {
      kernel.shutdown();
    });

    describe('#splitCell({})', () => {

      it('should split the active cell into two cells', () => {
        let cell = widget.activeCell;
        let source = 'thisisasamplestringwithnospaces';
        cell.model.value.text = source;
        let index = widget.activeCellIndex;
        let editor = cell.editor;
        editor.setCursorPosition(editor.getPositionAt(10));
        NotebookActions.splitCell(widget);
        let cells = widget.model.cells;
        let newSource = cells.at(index).value.text + cells.at(index + 1).value.text;
        expect(newSource).to.be(source);
      });

      it('should remove leading white space in the second cell', () => {
        let cell = widget.activeCell;
        let source = 'this\n\n   is a test';
        cell.model.value.text = source;
        let editor = cell.editor;
        editor.setCursorPosition(editor.getPositionAt(4));
        NotebookActions.splitCell(widget);
        expect(widget.activeCell.model.value.text).to.be('is a test');
      });

      it('should clear the existing selection', () => {
        each(widget.widgets, child => {
          widget.select(child);
        });
        NotebookActions.splitCell(widget);
        for (let i = 0; i < widget.widgets.length; i++) {
          if (i === widget.activeCellIndex) {
            continue;
          }
          expect(widget.isSelected(widget.widgets.at(i))).to.be(false);
        }
      });<commit_msg>test(splitCellWhiteSpace): change test to preserve leading whitespace<commit_after>// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.

import expect = require('expect.js');

import {
  Kernel
} from '@jupyterlab/services';

import {
  each
} from 'phosphor/lib/algorithm/iteration';

import {
  CodeCellWidget, MarkdownCellWidget, RawCellWidget
} from '../../../lib/cells/widget';

import {
 NotebookModel
} from '../../../lib/notebook/model';

import {
  NotebookActions
} from '../../../lib/notebook/actions';

import {
  Notebook, JUPYTER_CELL_MIME
} from '../../../lib/notebook/widget';

import {
  DEFAULT_CONTENT, createNotebookFactory, clipboard, rendermime,
  mimeTypeService
} from './utils';


const ERROR_INPUT = 'a = foo';
const kernelPromise = Kernel.startNew();


describe('notebook/notebook/actions', () => {

  describe('NotebookActions', () => {

    let widget: Notebook;
    let kernel: Kernel.IKernel;

    beforeEach((done) => {
      widget = new Notebook({
        rendermime,
        contentFactory: createNotebookFactory(),
        mimeTypeService
      });
      let model = new NotebookModel();
      model.fromJSON(DEFAULT_CONTENT);
      widget.model = model;

      kernelPromise.then(k => {
        kernel = k;
        done();
      });
      widget.activeCellIndex = 0;
    });

    afterEach(() => {
      widget.dispose();
      clipboard.clear();
    });

    after(() => {
      kernel.shutdown();
    });

    describe('#splitCell({})', () => {

      it('should split the active cell into two cells', () => {
        let cell = widget.activeCell;
        let source = 'thisisasamplestringwithnospaces';
        cell.model.value.text = source;
        let index = widget.activeCellIndex;
        let editor = cell.editor;
        editor.setCursorPosition(editor.getPositionAt(10));
        NotebookActions.splitCell(widget);
        let cells = widget.model.cells;
        let newSource = cells.at(index).value.text + cells.at(index + 1).value.text;
        expect(newSource).to.be(source);
      });

      it('should preserve leading white space in the second cell', () => {
        let cell = widget.activeCell;
        let source = 'this\n\n   is a test';
        cell.model.value.text = source;
        let editor = cell.editor;
        editor.setCursorPosition(editor.getPositionAt(4));
        NotebookActions.splitCell(widget);
        expect(widget.activeCell.model.value.text).to.be('   is a test');
      });

      it('should clear the existing selection', () => {
        each(widget.widgets, child => {
          widget.select(child);
        });
        NotebookActions.splitCell(widget);
        for (let i = 0; i < widget.widgets.length; i++) {
          if (i === widget.activeCellIndex) {
            continue;
          }
          expect(widget.isSelected(widget.widgets.at(i))).to.be(false);
        }
      });
{'fraction_non_alphanumeric': 0.14577209061040983, 'fraction_numerical': 0.0020750475531730937, 'mean_word_length': 2.513973268529769, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3083895', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1686, 'n_words': 361}