Skip to content

Commit c519b64

Browse files
committed
Adicionados testes e algumas correções feitas
1 parent 8a05bc3 commit c519b64

23 files changed

+372
-259
lines changed

angular.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
"tsConfig": "tsconfig.spec.json",
8080
"karmaConfig": "karma.conf.js",
8181
"assets": [
82-
"src/favicon.ico",
8382
"src/assets"
8483
],
8584
"styles": [

src/app/analise-combinatoria/Metodos.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export class Metodos {
2-
32
private isPositiveInteger(s: string): boolean {
43
return (/^\+?\d+$/).test(s);
54
}
@@ -194,5 +193,4 @@ export class Metodos {
194193
}
195194
return resultado;
196195
}
197-
198196
}
Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
2+
import { FormsModule } from '@angular/forms';
33
import { AnaliseCombinatoriaComponent } from './analise-combinatoria.component';
4+
import { Metodos } from './Metodos';
5+
6+
const metodos = new Metodos();
7+
8+
const N = '5';
9+
const PP = '3,2';
10+
const P = '3';
11+
12+
// Resultados esperados usando N = 5, P = 3.
13+
const R_P = 120;
14+
const R_PR = 10;
15+
const R_A = 60;
16+
const R_AR = 125;
17+
const R_C = 10;
18+
const R_CR = 35;
419

520
describe('AnaliseCombinatoriaComponent', () => {
621
let component: AnaliseCombinatoriaComponent;
722
let fixture: ComponentFixture<AnaliseCombinatoriaComponent>;
823

924
beforeEach(async(() => {
1025
TestBed.configureTestingModule({
11-
declarations: [ AnaliseCombinatoriaComponent ]
26+
declarations: [ AnaliseCombinatoriaComponent ],
27+
imports: [ FormsModule ]
1228
})
1329
.compileComponents();
1430
}));
@@ -19,7 +35,37 @@ describe('AnaliseCombinatoriaComponent', () => {
1935
fixture.detectChanges();
2036
});
2137

22-
it('should create', () => {
38+
it('Deve criar o componente corretamente', () => {
2339
expect(component).toBeTruthy();
2440
});
41+
42+
it(`Deve retornar o resultado correto da Permutação Simples de N: ${N}`, () => {
43+
let resultado = metodos.analiseCombinatoria(0, N, '');
44+
expect(`P(n) > P(${N}) = ${R_P}`).toEqual(resultado);
45+
});
46+
47+
it(`Deve retornar o resultado correto da Permutação com Repetição de N: ${N} e P: ${PP}`, () => {
48+
let resultado = metodos.analiseCombinatoria(1, N, PP);
49+
expect(`P(n,(p,p,...)) > P(${N},(${PP})) = ${R_PR}`).toEqual(resultado);
50+
});
51+
52+
it(`Deve retornar o resultado correto do Arranjo Simples de N: ${N} e P: ${P}`, () => {
53+
let resultado = metodos.analiseCombinatoria(2, N, P);
54+
expect(`A(n,p) > A(${N},${P}) = ${R_A}`).toEqual(resultado);
55+
});
56+
57+
it(`Deve retornar o resultado correto do Arranjo com Repetição de N: ${N} e P: ${P}`, () => {
58+
let resultado = metodos.analiseCombinatoria(3, N, P);
59+
expect(`AR(n,p) > AR(${N},${P}) = ${R_AR}`).toEqual(resultado);
60+
});
61+
62+
it(`Deve retornar o resultado correto da Combinação Simples de N: ${N} e P: ${P}`, () => {
63+
let resultado = metodos.analiseCombinatoria(4, N, P);
64+
expect(`C(n,p) > C(${N},${P}) = ${R_C}`).toEqual(resultado);
65+
});
66+
67+
it(`Deve retornar o resultado correto da Combinação com Repetição de N: ${N} e P: ${P}`, () => {
68+
let resultado = metodos.analiseCombinatoria(5, N, P);
69+
expect(`CR(n,p) > CR(${N},${P}) = ${R_CR}`).toEqual(resultado);
70+
});
2571
});

src/app/app.component.spec.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,9 @@ describe('AppComponent', () => {
1414
}).compileComponents();
1515
}));
1616

