Skip to content

Commit

Permalink
Add state functions with tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreKavalerski committed Jul 4, 2020
1 parent fdcd119 commit f58d4a0
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/functions/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { State } from "../utils/state";


// Using this function because literal comparison between 2 types (state1 == state2) always return false
// TODO: melhorar formato de verificação
function areEqual(state1: State, state2: State){
const equal = true;
for (let l in state1){
for (let c in state1){
if(!(state1[l][c] == state2[l][c])){
return false;
}
}
}
return true;
}

// TODO: melhorar formato de verificação
function includes(state: State, list: State[]){
for (let s of list){
if(areEqual(s, state)){
return true;
}
}
return false;
}

function readState(state: State){
console.log('---------');
for(let l of state){
console.log(l);
}
console.log('---------');
}

export { areEqual, includes, readState }
100 changes: 100 additions & 0 deletions tests/state.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { expect } from 'chai';
import { areEqual, includes } from '../src/functions/state';
import { State } from '../src/utils/state';

describe('State Smoke Tests', () => {
it('Should exist areEqual function', () => {
expect(areEqual).to.exist;
});

it('Should return boolean when call areEqual function', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, 8, null]];
expect(areEqual(state1, state2)).to.be.a('Boolean');
});

it('Should exist includes function', () => {
expect(includes).to.exist;
});

it('Should return boolean when call includes function', () => {
const state: State = [[1,2,3], [4,5,6], [7, 8, null]];
const list: State[] = [state];
expect(includes(state, list)).to.be.a('Boolean');
});
});

describe('Testing returns of areEqual function', () => {
it('Should return true', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, 8, null]];
expect(areEqual(state1, state2)).to.eql(true);
});

it('Should return false', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, null, 8]];
expect(areEqual(state1, state2)).to.eql(false);
});

it('Should return true', () => {
const state1: State = [[4,5,3], [2,1,6], [7, null, 8]];
const state2: State = [[4,5,3], [2,1,6], [7, null, 8]];
expect(areEqual(state1, state2)).to.eql(true);
});

it('Should return false', () => {
const state1: State = [[4,5,3], [null,5,6], [2, 7, 8]];
const state2: State = [[4,1,3], [2,1,6], [7, 7, 8]];
expect(areEqual(state1, state2)).to.eql(false);
});
});


describe('Testing returns of includes function', () => {
it('Should return true', () => {
const state: State = [[1,2,3], [4,5,6], [7, 8, null]];
const list: State[] = [state];
expect(includes(state, list)).to.eql(true);
});

it('Should return false', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, null, 8]];
const list: State[] = [state1];
expect(includes(state2, list)).to.eql(false);
});

it('Should return true', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, null, 8]];
const list: State[] = [];
list.push(state1);
list.push(state2);
expect(includes(state2, list)).to.eql(true);
});

it('Should return false', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state2: State = [[1,2,3], [4,5,6], [7, null, 8]];
const state3: State = [[1,2,5], [3,4,6], [7, null, 8]];
const list: State[] = [];
list.push(state1);
list.push(state2);
expect(includes(state3, list)).to.eql(false);
});

it('Should return false', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const list: State[] = [];
expect(includes(state1, list)).to.eql(false);
});

it('Should return true', () => {
const state1: State = [[1,2,3], [4,5,6], [7, 8, null]];
const state1Copy: State = [[1,2,3], [4,5,6], [7, 8, null]];
const list: State[] = [];
list.push(state1);
expect(includes(state1Copy, list)).to.eql(true);
});
});

0 comments on commit f58d4a0

Please sign in to comment.