From efa2393701bd5e3a6c9450ab088a7a286fb22e91 Mon Sep 17 00:00:00 2001 From: sricharankrishnan Date: Sun, 4 Feb 2024 17:30:24 +0530 Subject: [PATCH] [build] Unit Tests - created all required unit tests with jests for the package. all critical methods were covered in these tests - removed/corrected some methods in the package --- dist/index.js | 10 +-- package-lock.json | 4 +- package.json | 2 +- src/index.js | 4 +- tests/accepts-only-string-arg.test.js | 12 ++-- tests/count-all-vowels.test.js | 54 +++++++++++++++ tests/extract-all-vowels-from-string.test.js | 59 ++++++++++++++++ tests/get-position-of-vowels.test.js | 50 ++++++++++++++ tests/get-total-count-of-vowels.test.tsx | 49 ++++++++++++++ tests/remove-all-vowels-from-string.test.js | 2 +- tests/replace-vowels-with-character.test.js | 46 +++++++++++++ tests/set-vowels-to-lowercase.test.js | 36 ++++++++++ tests/set-vowels-to-uppercase.test.js | 36 ++++++++++ tests/vowel-encryption.test.js | 71 ++++++++++++++++++++ 14 files changed, 418 insertions(+), 17 deletions(-) create mode 100644 tests/count-all-vowels.test.js create mode 100644 tests/extract-all-vowels-from-string.test.js create mode 100644 tests/get-position-of-vowels.test.js create mode 100644 tests/get-total-count-of-vowels.test.tsx create mode 100644 tests/replace-vowels-with-character.test.js create mode 100644 tests/set-vowels-to-lowercase.test.js create mode 100644 tests/set-vowels-to-uppercase.test.js create mode 100644 tests/vowel-encryption.test.js diff --git a/dist/index.js b/dist/index.js index ce7ed7e..1d52ec2 100644 --- a/dist/index.js +++ b/dist/index.js @@ -96,8 +96,8 @@ var Vowelz = /*#__PURE__*/function () { /* sets all vowels to upper case */ }, { - key: "setAllVowelsToUppercase", - value: function setAllVowelsToUppercase(arg) { + key: "setAllVowelsToUpperCase", + value: function setAllVowelsToUpperCase(arg) { var $this = this; if (!$this.checkIfPropIsString(arg)) { throw new Error("Needed argument to be of type string but is not: ".concat(arg)); @@ -110,8 +110,8 @@ var Vowelz = /*#__PURE__*/function () { /* get how many vowels are there in a string value */ }, { - key: "getCount", - value: function getCount(arg) { + key: "getTotalCount", + value: function getTotalCount(arg) { var $this = this; if (!$this.checkIfPropIsString(arg)) { throw new Error("Needed argument to be of type string but is not: ".concat(arg)); @@ -158,7 +158,7 @@ var Vowelz = /*#__PURE__*/function () { throw new Error("Needed argument to be of type string but is not: ".concat(arg)); return; } - var uniqueVowels = new Set(arg.match(/[aeiou]/gi)); + var uniqueVowels = new Set(arg.toLowerCase().match(/[aeiou]/g)); return uniqueVowels.size; } diff --git a/package-lock.json b/package-lock.json index 00147d6..05163b5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vowelz", - "version": "1.1.3", + "version": "1.2.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vowelz", - "version": "1.1.3", + "version": "1.2.4", "license": "MIT", "devDependencies": { "@babel/cli": "^7.23.4", diff --git a/package.json b/package.json index 0600fe7..b95b016 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vowelz", - "version": "1.1.3", + "version": "1.2.4", "description": "A JavaScript library for efficient manipulation of vowels within strings. Count, transform case, extract positions, and perform various operations on vowels with ease. Enhance your text processing and string transformations using Vowelz.", "main": "index.js", "homepage": "https://github.com/sricharankrishnan/vowelz", diff --git a/src/index.js b/src/index.js index bb9ff07..324a334 100644 --- a/src/index.js +++ b/src/index.js @@ -75,7 +75,7 @@ class Vowelz { } /* sets all vowels to upper case */ - setAllVowelsToUppercase(arg) { + setAllVowelsToUpperCase(arg) { var $this = this; if (!$this.checkIfPropIsString(arg)) { throw new Error(`Needed argument to be of type string but is not: ${arg}`); @@ -88,7 +88,7 @@ class Vowelz { } /* get how many vowels are there in a string value */ - getCount(arg) { + getTotalCount(arg) { var $this = this; if (!$this.checkIfPropIsString(arg)) { throw new Error(`Needed argument to be of type string but is not: ${arg}`); diff --git a/tests/accepts-only-string-arg.test.js b/tests/accepts-only-string-arg.test.js index cf8f871..03941b6 100644 --- a/tests/accepts-only-string-arg.test.js +++ b/tests/accepts-only-string-arg.test.js @@ -55,23 +55,23 @@ describe("Vowelz - Methods Accepts Only String Args", () => { }).toThrow(); }); - test("getCount() - accepts only string arg", () => { + test("getTotalCount() - accepts only string arg", () => { expect(() => { - myVowelz.getCount(stringArg); + myVowelz.getTotalCount(stringArg); }).not.toThrow(); expect(() => { - myVowelz.getCount(nonStringArg); + myVowelz.getTotalCount(nonStringArg); }).toThrow(); }); - test("setAllVowelsToUppercase() - accepts only string arg", () => { + test("setAllVowelsToUpperCase() - accepts only string arg", () => { expect(() => { - myVowelz.setAllVowelsToUppercase(stringArg); + myVowelz.setAllVowelsToUpperCase(stringArg); }).not.toThrow(); expect(() => { - myVowelz.setAllVowelsToUppercase(nonStringArg); + myVowelz.setAllVowelsToUpperCase(nonStringArg); }).toThrow(); }); diff --git a/tests/count-all-vowels.test.js b/tests/count-all-vowels.test.js new file mode 100644 index 0000000..9235d9b --- /dev/null +++ b/tests/count-all-vowels.test.js @@ -0,0 +1,54 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - countAllVowels method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("returns all vowels as 'zero' count if input string is empty", () => { + const input = ""; + const output = { + a: 0, + e: 0, + i: 0, + o: 0, + u: 0 + }; + const result = myVowelz.countAllVowels(input); + expect(result).toEqual(output); + }); + + test("returns all vowels as 'zero' count if there are no vowels in input string", () => { + const input = "My Rhythms fly. My rhythms cry."; + const output = { + a: 0, + e: 0, + i: 0, + o: 0, + u: 0 + }; + const result = myVowelz.countAllVowels(input); + expect(result).toEqual(output); + }); + + test("returns the correct count of all vowels from an input string", () => { + const input = "Hello World. This is a test message"; + const output = { + a: 2, + e: 4, + i: 2, + o: 2, + u: 0 + }; + const result = myVowelz.countAllVowels(input); + expect(result).toEqual(output); + }); +}); diff --git a/tests/extract-all-vowels-from-string.test.js b/tests/extract-all-vowels-from-string.test.js new file mode 100644 index 0000000..64ecec0 --- /dev/null +++ b/tests/extract-all-vowels-from-string.test.js @@ -0,0 +1,59 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - extractAllVowelsFromString method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("will also extract duplicate strings from the input", () => { + const input = "Pool Table"; + const output = ["o", "o", "a", "e"]; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toEqual(output); + }); + + test("preserves the order of vowels in the input string", () => { + const input = "Beautiful World"; + const output = ["e", "a", "u", "i", "u", "o"]; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toEqual(output); + }); + + test("gives an empty array output if no vowels are present in the input", () => { + const input = "1234567890!@#$%^&*()~_+"; + const output = []; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toHaveLength(0); + expect(result).toEqual(output); + }); + + test("returns an empty array if the input string is empty", () => { + const input = ""; + const output = []; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toHaveLength(0); + expect(result).toEqual(output); + }); + + test("the ouput array of vowels is of the right length", () => { + const input = "Hello World"; + const output = 3; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toHaveLength(output); + }); + + test("correct extracts all vowels from a given string", () => { + const input = "Hello, World! This is a test string."; + const output = ["e", "o", "o", "i", "i", "a", "e", "i"]; + const result = myVowelz.extractAllVowelsFromString(input); + expect(result).toEqual(output); + }); +}); diff --git a/tests/get-position-of-vowels.test.js b/tests/get-position-of-vowels.test.js new file mode 100644 index 0000000..ca5db47 --- /dev/null +++ b/tests/get-position-of-vowels.test.js @@ -0,0 +1,50 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - getPositionsOfVowels method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("provides the correct result even when are duplicate vowels", () => { + const input = "Pool Table"; + const output = [1, 2, 6, 9]; + const result = myVowelz.getPositionsOfVowels(input); + expect(result).toEqual(output); + }); + + test("result prevserves order of indices for vowels in input string", () => { + const input = "hello world"; + const output = [1, 4, 7]; + const result = myVowelz.getPositionsOfVowels(input); + expect(result).toEqual(output); + }); + + test("no vowels in string: results in empty array", () => { + const input = "~!@#$%^&*()_+1234567890"; + const output = []; + const result = myVowelz.getPositionsOfVowels(input); + expect(result).toEqual(output); + }); + + test("empty string input: results in empty array", () => { + const input = ""; + const output = []; + const result = myVowelz.getPositionsOfVowels(input); + expect(result).toEqual(output); + }); + + test("returns an array of numbers, which are indices of vowels in input", () => { + const input = 'Hello, World! This is a test string.'; + const output = [1, 4, 8, 16, 19, 22, 25, 32]; + const result = myVowelz.getPositionsOfVowels(input); + expect(result).toEqual(output); + }); +}); diff --git a/tests/get-total-count-of-vowels.test.tsx b/tests/get-total-count-of-vowels.test.tsx new file mode 100644 index 0000000..7920ddd --- /dev/null +++ b/tests/get-total-count-of-vowels.test.tsx @@ -0,0 +1,49 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - getTotalCount method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("result includes duplicate vowels present in input string", () => { + const input = "Pool Table"; + const output = 4; + const result = myVowelz.getTotalCount(input); + expect(result).toBe(output); + }); + + test("will return 'zero' if there are no vowels present", () => { + const input = ""; + const output = 0; + const result = myVowelz.getTotalCount(input); + expect(result).toBe(output); + }); + + test("will return 'zero' if input string is empty", () => { + const input = ""; + const output = 0; + const result = myVowelz.getTotalCount(input); + expect(result).toBe(output); + }); + + test("evaluates and returns correct number of vowels in an input string", () => { + const input = "Hello, World! This is a test string."; + const output = 8; + const result = myVowelz.getTotalCount(input); + expect(result).toBe(output); + }); + + test("return a number type when caluclating total count of vowels", () => { + const input = "Hello World!"; + const result = myVowelz.getTotalCount(input); + expect(typeof result).toBe("number"); + }); +}); diff --git a/tests/remove-all-vowels-from-string.test.js b/tests/remove-all-vowels-from-string.test.js index ad0e220..9f91faf 100644 --- a/tests/remove-all-vowels-from-string.test.js +++ b/tests/remove-all-vowels-from-string.test.js @@ -2,7 +2,7 @@ import Vowelz from "../src/index.js"; /* t-suite */ -describe("Vowelz removeAllVowelsFromString method", () => { +describe("Vowelz - removeAllVowelsFromString method", () => { let myVowelz = null; beforeAll(() => { diff --git a/tests/replace-vowels-with-character.test.js b/tests/replace-vowels-with-character.test.js new file mode 100644 index 0000000..9df3bed --- /dev/null +++ b/tests/replace-vowels-with-character.test.js @@ -0,0 +1,46 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - replaceVowelsWithCharacter method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("throws an error if 'character arg' length is greater than 1", () => { + function demoWrapper() { + myVowelz.replaceVowelsWithCharacter("Hello World", "**"); + } + expect(demoWrapper).toThrow(); + }); + + test("will not modify the input string if there are no vowels", () => { + const character = "&"; + const input = "1234567890~!@#$%^&*()_+"; + const output = "1234567890~!@#$%^&*()_+"; + const results = myVowelz.replaceVowelsWithCharacter(input, character); + expect(results).toBe(output); + }); + + test("will return an empty string if the input string is empty", () => { + const character = "&"; + const input = ""; + const output = ""; + const results = myVowelz.replaceVowelsWithCharacter(input, character); + expect(results).toBe(output); + }); + + test("correctly replaces vowels with a given character", () => { + const character = "&"; + const input = "The sky is blue"; + const output = "Th& sky &s bl&&"; + const results = myVowelz.replaceVowelsWithCharacter(input, character); + expect(results).toBe(output); + }); +}); diff --git a/tests/set-vowels-to-lowercase.test.js b/tests/set-vowels-to-lowercase.test.js new file mode 100644 index 0000000..666a1cf --- /dev/null +++ b/tests/set-vowels-to-lowercase.test.js @@ -0,0 +1,36 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - setAllVowelsToLowerCase method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("does not alter the input string if there are no vowels", () => { + const input = "~!@#$%^&*()_+1234567890"; + const output = "~!@#$%^&*()_+1234567890"; + const result = myVowelz.setAllVowelsToLowerCase(input); + expect(result).toBe(output); + }); + + test("returns an empty string if the input was an empty string", () => { + const input = ""; + const output = ""; + const result = myVowelz.setAllVowelsToLowerCase(input); + expect(result).toBe(output); + }); + + test("correctly sets all vowels in a string to uppercase", () => { + const input = "HEllO WOrld! ThIs Is A tEst mEssAgE."; + const output = "Hello World! This is a test message."; + const result = myVowelz.setAllVowelsToLowerCase(input); + expect(result).toBe(output); + }); +}); diff --git a/tests/set-vowels-to-uppercase.test.js b/tests/set-vowels-to-uppercase.test.js new file mode 100644 index 0000000..ee92f7b --- /dev/null +++ b/tests/set-vowels-to-uppercase.test.js @@ -0,0 +1,36 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - setAllVowelsToUpperCase method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("does not alter the input string if there are no vowels", () => { + const input = "~!@#$%^&*()_+1234567890"; + const output = "~!@#$%^&*()_+1234567890"; + const result = myVowelz.setAllVowelsToUpperCase(input); + expect(result).toBe(output); + }); + + test("returns an empty string if the input was an empty string", () => { + const input = ""; + const output = ""; + const result = myVowelz.setAllVowelsToUpperCase(input); + expect(result).toBe(output); + }); + + test("correctly sets all vowels in a string to uppercase", () => { + const input = "Hello World! This is a test message."; + const output = "HEllO WOrld! ThIs Is A tEst mEssAgE."; + const result = myVowelz.setAllVowelsToUpperCase(input); + expect(result).toBe(output); + }); +}); diff --git a/tests/vowel-encryption.test.js b/tests/vowel-encryption.test.js new file mode 100644 index 0000000..a073e00 --- /dev/null +++ b/tests/vowel-encryption.test.js @@ -0,0 +1,71 @@ +/* app imports */ +import Vowelz from "../src/index.js"; + +/* t-suite */ +describe("Vowelz - vowelEncryption method", () => { + let myVowelz = null; + + beforeAll(() => { + myVowelz = new Vowelz(); + }); + + afterAll(() => { + myVowelz = null; + }); + + test("does not modify the input string if there are no vowels", () => { + const input = "My Rhythms fly. My rhythms cry."; + const output = "My Rhythms fly. My rhythms cry."; + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("returns an empty string if the input is an empty string as well", () => { + const input = ""; + const output = ""; + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("correctly encrpts all vowels in an input string", () => { + const input = "EutopiA"; + const output = "3*t0p!@"; + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("replaces 'u' or 'U' with '*' in the input string", () => { + const input = "unDug"; + const output = "*nD*g" + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("replaces 'o' or 'O' with '0' in the input string", () => { + const input = "BoOm"; + const output = "B00m" + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("replaces 'i' or 'I' with '!' in the input string", () => { + const input = "SkiIng"; + const output = "Sk!!ng" + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("replaces 'e' or 'E' with '3' in the input string", () => { + const input = "DeEp"; + const output = "D33p" + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); + + test("replaces 'a' or 'A' with '@' in the input string", () => { + const input = "BANaNa"; + const output = "B@N@N@"; + const result = myVowelz.vowelEncryption(input); + expect(result).toBe(output); + }); +});