Skip to content

Problem 1.1 - New test helper and tests for problem 1.1 #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions chapter01/1.1 - Is Unique/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { test, assert } = require('../../test/TestHelper');

// Write your code here...
function isUnique(str) {}

/* TESTS */
test('All unique characters should be true', () => {
const result = isUnique('abcd');
const expected = true;
assert(result).toBe(expected);
});

test('Duplicate character should be false', () => {
const result = isUnique('abccd');
const expected = false;
assert(result).toBe(expected);
});

test('Multiple duplicate characters should be false', () => {
const result = isUnique('bhjjb');
const expected = false;
assert(result).toBe(expected);
});

test('All unique characters should be true', () => {
const result = isUnique('mdjq');
const expected = true;
assert(result).toBe(expected);
});

test('When first and last character are the same, should be false', () => {
const result = isUnique('bob');
const expected = true;
assert(result).toBe(expected);
});

test('Single character should be true', () => {
const result = isUnique('a');
const expected = true;
assert(result).toBe(expected);
});
41 changes: 41 additions & 0 deletions test/TestHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// @params: { String: title, Function: callback }
function test(title, callback) {
try {
callback();
console.log(`✅ ${title}`);
} catch (error) {
console.log(`❌ ${title}`);
console.log('Error', error);
}
}

function assert(result) {
return {
toBe: function (expected) {
if (result !== expected) {
throw new Error(`Expected ${expected}. Received ${result}`);
}
},
};
}

module.exports = { test, assert };

// NOTE: The only assertion right now is:
// * toBe

// HOW TO USE:
// 1. Define your assertions.
/*
const result = add(10, 1);
const expected = 11;
*/

// 2. Call the test function:
/*
test('Should be able to add two positive integers', function addTwoPositiveInt() {
const result = add(10, 1);
const expected = 11;
assert(result).toBe(expected);
});
*/