示例#1
0
 def test_simple_fail(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     check(True is False)
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', 'check((True is False))', {}))
示例#2
0
 def test_var_fail(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     val = True
     check(val is False)
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', 'check((val is False))', {'val': 'True'}))
示例#3
0
 def test_key_error_in_short_circuited_expression(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     d = {'foo': 1, 'bar': 2}
     check('baz' in d and d['baz'] == 3)
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
       self.sanitize(c.failed_checks[0].frames[0]),
       self.mk('body', "check((('baz' in d) and (d['baz'] == 3)))",
               {'d.keys()': "['bar', 'foo']"}))
示例#4
0
 def test_dict_lookup(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     targ = {'a': {'sub': 'b'}, 'c': 'd'}
     check('cow' in targ['a'])
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', "check(('cow' in targ['a']))",
             {"targ['a'].keys()": "['sub']"}))
示例#5
0
 def test_dict_membership(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     targ = {'a': 'b', 'c': 'd'}
     check('a' not in targ)
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', "check(('a' not in targ))",
             {'targ.keys()': "['a', 'c']"}))
示例#6
0
 def test_dict_lookup_nest(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     sub = 'sub'
     targ = {'a': {'sub': 'whee'}, 'c': 'd'}
     check('me' == targ['a'][sub])
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', "check(('me' == targ['a'][sub]))",
             {"targ['a'][sub]": "'whee'", 'sub': "'sub'"}))
示例#7
0
 def test_if_test(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     vals = ['foo', 'bar']
     target = 'baz'
     if check(target in vals):
       pass
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
       self.sanitize(c.failed_checks[0].frames[0]),
       self.mk('body', 'check((target in vals))',
               {'target': "'baz'", 'vals': "['foo', 'bar']"}))
示例#8
0
  def test_simple_fail_multiline(self):
    c = Checker(HOOK_CONTEXT)
    def body(check):
      falsey = lambda: False
      check(
        True is

        falsey()
      )
    body(c)
    self.assertEqual(len(c.failed_checks), 1)
    self.assertEqual(len(c.failed_checks[0].frames), 1)
    self.assertEqual(
      self.sanitize(c.failed_checks[0].frames[0]),
      self.mk('body', 'check((True is falsey()))', {}))
示例#9
0
 def test_while_test(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     vals = ['foo', 'bar', 'baz']
     invalid_value = 'bar'
     i = 0
     while check(vals[i] != invalid_value):
       i += 1
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 1)
   self.assertEqual(
       self.sanitize(c.failed_checks[0].frames[0]),
       self.mk('body', 'check((vals[i] != invalid_value))',
               {'i': '1', 'invalid_value': "'bar'", 'vals[i]': "'bar'"}))
示例#10
0
  def test_simple_fail_multiline_multistatement(self):
    c = Checker(HOOK_CONTEXT)
    def body(check):
      other = 'thing'
      falsey = lambda: False
      check(
        True is

        falsey()); other  # pylint: disable=pointless-statement
    body(c)
    self.assertEqual(len(c.failed_checks), 1)
    self.assertEqual(len(c.failed_checks[0].frames), 1)
    self.assertEqual(
      self.sanitize(c.failed_checks[0].frames[0]),
      self.mk('body', 'check((True is falsey())); other', {
        'other': "'thing'" }))
示例#11
0
 def test_lambda_call(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     vals = ['whee', 'sub']
     targ = {'a': {'sub': 'whee'}, 'c': 'd'}
     map(lambda v: check(v in targ['a']), vals)
   body(c)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 2)
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[0]),
     self.mk('body', "map((lambda v: check((v in targ['a']))), vals)", None))
   self.assertEqual(
     self.sanitize(c.failed_checks[0].frames[1]),
     self.mk('<lambda>', "map((lambda v: check((v in targ['a']))), vals)",
             {"targ['a'].keys()": "['sub']", 'v': "'whee'"}))
示例#12
0
  def test_fail_nested_statement(self):
    c = Checker(HOOK_CONTEXT)
    def body(check):
      other = 'thing'
      falsey = lambda: False
      if True:
        while True:
          try:
            check(
              True is

              falsey()); other  # pylint: disable=pointless-statement
            break
          except Exception:
            pass
    body(c)
    self.assertEqual(len(c.failed_checks), 1)
    self.assertEqual(len(c.failed_checks[0].frames), 1)
    self.assertEqual(
      self.sanitize(c.failed_checks[0].frames[0]),
      self.mk('body', 'check((True is falsey())); other', {
        'other': "'thing'" }))
示例#13
0
 def test_lambda_in_multiline_expr_call(self):
   c = Checker(HOOK_CONTEXT)
   def wrap(f):
     return f
   def body(check, f):
     f(check)
   value = 'food'
   target = ['foo', 'bar', 'baz']
   # Make sure the lambda is part of a larger expression that ends on a
   # later line than the lambda
   func = [lambda check: check(value == target),
           lambda check: check(value in target),
           lambda check: check(value and target),
          ][1]
   body(c, func)
   self.assertEqual(len(c.failed_checks), 1)
   self.assertEqual(len(c.failed_checks[0].frames), 2)
   self.assertEqual(
       self.sanitize(c.failed_checks[0].frames[0]),
       self.mk('body', 'f(check)', None))
   self.assertEqual(
       self.sanitize(c.failed_checks[0].frames[1]),
       self.mk('<lambda>', '(lambda check: check((value in target)))',
               {'value': "'food'", 'target': "['foo', 'bar', 'baz']"}))
示例#14
0
 def test_success_call(self):
   c = Checker(HOOK_CONTEXT)
   def body(check):
     check(True is True)
   body(c)
   self.assertEqual(len(c.failed_checks), 0)
示例#15
0
 def test_no_calls(self):
   c = Checker(HOOK_CONTEXT)
   def body(_):
     pass
   body(c)
   self.assertEqual(len(c.failed_checks), 0)