|
| 1 | +/* { RETOS DE PROGRAMACIÓN } #13 PRUEBAS UNITARIAS */ |
| 2 | +// I refer to "JavaScript Notes for Professionals" from the people of Stack Overflow. https://goalkicker.com/JavaScriptBook |
| 3 | +// Additionally, I use GPT as a reference and sometimes to correct or generate proper comments. |
| 4 | + |
| 5 | +//short for console.log |
| 6 | +let log = console.log; |
| 7 | + |
| 8 | +window.addEventListener('load', ()=>{ |
| 9 | + const body = document.querySelector('body'); |
| 10 | + const title = document.createElement('h1'); |
| 11 | + |
| 12 | + body.style.setProperty('background', '#000'); |
| 13 | + body.style.setProperty('text-align', 'center'); |
| 14 | + |
| 15 | + title.textContent = 'Retosparaprogramadores #13.'; |
| 16 | + title.style.setProperty('font-size', '3.5vmax'); |
| 17 | + title.style.setProperty('color', '#fff'); |
| 18 | + title.style.setProperty('line-height', '100vh'); |
| 19 | + |
| 20 | + body.appendChild(title); |
| 21 | + |
| 22 | + setTimeout(()=>{ |
| 23 | + alert('Retosparaprogramadores #13. Please open the Browser Developer Tools.'); |
| 24 | + }, 2000); |
| 25 | + log( 'Retosparaprogramadores #13'); |
| 26 | +}); |
| 27 | + |
| 28 | +/* GoalKicker.com – JavaScript® Notes for Professionals 219 |
| 29 | +Section 30.9: Debugging with assertions - console.assert() Writes an error message to the console if the assertion is false. Otherwise, if the assertion is true, this does nothing. */ |
| 30 | + |
| 31 | +console.assert('one' === 1); // Assertion failed: |
| 32 | +//Note there's no message cause we didn't specify no one |
| 33 | + |
| 34 | +//Multiple arguments can be provided after the assertion–these can be strings or other objects–that will only be printed if the assertion is false: |
| 35 | + |
| 36 | +console.assert(true , 'Testing assertion...', null, undefined, Object); |
| 37 | + |
| 38 | +console.assert(false , 'Testing assertion...', null, undefined, Object); |
| 39 | +// Assertion failed: Testing assertion... NaN undefined funstion Object() { [native code] } |
| 40 | + |
| 41 | + |
| 42 | +/*console.assert does not throw an AssertionError (except in Node.js), meaning that this method is incompatible with most testing frameworks and that code execution will not break on a failed assertion. */ |
| 43 | + |
| 44 | +// Custom assert function that logs an error instead of throwing |
| 45 | +const assert = (condition, message, errors) => { |
| 46 | + if (!condition) { |
| 47 | + console.error(message); |
| 48 | + errors.push(message); // Collect the error message |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +const sum = (a, b) => { |
| 53 | + if (typeof a !== 'number' || typeof b !== 'number') { |
| 54 | + throw new TypeError('Both arguments must be numbers'); |
| 55 | + } |
| 56 | + return a + b; |
| 57 | +}; |
| 58 | + |
| 59 | +const testSum = (errors) => { |
| 60 | + assert(sum(2, 3) === 5, 'Error: 2 + 3 should be 5', errors); |
| 61 | + assert(sum(-1, 1) === 0, 'Error: -1 + 1 should be 0', errors); |
| 62 | + assert(sum(0, 0) === 0, 'Error: 0 + 0 should be 0', errors); |
| 63 | + |
| 64 | + try { |
| 65 | + sum(2, '3'); // This should throw an error |
| 66 | + } catch (e) { |
| 67 | + console.log('Caught an error:', e); // Log the caught error |
| 68 | + assert(e instanceof TypeError, 'Error: Invalid argument type not handled', errors); |
| 69 | + assert(e.message === 'Both arguments must be numbers', 'Error: Argument type mismatch', errors); |
| 70 | + } |
| 71 | + |
| 72 | + console.log('All sum tests have been executed.'); |
| 73 | +}; |
| 74 | + |
| 75 | +const personalInfo = { |
| 76 | + name: "Niko Zen", |
| 77 | + age: 41, |
| 78 | + birth_date: "1983/08/08", |
| 79 | + programming_languages: ["JavaScript", "Python", "Rust", "Ruby", "Java"] |
| 80 | +}; |
| 81 | + |
| 82 | +const testPersonalInfo = (obj, errors) => { |
| 83 | + if (Object.keys(obj).length === 0) { |
| 84 | + log('The obj is empty, skipping personal info tests.'); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Check that all fields exist |
| 89 | + assert(obj.hasOwnProperty('name'), 'Missing field "name"', errors); |
| 90 | + assert(obj.hasOwnProperty('age'), 'Missing field "age"', errors); |
| 91 | + assert(obj.hasOwnProperty('birth_date'), 'Missing field "birth_date"', errors); |
| 92 | + assert(obj.hasOwnProperty('programming_languages'), 'Missing field "programming_languages"', errors); |
| 93 | + |
| 94 | + // Check data types |
| 95 | + assert(typeof obj.name === 'string', '"name" must be a string', errors); |
| 96 | + assert(typeof obj.age === 'number', '"age" must be a number', errors); |
| 97 | + |
| 98 | + // Check if birth_date is a valid date |
| 99 | + const birthDate = new Date(obj.birth_date); |
| 100 | + assert(!isNaN(birthDate.getTime()), '"birth_date" must be a valid Date', errors); |
| 101 | + |
| 102 | + assert(Array.isArray(obj.programming_languages), '"programming_languages" is not an array', errors); |
| 103 | + |
| 104 | + // Validate each programming language |
| 105 | + obj.programming_languages.forEach(lang => assert(typeof lang === 'string', 'Each language must be a string', errors)); |
| 106 | + |
| 107 | + // Check fields are not empty |
| 108 | + assert(obj.name.length > 0, '"name" cannot be empty', errors); |
| 109 | + assert(obj.age > 0, '"age" must be greater than 0', errors); |
| 110 | + assert(obj.birth_date.length > 0, '"birth_date" cannot be empty', errors); |
| 111 | + assert(obj.programming_languages.length > 0, '"programming_languages" should have at least one language', errors); |
| 112 | + |
| 113 | + log('All personal info tests have been executed.'); |
| 114 | +}; |
| 115 | + |
| 116 | +/*Note: |
| 117 | + The getTime() method returns the number of milliseconds since January 1, 1970, 00:00:00 UTC for the Date object. |
| 118 | + If the date is invalid (e.g., if the input string is not a valid date format), getTime() will return NaN. |
| 119 | + The isNaN() function checks if the value is NaN. Therefore, !isNaN(birthDate.getTime()) will be true if the date is valid and false if it is not. |
| 120 | + */ |
| 121 | + |
| 122 | + |
| 123 | +const nikitaInfo = {}; |
| 124 | + |
| 125 | +const errors = []; |
| 126 | +testSum(errors); |
| 127 | +testPersonalInfo(personalInfo, errors); // Should pass |
| 128 | +testPersonalInfo(nikitaInfo, errors); // Should fail |
| 129 | + |
| 130 | +// Log all collected errors at the end |
| 131 | +if (errors.length > 0) { |
| 132 | + log('Errors encountered during tests:'); |
| 133 | + errors.forEach(error => log(error)); |
| 134 | +} else { |
| 135 | + log('No errors encountered during tests.'); |
| 136 | +} |
| 137 | + |
| 138 | +/* Assertion failed: |
| 139 | +Assertion failed: Testing assertion... null undefined |
| 140 | +function Object() |
| 141 | +
|
| 142 | +Caught an error: TypeError: Both arguments must be numbers |
| 143 | +All sum tests have been executed. |
| 144 | +All personal info tests have been executed. |
| 145 | +The obj is empty, skipping personal info tests. |
| 146 | +No errors encountered during tests. |
| 147 | +Retosparaprogramadores #13 |
| 148 | + */ |
| 149 | + |
0 commit comments