summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/sourceeditor/test/browser_detectindent.js
blob: 80f248741767709c9e11d4de285f05e20e419d39 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TWO_SPACES_CODE = [
  "/*",
  " * tricky comment block",
  " */",
  "div {",
  "  color: red;",
  "  background: blue;",
  "}",
  "     ",
  "span {",
  "  padding-left: 10px;",
  "}",
].join("\n");

const FOUR_SPACES_CODE = [
  "var obj = {",
  "    addNumbers: function() {",
  "        var x = 5;",
  "        var y = 18;",
  "        return x + y;",
  "    },",
  "   ",
  "    /*",
  "     * Do some stuff to two numbers",
  "     * ",
  "     * @param x",
  "     * @param y",
  "     * ",
  "     * @return the result of doing stuff",
  "     */",
  "    subtractNumbers: function(x, y) {",
  "        var x += 7;",
  "        var y += 18;",
  "        var result = x - y;",
  "        result %= 2;",
  "    }",
  "}",
].join("\n");

const TABS_CODE = [
  "/*",
  " * tricky comment block",
  " */",
  "div {",
  "\tcolor: red;",
  "\tbackground: blue;",
  "}",
  "",
  "span {",
  "\tpadding-left: 10px;",
  "}",
].join("\n");

const NONE_CODE = [
  "var x = 0;",
  "           // stray thing",
  "var y = 9;",
  "    ",
  "",
].join("\n");

async function test() {
  waitForExplicitFinish();

  const { ed, win } = await setup();
  is(ed.getOption("indentUnit"), 2, "2 spaces before code added");
  is(ed.getOption("indentWithTabs"), false, "spaces is default");

  ed.setText(NONE_CODE);
  is(ed.getOption("indentUnit"), 2, "2 spaces after un-detectable code");
  is(
    ed.getOption("indentWithTabs"),
    false,
    "spaces still set after un-detectable code"
  );

  ed.setText(FOUR_SPACES_CODE);
  is(ed.getOption("indentUnit"), 4, "4 spaces detected in 4 space code");
  is(ed.getOption("indentWithTabs"), false, "spaces detected in 4 space code");

  ed.setText(TWO_SPACES_CODE);
  is(ed.getOption("indentUnit"), 2, "2 spaces detected in 2 space code");
  is(ed.getOption("indentWithTabs"), false, "spaces detected in 2 space code");

  ed.setText(TABS_CODE);
  is(ed.getOption("indentUnit"), 2, "2 space indentation unit");
  is(
    ed.getOption("indentWithTabs"),
    true,
    "tabs detected in majority tabs code"
  );

  teardown(ed, win);
}