From 6e61ac6ce4be52bd7a65572cecdc0fef3822d8cb Mon Sep 17 00:00:00 2001 From: Bob Swift Date: Thu, 26 May 2022 11:59:40 -0600 Subject: [PATCH] Add case-insensitive testing option to $lt, $lte, $gt and $gte --- picard/script/functions.py | 32 ++++++++++++++++++++------------ test/test_script.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/picard/script/functions.py b/picard/script/functions.py index b3d54a5c5..b6d0a8ad2 100644 --- a/picard/script/functions.py +++ b/picard/script/functions.py @@ -684,9 +684,9 @@ def func_ne(parser, x, y): """`$lt(x,y[,type])` Returns true if `x` is less than `y` using the comparison specified in `type`. -Possible values of `type` are "int" (integer), "float" (floating point) and -"text" (case-sensitive text), with "int" used as the default comparison method -if `type` is not specified.""" +Possible values of `type` are "int" (integer), "float" (floating point), "text" +(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as +the default comparison method if `type` is not specified.""" )) def func_lt(parser, x, y, _type=None): if not _type: @@ -694,6 +694,8 @@ def func_lt(parser, x, y, _type=None): _typer = None if _type == 'text': return "1" if x < y else "" + elif _type == 'nocase': + return "1" if x.lower() < y.lower() else "" elif _type == 'float': _typer = float elif _type == 'int': @@ -711,9 +713,9 @@ def func_lt(parser, x, y, _type=None): """`$lte(x,y[,type])` Returns true if `x` is less than or equal to `y` using the comparison specified in `type`. -Possible values of `type` are "int" (integer), "float" (floating point) and -"text" (case-sensitive text), with "int" used as the default comparison method -if `type` is not specified.""" +Possible values of `type` are "int" (integer), "float" (floating point), "text" +(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as +the default comparison method if `type` is not specified.""" )) def func_lte(parser, x, y, _type=None): if not _type: @@ -721,6 +723,8 @@ def func_lte(parser, x, y, _type=None): _typer = None if _type == 'text': return "" if x > y else "1" + elif _type == 'nocase': + return "" if x.lower() > y.lower() else "1" elif _type == 'float': _typer = float elif _type == 'int': @@ -738,9 +742,9 @@ def func_lte(parser, x, y, _type=None): """`$gt(x,y[,type])` Returns true if `x` is greater than `y` using the comparison specified in `type`. -Possible values of `type` are "int" (integer), "float" (floating point) and -"text" (case-sensitive text), with "int" used as the default comparison method -if `type` is not specified.""" +Possible values of `type` are "int" (integer), "float" (floating point), "text" +(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as +the default comparison method if `type` is not specified.""" )) def func_gt(parser, x, y, _type=None): if not _type: @@ -748,6 +752,8 @@ def func_gt(parser, x, y, _type=None): _typer = None if _type == 'text': return "1" if x > y else "" + elif _type == 'nocase': + return "1" if x.lower() > y.lower() else "" elif _type == 'float': _typer = float elif _type == 'int': @@ -765,9 +771,9 @@ def func_gt(parser, x, y, _type=None): """`$gte(x,y[,type])` Returns true if `x` is greater than or equal to `y` using the comparison specified in `type`. -Possible values of `type` are "int" (integer), "float" (floating point) and -"text" (case-sensitive text), with "int" used as the default comparison method -if `type` is not specified.""" +Possible values of `type` are "int" (integer), "float" (floating point), "text" +(case-sensitive text) and "nocase" (case-insensitive text), with "int" used as +the default comparison method if `type` is not specified.""" )) def func_gte(parser, x, y, _type=None): if not _type: @@ -775,6 +781,8 @@ def func_gte(parser, x, y, _type=None): _typer = None if _type == 'text': return "" if x < y else "1" + elif _type == 'nocase': + return "" if x.lower() < y.lower() else "1" elif _type == 'float': _typer = float elif _type == 'int': diff --git a/test/test_script.py b/test/test_script.py index f1b9adacd..79aaacc8b 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -658,6 +658,12 @@ class ScriptParserTest(PicardTestCase): self.assertScriptResultEquals("$gt(A,a,text)", "", context) self.assertScriptResultEquals("$gt(a,A,text)", "1", context) + # Test case insensitive arguments ("nocase" processing) + self.assertScriptResultEquals("$gt(a,B,nocase)", "", context) + self.assertScriptResultEquals("$gt(A,b,nocase)", "", context) + self.assertScriptResultEquals("$gt(B,a,nocase)", "1", context) + self.assertScriptResultEquals("$gt(b,A,nocase)", "1", context) + # Test unknown processing type self.assertScriptResultEquals("$gt(2,1,unknown)", "", context) @@ -742,6 +748,14 @@ class ScriptParserTest(PicardTestCase): self.assertScriptResultEquals("$gte(A,a,text)", "", context) self.assertScriptResultEquals("$gte(a,A,text)", "1", context) + # Test case insensitive arguments ("nocase" processing) + self.assertScriptResultEquals("$gte(a,B,nocase)", "", context) + self.assertScriptResultEquals("$gte(A,b,nocase)", "", context) + self.assertScriptResultEquals("$gte(B,a,nocase)", "1", context) + self.assertScriptResultEquals("$gte(b,A,nocase)", "1", context) + self.assertScriptResultEquals("$gte(a,A,nocase)", "1", context) + self.assertScriptResultEquals("$gte(A,a,nocase)", "1", context) + # Test unknown processing type self.assertScriptResultEquals("$gte(2,1,unknown)", "", context) @@ -827,6 +841,12 @@ class ScriptParserTest(PicardTestCase): self.assertScriptResultEquals("$lt(A,a,text)", "1", context) self.assertScriptResultEquals("$lt(a,A,text)", "", context) + # Test case insensitive arguments ("nocase" processing) + self.assertScriptResultEquals("$lt(a,B,nocase)", "1", context) + self.assertScriptResultEquals("$lt(A,b,nocase)", "1", context) + self.assertScriptResultEquals("$lt(B,a,nocase)", "", context) + self.assertScriptResultEquals("$lt(b,A,nocase)", "", context) + # Test unknown processing type self.assertScriptResultEquals("$lt(1,2,unknown)", "", context) @@ -908,6 +928,14 @@ class ScriptParserTest(PicardTestCase): self.assertScriptResultEquals("$lte(A,a,text)", "1", context) self.assertScriptResultEquals("$lte(a,A,text)", "", context) + # Test case insensitive arguments ("nocase" processing) + self.assertScriptResultEquals("$lte(a,B,nocase)", "1", context) + self.assertScriptResultEquals("$lte(A,b,nocase)", "1", context) + self.assertScriptResultEquals("$lte(B,a,nocase)", "", context) + self.assertScriptResultEquals("$lte(b,A,nocase)", "", context) + self.assertScriptResultEquals("$lte(a,A,nocase)", "1", context) + self.assertScriptResultEquals("$lte(A,a,nocase)", "1", context) + # Test unknown processing type self.assertScriptResultEquals("$lte(1,2,unknown)", "", context)