Test coverage for the FileType classes
All checks were successful
timw4mail/scroll/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2024-07-24 09:02:29 -04:00
parent c31933ed9b
commit a7fcc982fe
4 changed files with 37 additions and 18 deletions

View File

@ -2,12 +2,13 @@ import Ansi, * as _Ansi from './ansi.ts';
import Buffer from './buffer.ts';
import Document from './document.ts';
import Editor from './editor.ts';
import { FileLang, FileType } from './filetype/mod.ts';
import { FileLang } from './filetype/mod.ts';
import { highlightToColor, HighlightType } from './highlight.ts';
import Option, { None, Some } from './option.ts';
import Position from './position.ts';
import Row from './row.ts';
import FileType, * as FT from './filetype/mod.ts';
import * as Fn from './fns.ts';
import { defaultTerminalSize, SCROLL_TAB_SIZE } from './config.ts';
import { getTestRunner } from './runtime/mod.ts';
@ -472,6 +473,19 @@ const EditorTest = {
// ----------------------------------------------------------------------------
const FileTypeTest = {
'FileType.from()': () => {
for (const [ext, typeClass] of FT.fileTypeMap.entries()) {
const file = `test${ext}`;
const syntax = FileType.from(file);
assertInstanceOf(syntax, typeClass);
}
},
};
// ----------------------------------------------------------------------------
const OptionTest = {
'Option.from()': () => {
assertNone(Option.from(null));
@ -633,6 +647,7 @@ testSuite({
Buffer: BufferTest,
Document: DocumentTest,
Editor: EditorTest,
FileType: FileTypeTest,
Option: OptionTest,
Position: PositionTest,
Row: RowTest,

View File

@ -76,8 +76,7 @@ export abstract class AbstractFileType implements IFileType {
}
public hasMultilineComments(): boolean {
return this.multiLineCommentStart.isSome() &&
this.multiLineCommentEnd.isSome();
return this.multiLineCommentStart.and(this.multiLineCommentEnd).isSome();
}
}

View File

@ -9,8 +9,7 @@ import { ShellFile } from './shell.ts';
// External interface
// ----------------------------------------------------------------------------
export class FileType extends AbstractFileType {
static #fileTypeMap = new Map([
export const fileTypeMap = new Map([
['.c', CFile],
['.h', CFile],
['.css', CSSFile],
@ -22,16 +21,19 @@ export class FileType extends AbstractFileType {
['.sh', ShellFile],
['.ts', TypeScriptFile],
['.tsx', TypeScriptFile],
]);
]);
export class FileType extends AbstractFileType {
public static default(): FileType {
return new FileType();
}
public static from(filename: string): FileType {
const ext = path.extname(filename);
const type = FileType.#fileTypeMap.get(ext) ?? FileType;
const type = fileTypeMap.get(ext) ?? FileType;
return new type();
}
}
export default FileType;

View File

@ -1,2 +1,5 @@
export * from './base.ts';
export * from './filetype.ts';
import FileType from './filetype.ts';
export default FileType;