<commit_before>// Copyright (c) 2014, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

@TestOn('browser')
library codemirror.tests;

import 'dart:html';

import 'package:codemirror/codemirror.dart';
import 'package:test/test.dart';

// TODO: test double click

// TODO: document mutation

final Element editorHost = new DivElement();

void main() {
  document.body.children.add(editorHost);

  group('simple', createSimpleTests);
  group('CodeMirror', createCodeMirrorTests);
  group('CodeMirror (static) ', createCodeMirrorStaticTests);
  group('Doc', createDocTests);
  group('HtmlDoc', createHtmlDocTests);
  group('history', createHistoryTests);
}

createSimpleTests() {
  test('create', () {
    CodeMirror editor = new CodeMirror.fromElement(editorHost);
    expect(editor, isNotNull);
    expect(editorHost.parent, isNotNull);
    editor.dispose();
    editorHost.children.clear();
  });
}

createCodeMirrorStaticTests() {
  test('modes', () {
    expect(CodeMirror.MODES.length, greaterThanOrEqualTo(10));
  });

  test('mime modes', () {
    expect(CodeMirror.MIME_MODES.length, greaterThanOrEqualTo(10));
  });

  test('commands', () {
    expect(CodeMirror.COMMANDS.length, greaterThanOrEqualTo(10));
  });

  test('key map', () {
    expect(CodeMirror.KEY_MAPS.length, 4);
  });

  test('themes', () {
    expect(CodeMirror.THEMES.length, greaterThanOrEqualTo(10));
  });

  test('version', () {
    expect(CodeMirror.version.length, greaterThanOrEqualTo(3));
  });
}

createCodeMirrorTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost);
  });

  tearDown(() {
    editor.dispose();
    editorHost.children.clear();
  });

  test('simple', () {
    expect(editor, isNotNull);
    expect(editorHost.parent, isNotNull);
  });

  test('getOption / setOption', () {
    expect(editor.getOption('lineWrapping'), false);
    editor.setOption('lineWrapping', true);
    expect(editor.getOption('lineWrapping'), true);
  });

  test('getLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    expect(editor.getLine(0), 'one');
    expect(editor.getLine(1), 'two');
    expect(editor.getLine(2), 'three');
  });
}

createDocTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost);
    editor.setMode('clike');
  });

  tearDown(() {
    editor.dispose();
    editorHost.children.clear();
  });

  test('getValue / getValue', () {
    Doc doc = editor.getDoc();
    expect(doc.getValue(), '');
    doc.setValue('foo bar');
    expect(doc.getValue(), 'foo bar');
  });

  test('getLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    expect(doc.getLine(0), 'one');
    expect(doc.getLine(1), 'two');
    expect(doc.getLine(2), 'three');
  });

  test('getMode', () {
    Doc doc = editor.getDoc();
    expect(doc.getMode()['name'], 'clike');
    expect(doc.getModeName(), 'clike');
  });

  test('eachLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    var lines = [];
    doc.eachLine((LineHandle line) {
      lines.add(line.text);
    });
    expect(lines.length, 3);
    expect(lines[0], 'one');
    expect(lines[1], 'two');
    expect(lines[2], 'three');
  });
}

createHtmlDocTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost, options: {"mode": "text/html"});<commit_msg>Add tests for selection extension methods.<commit_after>// Copyright (c) 2014, Google Inc. Please see the AUTHORS file for details.
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

@TestOn('browser')
library codemirror.tests;

import 'dart:html';

import 'package:codemirror/codemirror.dart';
import 'package:test/test.dart';

// TODO: test double click

// TODO: document mutation

final Element editorHost = new DivElement();

void main() {
  document.body.children.add(editorHost);

  group('simple', createSimpleTests);
  group('CodeMirror', createCodeMirrorTests);
  group('CodeMirror (static) ', createCodeMirrorStaticTests);
  group('Doc', createDocTests);
  group('HtmlDoc', createHtmlDocTests);
  group('history', createHistoryTests);
}

createSimpleTests() {
  test('create', () {
    CodeMirror editor = new CodeMirror.fromElement(editorHost);
    expect(editor, isNotNull);
    expect(editorHost.parent, isNotNull);
    editor.dispose();
    editorHost.children.clear();
  });
}

