mirror of
https://github.com/fergalmoran/picard.git
synced 2025-12-24 02:09:56 +00:00
Trap ValueError exception in $datetime function and add tests
This commit is contained in:
@@ -31,6 +31,11 @@
|
||||
|
||||
import copy
|
||||
import datetime
|
||||
import os
|
||||
from unittest import (
|
||||
skipIf,
|
||||
skipUnless,
|
||||
)
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from test.picardtestcase import PicardTestCase
|
||||
@@ -54,6 +59,9 @@ from picard.script import (
|
||||
)
|
||||
|
||||
|
||||
IS_WIN = os.name == 'nt'
|
||||
|
||||
|
||||
class _TestTimezone(datetime.tzinfo):
|
||||
def utcoffset(self, dt):
|
||||
# Set to GMT+2
|
||||
@@ -1176,6 +1184,41 @@ class ScriptParserTest(PicardTestCase):
|
||||
# Restore original datetime object
|
||||
datetime.datetime = original_datetime
|
||||
|
||||
@skipUnless(IS_WIN, "windows test")
|
||||
def test_cmd_datetime_windows(self):
|
||||
''' Test failure specific to Windows only. '''
|
||||
# Save origninal datetime object and substitute one returning
|
||||
# a fixed now() value for testing.
|
||||
original_datetime = datetime.datetime
|
||||
datetime.datetime = _DateTime
|
||||
|
||||
try:
|
||||
context = Metadata()
|
||||
# Tests with invalid format
|
||||
self.assertScriptResultEquals(r"$datetime(\%B \%-d\, \%Y)", "", context)
|
||||
except (AssertionError, ValueError, IndexError) as err:
|
||||
raise err
|
||||
finally:
|
||||
# Restore original datetime object
|
||||
datetime.datetime = original_datetime
|
||||
|
||||
@skipIf(IS_WIN, "non-windows test")
|
||||
def test_cmd_datetime_non_windows(self):
|
||||
# Save origninal datetime object and substitute one returning
|
||||
# a fixed now() value for testing.
|
||||
original_datetime = datetime.datetime
|
||||
datetime.datetime = _DateTime
|
||||
|
||||
try:
|
||||
context = Metadata()
|
||||
# Tests with invalid format
|
||||
self.assertScriptResultEquals(r"$datetime(\%Y-\%-m-\%-d)", "2020-1-2", context)
|
||||
except (AssertionError, ValueError, IndexError) as err:
|
||||
raise err
|
||||
finally:
|
||||
# Restore original datetime object
|
||||
datetime.datetime = original_datetime
|
||||
|
||||
def test_multivalue_1(self):
|
||||
self.parser.context = Metadata({'foo': ':', 'bar': 'x:yz', 'empty': ''})
|
||||
expr = self.parser.parse('a:bc; d:ef')
|
||||
|
||||
Reference in New Issue
Block a user