Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | import unittest |
michael@0 | 6 | import mozunit |
michael@0 | 7 | from mozpack.chrome.flags import ( |
michael@0 | 8 | Flag, |
michael@0 | 9 | StringFlag, |
michael@0 | 10 | VersionFlag, |
michael@0 | 11 | Flags, |
michael@0 | 12 | ) |
michael@0 | 13 | from mozpack.errors import ErrorMessage |
michael@0 | 14 | |
michael@0 | 15 | |
michael@0 | 16 | class TestFlag(unittest.TestCase): |
michael@0 | 17 | def test_flag(self): |
michael@0 | 18 | flag = Flag('flag') |
michael@0 | 19 | self.assertEqual(str(flag), '') |
michael@0 | 20 | self.assertTrue(flag.matches(False)) |
michael@0 | 21 | self.assertTrue(flag.matches('false')) |
michael@0 | 22 | self.assertFalse(flag.matches('true')) |
michael@0 | 23 | self.assertRaises(ErrorMessage, flag.add_definition, 'flag=') |
michael@0 | 24 | self.assertRaises(ErrorMessage, flag.add_definition, 'flag=42') |
michael@0 | 25 | self.assertRaises(ErrorMessage, flag.add_definition, 'flag!=false') |
michael@0 | 26 | |
michael@0 | 27 | flag.add_definition('flag=1') |
michael@0 | 28 | self.assertEqual(str(flag), 'flag=1') |
michael@0 | 29 | self.assertTrue(flag.matches(True)) |
michael@0 | 30 | self.assertTrue(flag.matches('1')) |
michael@0 | 31 | self.assertFalse(flag.matches('no')) |
michael@0 | 32 | |
michael@0 | 33 | flag.add_definition('flag=true') |
michael@0 | 34 | self.assertEqual(str(flag), 'flag=true') |
michael@0 | 35 | self.assertTrue(flag.matches(True)) |
michael@0 | 36 | self.assertTrue(flag.matches('true')) |
michael@0 | 37 | self.assertFalse(flag.matches('0')) |
michael@0 | 38 | |
michael@0 | 39 | flag.add_definition('flag=no') |
michael@0 | 40 | self.assertEqual(str(flag), 'flag=no') |
michael@0 | 41 | self.assertTrue(flag.matches('false')) |
michael@0 | 42 | self.assertFalse(flag.matches('1')) |
michael@0 | 43 | |
michael@0 | 44 | flag.add_definition('flag') |
michael@0 | 45 | self.assertEqual(str(flag), 'flag') |
michael@0 | 46 | self.assertFalse(flag.matches('false')) |
michael@0 | 47 | self.assertTrue(flag.matches('true')) |
michael@0 | 48 | self.assertFalse(flag.matches(False)) |
michael@0 | 49 | |
michael@0 | 50 | def test_string_flag(self): |
michael@0 | 51 | flag = StringFlag('flag') |
michael@0 | 52 | self.assertEqual(str(flag), '') |
michael@0 | 53 | self.assertTrue(flag.matches('foo')) |
michael@0 | 54 | self.assertRaises(ErrorMessage, flag.add_definition, 'flag>=2') |
michael@0 | 55 | |
michael@0 | 56 | flag.add_definition('flag=foo') |
michael@0 | 57 | self.assertEqual(str(flag), 'flag=foo') |
michael@0 | 58 | self.assertTrue(flag.matches('foo')) |
michael@0 | 59 | self.assertFalse(flag.matches('bar')) |
michael@0 | 60 | |
michael@0 | 61 | flag.add_definition('flag=bar') |
michael@0 | 62 | self.assertEqual(str(flag), 'flag=foo flag=bar') |
michael@0 | 63 | self.assertTrue(flag.matches('foo')) |
michael@0 | 64 | self.assertTrue(flag.matches('bar')) |
michael@0 | 65 | self.assertFalse(flag.matches('baz')) |
michael@0 | 66 | |
michael@0 | 67 | flag = StringFlag('flag') |
michael@0 | 68 | flag.add_definition('flag!=bar') |
michael@0 | 69 | self.assertEqual(str(flag), 'flag!=bar') |
michael@0 | 70 | self.assertTrue(flag.matches('foo')) |
michael@0 | 71 | self.assertFalse(flag.matches('bar')) |
michael@0 | 72 | |
michael@0 | 73 | def test_version_flag(self): |
michael@0 | 74 | flag = VersionFlag('flag') |
michael@0 | 75 | self.assertEqual(str(flag), '') |
michael@0 | 76 | self.assertTrue(flag.matches('1.0')) |
michael@0 | 77 | self.assertRaises(ErrorMessage, flag.add_definition, 'flag!=2') |
michael@0 | 78 | |
michael@0 | 79 | flag.add_definition('flag=1.0') |
michael@0 | 80 | self.assertEqual(str(flag), 'flag=1.0') |
michael@0 | 81 | self.assertTrue(flag.matches('1.0')) |
michael@0 | 82 | self.assertFalse(flag.matches('2.0')) |
michael@0 | 83 | |
michael@0 | 84 | flag.add_definition('flag=2.0') |
michael@0 | 85 | self.assertEqual(str(flag), 'flag=1.0 flag=2.0') |
michael@0 | 86 | self.assertTrue(flag.matches('1.0')) |
michael@0 | 87 | self.assertTrue(flag.matches('2.0')) |
michael@0 | 88 | self.assertFalse(flag.matches('3.0')) |
michael@0 | 89 | |
michael@0 | 90 | flag = VersionFlag('flag') |
michael@0 | 91 | flag.add_definition('flag>=2.0') |
michael@0 | 92 | self.assertEqual(str(flag), 'flag>=2.0') |
michael@0 | 93 | self.assertFalse(flag.matches('1.0')) |
michael@0 | 94 | self.assertTrue(flag.matches('2.0')) |
michael@0 | 95 | self.assertTrue(flag.matches('3.0')) |
michael@0 | 96 | |
michael@0 | 97 | flag.add_definition('flag<1.10') |
michael@0 | 98 | self.assertEqual(str(flag), 'flag>=2.0 flag<1.10') |
michael@0 | 99 | self.assertTrue(flag.matches('1.0')) |
michael@0 | 100 | self.assertTrue(flag.matches('1.9')) |
michael@0 | 101 | self.assertFalse(flag.matches('1.10')) |
michael@0 | 102 | self.assertFalse(flag.matches('1.20')) |
michael@0 | 103 | self.assertTrue(flag.matches('2.0')) |
michael@0 | 104 | self.assertTrue(flag.matches('3.0')) |
michael@0 | 105 | self.assertRaises(Exception, flag.add_definition, 'flag<') |
michael@0 | 106 | self.assertRaises(Exception, flag.add_definition, 'flag>') |
michael@0 | 107 | self.assertRaises(Exception, flag.add_definition, 'flag>=') |
michael@0 | 108 | self.assertRaises(Exception, flag.add_definition, 'flag<=') |
michael@0 | 109 | self.assertRaises(Exception, flag.add_definition, 'flag!=1.0') |
michael@0 | 110 | |
michael@0 | 111 | |
michael@0 | 112 | class TestFlags(unittest.TestCase): |
michael@0 | 113 | def setUp(self): |
michael@0 | 114 | self.flags = Flags('contentaccessible=yes', |
michael@0 | 115 | 'appversion>=3.5', |
michael@0 | 116 | 'application=foo', |
michael@0 | 117 | 'application=bar', |
michael@0 | 118 | 'appversion<2.0', |
michael@0 | 119 | 'platform', |
michael@0 | 120 | 'abi!=Linux_x86-gcc3') |
michael@0 | 121 | |
michael@0 | 122 | def test_flags_str(self): |
michael@0 | 123 | self.assertEqual(str(self.flags), 'contentaccessible=yes ' + |
michael@0 | 124 | 'appversion>=3.5 appversion<2.0 application=foo ' + |
michael@0 | 125 | 'application=bar platform abi!=Linux_x86-gcc3') |
michael@0 | 126 | |
michael@0 | 127 | def test_flags_match_unset(self): |
michael@0 | 128 | self.assertTrue(self.flags.match(os='WINNT')) |
michael@0 | 129 | |
michael@0 | 130 | def test_flags_match(self): |
michael@0 | 131 | self.assertTrue(self.flags.match(application='foo')) |
michael@0 | 132 | self.assertFalse(self.flags.match(application='qux')) |
michael@0 | 133 | |
michael@0 | 134 | def test_flags_match_different(self): |
michael@0 | 135 | self.assertTrue(self.flags.match(abi='WINNT_x86-MSVC')) |
michael@0 | 136 | self.assertFalse(self.flags.match(abi='Linux_x86-gcc3')) |
michael@0 | 137 | |
michael@0 | 138 | def test_flags_match_version(self): |
michael@0 | 139 | self.assertTrue(self.flags.match(appversion='1.0')) |
michael@0 | 140 | self.assertTrue(self.flags.match(appversion='1.5')) |
michael@0 | 141 | self.assertFalse(self.flags.match(appversion='2.0')) |
michael@0 | 142 | self.assertFalse(self.flags.match(appversion='3.0')) |
michael@0 | 143 | self.assertTrue(self.flags.match(appversion='3.5')) |
michael@0 | 144 | self.assertTrue(self.flags.match(appversion='3.10')) |
michael@0 | 145 | |
michael@0 | 146 | |
michael@0 | 147 | if __name__ == '__main__': |
michael@0 | 148 | mozunit.main() |