createCodeMirrorStaticTests() {
  test('modes', () {
    expect(CodeMirror.MODES.length, greaterThanOrEqualTo(10));
  });

  test('mime modes', () {
    expect(CodeMirror.MIME_MODES.length, greaterThanOrEqualTo(10));
  });

  test('commands', () {
    expect(CodeMirror.COMMANDS.length, greaterThanOrEqualTo(10));
  });

  test('key map', () {
    expect(CodeMirror.KEY_MAPS.length, 4);
  });

  test('themes', () {
    expect(CodeMirror.THEMES.length, greaterThanOrEqualTo(10));
  });

  test('version', () {
    expect(CodeMirror.version.length, greaterThanOrEqualTo(3));
  });
}

createCodeMirrorTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost);
  });

  tearDown(() {
    editor.dispose();
    editorHost.children.clear();
  });

  test('simple', () {
    expect(editor, isNotNull);
    expect(editorHost.parent, isNotNull);
  });

  test('getOption / setOption', () {
    expect(editor.getOption('lineWrapping'), false);
    editor.setOption('lineWrapping', true);
    expect(editor.getOption('lineWrapping'), true);
  });

  test('getLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    expect(editor.getLine(0), 'one');
    expect(editor.getLine(1), 'two');
    expect(editor.getLine(2), 'three');
  });
}

createDocTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost);
    editor.setMode('clike');
  });

  tearDown(() {
    editor.dispose();
    editorHost.children.clear();
  });

  test('getValue / getValue', () {
    Doc doc = editor.getDoc();
    expect(doc.getValue(), '');
    doc.setValue('foo bar');
    expect(doc.getValue(), 'foo bar');
  });

  test('getLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    expect(doc.getLine(0), 'one');
    expect(doc.getLine(1), 'two');
    expect(doc.getLine(2), 'three');
  });

  test('getMode', () {
    Doc doc = editor.getDoc();
    expect(doc.getMode()['name'], 'clike');
    expect(doc.getModeName(), 'clike');
  });

  test('eachLine', () {
    Doc doc = editor.getDoc();
    doc.setValue('one\ntwo\nthree');
    var lines = [];
    doc.eachLine((LineHandle line) {
      lines.add(line.text);
    });
    expect(lines.length, 3);
    expect(lines[0], 'one');
    expect(lines[1], 'two');
    expect(lines[2], 'three');
  });

  test('extendSelection', () {
    Doc doc = editor.getDoc();
    doc.setValue('foo bar');

    // Extending flag is off.
    doc.setSelection(new Position(0, 0));
    doc.setExtending(false);
    doc.extendSelection(new Position(0, 3));
    expect(doc.getSelection(), '');

    // Extending flag is on.
    doc.setSelection(new Position(0, 0));
    doc.setExtending(true);
    doc.extendSelection(new Position(0, 3));
    expect(doc.getSelection(), 'foo');
  });

  test('extendSelections', () {
    Doc doc = editor.getDoc();
    doc.setValue('foo bar');

    doc.addSelection(anchor: new Position(0, 0));
    doc.addSelection(anchor: new Position(0, 4));
    doc.setExtending(true);
    doc.extendSelections([new Position(0, 3), new Position(0, 7)]);
    expect(doc.getSelections(), ['foo', 'bar']);
  });

  test('extendSelectionsBy', () {
    Doc doc = editor.getDoc();
    doc.setValue('foo bar');

    doc.addSelection(anchor: new Position(0, 0));
    doc.addSelection(anchor: new Position(0, 4));
    doc.setExtending(true);
    doc.extendSelectionsBy((Span range, int i) {
      return new Position(range.head.line, range.head.ch + 3);
    });
    expect(doc.getSelections(), ['foo', 'bar']);
  });
}

createHtmlDocTests() {
  CodeMirror editor;

  setUp(() {
    editor = new CodeMirror.fromElement(editorHost, options: {"mode": "text/html"});
{'fraction_non_alphanumeric': 0.18132530120481927, 'fraction_numerical': 0.008313253012048192, 'mean_word_length': 3.917654028436019, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6568678', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2711, 'n_words': 502}