17-
it('should create the app', () => {
17+
it('Deve criar o app corretamente', () => {
1818
const fixture = TestBed.createComponent(AppComponent);
1919
const app = fixture.debugElement.componentInstance;
2020
expect(app).toBeTruthy();
2121
});
22-
23-
it(`should have as title 'github'`, () => {
24-
const fixture = TestBed.createComponent(AppComponent);
25-
const app = fixture.debugElement.componentInstance;
26-
expect(app.title).toEqual('github');
27-
});
28-
29-
it('should render title in a h1 tag', () => {
30-
const fixture = TestBed.createComponent(AppComponent);
31-
fixture.detectChanges();
32-
const compiled = fixture.debugElement.nativeElement;
33-
expect(compiled.querySelector('h1').textContent).toContain('Welcome to github!');
34-
});
3522
});

src/app/conversor-de-base/Metodos.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export class Metodos {
2-
32
private isPositiveInteger(s: string): boolean {
43
return (/^\+?\d+$/).test(s);
54
}
@@ -106,5 +105,4 @@ export class Metodos {
106105
throw erro;
107106
}
108107
}
109-
110108
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
2+
import { FormsModule } from '@angular/forms';
33
import { ConversorDeBaseComponent } from './conversor-de-base.component';
4+
import { Metodos } from './Metodos';
5+
6+
const metodos = new Metodos();
7+
8+
const ALGARISMO_1 = '255';
9+
const BASE_1 = 10;
10+
const ALGARISMO_2 = 'FF';
11+
const BASE_2 = 16;
412

