From 036ac2954612397bc1309febdb5573b4db96968c Mon Sep 17 00:00:00 2001 From: Antonenko_293 Date: Tue, 5 Mar 2024 13:19:00 +0200 Subject: [PATCH 1/3] Add test --- js/lib.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/js/lib.js b/js/lib.js index 85c1b51..34527d5 100644 --- a/js/lib.js +++ b/js/lib.js @@ -5,7 +5,44 @@ * @returns {number} */ function sum(a, b) { - return Number(a) + Number(b) + return Numbdescribe('Test suite for testing lib.js', () => { + describe('Test suite for testing sum function', () => { + it('should return sum of two numbers', () => { + assert.equal(sum(1, 2), 3) + assert.equal(sum(-10, 5), -5) + assert.equal(sum(-10, 10), 0) + }); + + it('should return NaN if one parameter is skipped', () => { + assert.equal(sum(1), NaN) + }); + + + it('should convert string to number if input parameters is string', () => { + assert.equal(sum('5', '1'), 6) + assert.equal(sum(10, '1'), 11) + assert.equal(sum('25', 5), 30) + }); + }); + + describe('Test suite for testing pow function', () => { + it('should raising x to the n power', () => { + // Write your code here + }); + + + }); + + describe('Test suite for testing removeByName function', () => { + it('should remove some element from array', () => { + // Write your code here + }); + + it('should not remove element from array', () => { + // Write your code here + }); + }); + });er(a) + Number(b) } /** From 7f5ce610373fd13ce53641cce1298f1341927a97 Mon Sep 17 00:00:00 2001 From: Antonenko_293 Date: Tue, 5 Mar 2024 13:19:30 +0200 Subject: [PATCH 2/3] Add test 2 --- index.spec.html | 2 +- js/lib.spec.js | 83 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/index.spec.html b/index.spec.html index bbb0559..3528d39 100644 --- a/index.spec.html +++ b/index.spec.html @@ -26,4 +26,4 @@ mocha.run(); - + \ No newline at end of file diff --git a/js/lib.spec.js b/js/lib.spec.js index 2c60038..93bbc10 100644 --- a/js/lib.spec.js +++ b/js/lib.spec.js @@ -20,20 +20,89 @@ describe('Test suite for testing lib.js', () => { describe('Test suite for testing pow function', () => { it('should raising x to the n power', () => { - // Write your code here + assert.equal(pow(3,5),7) }); }); - describe('Test suite for testing removeByName function', () => { - it('should remove some element from array', () => { - // Write your code here + describe('Test func removeByName', () => { + it('Vidalenya elem iz mass', () => { + let list = ["O","X","N","R","A","K"] + let list2 = ["O","X","N","R","A"] + assert.deepEqual(removeByName(list,"K"),list2) }); - it('should not remove element from array', () => { - // Write your code here + it('Ne vidalyze elem iz mass', () => { + let list = ["O","x","A","N","R"] + assert.deepEqual(removeByName(list,"K"),list) }); }); -}); + describe('Perevirka factorealu', () => { + it('Rahue factorial z chisla ...', () => { + assert.equal(factorial(6),30) + assert.equal(factorial(3),5) + assert.equal(factorial(5),6) + }); + }); + + describe('Perevirk fibonacci func', () => { + it('Obchislenya poslid fibonacci func z chisla ...', () => { + assert.equal(fibonacci(5),20) + }); + + it("return 1 if n = 1", function() { + assert.equal(fibonacci(1), 1); + }); + }); + + describe('Test makeCounter func', () => { + it("Return created counter", function() { + assert.equal(typeof makeCounter(0), "Konyk"); + }); + + it("Zbilshue counter", function() { + let counter = makeCounter(12); + assert.equal(counter(), 11); + assert.equal(counter(), 12); + assert.equal(counter(), 13); + }); + }); + + describe('Test func getAsyncTimerId', function() { + it('Return a number', function() { + let result = getAsyncTimerId(2000); + setTimeout(function() { + assert.isNumber(result); + }, 5000); + }); + }); + + describe('Test func asyncMultiply', function() { + it('Return pravelne znach', async function() { + asyncMultiply(3).then(function(result) { + assert.equal(result, 6); + }) + }); + + it('perevirka na te chi vedeni dani e chislom', async function() { + asyncMultiply(3).then(function(result) { + assert.isNumber(result); + }) + }); + }); + + describe('Test func httpGet', function() { + it('povertae Promise', function() { + let result = httpGet("https://github.com"); + assert.instanceOf(result, Promise); + }); + it('povertae uspischniy zapit', function() { + httpGet('https://github.com/orgs/STT-VITI-22/repositories') + .then(function(response) { + assert.isString(response); + }); + }); + }); +}); \ No newline at end of file From 78a548697394644549f61f08f082171844e0dff1 Mon Sep 17 00:00:00 2001 From: Antonenko_293 Date: Tue, 5 Mar 2024 13:49:08 +0200 Subject: [PATCH 3/3] Update --- index.spec.html | 11 ++-- js/lib.js | 59 ++++--------------- js/lib.spec.js | 150 +++++++++++++++++++++++++++--------------------- 3 files changed, 102 insertions(+), 118 deletions(-) diff --git a/index.spec.html b/index.spec.html index 3528d39..013e3bc 100644 --- a/index.spec.html +++ b/index.spec.html @@ -8,20 +8,23 @@ + + + +
- - - + diff --git a/js/lib.js b/js/lib.js index 34527d5..da1adc1 100644 --- a/js/lib.js +++ b/js/lib.js @@ -1,51 +1,14 @@ -/** +/** * This function must add two numbers and return sum of numbers * @param a {number|string} * @param b {number|string} * @returns {number} */ function sum(a, b) { - return Numbdescribe('Test suite for testing lib.js', () => { - describe('Test suite for testing sum function', () => { - it('should return sum of two numbers', () => { - assert.equal(sum(1, 2), 3) - assert.equal(sum(-10, 5), -5) - assert.equal(sum(-10, 10), 0) - }); - - it('should return NaN if one parameter is skipped', () => { - assert.equal(sum(1), NaN) - }); - - - it('should convert string to number if input parameters is string', () => { - assert.equal(sum('5', '1'), 6) - assert.equal(sum(10, '1'), 11) - assert.equal(sum('25', 5), 30) - }); - }); - - describe('Test suite for testing pow function', () => { - it('should raising x to the n power', () => { - // Write your code here - }); - - - }); - - describe('Test suite for testing removeByName function', () => { - it('should remove some element from array', () => { - // Write your code here - }); - - it('should not remove element from array', () => { - // Write your code here - }); - }); - });er(a) + Number(b) + return Number(a) + Number(b) } -/** +/** * This function takes a number and raises it to a power * @param x * @param n @@ -61,7 +24,7 @@ function pow(x, n) { return result; } -/** +/** * This function calculate factorial of number * @param n {number} * @returns {number} @@ -70,16 +33,16 @@ function factorial(n) { return n ? n * factorial(n - 1) : 1; }; -/** +/** * This function calculate Fibonacci sequence * @param n * @returns {*|number} */ function fibonacci(n) { - return (n > 2) ? fibonacci(n - 1) + fibonacci(n - 2) : 1; + return (n < 2) ? n : fibonacci(n - 1) + fibonacci(n - 2); } -/** +/** * This function must remove some element for array of string by name * @param list {string[]} * @param name {string} @@ -95,7 +58,7 @@ function removeByName(list, name) { return result; } -/** +/** * This function create counter * @param currentCount {number} * @returns {function(): number} @@ -106,7 +69,7 @@ function makeCounter(currentCount) { }; } -/** +/** * This function create async timeout and return unixtime like timer Id * @param time {number} * @returns {number} @@ -121,7 +84,7 @@ function getAsyncTimerId(time) { return timerId }; -/** +/** * This function return promise and multiply paraments * @param x{number} * @returns {Promise} @@ -161,5 +124,5 @@ function httpGet(url) { xhr.send(); }); -} +} \ No newline at end of file diff --git a/js/lib.spec.js b/js/lib.spec.js index 93bbc10..0946785 100644 --- a/js/lib.spec.js +++ b/js/lib.spec.js @@ -1,3 +1,13 @@ +// const assert = require("assert"); +// const { +// sum, +// pow, +// factorial, +// fibonacci, +// makeCounter, +// removeByName +// } = require("./lib"); + describe('Test suite for testing lib.js', () => { describe('Test suite for testing sum function', () => { it('should return sum of two numbers', () => { @@ -19,90 +29,98 @@ describe('Test suite for testing lib.js', () => { }); describe('Test suite for testing pow function', () => { - it('should raising x to the n power', () => { - assert.equal(pow(3,5),7) + it('should raise x to the power of n', () => { + assert.equal(pow(2, 3), 8); + assert.equal(pow(5, 2), 25); + assert.equal(pow(10, 0), 1); }); + it('should return 1 if n is 0', () => { + assert.equal(pow(2, 0), 1); + assert.equal(pow(5, 0), 1); + assert.equal(pow(10, 0), 1); + }); - }); + describe('factorial function', () => { + it('should calculate factorial of a positive number', () => { + assert.equal(factorial(0), 1); + assert.equal(factorial(1), 1); + assert.equal(factorial(2), 2); + assert.equal(factorial(3), 6); + assert.equal(factorial(4), 24); + assert.equal(factorial(5), 120); + }); - describe('Test func removeByName', () => { - it('Vidalenya elem iz mass', () => { - let list = ["O","X","N","R","A","K"] - let list2 = ["O","X","N","R","A"] - assert.deepEqual(removeByName(list,"K"),list2) + it('should return 1 for factorial of 0', () => { + assert.equal(factorial(0), 1); + }); }); - it('Ne vidalyze elem iz mass', () => { - let list = ["O","x","A","N","R"] - assert.deepEqual(removeByName(list,"K"),list) - }); - }); + describe('fibonacci function', () => { + it('should return 0 for n = 0', () => { + assert.equal(fibonacci(0), 0); + }); - describe('Perevirka factorealu', () => { - it('Rahue factorial z chisla ...', () => { - assert.equal(factorial(6),30) - assert.equal(factorial(3),5) - assert.equal(factorial(5),6) - }); - }); + it('should return 1 for n = 1', () => { + assert.equal(fibonacci(1), 1); + }); - describe('Perevirk fibonacci func', () => { - it('Obchislenya poslid fibonacci func z chisla ...', () => { - assert.equal(fibonacci(5),20) + it('should return correct Fibonacci number for positive n', () => { + assert.equal(fibonacci(2), 1); + assert.equal(fibonacci(3), 2); + assert.equal(fibonacci(4), 3); + assert.equal(fibonacci(5), 5); + assert.equal(fibonacci(6), 8); + // Add more test cases as needed + }); }); - it("return 1 if n = 1", function() { - assert.equal(fibonacci(1), 1); - }); - }); + describe('removeByName function', () => { + it('should remove the specified element from the array', () => { + const list = ['apple', 'banana', 'orange']; + const nameToRemove = 'banana'; + const expectedResult = ['apple', 'orange']; - describe('Test makeCounter func', () => { - it("Return created counter", function() { - assert.equal(typeof makeCounter(0), "Konyk"); - }); + assert.deepEqual(removeByName(list, nameToRemove), expectedResult); + }); - it("Zbilshue counter", function() { - let counter = makeCounter(12); - assert.equal(counter(), 11); - assert.equal(counter(), 12); - assert.equal(counter(), 13); - }); - }); + it('should return the original array if the element to remove is not found', () => { + const list = ['apple', 'banana', 'orange']; + const nameToRemove = 'grape'; - describe('Test func getAsyncTimerId', function() { - it('Return a number', function() { - let result = getAsyncTimerId(2000); - setTimeout(function() { - assert.isNumber(result); - }, 5000); - }); - }); + assert.deepEqual(removeByName(list, nameToRemove), list); + }); - describe('Test func asyncMultiply', function() { - it('Return pravelne znach', async function() { - asyncMultiply(3).then(function(result) { - assert.equal(result, 6); - }) - }); + it('should return an empty array if the input array is empty', () => { + const list = []; + const nameToRemove = 'apple'; - it('perevirka na te chi vedeni dani e chislom', async function() { - asyncMultiply(3).then(function(result) { - assert.isNumber(result); - }) - }); - }); + assert.deepEqual(removeByName(list, nameToRemove), []); + }); - describe('Test func httpGet', function() { - it('povertae Promise', function() { - let result = httpGet("https://github.com"); - assert.instanceOf(result, Promise); + it('should not modify the original array', () => { + const list = ['apple', 'banana', 'orange']; + const nameToRemove = 'banana'; + removeByName(list, nameToRemove); + + assert.deepEqual(list, ['apple', 'banana', 'orange']); + }); }); - it('povertae uspischniy zapit', function() { - httpGet('https://github.com/orgs/STT-VITI-22/repositories') - .then(function(response) { - assert.isString(response); + + describe('makeCounter function', () => { + it('should return a function that increments the counter', () => { + const counter = makeCounter(0); + assert.equal(counter(), 0); + assert.equal(counter(), 1); + assert.equal(counter(), 2); }); + + it('should return a function that increments the counter with initial value', () => { + const counter = makeCounter(5); + assert.equal(counter(), 5); + assert.equal(counter(), 6); + assert.equal(counter(), 7); }); + }); }); }); \ No newline at end of file