]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-duplicate-case.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / no-duplicate-case.js
1 /**
2 * @fileoverview Tests for no-duplicate-case rule.
3 * @author Dieter Oberkofler
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-duplicate-case"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-duplicate-case", rule, {
22 valid: [
23 "var a = 1; switch (a) {case 1: break; case 2: break; default: break;}",
24 "var a = 1; switch (a) {case 1: break; case '1': break; default: break;}",
25 "var a = 1; switch (a) {case 1: break; case true: break; default: break;}",
26 "var a = 1; switch (a) {default: break;}",
27 "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p.p.p2: break; default: break;}",
28 "var a = 1, f = function(b) { return b ? { p1: 1 } : { p1: 2 }; }; switch (a) {case f(true).p1: break; case f(true, false).p1: break; default: break;}",
29 "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a + 1).p1: break; case f(a + 2).p1: break; default: break;}",
30 "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a == 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
31 "var a = 1, f1 = function() { return { p1: 1 } }, f2 = function() { return { p1: 2 } }; switch (a) {case f1().p1: break; case f2().p1: break; default: break;}",
32 "var a = [1,2]; switch(a.toString()){case ([1,2]).toString():break; case ([1]).toString():break; default:break;}",
33 "switch(a) { case a: break; } switch(a) { case a: break; }",
34 "switch(a) { case toString: break; }"
35 ],
36 invalid: [
37 {
38 code: "var a = 1; switch (a) {case 1: break; case 1: break; case 2: break; default: break;}",
39 errors: [{
40 messageId: "unexpected",
41 type: "SwitchCase",
42 column: 39
43 }]
44 },
45 {
46 code: "var a = '1'; switch (a) {case '1': break; case '1': break; case '2': break; default: break;}",
47 errors: [{
48 messageId: "unexpected",
49 type: "SwitchCase",
50 column: 43
51 }]
52 },
53 {
54 code: "var a = 1, one = 1; switch (a) {case one: break; case one: break; case 2: break; default: break;}",
55 errors: [{
56 messageId: "unexpected",
57 type: "SwitchCase",
58 column: 50
59 }]
60 },
61 {
62 code: "var a = 1, p = {p: {p1: 1, p2: 1}}; switch (a) {case p.p.p1: break; case p.p.p1: break; default: break;}",
63 errors: [{
64 messageId: "unexpected",
65 type: "SwitchCase",
66 column: 69
67 }]
68 },
69 {
70 code: "var a = 1, f = function(b) { return b ? { p1: 1 } : { p1: 2 }; }; switch (a) {case f(true).p1: break; case f(true).p1: break; default: break;}",
71 errors: [{
72 messageId: "unexpected",
73 type: "SwitchCase",
74 column: 103
75 }]
76 },
77 {
78 code: "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a + 1).p1: break; case f(a + 1).p1: break; default: break;}",
79 errors: [{
80 messageId: "unexpected",
81 type: "SwitchCase",
82 column: 87
83 }]
84 },
85 {
86 code: "var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(a === 1 ? 2 : 3).p1: break; case f(a === 1 ? 2 : 3).p1: break; default: break;}",
87 errors: [{
88 messageId: "unexpected",
89 type: "SwitchCase",
90 column: 97
91 }]
92 },
93 {
94 code: "var a = 1, f1 = function() { return { p1: 1 } }; switch (a) {case f1().p1: break; case f1().p1: break; default: break;}",
95 errors: [{
96 messageId: "unexpected",
97 type: "SwitchCase",
98 column: 83
99 }]
100 },
101 {
102 code: "var a = [1, 2]; switch(a.toString()){case ([1, 2]).toString():break; case ([1, 2]).toString():break; default:break;}",
103 errors: [{
104 messageId: "unexpected",
105 type: "SwitchCase",
106 column: 70
107 }]
108 },
109 {
110 code: "switch (a) { case a: case a: }",
111 errors: [{
112 messageId: "unexpected",
113 type: "SwitchCase",
114 column: 22
115 }]
116 },
117 {
118 code: "switch (a) { case a: break; case b: break; case a: break; case c: break; case a: break; }",
119 errors: [
120 {
121 messageId: "unexpected",
122 type: "SwitchCase",
123 column: 44
124 },
125 {
126 messageId: "unexpected",
127 type: "SwitchCase",
128 column: 74
129 }
130 ]
131 }
132 ]
133 });