durusmail: qp: ConnectiveSpecOperator: how to know which spec gave out?
ConnectiveSpecOperator: how to know which spec gave out?
2006-02-26
2006-02-27
2006-02-27
2006-02-27
2006-02-27
2006-02-27
2006-02-28
2006-02-28
ConnectiveSpecOperator: how to know which spec gave out?
David Binger
2006-02-27
On Feb 27, 2006, at 2:39 AM, mario ruggier wrote:

> I of course meant something like this

I may have lost track of what it is that your suggested
change fixes.

If I remember correctly, this is just about wanting more
detailed error explanations when an "both" spec is not
matched.  It isn't clear to me that that is really necessary,
but if it is, I think it could be dealt with more directly
instead of imposing change on all of these other classes.

===================================================================
--- lib/spec.py (revision 27998)
+++ lib/spec.py (working copy)
@@ -52,6 +52,9 @@
      def __str__(self):
          return "%s(%s)" % (self.__class__.__name__, self.format_args
())
+    def explain_difference(self, value):
+        return format_expected_got(self, value)
+
def match(value, spec):
      """
      Return True or False depending on whether or not value matches
@@ -141,10 +144,16 @@
          pass
      return False
+def format_expected_got(value, spec):
+    return ('\n  Expected: %s\n'
+            '  Got: %r\n') % (format_spec(spec), value)
+
def require(value, spec, message=None):
      if not match(value, spec):
-        error = ('\n  Expected: %s\n'
-                 '  Got: %r\n') % (format_spec(spec), value)
+        if isinstance(spec, SpecOperator):
+            error = spec.explain_difference(value)
+        else:
+            error = format_expected_got(spec, value)
          if message:
              error = '(%s)%s' % (message, error)
          raise TypeError(error)
@@ -272,6 +281,13 @@
                  return False
          return True
+    def explain_difference(self, value):
+        error = format_expected_got(value, self)
+        for spec in self.specs:
+            if not match(value, spec):
+                error += "  (which does not match %s)" % format_spec
(spec)
+                break
+        return error




reply