1// Copyright 2016 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4/* eslint-disable */ 5 6/** 7 * @fileoverview Tests for camelcase rule. 8 * @author Nicholas C. Zakas 9 */ 10 11'use strict'; 12 13//------------------------------------------------------------------------------ 14// Requirements 15//------------------------------------------------------------------------------ 16 17var rule = require("../rules/catapult-camelcase"), 18 RuleTester = require("../../node_runner/node_runner/node_modules/eslint/lib/testers/rule-tester"); 19 20//------------------------------------------------------------------------------ 21// Tests 22//------------------------------------------------------------------------------ 23 24var ruleTester = new RuleTester(); 25 26ruleTester.run("camelcase", rule, { 27 valid: [ 28 "firstName = \"Nicholas\"", 29 "FIRST_NAME = \"Nicholas\"", 30 "__myPrivateVariable = \"Patrick\"", 31 "myPrivateVariable_ = \"Patrick\"", 32 "function doSomething(){}", 33 "do_something()", 34 "foo.do_something()", 35 "var foo = bar.baz_boom;", 36 "var foo = bar.baz_boom.something;", 37 "foo.boom_pow.qux = bar.baz_boom.something;", 38 "if (bar.baz_boom) {}", 39 "var obj = { key: foo.bar_baz };", 40 "var arr = [foo.bar_baz];", 41 "[foo.bar_baz]", 42 "var arr = [foo.bar_baz.qux];", 43 "[foo.bar_baz.nesting]", 44 "if (foo.bar_baz === boom.bam_pow) { [foo.baz_boom] }", 45 // These tests are for Catapult-specific exceptions. 46 "opt_firstName = \"Nicholas\"", 47 "g_firstName = \"Nicholas\"", 48 "sizeInBytes_smallerIsBetter = \"Nicholas\"", 49 "sizeInBytes_biggerIsBetter = \"Nicholas\"", 50 { 51 code: "var o = {key: 1}", 52 options: [{properties: "always"}] 53 }, 54 { 55 code: "var o = {bar_baz: 1}", 56 options: [{properties: "never"}] 57 }, 58 { 59 code: "obj.a_b = 2;", 60 options: [{properties: "never"}] 61 }, 62 { 63 code: "var obj = {\n a_a: 1 \n};\n obj.a_b = 2;", 64 options: [{properties: "never"}] 65 }, 66 { 67 code: "obj.foo_bar = function(){};", 68 options: [{properties: "never"}] 69 }, 70 { 71 code: "var { category_id: category } = query;", 72 parserOptions: { ecmaVersion: 6 } 73 }, 74 { 75 code: "var { category_id: category } = query;", 76 parserOptions: { ecmaVersion: 6 }, 77 options: [{properties: "never"}] 78 }, 79 { 80 code: "import { camelCased } from \"external module\";", 81 parserOptions: { ecmaVersion: 6, sourceType: "module" } 82 }, 83 { 84 code: "import { no_camelcased as camelCased } from \"external-module\";", 85 parserOptions: { ecmaVersion: 6, sourceType: "module" } 86 }, 87 { 88 code: "import { no_camelcased as camelCased, anoterCamelCased } from \"external-module\";", 89 parserOptions: { ecmaVersion: 6, sourceType: "module" } 90 } 91 ], 92 invalid: [ 93 { 94 code: "first_name = \"Nicholas\"", 95 errors: [ 96 { 97 message: "Identifier 'first_name' is not in camel case.", 98 type: "Identifier" 99 } 100 ] 101 }, 102 { 103 code: "__private_first_name = \"Patrick\"", 104 errors: [ 105 { 106 message: "Identifier '__private_first_name' is not in camel case.", 107 type: "Identifier" 108 } 109 ] 110 }, 111 { 112 code: "function foo_bar(){}", 113 errors: [ 114 { 115 message: "Identifier 'foo_bar' is not in camel case.", 116 type: "Identifier" 117 } 118 ] 119 }, 120 { 121 code: "obj.foo_bar = function(){};", 122 errors: [ 123 { 124 message: "Identifier 'foo_bar' is not in camel case.", 125 type: "Identifier" 126 } 127 ] 128 }, 129 { 130 code: "bar_baz.foo = function(){};", 131 errors: [ 132 { 133 message: "Identifier 'bar_baz' is not in camel case.", 134 type: "Identifier" 135 } 136 ] 137 }, 138 { 139 code: "[foo_bar.baz]", 140 errors: [ 141 { 142 message: "Identifier 'foo_bar' is not in camel case.", 143 type: "Identifier" 144 } 145 ] 146 }, 147 { 148 code: "if (foo.bar_baz === boom.bam_pow) { [foo_bar.baz] }", 149 errors: [ 150 { 151 message: "Identifier 'foo_bar' is not in camel case.", 152 type: "Identifier" 153 } 154 ] 155 }, 156 { 157 code: "foo.bar_baz = boom.bam_pow", 158 errors: [ 159 { 160 message: "Identifier 'bar_baz' is not in camel case.", 161 type: "Identifier" 162 } 163 ] 164 }, 165 { 166 code: "var foo = { bar_baz: boom.bam_pow }", 167 errors: [ 168 { 169 message: "Identifier 'bar_baz' is not in camel case.", 170 type: "Identifier" 171 } 172 ] 173 }, 174 { 175 code: "foo.qux.boom_pow = { bar: boom.bam_pow }", 176 errors: [ 177 { 178 message: "Identifier 'boom_pow' is not in camel case.", 179 type: "Identifier" 180 } 181 ] 182 }, 183 { 184 code: "var o = {bar_baz: 1}", 185 options: [{properties: "always"}], 186 errors: [ 187 { 188 message: "Identifier 'bar_baz' is not in camel case.", 189 type: "Identifier" 190 } 191 ] 192 }, 193 { 194 code: "obj.a_b = 2;", 195 options: [{properties: "always"}], 196 errors: [ 197 { 198 message: "Identifier 'a_b' is not in camel case.", 199 type: "Identifier" 200 } 201 ] 202 }, 203 { 204 code: "obj.a_b = 2;", 205 options: [{properties: "always"}], 206 errors: [ 207 { 208 message: "Identifier 'a_b' is not in camel case.", 209 type: "Identifier" 210 } 211 ] 212 }, 213 { 214 code: "var { category_id: category_id } = query;", 215 parserOptions: { ecmaVersion: 6 }, 216 errors: [ 217 { 218 message: "Identifier 'category_id' is not in camel case.", 219 type: "Identifier" 220 } 221 ] 222 }, 223 { 224 code: "var { category_id } = query;", 225 parserOptions: { ecmaVersion: 6 }, 226 errors: [ 227 { 228 message: "Identifier 'category_id' is not in camel case.", 229 type: "Identifier" 230 } 231 ] 232 }, 233 { 234 code: "import no_camelcased from \"external-module\";", 235 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 236 errors: [ 237 { 238 message: "Identifier 'no_camelcased' is not in camel case.", 239 type: "Identifier" 240 } 241 ] 242 }, 243 { 244 code: "import * as no_camelcased from \"external-module\";", 245 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 246 errors: [ 247 { 248 message: "Identifier 'no_camelcased' is not in camel case.", 249 type: "Identifier" 250 } 251 ] 252 }, 253 { 254 code: "import { no_camelcased } from \"external-module\";", 255 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 256 errors: [ 257 { 258 message: "Identifier 'no_camelcased' is not in camel case.", 259 type: "Identifier" 260 } 261 ] 262 }, 263 { 264 code: "import { no_camelcased as no_camel_cased } from \"external module\";", 265 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 266 errors: [ 267 { 268 message: "Identifier 'no_camel_cased' is not in camel case.", 269 type: "Identifier" 270 } 271 ] 272 }, 273 { 274 code: "import { camelCased as no_camel_cased } from \"external module\";", 275 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 276 errors: [ 277 { 278 message: "Identifier 'no_camel_cased' is not in camel case.", 279 type: "Identifier" 280 } 281 ] 282 }, 283 { 284 code: "import { camelCased, no_camelcased } from \"external-module\";", 285 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 286 errors: [ 287 { 288 message: "Identifier 'no_camelcased' is not in camel case.", 289 type: "Identifier" 290 } 291 ] 292 }, 293 { 294 code: "import { no_camelcased as camelCased, another_no_camelcased } from \"external-module\";", 295 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 296 errors: [ 297 { 298 message: "Identifier 'another_no_camelcased' is not in camel case.", 299 type: "Identifier" 300 } 301 ] 302 }, 303 { 304 code: "import camelCased, { no_camelcased } from \"external-module\";", 305 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 306 errors: [ 307 { 308 message: "Identifier 'no_camelcased' is not in camel case.", 309 type: "Identifier" 310 } 311 ] 312 }, 313 { 314 code: "import no_camelcased, { another_no_camelcased as camelCased } from \"external-module\";", 315 parserOptions: { ecmaVersion: 6, sourceType: "module" }, 316 errors: [ 317 { 318 message: "Identifier 'no_camelcased' is not in camel case.", 319 type: "Identifier" 320 } 321 ] 322 } 323 ] 324}); 325