Skip to content
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

Modulo.2.2/introduccion testing #6

Open
wants to merge 4 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: 39 additions & 2 deletions Modulo2/Testing/__tests__/caesarCypher.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
import { fizzBuzz } from "../src/fizzBuzz";
import { caesarCypher } from "../src/caesarCypher";

describe("Test here....", () => {});
describe("caesarCypher", () => {
it("Debe devolver el mísmo caracter si no es una letra del alfabeto independientemente del offset", () => {
expect(caesarCypher("!", 1)).toBe("!");
expect(caesarCypher("!", 2)).toBe("!");
expect(caesarCypher("2", 3)).toBe("2");
expect(caesarCypher("z", 1)).toBe("a");
});
it("debería desplazar las letras del alfabeto por el número dado", () => {
expect(caesarCypher("abcd", 1)).toBe("bdfh");
});

it("debería ignorar los espacios en blanco para aumentar el desplazamiento", () => {
expect(caesarCypher("a b c d", 1)).toBe("b d f h");
});

it("debería funcionar con desplazamientos negativos", () => {
expect(caesarCypher("abcd", -1)).toBe("zbdf");
});

it("debería funcionar con desplazamientos grandes", () => {
expect(caesarCypher("abcd", 27)).toBe("bdfh");
});

it("debería funcionar con desplazamientos grandes y negativos", () => {
expect(caesarCypher("abcd", -27)).toBe("zbdf");
});

it("debería funcionar con el ejemplo del README", () => {
expect(
caesarCypher(
"I should have known that you would have a perfect answer for me!!!",
1
)
).toBe(
"J ukszrk pjfp wacld kztn tkr unumf keak h xnbqqph pdjoxl ako kd!!!"
);
});
});
41 changes: 40 additions & 1 deletion Modulo2/Testing/__tests__/fizzBuzz.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
import { fizzBuzz } from "../src/fizzBuzz";

describe("Test here....", () => {});
describe("FizzBuzz", () => {
it('debería devolver "Fizz" para múltiplos de 3', () => {
expect(fizzBuzz(3)).toEqual("Fizz");
expect(fizzBuzz(6)).toEqual("Fizz");
});

it('debería devolver "Buzz" para múltiplos de 5', () => {
expect(fizzBuzz(5)).toEqual("Buzz");
expect(fizzBuzz(50)).toEqual("Buzz");
});

it('debería devolver "FizzBuzz" para múltiplos de 3 y 5', () => {
expect(fizzBuzz(15)).toEqual("FizzBuzz");
expect(fizzBuzz(30)).toEqual("FizzBuzz");
});

it("debería devolver el número mismo para otros casos", () => {
expect(fizzBuzz(1)).toEqual("1");
expect(fizzBuzz(2)).toEqual("2");
expect(fizzBuzz(7)).toEqual("7");
expect(fizzBuzz(74)).toEqual("74");
});

it('debería devolver "Fizz" para numeros que contienen el 3 pero no son múltiplos', () => {
expect(fizzBuzz(73)).toEqual("Fizz");
});
it('debería devolver "Buzz" para numeros que contienen el 5 pero no son múltiplos', () => {
expect(fizzBuzz(52)).toEqual("Buzz");
});
it('debería devolver "FizBuzz" para numeros que contienen el 5 y son múltiplos 3', () => {
expect(fizzBuzz(51)).toEqual("FizzBuzz");
});

it('debería devolver "FizBuzz" para numeros que contienen el 3 y son múltiplos 5', () => {
expect(fizzBuzz(130)).toEqual("FizzBuzz");
});
it('debería devolver "FizBuzz" para numeros que contienen el 3 y 5 pero no son divisibles entre 3 ni 5', () => {
expect(fizzBuzz(53)).toEqual("FizzBuzz");
});
});
91 changes: 90 additions & 1 deletion Modulo2/Testing/__tests__/romanConverter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,92 @@
import { arabicToRoman } from "../src/romanConverter";

describe("Test here....", () => {});
describe("Prueba básica:", () => {
it('Convierte 1 a "I"', () => {
expect(arabicToRoman(1)).toBe("I");
});
it('Convierte 5 a "V"', () => {
expect(arabicToRoman(5)).toBe("V");
});
it('Convierte 10 a "X"', () => {
expect(arabicToRoman(10)).toBe("X");
});
it('Convierte 50 a "L"', () => {
expect(arabicToRoman(50)).toBe("L");
});
it('Convierte 100 a "C"', () => {
expect(arabicToRoman(100)).toBe("C");
});
});

