Files
pybricks-code/src/pybricksMicropython/lib.test.ts
T
David Lechner 7b095765ac pybricksMicropython/lib: add pythonFileExtensionRegex
This can be reused in code to work with file names.
2022-03-11 15:41:16 -06:00

79 lines
2.5 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import {
FileNameValidationResult,
pythonFileExtension,
pythonFileExtensionRegex,
validateFileName,
} from './lib';
describe('pythonFileExtensionRegex', () => {
it.each(['test.py', 'test.PY', 'test.Py', 'test.pY'])(
'should be case insensitive',
(testCase) => {
expect(testCase.match(pythonFileExtensionRegex)).not.toBeNull();
},
);
it('should not match the extension in the middle of a string', () => {
expect('test.py.test'.match(pythonFileExtensionRegex)).toBeNull();
});
it('should only match the extension and not any other part of the string', () => {
const match = 'test.py'.match(pythonFileExtensionRegex);
expect(match).toHaveLength(1);
expect(match).toContain(pythonFileExtension);
});
});
describe('validateFileName', () => {
it('should allow file names with underscores', () => {
expect(validateFileName('file_name', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsOk,
);
});
it('should allow file names with dashes', () => {
expect(validateFileName('file-name', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsOk,
);
});
it('should not allow empty strings', () => {
expect(validateFileName('', pythonFileExtension, [])).toBe(
FileNameValidationResult.IsEmpty,
);
});
it('should not allow file names with spaces', () => {
expect(validateFileName('file name', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasSpaces,
);
});
it('should not allow file names that start with numbers', () => {
expect(validateFileName('1test', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasInvalidFirstCharacter,
);
});
it('should not allow file names with symbols', () => {
expect(validateFileName('test$', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasInvalidCharacters,
);
});
it('it should not allow file names that include the file extension', () => {
expect(validateFileName('test.py', pythonFileExtension, [])).toBe(
FileNameValidationResult.HasFileExtension,
);
});
it('should not allow file names that match existing files', () => {
expect(validateFileName('test', pythonFileExtension, ['test.py'])).toBe(
FileNameValidationResult.AlreadyExists,
);
});
});