1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import {describe, it} from 'mocha'; 17import {assert} from 'chai'; 18import {getNameGenerator, NameGeneratorType} from '../../src/generator/NameFactory'; 19import {NameGeneratorOptions} from '../../src/generator/INameGenerator'; 20 21const orderedName = [ 22 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 23 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 24 'u', 'v', 'w', 'x', 'y', 'z', 'a1', 'b1', 'c1', 'd1', 25 'e1', 'f1', 'g1', 'h1', 'i1', 'j1', 'k1', 'l1', 'm1', 'n1', 26 'o1', 'p1', 'q1', 'r1', 's1', 't1', 'u1', 'v1', 'w1', 'x1', 27 'y1', 'z1', 'a2', 'b2', 'c2', 'd2', 'e2', 'f2', 'g2', 'h2', 28 'i2', 'j2', 'k2', 'l2', 'm2', 'n2', 'o2', 'p2', 'q2', 'r2', 29 's2', 't2', 'u2', 'v2', 'w2', 'x2', 'y2', 'z2', 'a3', 'b3', 30 'c3', 'd3', 'e3', 'f3', 'g3', 'h3', 'i3', 'j3', 'k3', 'l3', 31 'm3', 'n3', 'o3', 'p3', 'q3', 'r3', 's3', 't3', 'u3', 'v3', 32 'w3', 'x3', 'y3', 'z3' 33]; 34 35describe("test for name generator", function () { 36 describe('ordered name generator test', function () { 37 it('ordered name generator check', function () { 38 const orderedGenerator = getNameGenerator(NameGeneratorType.ORDERED); 39 40 for (let i = 0; i < 104; i++) { 41 assert.strictEqual(orderedGenerator.getName(), orderedName[i]); 42 } 43 }); 44 }); 45 46 describe('disordered name generator test', function () { 47 const disorderGenerator = getNameGenerator(NameGeneratorType.DISORDERED); 48 let arr = [] 49 for (let i = 0; i < 104; i++) { 50 arr.push(disorderGenerator.getName()); 51 } 52 53 it('disordered name generator check value', function () { 54 for (let i = 1; i < 5; i++) { 55 const targetName = orderedName.slice(26 * (i - 1), 26 * i); 56 const sliceArr = arr.slice(26 * (i - 1), 26 * i); 57 sliceArr.sort(); 58 sliceArr.forEach(((value, index) => { 59 assert.strictEqual(value, targetName[index]); 60 })); 61 } 62 }); 63 64 it('disordered name generator check order', function () { 65 let count = 0; 66 67 for (let i = 2; i < 26; i++) { 68 const downConsecutive = (arr[i - 2].charCodeAt(0) - arr[i - 1].charCodeAt(0) === 1) && 69 (arr[i - 1].charCodeAt(0) - arr[i].charCodeAt(0) === 1); 70 if (downConsecutive) { 71 count++; 72 } 73 74 const upConsecutive = (arr[i].charCodeAt(0) - arr[i - 1].charCodeAt(0) === 1) && 75 (arr[i - 1].charCodeAt(0) - arr[i - 2].charCodeAt(0) === 1); 76 if (upConsecutive) { 77 count = count + 1; 78 } 79 } 80 81 assert.isNotTrue(count > 5); 82 }); 83 }); 84 85 describe('underline name generator test', function () { 86 it('underline name generator no option check value', function () { 87 const underlineGenerator = getNameGenerator(NameGeneratorType.UNDERLINE); 88 for (let i = 0; i < 128; i++) { 89 assert.strictEqual(underlineGenerator.getName(), '_'.repeat(i + 1)); 90 } 91 92 assert.isNull(underlineGenerator.getName()); 93 }); 94 95 it('underline name generator with option check value', function () { 96 const options: NameGeneratorOptions = { 97 underlineMaxLength: 120 98 }; 99 100 const underlineGenerator = getNameGenerator(NameGeneratorType.UNDERLINE, options); 101 for (let i = 0; i < options.underlineMaxLength; i++) { 102 assert.strictEqual(underlineGenerator.getName(), '_'.repeat(i + 1)); 103 } 104 105 assert.isNull(underlineGenerator.getName()); 106 }); 107 }); 108 109 describe('hex name generator test', function () { 110 it('hex name generator no option check length', function () { 111 const hexGenerator = getNameGenerator(NameGeneratorType.HEX); 112 for (let i = 0; i < 128; i++) { 113 assert.strictEqual(hexGenerator.getName().length, 8); 114 } 115 }); 116 117 it('hex name generator no option check value', function () { 118 const hexGenerator = getNameGenerator(NameGeneratorType.HEX); 119 for (let i = 0; i < 128; i++) { 120 const hexName = hexGenerator.getName(); 121 122 assert.isFalse(hexName.startsWith('_0x')); 123 assert.isFalse(hexName.endsWith('_')); 124 125 for (let j = 0; j < 8; j++) { 126 const isHex = (hexName[j] >= '0' && hexName[j] <= '9') || (hexName[j] >= 'a' && hexName[j] <= 'f'); 127 assert.isTrue(isHex); 128 } 129 } 130 }); 131 132 it('hex name generator with option check length', function () { 133 const options = { 134 hexLength: 10 135 }; 136 137 const hexGenerator = getNameGenerator(NameGeneratorType.HEX, options); 138 for (let i = 0; i < 128; i++) { 139 assert.strictEqual(hexGenerator.getName().length, 20); 140 } 141 }); 142 143 it('hex name generator with option check value', function () { 144 const options = { 145 hexLength: 10 146 }; 147 148 const hexGenerator = getNameGenerator(NameGeneratorType.HEX, options); 149 for (let i = 0; i < 128; i++) { 150 const hexName = hexGenerator.getName(); 151 152 assert.isFalse(hexName.startsWith('_0x')); 153 assert.isFalse(hexName.endsWith('_')); 154 155 for (let j = 0; j < 20; j++) { 156 const isHex = (hexName[j] >= '0' && hexName[j] <= '9') || (hexName[j] >= 'a' && hexName[j] <= 'f'); 157 assert.isTrue(isHex); 158 } 159 } 160 }); 161 }); 162 163 describe('dictionary name generator test', function () { 164 const noOptionTarget = [ 165 'hello', 'Hello', 'hEllo', 'HEllo', 'heLlo', 166 'HeLlo', 'hELlo', 'HELlo', 'helLo', 'HelLo', 167 'hElLo', 'HElLo', 'heLLo', 'HeLLo', 'hELLo', 168 'HELLo', 'hellO', 'HellO', 'hEllO', 'HEllO', 169 'heLlO', 'HeLlO', 'hELlO', 'HELlO', 'helLO', 170 'HelLO', 'hElLO', 'HElLO', 'heLLO', 'HeLLO', 171 'hELLO', 'HELLO', 'world', 'World', 'wOrld', 172 'WOrld', 'woRld', 'WoRld', 'wORld', 'WORld', 173 'worLd', 'WorLd', 'wOrLd', 'WOrLd', 'woRLd', 174 'WoRLd', 'wORLd', 'WORLd', 'worlD', 'WorlD', 175 'wOrlD', 'WOrlD', 'woRlD', 'WoRlD', 'wORlD', 176 'WORlD', 'worLD', 'WorLD', 'wOrLD', 'WOrLD', 177 'woRLD', 'WoRLD', 'wORLD', 'WORLD', 'dictionary', 178 'Dictionary', 'dIctionary', 'DIctionary', 'diCtionary', 'DiCtionary', 179 'dICtionary', 'DICtionary', 'dicTionary', 'DicTionary', 'dIcTionary', 180 'DIcTionary', 'diCTionary', 'DiCTionary', 'dICTionary', 'DICTionary', 181 'dictIonary', 'DictIonary', 'dIctIonary', 'DIctIonary', 'diCtIonary', 182 'DiCtIonary', 'dICtIonary', 'DICtIonary', 'dicTIonary', 'DicTIonary', 183 'dIcTIonary', 'DIcTIonary', 'diCTIonary', 'DiCTIonary', 'dICTIonary', 184 'DICTIonary', 'dictiOnary', 'DictiOnary', 'dIctiOnary', 'DIctiOnary' 185 ]; 186 187 const optionTarget = [ 188 'tom', 'Tom', 'tOm', 'TOm', 'toM', 'ToM', 'tOM', 'TOM', 'jerry', 'Jerry', 189 'jErry', 'JErry', 'jeRry', 'JeRry', 'jERry', 'JERry', 'jerRy', 'JerRy', 190 'jErRy', 'JErRy', 'jeRRy', 'JeRRy', 'jERRy', 'JERRy', 'jerrY', 'JerrY', 191 'jErrY', 'JErrY', 'jeRrY', 'JeRrY', 'jERrY', 'JERrY', 'jerRY', 'JerRY', 192 'jErRY', 'JErRY', 'jeRRY', 'JeRRY', 'jERRY', 'JERRY', 'hellokitty', 'Hellokitty', 193 'hEllokitty', 'HEllokitty', 'heLlokitty', 'HeLlokitty', 'hELlokitty', 'HELlokitty', 194 'helLokitty', 'HelLokitty', 'hElLokitty', 'HElLokitty', 'heLLokitty', 'HeLLokitty', 195 'hELLokitty', 'HELLokitty', 'hellOkitty', 'HellOkitty', 'hEllOkitty', 'HEllOkitty', 196 'heLlOkitty', 'HeLlOkitty', 'hELlOkitty', 'HELlOkitty', 'helLOkitty', 'HelLOkitty', 197 'hElLOkitty', 'HElLOkitty', 'heLLOkitty', 'HeLLOkitty', 'hELLOkitty', 'HELLOkitty', 198 'helloKitty', 'HelloKitty', 'hElloKitty', 'HElloKitty', 'heLloKitty', 'HeLloKitty', 199 'hELloKitty', 'HELloKitty', 'helLoKitty', 'HelLoKitty', 'hElLoKitty', 'HElLoKitty', 200 'heLLoKitty', 'HeLLoKitty', 'hELLoKitty', 'HELLoKitty', 'hellOKitty', 'HellOKitty', 201 'hEllOKitty', 'HEllOKitty', 'heLlOKitty', 'HeLlOKitty', 'hELlOKitty', 'HELlOKitty', 202 'helLOKitty', 'HelLOKitty', 'hElLOKitty', 'HElLOKitty']; 203 204 it('dictionary name generator no option check value', function () { 205 const dictGenerator = getNameGenerator(NameGeneratorType.DICTIONARY); 206 let arr = []; 207 for (let i = 0; i < 100; i++) { 208 const nameGet = dictGenerator.getName(); 209 assert.isNotNull(nameGet); 210 arr.push(nameGet); 211 } 212 213 arr.forEach(((value, index) => { 214 assert.strictEqual(value, noOptionTarget[index]); 215 })); 216 }); 217 218 it('dictionary name generator with option check value', function () { 219 const options = { 220 dictionaryList: ['tom', 'jerry', 'helloKitty', 'jojo', 'betty'] 221 }; 222 223 const dictGenerator = getNameGenerator(NameGeneratorType.DICTIONARY, options); 224 let arr = []; 225 for (let i = 0; i < 100; i++) { 226 const nameGet = dictGenerator.getName(); 227 assert.isNotNull(nameGet); 228 arr.push(nameGet); 229 } 230 231 arr.forEach(((value, index) => { 232 assert.strictEqual(value, optionTarget[index]); 233 })); 234 }); 235 }); 236 237 describe('reserved name generator test', function () { 238 const targetNames = [ 239 'ιet', 'lèt', 'ιèt', 'rèturn', 'retυrn', 'rètυrn', 'returη', 'rèturη', 240 'retυrη', 'rètυrη', 'þreak', 'brèak', 'þrèak', 'breαk', 'þreαk', 'brèαk', 241 'þrèαk', 'breaκ', 'þreaκ', 'brèaκ', 'þrèaκ', 'breακ', 'þreακ', 'brèακ', 242 'þrèακ', 'çontinue', 'cοntinue', 'çοntinue', 'coηtinue', 'çoηtinue', 243 'cοηtinue', 'çοηtinue', 'contìnue', 'çontìnue', 'cοntìnue', 'çοntìnue', 244 'coηtìnue', 'çoηtìnue', 'cοηtìnue', 'çοηtìnue', 'contiηue', 'çontiηue', 245 'cοntiηue', 'çοntiηue', 'coηtiηue', 'çoηtiηue', 'cοηtiηue', 'çοηtiηue', 246 'contìηue', 'çontìηue', 'cοntìηue', 'çοntìηue', 'coηtìηue', 'çoηtìηue', 247 'cοηtìηue', 'çοηtìηue', 'continυe', 'çontinυe', 'cοntinυe', 'çοntinυe', 248 'coηtinυe', 'çoηtinυe', 'cοηtinυe', 'çοηtinυe', 'contìnυe', 'çontìnυe', 249 'cοntìnυe', 'çοntìnυe', 'coηtìnυe', 'çoηtìnυe', 'cοηtìnυe', 'çοηtìnυe', 250 'contiηυe', 'çontiηυe', 'cοntiηυe', 'çοntiηυe', 'coηtiηυe', 'çoηtiηυe', 251 'cοηtiηυe', 'çοηtiηυe', 'contìηυe', 'çontìηυe', 'cοntìηυe', 'çοntìηυe', 252 'coηtìηυe', 'çoηtìηυe', 'cοηtìηυe', 'çοηtìηυe', 'continuè', 'çontinuè', 253 'cοntinuè', 'çοntinuè', 'coηtinuè', 'çoηtinuè', 'cοηtinuè', 'çοηtinuè', 254 'contìnuè', 'çontìnuè', 'cοntìnuè', 'çοntìnuè' 255 ]; 256 257 it('reserved name generator check value', function () { 258 259 const reservedGenerator = getNameGenerator(NameGeneratorType.RESERVED_NAME); 260 let arr = []; 261 for (let i = 0; i < 100; i++) { 262 const nameGet = reservedGenerator.getName(); 263 assert.isNotNull(nameGet); 264 arr.push(nameGet); 265 } 266 267 arr.forEach(((value, index) => { 268 assert.strictEqual(value, targetNames[index]); 269 })); 270 }); 271 }); 272}); 273