describe("Prueba de números múltiples:", () => {
it('Convierte 2 a "II"', () => {
expect(arabicToRoman(2)).toBe("II");
});

it('Convierte 3 a "III"', () => {
expect(arabicToRoman(3)).toBe("III");
});
it('Convierte 8 a "VIII"', () => {
expect(arabicToRoman(8)).toBe("VIII");
});
it('Convierte 20 a "XX"', () => {
expect(arabicToRoman(20)).toBe("XX");
});
it('Convierte 200 a "CC"', () => {
expect(arabicToRoman(200)).toBe("CC");
});
});

describe("Prueba de números especiales:", () => {
it('Convierte 4 a "IV"', () => {
expect(arabicToRoman(4)).toBe("IV");
});
it('Convierte 9 a "IX"', () => {
expect(arabicToRoman(9)).toBe("IX");
});
it('Convierte 40 a "XL"', () => {
expect(arabicToRoman(40)).toBe("XL");
});
it('Convierte 90 a "XC"', () => {
expect(arabicToRoman(90)).toBe("XC");
});
});

describe("Prueba de números compuestos:", () => {
it('Convierte 93 a "XCIII"', () => {
expect(arabicToRoman(93)).toBe("XCIII");
});
it('Convierte 57 a "LVII"', () => {
expect(arabicToRoman(57)).toBe("LVII");
});
it('Convierte 14 a "XIV"', () => {
expect(arabicToRoman(14)).toBe("XIV");
});
it('Convierte 2984 a "MMCMLXXXIV"', () => {
expect(arabicToRoman(2984)).toBe("MMCMLXXXIV");
});
});

describe("Prueba de números grandes:", () => {
it('Convierte 500 a "D"', () => {
expect(arabicToRoman(500)).toBe("D");
});
it('Convierte 1000 a "M"', () => {
expect(arabicToRoman(1000)).toBe("M");
});
it('Convierte 3999 a "MMMCMXCIX"', () => {
expect(arabicToRoman(3999)).toBe("MMMCMXCIX");
});
});

describe("Prueba de límites:", () => {
it("Con 0 debe lanzar una excepción", () => {
expect(() => arabicToRoman(0)).toThrow("Invalid range");
});
it("Con mayores de 3999 debe lanzar una excepción", () => {
expect(() => arabicToRoman(4000)).toThrow("Invalid range");
});
it("Con menores de 0 debe lanzar una excepción", () => {
expect(() => arabicToRoman(-1)).toThrow("Invalid range");
});
});
38 changes: 37 additions & 1 deletion Modulo2/Testing/src/caesarCypher.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
const ALPHABET = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
const ALPHABET_LENGTH = ALPHABET.length;

const isInAlphabet = (char: string) => {
return ALPHABET.includes(char.toLowerCase());
};

const isUpperCase = (char: string) => {
return char === char.toUpperCase();
};

export function caesarCypher(text: string, offset: number): string {
return "";
let decypheredString = "";
const isPositive = offset > 0;

if (text.length === 0) {
throw new Error("EmptyString");
}

for (const char of text) {
if (!isInAlphabet(char)) {
decypheredString += char;
continue;
}

const currentIndex = ALPHABET.indexOf(char.toLowerCase());

let nextIndex = (currentIndex + offset) % ALPHABET_LENGTH;
if (nextIndex < 0) {
nextIndex += ALPHABET_LENGTH;
}

const nextChar = ALPHABET[nextIndex];
decypheredString += !isUpperCase(char) ? nextChar : nextChar.toUpperCase();

offset++;
}
return decypheredString;
}
15 changes: 14 additions & 1 deletion Modulo2/Testing/src/fizzBuzz.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export function fizzBuzz(num: number): string {
return "";
let result: string = "";

if (num % 3 === 0 || num.toString().includes("3")) {
result += "Fizz";
}
if (num % 5 === 0 || num.toString().includes("5")) {
result += "Buzz";
}

if (result.length === 0) {
return (result += num.toString());
}

return result;
}
31 changes: 30 additions & 1 deletion Modulo2/Testing/src/romanConverter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
export function arabicToRoman(num: number): string {
return "";
if (num < 1 || num > 3999) {
throw new Error("Invalid range");
}

const codigosRomanos: { letter: string; value: number }[] = [
{ letter: "M", value: 1000 },
{ letter: "CM", value: 900 },
{ letter: "D", value: 500 },
{ letter: "CD", value: 400 },
{ letter: "C", value: 100 },
{ letter: "XC", value: 90 },
{ letter: "L", value: 50 },
{ letter: "XL", value: 40 },
{ letter: "X", value: 10 },
{ letter: "IX", value: 9 },
{ letter: "V", value: 5 },
{ letter: "IV", value: 4 },
{ letter: "I", value: 1 },
];

let result = "";

codigosRomanos.forEach((codigo) => {
while (num >= codigo.value) {
result += codigo.letter;
num -= codigo.value;
}
});

return result;
}