-
Notifications
You must be signed in to change notification settings - Fork 0
/
hasSpecialChars.js
94 lines (77 loc) · 2.53 KB
/
hasSpecialChars.js
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
/* eslint-disable */
const {hasSpecialChars} = require("../functions/hasSpecialChars");
const fs = require("fs");
function generateImageBadgeURL(label, value, passed) {
return `https://img.shields.io/badge/${encodeURIComponent(label)}-${value}-${passed === 0 ? '13b285' : 'ff69b4'}`;}
function generateTestBadge(functionName, numberOfPassed, numberOfFailed) {
const url = generateImageBadgeURL(functionName + '()', encodeURIComponent(numberOfPassed + ' passed, ' + numberOfFailed + ' failed.'), numberOfFailed);
return '\n\n### Tests for ' + functionName + '\n\n' + '![' + functionName + '](' + url + ')';}
function addToReadme(str) {fs.appendFile('./README.md', str, function (err) {if (err) throw err;console.log('String added to the file');});}/**
* Test for hasSpecialChars function.
*/
function testHasSpecialChars() {
const name_of_function = "hasSpecialChars";
let number_of_tests_passed = 0;
let number_of_tests_failed = 0;
try {
if (hasSpecialChars("abc!@#123", 3)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
try {
if (!hasSpecialChars("abc!@#123", 4)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
try {
if (!hasSpecialChars("abcdef123", 1)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
try {
if (hasSpecialChars("abc!@#&*^%$123", 8)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Testing very long strings
try {
const longString = 'a'.repeat(10000) + '!'.repeat(100);
if (hasSpecialChars(longString, 100)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
// Testing with n=0
try {
if (hasSpecialChars("abcd1234", 0)) {
number_of_tests_passed++;
} else {
number_of_tests_failed++;
}
} catch (err) {
number_of_tests_failed++;
}
addToReadme(generateTestBadge(name_of_function, number_of_tests_passed, number_of_tests_failed));
}
module.exports = {
testHasSpecialChars
};