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