Skip to content

Commit

Permalink
feat: add commands with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidvdn committed Oct 9, 2024
1 parent 2f668dc commit aeb0889
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/command/commands/bold-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { TextDocument } from "../text-document";
import { BoldCommand } from "./bold-command";

describe('BoldCommand', () => {
it('should bold text', () => {
const document = new TextDocument();
document.setContent('Hello, World!');
const boldCommand = new BoldCommand(document, 7, 9);
boldCommand.execute();
expect(document.getContent()).toBe('Hello, **Wo**rld!');
})

it('should undo bold text', () => {
const document = new TextDocument();
document.setContent('Hello, World!');
const boldCommand = new BoldCommand(document, 7, 9);
boldCommand.execute();
boldCommand.undo();
expect(document.getContent()).toBe('Hello, World!');
})

it('should undo bold text', () => {
const document = new TextDocument();
document.setContent('Hello, World!');
const boldCommand = new BoldCommand(document, 7, 5);
expect(() => boldCommand.execute()).toThrow(/Invalid range/);
})
});
33 changes: 33 additions & 0 deletions app/command/commands/bold-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ICommand, IDocument } from "../interface";

export class BoldCommand implements ICommand {
private document: IDocument;
private from: number;
private to: number;

constructor(document: IDocument, from: number, to: number) {
this.document = document;
this.from = from;
this.to = to;
}

execute(): void {
if(this.to <= this.from) throw new Error('Invalid range');

let doc = this.document.getContent();
doc = doc.slice(0, this.from)
+ "**"
+ doc.slice(this.from, this.to)
+ "**"
+ doc.slice(this.to);
this.document.setContent(doc);
}

undo(): void {
let doc = this.document.getContent();
doc = doc.slice(0, this.from)
+ doc.slice(this.from+2, this.to+2)
+ doc.slice(this.to+4);
this.document.setContent(doc);
}
}
21 changes: 21 additions & 0 deletions app/command/commands/erase-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TextDocument } from "../text-document";
import { EraseCommand } from "./erase-command";

describe('EraseCommand', () => {
it('should erase', () => {
const document = new TextDocument();
document.setContent('Hello, World!');
const eraseCommand = new EraseCommand(document, 7);
eraseCommand.execute();
expect(document.getContent()).toBe('Hello,');
})

it('should redo erase', () => {
const document = new TextDocument();
document.setContent('Hello, World!');
const eraseCommand = new EraseCommand(document, 7);
eraseCommand.execute();
eraseCommand.undo();
expect(document.getContent()).toBe('Hello, World!');
})
});
25 changes: 25 additions & 0 deletions app/command/commands/erase-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ICommand, IDocument } from "../interface";

export class EraseCommand implements ICommand {
private document: IDocument;
private eraseCount: number;
private remainingPart: string = '';

constructor(document: IDocument, eraseCount: number) {
this.document = document;
this.eraseCount = eraseCount;
}

execute(): void {
let doc = this.document.getContent();
const newDoc = doc.slice(0, -this.eraseCount);
this.remainingPart = doc.slice(doc.length-this.eraseCount);
this.document.setContent(newDoc);
}

undo(): void {
let doc = this.document.getContent();
doc += this.remainingPart;
this.document.setContent(doc);
}
}
3 changes: 3 additions & 0 deletions app/command/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './bold-command';
export * from './erase-command';
export * from './write-command';
19 changes: 19 additions & 0 deletions app/command/commands/write-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { TextDocument } from "../text-document";
import { WriteCommand } from "./write-command";

describe('WriteCommand', () => {
it('should write text', () => {
const document = new TextDocument();
const writeCommand = new WriteCommand(document, 'test');
writeCommand.execute();
expect(document.getContent()).toBe('test');
})

it('should undo write text', () => {
const document = new TextDocument();
const writeCommand = new WriteCommand(document, 'hello');
writeCommand.execute();
writeCommand.undo();
expect(document.getContent()).toBe('');
})
});
23 changes: 23 additions & 0 deletions app/command/commands/write-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ICommand, IDocument } from "../interface";

export class WriteCommand implements ICommand {
private document: IDocument;
private text: string;

constructor(document: IDocument, text: string) {
this.document = document;
this.text = text;
}

execute(): void {
let doc = this.document.getContent();
doc += this.text;
this.document.setContent(doc);
}

undo(): void {
let doc = this.document.getContent();
doc = doc.slice(0, -this.text.length);
this.document.setContent(doc);
}
}

0 comments on commit aeb0889

Please sign in to comment.