Skip to content

Commit

Permalink
test: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-ahmadbilal committed Sep 9, 2024
1 parent fea5736 commit 456ad85
Show file tree
Hide file tree
Showing 21 changed files with 1,552 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![npm package][npm-img]][npm-url]
[![Build Status][build-img]][build-url]
[![Downloads][download-img]][npm-url]
[![Issues][issues-img]][issues-url]
[![Code Coverage][codecov-img]][codecov-url]
[![Semantic Release][semantic-release-img]][semantic-release-url]
Expand Down Expand Up @@ -632,6 +633,7 @@ I look forward to hearing from you!
[build-url]:https://github.com/dev-ahmadbilal/string-master/actions/workflows/release.yml
[npm-img]:https://img.shields.io/npm/v/string-master
[npm-url]:https://www.npmjs.com/package/string-master
[download-img]: https://badgen.net/npm/dt/currency-master
[issues-img]:https://img.shields.io/github/issues/dev-ahmadbilal/string-master
[issues-url]:https://github.com/dev-ahmadbilal/string-master/issues
[codecov-img]:https://codecov.io/gh/dev-ahmadbilal/string-master/branch/main/graph/badge.svg
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npm-pkg-boilerplate",
"version": "0.0.x",
"version": "0.0.1",
"description": "A boilerplate project for npm packages",
"main": "./lib/index.js",
"files": [
Expand Down
48 changes: 48 additions & 0 deletions test/casing-master.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { CasingMaster } from '../src/case-master';

describe('CasingMaster', () => {
test('should convert to camelCase', () => {
expect(CasingMaster.toCamelCase('hello world')).toBe('helloWorld');
expect(CasingMaster.toCamelCase('Hello world')).toBe('helloWorld');
});

test('should convert to snake_case', () => {
expect(CasingMaster.toSnakeCase('hello world')).toBe('hello_world');
expect(CasingMaster.toSnakeCase('Hello world')).toBe('hello_world');
});

test('should convert to PascalCase', () => {
expect(CasingMaster.toPascalCase('hello world')).toBe('HelloWorld');
expect(CasingMaster.toPascalCase('Hello world')).toBe('HelloWorld');
});

test('should convert to kebab-case', () => {
expect(CasingMaster.toKebabCase('hello world')).toBe('hello-world');
expect(CasingMaster.toKebabCase('Hello world')).toBe('hello-world');
});

test('should convert to SCREAMING_SNAKE_CASE', () => {
expect(CasingMaster.toScreamingSnakeCase('hello world')).toBe('HELLO_WORLD');
expect(CasingMaster.toScreamingSnakeCase('Hello world')).toBe('HELLO_WORLD');
});

test('should convert to Sentence case', () => {
expect(CasingMaster.toSentenceCase('hello world')).toBe('Hello world');
expect(CasingMaster.toSentenceCase('HELLO WORLD')).toBe('Hello world');
});

test('should convert to Title Case', () => {
expect(CasingMaster.toTitleCase('hello world')).toBe('Hello World');
expect(CasingMaster.toTitleCase('HELLO WORLD')).toBe('Hello World');
});

test('should smartly convert case formats', () => {
expect(CasingMaster.smartCaseConvert('hello world', 'camel')).toBe('helloWorld');
expect(CasingMaster.smartCaseConvert('hello world', 'snake')).toBe('hello_world');
expect(CasingMaster.smartCaseConvert('hello world', 'pascal')).toBe('HelloWorld');
expect(CasingMaster.smartCaseConvert('hello world', 'kebab')).toBe('hello-world');
expect(CasingMaster.smartCaseConvert('hello world', 'screaming-snake')).toBe('HELLO_WORLD');
expect(CasingMaster.smartCaseConvert('hello world', 'sentence')).toBe('Hello world');
expect(CasingMaster.smartCaseConvert('hello world', 'title')).toBe('Hello World');
});
});
35 changes: 35 additions & 0 deletions test/compression-master.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { CompressionMaster } from '../src/compression-master';

describe('CompressionMaster', () => {
test('should compress and decompress strings correctly', () => {
const originalString = 'Hello, world!';
const compressed = CompressionMaster.compress(originalString);
const decompressed = CompressionMaster.decompress(compressed);

expect(decompressed).toBe(originalString);
});

test('should handle empty strings', () => {
const originalString = '';
const compressed = CompressionMaster.compress(originalString);
const decompressed = CompressionMaster.decompress(compressed);

expect(decompressed).toBe(originalString);
});

test('should handle strings with special characters', () => {
const originalString = 'Special characters: !@#$%^&*()_+[]{}|;:",.<>?/';
const compressed = CompressionMaster.compress(originalString);
const decompressed = CompressionMaster.decompress(compressed);

expect(decompressed).toBe(originalString);
});

test('should handle large strings', () => {
const originalString = 'A'.repeat(10000); // Large string of 10,000 characters
const compressed = CompressionMaster.compress(originalString);
const decompressed = CompressionMaster.decompress(compressed);

expect(decompressed).toBe(originalString);
});
});
119 changes: 119 additions & 0 deletions test/conversion-master.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { ConversionMaster } from '../src/conversion-master';

describe('ConversionMaster', () => {
describe('toBoolean', () => {
it('should convert "yes" to true', () => {
expect(ConversionMaster.toBoolean('yes')).toBe(true);
});

it('should convert "no" to false', () => {
expect(ConversionMaster.toBoolean('no')).toBe(false);
});

it('should convert "on" to true', () => {
expect(ConversionMaster.toBoolean('on')).toBe(true);
});

it('should convert "off" to false', () => {
expect(ConversionMaster.toBoolean('off')).toBe(false);
});

it('should convert "1" to true', () => {
expect(ConversionMaster.toBoolean('1')).toBe(true);
});

it('should convert "0" to false', () => {
expect(ConversionMaster.toBoolean('0')).toBe(false);
});

it('should convert "true" to true', () => {
expect(ConversionMaster.toBoolean('true')).toBe(true);
});

it('should convert "false" to false', () => {
expect(ConversionMaster.toBoolean('false')).toBe(false);
});
});

describe('toFloat', () => {
it('should convert string to float with precision', () => {
expect(ConversionMaster.toFloat('123.456', 2)).toBe(123.46);
});

it('should convert string to float without precision', () => {
expect(ConversionMaster.toFloat('123.456')).toBe(123.456);
});

it('should handle non-numeric strings gracefully', () => {
expect(ConversionMaster.toFloat('abc')).toBeNaN();
});
});

describe('toInt', () => {
it('should convert decimal string to integer', () => {
expect(ConversionMaster.toInt('42')).toBe(42);
});

it('should convert hexadecimal string to integer', () => {
expect(ConversionMaster.toInt('0x1A')).toBe(26);
});

it('should handle non-numeric strings gracefully', () => {
expect(ConversionMaster.toInt('abc')).toBeNaN();
});
});

describe('toJson', () => {
it('should convert a JSON string to an object', () => {
expect(ConversionMaster.toJson('{"name": "John"}')).toEqual({ name: 'John' });
});

it('should throw an error for invalid JSON', () => {
expect(() => ConversionMaster.toJson('{name: John}')).toThrow(SyntaxError);
});
});

describe('fromJson', () => {
it('should convert a JSON object to a string', () => {
expect(ConversionMaster.fromJson({ name: 'John' })).toBe('{"name":"John"}');
});

it('should handle nested objects', () => {
const nestedObj = { user: { name: 'John', age: 30 }, active: true };
expect(ConversionMaster.fromJson(nestedObj)).toBe('{"user":{"name":"John","age":30},"active":true}');
});

it('should convert an empty object to an empty JSON string', () => {
expect(ConversionMaster.fromJson({})).toBe('{}');
});

it('should handle arrays in JSON objects', () => {
const objWithArray = { name: 'John', hobbies: ['reading', 'gaming'] };
expect(ConversionMaster.fromJson(objWithArray)).toBe('{"name":"John","hobbies":["reading","gaming"]}');
});
});

describe('toBase64', () => {
it('should convert a string to Base64', () => {
expect(ConversionMaster.toBase64('Hello World!')).toBe('SGVsbG8gV29ybGQh');
});
});

describe('fromBase64', () => {
it('should convert Base64 to a string', () => {
expect(ConversionMaster.fromBase64('SGVsbG8gV29ybGQh')).toBe('Hello World!');
});
});

describe('toHex', () => {
it('should convert a string to hexadecimal', () => {
expect(ConversionMaster.toHex('Hello')).toBe('48656c6c6f');
});
});

describe('fromHex', () => {
it('should convert hexadecimal to a string', () => {
expect(ConversionMaster.fromHex('48656c6c6f')).toBe('Hello');
});
});
});
108 changes: 108 additions & 0 deletions test/emoji-master.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { EmojiMaster } from '../src/emoji-master';

describe('EmojiMaster', () => {
describe('findEmojis', () => {
it('should find all emojis in a text', () => {
const result = EmojiMaster.findEmojis('Hello 👋 World 🌍!');
expect(result).toEqual(['👋', '🌍']);
});

it('should return an empty array when no emojis are found', () => {
const result = EmojiMaster.findEmojis('Hello World!');
expect(result).toEqual([]);
});

it('should handle a text with no emojis gracefully', () => {
const result = EmojiMaster.findEmojis('');
expect(result).toEqual([]);
});
});

describe('countEmojis', () => {
it('should count the number of emojis in a text', () => {
const result = EmojiMaster.countEmojis('Hello 👋 World 🌍!');
expect(result).toBe(2);
});

it('should return 0 when no emojis are found', () => {
const result = EmojiMaster.countEmojis('Hello World!');
expect(result).toBe(0);
});

it('should handle a text with no emojis gracefully', () => {
const result = EmojiMaster.countEmojis('');
expect(result).toBe(0);
});
});

describe('replaceEmojis', () => {
it('should replace all emojis with the specified placeholder', () => {
const result = EmojiMaster.replaceEmojis('Hello 👋 World 🌍!', '[emoji]');
expect(result).toBe('Hello [emoji] World [emoji]!');
});

it('should replace all emojis with the default placeholder', () => {
const result = EmojiMaster.replaceEmojis('Hello 👋 World 🌍!');
expect(result).toBe('Hello [emoji] World [emoji]!');
});

it('should handle a text with no emojis gracefully', () => {
const result = EmojiMaster.replaceEmojis('Hello World!');
expect(result).toBe('Hello World!');
});
});

describe('replaceWordsWithEmojis', () => {
it('should replace specified words with emojis', () => {
const replacements = { happy: '😊', sad: '😢' };
const result = EmojiMaster.replaceWordsWithEmojis('I am very happy and a bit sad.', replacements);
expect(result).toBe('I am very 😊 and a bit 😢.');
});

it('should handle text with no replacements', () => {
const replacements = { happy: '😊' };
const result = EmojiMaster.replaceWordsWithEmojis('I am neutral.', replacements);
expect(result).toBe('I am neutral.');
});

it('should handle empty text gracefully', () => {
const replacements = { happy: '😊' };
const result = EmojiMaster.replaceWordsWithEmojis('', replacements);
expect(result).toBe('');
});
});

describe('extractEmojiSequences', () => {
it('should extract all emoji sequences from a text', () => {
const result = EmojiMaster.extractEmojiSequences('Hello 👋 World 🌍! 🎉');
expect(result).toEqual(['👋', '🌍', '🎉']);
});

it('should return an empty array when no emojis are found', () => {
const result = EmojiMaster.extractEmojiSequences('Hello World!');
expect(result).toEqual([]);
});

it('should handle a text with no emojis gracefully', () => {
const result = EmojiMaster.extractEmojiSequences('');
expect(result).toEqual([]);
});
});

describe('containsEmojis', () => {
it('should return true if the text contains emojis', () => {
const result = EmojiMaster.containsEmojis('Hello 👋 World!');
expect(result).toBe(true);
});

it('should return false if the text does not contain emojis', () => {
const result = EmojiMaster.containsEmojis('Hello World!');
expect(result).toBe(false);
});

it('should handle a text with no emojis gracefully', () => {
const result = EmojiMaster.containsEmojis('');
expect(result).toBe(false);
});
});
});
47 changes: 47 additions & 0 deletions test/entropy-master.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { EntropyMaster } from '../src/entropy-master';

describe('EntropyMaster', () => {
describe('calculateEntropy', () => {
it('should calculate the entropy of a string', () => {
const entropy = EntropyMaster.calculateEntropy('password123');
expect(entropy).toBeCloseTo(3.2776, 4);
});

it('should handle an empty string', () => {
const entropy = EntropyMaster.calculateEntropy('');
expect(entropy).toBe(0); // Entropy of an empty string should be zero
});

it('should handle a single character string', () => {
const entropy = EntropyMaster.calculateEntropy('a');
expect(entropy).toBe(0); // Entropy of a single character string should be zero
});

it('should handle a string with repeating characters', () => {
const entropy = EntropyMaster.calculateEntropy('aaaaaa');
expect(entropy).toBe(0); // Entropy of a string with repeating characters should be 0
});
});

describe('getStrength', () => {
it('should return "Strong" for a string with high entropy', () => {
const strength = EntropyMaster.getStrength('A1b2C3d4E5');
expect(strength).toBe('Strong');
});

it('should return "Moderate" for a string with moderate entropy', () => {
const strength = EntropyMaster.getStrength('password123');
expect(strength).toBe('Moderate');
});

it('should return "Weak" for a string with low entropy', () => {
const strength = EntropyMaster.getStrength('aaaaaa');
expect(strength).toBe('Weak');
});

it('should handle an empty string and return "Weak"', () => {
const strength = EntropyMaster.getStrength('');
expect(strength).toBe('Weak');
});
});
});
Loading

0 comments on commit 456ad85

Please sign in to comment.