summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/catapult/common/py_utils/py_utils/contextlib_ext_unittest.py
blob: be40e7b641f719217bab73b8cd8d35466c56d260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

from __future__ import absolute_import
import unittest

from py_utils import contextlib_ext


class OptionalUnittest(unittest.TestCase):

  class SampleContextMgr():

    def __init__(self):
      self.entered = False
      self.exited = False

    def __enter__(self):
      self.entered = True

    def __exit__(self, exc_type, exc_val, exc_tb):
      self.exited = True

  def testConditionTrue(self):
    c = self.SampleContextMgr()
    with contextlib_ext.Optional(c, True):
      self.assertTrue(c.entered)
    self.assertTrue(c.exited)

  def testConditionFalse(self):
    c = self.SampleContextMgr()
    with contextlib_ext.Optional(c, False):
      self.assertFalse(c.entered)
    self.assertFalse(c.exited)