From f43d78d358aebba94b8cf644b1352a8b9e401f53 Mon Sep 17 00:00:00 2001 From: Lucas Alvares Gomes Date: Wed, 27 Jul 2016 17:06:50 +0100 Subject: [PATCH] Extend specs matcher to support ">" and "<" This patch is exteding the specs matcher and adding support for the ">" and "<" operators. Prior to this patch, only the "equal" forms were supported (">=" and "<="). It also looks inconsistent because for the "s" prefixed operators all the 4 forms are supported already: "s<=", "s<", "s>=" and "s>". Change-Id: I82a72b0ef6ee277e7b09b0eb138e058687ce8804 --- oslo_utils/specs_matcher.py | 3 ++ oslo_utils/tests/test_specs_matcher.py | 38 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/oslo_utils/specs_matcher.py b/oslo_utils/specs_matcher.py index a384a6c..5e4177a 100644 --- a/oslo_utils/specs_matcher.py +++ b/oslo_utils/specs_matcher.py @@ -28,8 +28,10 @@ op_methods = { # More sane ops/methods '!=': lambda x, y: float(x) != float(y), '<=': lambda x, y: float(x) <= float(y), + '<': lambda x, y: float(x) < float(y), '==': lambda x, y: float(x) == float(y), '>=': lambda x, y: float(x) >= float(y), + '>': lambda x, y: float(x) > float(y), 's!=': operator.ne, 's<': operator.lt, 's<=': operator.le, @@ -52,6 +54,7 @@ def make_grammar(): Literal("==") | Literal("=") | Literal("!=") | Literal("") | Literal(">=") | Literal("<=") | + Literal(">") | Literal("<") | Literal("s==") | Literal("s!=") | # Order matters here (so that '<' doesn't match before '<=') Literal("s<=") | Literal("s<") | diff --git a/oslo_utils/tests/test_specs_matcher.py b/oslo_utils/tests/test_specs_matcher.py index 8cbd502..30c919b 100644 --- a/oslo_utils/tests/test_specs_matcher.py +++ b/oslo_utils/tests/test_specs_matcher.py @@ -47,7 +47,7 @@ class SpecsMatcherTestCase(test_base.BaseTestCase): def test_specs_fails_with_bogus_ops(self): self._do_specs_matcher_test( value='4', - req='> 2', + req='! 2', matches=False) def test_specs_matches_with_op_eq(self): @@ -266,6 +266,42 @@ class SpecsMatcherTestCase(test_base.BaseTestCase): req='>= 3', matches=True) + def test_specs_matches_with_op_g(self): + self._do_specs_matcher_test( + value='3', + req='> 1', + matches=True) + + def test_specs_matches_with_op_g2(self): + self._do_specs_matcher_test( + value='3', + req='> 3', + matches=False) + + def test_specs_matches_with_op_g3(self): + self._do_specs_matcher_test( + value='3.0', + req='> 2', + matches=True) + + def test_specs_matches_with_op_l(self): + self._do_specs_matcher_test( + value='3', + req='< 5', + matches=True) + + def test_specs_matches_with_op_l2(self): + self._do_specs_matcher_test( + value='3', + req='< 3', + matches=False) + + def test_specs_matches_with_op_l3(self): + self._do_specs_matcher_test( + value='1.0', + req='< 6', + matches=True) + def test_specs_fails_with_op_ge(self): self._do_specs_matcher_test( value='2',