513
describe('ConversorDeBaseComponent', () => {
614
let component: ConversorDeBaseComponent;
715
let fixture: ComponentFixture<ConversorDeBaseComponent>;
816

917
beforeEach(async(() => {
1018
TestBed.configureTestingModule({
11-
declarations: [ ConversorDeBaseComponent ]
19+
declarations: [ ConversorDeBaseComponent ],
20+
imports: [ FormsModule ]
1221
})
1322
.compileComponents();
1423
}));
@@ -19,7 +28,17 @@ describe('ConversorDeBaseComponent', () => {
1928
fixture.detectChanges();
2029
});
2130

22-
it('should create', () => {
31+
it('Deve criar o componente corretamente', () => {
2332
expect(component).toBeTruthy();
2433
});
34+
35+
it(`Deve converter o algarismo '${ALGARISMO_1}' (${BASE_1}) para '${ALGARISMO_2}' (${BASE_2})`, () => {
36+
let algarismoConvertido = metodos.converterAlgarismo(ALGARISMO_1, BASE_1, BASE_2);
37+
expect(algarismoConvertido).toEqual(ALGARISMO_2);
38+
});
39+
40+
it(`Deve converter o algarismo '${ALGARISMO_2}' (${BASE_2}) para '${ALGARISMO_1}' (${BASE_1})`, () => {
41+
let algarismoConvertido = metodos.converterAlgarismo(ALGARISMO_2, BASE_2, BASE_1);
42+
expect(algarismoConvertido).toEqual(ALGARISMO_1);
43+
});
2544
});

src/app/criptografia/Util.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export class Util {
2-
32
public isPositiveInteger(s: string): boolean {
43
return (/^\+?\d+$/).test(s);
54
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { Util } from '../Util';
2+
3+
const util = new Util();
4+
5+
export class Metodos {
6+
cifrar(text: string, deslocamento: number, min: number = 12, max: number = 255): string {
7+
let MAX_DESLOCAMENTO = ((max + 1) - min);
8+
if (text == '') {
9+
throw 'Digite algum(a) texto/palavra para ser cifrado(a).';
10+
}
11+
if (deslocamento < 0 || deslocamento > MAX_DESLOCAMENTO) {
12+
throw 'O deslocamento não pode ser menor que 0 e não pode ser maior que ' + MAX_DESLOCAMENTO + '.';
13+
}
14+
let invalidchars = util.invalidCharsInString(text, min, max);
15+
if (invalidchars != '') {
16+
throw 'Existe(m) caractere(s) inválido(s) no(a) texto/palavra.\nCaracteres inválidos: ' + invalidchars;
17+
}
18+
let textCifrado = '';
19+
for (let i = 0; i < text.length; i++) {
20+
if ((text.charCodeAt(i) - deslocamento) < min) {
21+
textCifrado += String.fromCharCode((max + 1) - (min - (text.charCodeAt(i) - deslocamento)));
22+
} else {
23+
textCifrado += String.fromCharCode(text.charCodeAt(i) - deslocamento);
24+
}
25+
}
26+
return textCifrado;
27+
}
28+
29+
decifrar(text: string, deslocamento: number, min: number = 12, max: number = 255): string {
30+
let MAX_DESLOCAMENTO = ((max + 1) - min);
31+
if (text == '') {
32+
throw 'Digite algum(a) texto/palavra para ser decifrado(a).';
33+
}
34+
if (deslocamento < 0 || deslocamento > MAX_DESLOCAMENTO) {
35+
throw 'O deslocamento não pode ser menor que 0 e não pode ser maior que ' + MAX_DESLOCAMENTO;
36+
}
37+
let invalidchars = util.invalidCharsInString(text, min, max);
38+
if (invalidchars != '') {
39+
throw 'Existe(m) caractere(s) inválido(s) no(a) texto/palavra.\nCaracteres inválidos: ' + invalidchars;
40+
}
41+
let textDecifrado = '';
42+
for (let i = 0; i < text.length; i++) {
43+
if ((text.charCodeAt(i) + deslocamento) > max) {
44+
textDecifrado += String.fromCharCode((text.charCodeAt(i) + deslocamento) - MAX_DESLOCAMENTO);
45+
} else {
46+
textDecifrado += String.fromCharCode(text.charCodeAt(i) + deslocamento);
47+
}
48+
}
49+
return textDecifrado;
50+
}
51+
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
2+
import { FormsModule } from '@angular/forms';
33
import { CifraDeCesarComponent } from './cifra-de-cesar.component';
4+
import { Metodos } from './Metodos';
5+
6+
const metodos = new Metodos();
7+
8+
const TEXTO = 'teste';
9+
const DESLOCAMENTO = 15;
410

511
describe('CifraDeCesarComponent', () => {
612
let component: CifraDeCesarComponent;
713
let fixture: ComponentFixture<CifraDeCesarComponent>;
814

915
beforeEach(async(() => {
1016
TestBed.configureTestingModule({
11-
declarations: [ CifraDeCesarComponent ]
17+
declarations: [ CifraDeCesarComponent ],
18+
imports: [ FormsModule ]
1219
})
1320
.compileComponents();
1421
}));
@@ -19,7 +26,14 @@ describe('CifraDeCesarComponent', () => {
1926
fixture.detectChanges();
2027
});
2128

22-
it('should create', () => {
29+
it('Deve criar o componente corretamente', () => {
2330
expect(component).toBeTruthy();
2431
});
32+
33+
it(`Deve cifrar e decifrar o texto: '${TEXTO}' usando o deslocamento: '${DESLOCAMENTO}'`, () => {
34+
let textoCifrado = metodos.cifrar(TEXTO, DESLOCAMENTO);
35+
expect(TEXTO).not.toEqual(textoCifrado);
36+
let textoDecifrado = metodos.decifrar(textoCifrado, DESLOCAMENTO);
37+
expect(TEXTO).toEqual(textoDecifrado);
38+
});
2539
});
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { Component } from '@angular/core';
22
import { Title } from '@angular/platform-browser';
33
import { Util } from '../Util';
4+
import { Metodos } from './Metodos';
5+
6+
const util = new Util();
7+
const metodos = new Metodos();
48

59
@Component({
610
selector: 'app-cifra-de-cesar',
@@ -9,8 +13,6 @@ import { Util } from '../Util';
913
})
1014
export class CifraDeCesarComponent {
1115

12-
util = new Util();
13-
1416
title = 'Cifra de César';
1517

1618
texto = '';
@@ -23,9 +25,9 @@ export class CifraDeCesarComponent {
2325

2426
cifrar() {
2527
if(this.texto.length > 0 && this.deslocamento.length > 0) {
26-
if (this.util.isPositiveInteger(this.deslocamento)) {
28+
if (util.isPositiveInteger(this.deslocamento)) {
2729
try {
28-
this.resultado = this._cifrar(this.texto, parseInt(this.deslocamento), 12, 255);
30+
this.resultado = metodos.cifrar(this.texto, parseInt(this.deslocamento));
2931
} catch (erro) {
3032
this.resultado = erro;
3133
}
@@ -37,9 +39,9 @@ export class CifraDeCesarComponent {
3739

3840
decifrar() {
3941
if(this.texto.length > 0 && this.deslocamento.length > 0) {
40-
if (this.util.isPositiveInteger(this.deslocamento)) {
42+
if (util.isPositiveInteger(this.deslocamento)) {
4143
try {
42-
this.resultado = this._decifrar(this.texto, parseInt(this.deslocamento), 12, 255);
44+
this.resultado = metodos.decifrar(this.texto, parseInt(this.deslocamento));
4345
} catch (erro) {
4446
this.resultado = erro;
4547
}
@@ -48,54 +50,4 @@ export class CifraDeCesarComponent {
4850
}
4951
}
5052
}
51-
52-
private _cifrar(text: string, deslocamento: number, min: number, max: number): string {
53-
let MAX_DESLOCAMENTO = ((max + 1) - min);
54-
if (text == '') {
55-
throw 'Digite algum(a) texto/palavra para ser cifrado(a).';
56-
}
57-
if (deslocamento < 0 || deslocamento > MAX_DESLOCAMENTO) {
58-
throw 'O deslocamento não pode ser menor que 0 e não pode ser maior que ' + MAX_DESLOCAMENTO + '.';
59-
}
60-
let invalidchars = this.util.invalidCharsInString(text, min, max);
61-
if (invalidchars != '') {
62-
throw 'Existe(m) caractere(s) inválido(s) no(a) texto/palavra.\nCaracteres inválidos: ' + invalidchars;
63-
}
64-
65-
let textCifrado = '';
66-
for (let i = 0; i < text.length; i++) {
67-
if ((text.charCodeAt(i) - deslocamento) < min) {
68-
textCifrado += String.fromCharCode((max + 1) - (min - (text.charCodeAt(i) - deslocamento)));
69-
} else {
70-
textCifrado += String.fromCharCode(text.charCodeAt(i) - deslocamento);
71-
}
72-
}
73-
return textCifrado;
74-
}
75-
76-
private _decifrar(text: string, deslocamento: number, min: number, max: number): string {
77-
let MAX_DESLOCAMENTO = ((max + 1) - min);
78-
if (text == '') {
79-
throw 'Digite algum(a) texto/palavra para ser decifrado(a).';
80-
}
81-
if (deslocamento < 0 || deslocamento > MAX_DESLOCAMENTO) {
82-
throw 'O deslocamento não pode ser menor que 0 e não pode ser maior que ' + MAX_DESLOCAMENTO;
83-
}
84-
let invalidchars = this.util.invalidCharsInString(text, min, max);
85-
if (invalidchars != '') {
86-
throw 'Existe(m) caractere(s) inválido(s) no(a) texto/palavra.\nCaracteres inválidos: ' + invalidchars;
87-
}
88-
89-
let textDecifrado = '';
90-
91-
for (let i = 0; i < text.length; i++) {
92-
if ((text.charCodeAt(i) + deslocamento) > max) {
93-
textDecifrado += String.fromCharCode((text.charCodeAt(i) + deslocamento) - MAX_DESLOCAMENTO);
94-
} else {
95-
textDecifrado += String.fromCharCode(text.charCodeAt(i) + deslocamento);
96-
}
97-
}
98-
return textDecifrado;
99-
}
100-
10153
}

0 commit comments

Comments
 (0)