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 Buffer from './buffer.ts';
import Document from './document.ts'; import Document from './document.ts';
import Editor from './editor.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 { highlightToColor, HighlightType } from './highlight.ts';
import Option, { None, Some } from './option.ts'; import Option, { None, Some } from './option.ts';
import Position from './position.ts'; import Position from './position.ts';
import Row from './row.ts'; import Row from './row.ts';
import FileType, * as FT from './filetype/mod.ts';
import * as Fn from './fns.ts'; import * as Fn from './fns.ts';
import { defaultTerminalSize, SCROLL_TAB_SIZE } from './config.ts'; import { defaultTerminalSize, SCROLL_TAB_SIZE } from './config.ts';
import { getTestRunner } from './runtime/mod.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 = { const OptionTest = {
'Option.from()': () => { 'Option.from()': () => {
assertNone(Option.from(null)); assertNone(Option.from(null));
@ -633,6 +647,7 @@ testSuite({
Buffer: BufferTest, Buffer: BufferTest,
Document: DocumentTest, Document: DocumentTest,
Editor: EditorTest, Editor: EditorTest,
FileType: FileTypeTest,
Option: OptionTest, Option: OptionTest,
Position: PositionTest, Position: PositionTest,
Row: RowTest, Row: RowTest,

View File

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

View File

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

View File

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