Skip to content

Commit

Permalink
Make search option props optional, test optional typings
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Sep 8, 2018
1 parent e546584 commit 599271b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
12 changes: 9 additions & 3 deletions src/addons/search/Interfaces.ts
Expand Up @@ -22,7 +22,13 @@ export interface ISearchHelper {
}

export interface ISearchOptions {
regex: boolean;
wholeWord: boolean;
caseSensitive: boolean;
regex?: boolean;
wholeWord?: boolean;
caseSensitive?: boolean;
}

export interface ISearchResult {
term: string;
col: number;
row: number;
}
14 changes: 4 additions & 10 deletions src/addons/search/SearchHelper.ts
Expand Up @@ -3,13 +3,7 @@
* @license MIT
*/

import { ISearchHelper, ISearchAddonTerminal, ISearchOptions } from './Interfaces';

interface ISearchResult {
term: string;
col: number;
row: number;
}
import { ISearchHelper, ISearchAddonTerminal, ISearchOptions, ISearchResult } from './Interfaces';

/**
* A class that knows how to search the terminal and how to display the results.
Expand All @@ -28,7 +22,7 @@ export class SearchHelper implements ISearchHelper {
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findNext(term: string, searchOptions: ISearchOptions): boolean {
public findNext(term: string, searchOptions?: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
Expand Down Expand Up @@ -70,7 +64,7 @@ export class SearchHelper implements ISearchHelper {
* @param searchOptions Search options.
* @return Whether a result was found.
*/
public findPrevious(term: string, searchOptions: ISearchOptions): boolean {
public findPrevious(term: string, searchOptions?: ISearchOptions): boolean {
if (!term || term.length === 0) {
return false;
}
Expand Down Expand Up @@ -112,7 +106,7 @@ export class SearchHelper implements ISearchHelper {
* @param searchOptions Search options.
* @return The search result if it was found.
*/
private _findInLine(term: string, y: number, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): ISearchResult {
protected _findInLine(term: string, y: number, searchOptions: ISearchOptions = {}): ISearchResult {
const lowerStringLine = this._terminal._core.buffer.translateBufferLineToString(y, true).toLowerCase();
const lowerTerm = term.toLowerCase();
let searchIndex = -1;
Expand Down
30 changes: 18 additions & 12 deletions src/addons/search/search.test.ts
Expand Up @@ -6,17 +6,17 @@
import { assert, expect } from 'chai';
import * as search from './search';
import { SearchHelper } from './SearchHelper';
import { ISearchHelper } from './Interfaces';
import { ISearchOptions, ISearchResult } from './Interfaces';


class MockTerminalPlain {}

class MockTerminal {
private _core: any;
public searchHelper: ISearchHelper;
public searchHelper: TestSearchHelper;
constructor(options: any) {
this._core = new (require('../../../lib/Terminal').Terminal)(options);
this.searchHelper = new SearchHelper(this as any);
this.searchHelper = new TestSearchHelper(this as any);
}
get core(): any {
return this._core;
Expand All @@ -26,6 +26,12 @@ class MockTerminal {
}
}

class TestSearchHelper extends SearchHelper {
public findInLine(term: string, y: number, searchOptions?: ISearchOptions): ISearchResult {
return this._findInLine(term, y, searchOptions);
}
}

describe('search addon', function(): void {
describe('apply', () => {
it('should register findNext and findPrevious', () => {
Expand All @@ -39,9 +45,9 @@ describe('search addon', function(): void {
const term = new MockTerminal({cols: 20, rows: 3});
term.core.write('Hello World\r\ntest\n123....hello');
term.pushWriteData();
const hello0 = (term.searchHelper as any)._findInLine('Hello', 0);
const hello1 = (term.searchHelper as any)._findInLine('Hello', 1);
const hello2 = (term.searchHelper as any)._findInLine('Hello', 2);
const hello0 = term.searchHelper.findInLine('Hello', 0);
const hello1 = term.searchHelper.findInLine('Hello', 1);
const hello2 = term.searchHelper.findInLine('Hello', 2);
expect(hello0).eql({col: 0, row: 0, term: 'Hello'});
expect(hello1).eql(undefined);
expect(hello2).eql({col: 11, row: 2, term: 'Hello'});
Expand All @@ -62,12 +68,12 @@ describe('search addon', function(): void {
wholeWord: false,
caseSensitive: false
};
const hello0 = (term.searchHelper as any)._findInLine('dee*', 0, searchOptions);
const hello1 = (term.searchHelper as any)._findInLine('jkk*', 0, searchOptions);
const hello2 = (term.searchHelper as any)._findInLine('mnn*', 1, searchOptions);
const tilda0 = (term.searchHelper as any)._findInLine('^~', 3, searchOptions);
const tilda1 = (term.searchHelper as any)._findInLine('^[~]', 3, searchOptions);
const tilda2 = (term.searchHelper as any)._findInLine('^\\~', 3, searchOptions);
const hello0 = term.searchHelper.findInLine('dee*', 0, searchOptions);
term.searchHelper.findInLine('jkk*', 0, searchOptions);
term.searchHelper.findInLine('mnn*', 1, searchOptions);
const tilda0 = term.searchHelper.findInLine('^~', 3, searchOptions);
const tilda1 = term.searchHelper.findInLine('^[~]', 3, searchOptions);
const tilda2 = term.searchHelper.findInLine('^\\~', 3, searchOptions);
expect(hello0).eql({col: 3, row: 0, term: 'de'});
// TODO: uncomment this test when line wrap search is checked in expect(hello1).eql({col: 9, row: 0, term: 'jk'});
// TODO: uncomment this test when line wrap search is checked in expect(hello2).eql(undefined);
Expand Down
4 changes: 2 additions & 2 deletions src/addons/search/search.ts
Expand Up @@ -14,7 +14,7 @@ import { ISearchAddonTerminal, ISearchOptions } from './Interfaces';
* @param searchOptions Search options
* @return Whether a result was found.
*/
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
export function findNext(terminal: Terminal, term: string, searchOptions: ISearchOptions = {}): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
Expand All @@ -29,7 +29,7 @@ export function findNext(terminal: Terminal, term: string, searchOptions: ISearc
* @param searchOptions Search options
* @return Whether a result was found.
*/
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions = {regex: false, wholeWord: false, caseSensitive: false}): boolean {
export function findPrevious(terminal: Terminal, term: string, searchOptions: ISearchOptions): boolean {
const addonTerminal = <ISearchAddonTerminal>terminal;
if (!addonTerminal.__searchHelper) {
addonTerminal.__searchHelper = new SearchHelper(addonTerminal);
Expand Down

0 comments on commit 599271b

Please sign in to comment.