Rewrite generate-xcfilelists in Python
[WebKit-https.git] / Tools / Scripts / webkitpy / common / attribute_saver_unittest.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (C) 2019 Apple Inc.  All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 import unittest
29
30 from webkitpy.common.attribute_saver import AttributeSaver
31
32
33 class AttributeSaverTest(unittest.TestCase):
34
35     class SimpleTestClass:
36         def __init__(self):
37             self.value = 0
38
39     def test_class(self):
40         obj = self.SimpleTestClass()
41         self.assertEquals(obj.value, 0)
42
43     def test_normal_default(self):
44         obj = self.SimpleTestClass()
45         self.assertEquals(obj.value, 0)
46         with AttributeSaver(obj, "value"):
47             self.assertEquals(obj.value, None)
48         self.assertEquals(obj.value, 0)
49
50     def test_normal_value(self):
51         obj = self.SimpleTestClass()
52         self.assertEquals(obj.value, 0)
53         with AttributeSaver(obj, "value", 1):
54             self.assertEquals(obj.value, 1)
55         self.assertEquals(obj.value, 0)
56
57     def test_normal_value_on_exception(self):
58         with self.assertRaises(RuntimeError):
59             obj = self.SimpleTestClass()
60             self.assertEquals(obj.value, 0)
61             try:
62                 with AttributeSaver(obj, "value", 1):
63                     self.assertEquals(obj.value, 1)
64                     raise RuntimeError()
65             except:
66                 self.assertEquals(obj.value, 0)
67                 raise
68             self.assertEquals(obj.value, 0)
69
70     def test_normal_value_on_normal_exit(self):
71         obj = self.SimpleTestClass()
72         self.assertEquals(obj.value, 0)
73         try:
74             with AttributeSaver(obj, "value", 1):
75                 self.assertEquals(obj.value, 1)
76         except:
77             self.assertEquals(obj.value, 0)
78             raise
79         self.assertEquals(obj.value, 0)
80
81     def test_normal_value_with_finally_on_exception(self):
82         with self.assertRaises(RuntimeError):
83             obj = self.SimpleTestClass()
84             self.assertEquals(obj.value, 0)
85             try:
86                 with AttributeSaver(obj, "value", 1):
87                     self.assertEquals(obj.value, 1)
88                     raise RuntimeError()
89             finally:
90                 self.assertEquals(obj.value, 0)
91             self.assertEquals(obj.value, 0)
92
93     def test_normal_value_with_finally_on_normal_exit(self):
94         obj = self.SimpleTestClass()
95         self.assertEquals(obj.value, 0)
96         try:
97             with AttributeSaver(obj, "value", 1):
98                 self.assertEquals(obj.value, 1)
99         finally:
100             self.assertEquals(obj.value, 0)
101         self.assertEquals(obj.value, 0)
102
103     def test_normal_value_with_else_on_exception(self):
104         with self.assertRaises(RuntimeError):
105             obj = self.SimpleTestClass()
106             self.assertEquals(obj.value, 0)
107             try:
108                 with AttributeSaver(obj, "value", 1):
109                     self.assertEquals(obj.value, 1)
110                     raise RuntimeError()
111             except IOError:
112                 self.assertFalse(True)
113             else:
114                 self.assertEquals(obj.value, 0)
115             self.assertEquals(obj.value, 0)
116
117     def test_normal_value_with_else_on_normal_exit(self):
118         obj = self.SimpleTestClass()
119         self.assertEquals(obj.value, 0)
120         try:
121             with AttributeSaver(obj, "value", 1):
122                 self.assertEquals(obj.value, 1)
123         except IOError:
124             self.assertFalse(True)
125         else:
126             self.assertEquals(obj.value, 0)
127         self.assertEquals(obj.value, 0)