Verified Commit fa8415a1 authored by Jules Sagot--Gentil's avatar Jules Sagot--Gentil
Browse files

feat(console): ConsoleStdout affiche les flux sur la sortie standard

parent 3a3b6906
import { WritableGetSortie } from "../../test/writable";
import { ConsoleStdout } from "../console";
describe('La console écrivant sur l\'entrée standard', () => {
const articleA = {
affiche: jest.fn(),
egal: jest.fn().mockImplementation((a) => a == doublonArticleA)
};
const articleB = {
affiche: jest.fn(),
egal: jest.fn().mockReturnValue(false)
};
const doublonArticleA = {
affiche: jest.fn(),
egal: jest.fn().mockReturnValue(false)
};
const mockFluxA = {
getArticles: jest.fn().mockResolvedValue([
articleA,
])
};
const mockFluxB = {
getArticles: jest.fn().mockResolvedValue([
articleB,
doublonArticleA,
])
};
let consoleStdout: ConsoleStdout;
beforeEach(() => {
jest.clearAllMocks();
consoleStdout = new ConsoleStdout();
consoleStdout['output'] = new WritableGetSortie();
});
it('doit pouvoir afficher les articles d\'un flux', async () => {
consoleStdout.ajouteFlux(mockFluxA);
await consoleStdout.afficheTousFlux();
expect(articleA.affiche.mock.calls.length).toBe(1);
})
it('doit pouvoir afficher les articles de deux flux', async () => {
consoleStdout.ajouteFlux(mockFluxA);
consoleStdout.ajouteFlux(mockFluxB);
await consoleStdout.afficheTousFlux();
expect(articleA.affiche.mock.calls.length).toBe(1);
expect(articleB.affiche.mock.calls.length).toBe(1);
})
it('doit éliminer les articles doublons', async () => {
consoleStdout.ajouteFlux(mockFluxA);
consoleStdout.ajouteFlux(mockFluxB);
await consoleStdout.afficheTousFlux();
expect(doublonArticleA.affiche.mock.calls.length).toBe(0);
})
})
import { Writable } from 'stream';
import { Article } from './article';
import { Flux } from './flux';
interface Console {
ajouteFlux(flux: Flux): void;
afficheTousFlux(): void;
afficheTousFlux(): Promise<void>;
}
export class ConsoleStdout implements Console {
private output: Writable = process.stdout;
private flux: Flux[] = [];
private articlesAffiches: Article[] = [];
public ajouteFlux(flux: Flux): void {
this.flux.push(flux);
};
public async afficheTousFlux(): Promise<void> {
const articles = (
await Promise.all(this.flux.map((f) => f.getArticles()))
).flat();
for(const article of articles) {
if (this.articleDejaAffiche(article)) {
continue;
} else {
this.afficheArticle(article);
this.articlesAffiches.push(article);
}
}
};
private afficheArticle(article: Article) {
this.output.write(`Article n° ${this.articlesAffiches.length + 1}\n`);
article.affiche(this.output);
this.output.write('------\n\n');
};
private articleDejaAffiche(article: Article) {
return !!this.articlesAffiches.find((a) => a.egal(article));
};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment