summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-mozilla/tests/no-comparison-or-assignment-inside-ok.js
blob: 59e31d7361e111cae9a8b5bae43e8de70fbdcf43 (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
131
132
133
134
135
136
137
138
139
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

var rule = require("../lib/rules/no-comparison-or-assignment-inside-ok");
var RuleTester = require("eslint").RuleTester;

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: "latest" } });

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

function invalidCode(code, output, messageId) {
  let rv = {
    code,
    errors: [{ messageId }],
  };
  if (output) {
    rv.output = output;
  }
  return rv;
}

ruleTester.run("no-comparison-or-assignment-inside-ok", rule, {
  valid: [
    "ok(foo)",
    "ok(!bar)",
    "ok(!foo, 'Message')",
    "ok(bar, 'Message')",
    "ok(!!foo, 'Message')",
  ],
  invalid: [
    // Assignment
    invalidCode("ok(foo = bar)", null, "assignment"),
    invalidCode("ok(foo = bar, 'msg')", null, "assignment"),

    // Comparisons:
    invalidCode("ok(foo == bar)", "Assert.equal(foo, bar)", "comparison", {
      operator: "==",
    }),
    invalidCode("ok(foo != bar)", "Assert.notEqual(foo, bar)", "comparison", {
      operator: "!=",
    }),
    invalidCode("ok(foo < bar)", "Assert.less(foo, bar)", "comparison", {
      operator: "<",
    }),
    invalidCode("ok(foo > bar)", "Assert.greater(foo, bar)", "comparison", {
      operator: ">",
    }),
    invalidCode(
      "ok(foo <= bar)",
      "Assert.lessOrEqual(foo, bar)",
      "comparison",
      { operator: "<=" }
    ),
    invalidCode(
      "ok(foo >= bar)",
      "Assert.greaterOrEqual(foo, bar)",
      "comparison",
      { operator: ">=" }
    ),
    invalidCode(
      "ok(foo === bar)",
      "Assert.strictEqual(foo, bar)",
      "comparison",
      { operator: "===" }
    ),
    invalidCode(
      "ok(foo !== bar)",
      "Assert.notStrictEqual(foo, bar)",
      "comparison",
      { operator: "!==" }
    ),

    // Comparisons with messages:
    invalidCode(
      "ok(foo == bar, 'hi')",
      "Assert.equal(foo, bar, 'hi')",
      "comparison",
      { operator: "==" }
    ),
    invalidCode(
      "ok(foo != bar, 'hi')",
      "Assert.notEqual(foo, bar, 'hi')",
      "comparison",
      { operator: "!=" }
    ),
    invalidCode(
      "ok(foo < bar, 'hi')",
      "Assert.less(foo, bar, 'hi')",
      "comparison",
      { operator: "<" }
    ),
    invalidCode(
      "ok(foo > bar, 'hi')",
      "Assert.greater(foo, bar, 'hi')",
      "comparison",
      { operator: ">" }
    ),
    invalidCode(
      "ok(foo <= bar, 'hi')",
      "Assert.lessOrEqual(foo, bar, 'hi')",
      "comparison",
      { operator: "<=" }
    ),
    invalidCode(
      "ok(foo >= bar, 'hi')",
      "Assert.greaterOrEqual(foo, bar, 'hi')",
      "comparison",
      { operator: ">=" }
    ),
    invalidCode(
      "ok(foo === bar, 'hi')",
      "Assert.strictEqual(foo, bar, 'hi')",
      "comparison",
      { operator: "===" }
    ),
    invalidCode(
      "ok(foo !== bar, 'hi')",
      "Assert.notStrictEqual(foo, bar, 'hi')",
      "comparison",
      { operator: "!==" }
    ),

    // Confusing bits that break fixup:
    invalidCode(
      "async () => ok((await foo) === bar, 'Oh no')",
      "async () => Assert.strictEqual(await foo, bar, 'Oh no')",
      "comparison",
      { operator: "===" }
    ),
  ],
});