diff --git a/src/pybricksMicropython/lib.test.ts b/src/pybricksMicropython/lib.test.ts index 6bbb0a54..139f5135 100644 --- a/src/pybricksMicropython/lib.test.ts +++ b/src/pybricksMicropython/lib.test.ts @@ -1,7 +1,31 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2022 The Pybricks Authors -import { FileNameValidationResult, pythonFileExtension, validateFileName } from './lib'; +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', () => { diff --git a/src/pybricksMicropython/lib.ts b/src/pybricksMicropython/lib.ts index c093edf7..47a85fbb 100644 --- a/src/pybricksMicropython/lib.ts +++ b/src/pybricksMicropython/lib.ts @@ -4,6 +4,9 @@ /** The Python file extension ('.py') */ export const pythonFileExtension = '.py'; +/** A regular expression that matches the Python file extension. */ +export const pythonFileExtensionRegex = /\.[Pp][Yy]$/; + /** The Python file MIME type ('text/x-python') */ export const pythonFileMimeType = 'text/x-python';