michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import unittest michael@0: import mozunit michael@0: from mozpack.chrome.flags import ( michael@0: Flag, michael@0: StringFlag, michael@0: VersionFlag, michael@0: Flags, michael@0: ) michael@0: from mozpack.errors import ErrorMessage michael@0: michael@0: michael@0: class TestFlag(unittest.TestCase): michael@0: def test_flag(self): michael@0: flag = Flag('flag') michael@0: self.assertEqual(str(flag), '') michael@0: self.assertTrue(flag.matches(False)) michael@0: self.assertTrue(flag.matches('false')) michael@0: self.assertFalse(flag.matches('true')) michael@0: self.assertRaises(ErrorMessage, flag.add_definition, 'flag=') michael@0: self.assertRaises(ErrorMessage, flag.add_definition, 'flag=42') michael@0: self.assertRaises(ErrorMessage, flag.add_definition, 'flag!=false') michael@0: michael@0: flag.add_definition('flag=1') michael@0: self.assertEqual(str(flag), 'flag=1') michael@0: self.assertTrue(flag.matches(True)) michael@0: self.assertTrue(flag.matches('1')) michael@0: self.assertFalse(flag.matches('no')) michael@0: michael@0: flag.add_definition('flag=true') michael@0: self.assertEqual(str(flag), 'flag=true') michael@0: self.assertTrue(flag.matches(True)) michael@0: self.assertTrue(flag.matches('true')) michael@0: self.assertFalse(flag.matches('0')) michael@0: michael@0: flag.add_definition('flag=no') michael@0: self.assertEqual(str(flag), 'flag=no') michael@0: self.assertTrue(flag.matches('false')) michael@0: self.assertFalse(flag.matches('1')) michael@0: michael@0: flag.add_definition('flag') michael@0: self.assertEqual(str(flag), 'flag') michael@0: self.assertFalse(flag.matches('false')) michael@0: self.assertTrue(flag.matches('true')) michael@0: self.assertFalse(flag.matches(False)) michael@0: michael@0: def test_string_flag(self): michael@0: flag = StringFlag('flag') michael@0: self.assertEqual(str(flag), '') michael@0: self.assertTrue(flag.matches('foo')) michael@0: self.assertRaises(ErrorMessage, flag.add_definition, 'flag>=2') michael@0: michael@0: flag.add_definition('flag=foo') michael@0: self.assertEqual(str(flag), 'flag=foo') michael@0: self.assertTrue(flag.matches('foo')) michael@0: self.assertFalse(flag.matches('bar')) michael@0: michael@0: flag.add_definition('flag=bar') michael@0: self.assertEqual(str(flag), 'flag=foo flag=bar') michael@0: self.assertTrue(flag.matches('foo')) michael@0: self.assertTrue(flag.matches('bar')) michael@0: self.assertFalse(flag.matches('baz')) michael@0: michael@0: flag = StringFlag('flag') michael@0: flag.add_definition('flag!=bar') michael@0: self.assertEqual(str(flag), 'flag!=bar') michael@0: self.assertTrue(flag.matches('foo')) michael@0: self.assertFalse(flag.matches('bar')) michael@0: michael@0: def test_version_flag(self): michael@0: flag = VersionFlag('flag') michael@0: self.assertEqual(str(flag), '') michael@0: self.assertTrue(flag.matches('1.0')) michael@0: self.assertRaises(ErrorMessage, flag.add_definition, 'flag!=2') michael@0: michael@0: flag.add_definition('flag=1.0') michael@0: self.assertEqual(str(flag), 'flag=1.0') michael@0: self.assertTrue(flag.matches('1.0')) michael@0: self.assertFalse(flag.matches('2.0')) michael@0: michael@0: flag.add_definition('flag=2.0') michael@0: self.assertEqual(str(flag), 'flag=1.0 flag=2.0') michael@0: self.assertTrue(flag.matches('1.0')) michael@0: self.assertTrue(flag.matches('2.0')) michael@0: self.assertFalse(flag.matches('3.0')) michael@0: michael@0: flag = VersionFlag('flag') michael@0: flag.add_definition('flag>=2.0') michael@0: self.assertEqual(str(flag), 'flag>=2.0') michael@0: self.assertFalse(flag.matches('1.0')) michael@0: self.assertTrue(flag.matches('2.0')) michael@0: self.assertTrue(flag.matches('3.0')) michael@0: michael@0: flag.add_definition('flag<1.10') michael@0: self.assertEqual(str(flag), 'flag>=2.0 flag<1.10') michael@0: self.assertTrue(flag.matches('1.0')) michael@0: self.assertTrue(flag.matches('1.9')) michael@0: self.assertFalse(flag.matches('1.10')) michael@0: self.assertFalse(flag.matches('1.20')) michael@0: self.assertTrue(flag.matches('2.0')) michael@0: self.assertTrue(flag.matches('3.0')) michael@0: self.assertRaises(Exception, flag.add_definition, 'flag<') michael@0: self.assertRaises(Exception, flag.add_definition, 'flag>') michael@0: self.assertRaises(Exception, flag.add_definition, 'flag>=') michael@0: self.assertRaises(Exception, flag.add_definition, 'flag<=') michael@0: self.assertRaises(Exception, flag.add_definition, 'flag!=1.0') michael@0: michael@0: michael@0: class TestFlags(unittest.TestCase): michael@0: def setUp(self): michael@0: self.flags = Flags('contentaccessible=yes', michael@0: 'appversion>=3.5', michael@0: 'application=foo', michael@0: 'application=bar', michael@0: 'appversion<2.0', michael@0: 'platform', michael@0: 'abi!=Linux_x86-gcc3') michael@0: michael@0: def test_flags_str(self): michael@0: self.assertEqual(str(self.flags), 'contentaccessible=yes ' + michael@0: 'appversion>=3.5 appversion<2.0 application=foo ' + michael@0: 'application=bar platform abi!=Linux_x86-gcc3') michael@0: michael@0: def test_flags_match_unset(self): michael@0: self.assertTrue(self.flags.match(os='WINNT')) michael@0: michael@0: def test_flags_match(self): michael@0: self.assertTrue(self.flags.match(application='foo')) michael@0: self.assertFalse(self.flags.match(application='qux')) michael@0: michael@0: def test_flags_match_different(self): michael@0: self.assertTrue(self.flags.match(abi='WINNT_x86-MSVC')) michael@0: self.assertFalse(self.flags.match(abi='Linux_x86-gcc3')) michael@0: michael@0: def test_flags_match_version(self): michael@0: self.assertTrue(self.flags.match(appversion='1.0')) michael@0: self.assertTrue(self.flags.match(appversion='1.5')) michael@0: self.assertFalse(self.flags.match(appversion='2.0')) michael@0: self.assertFalse(self.flags.match(appversion='3.0')) michael@0: self.assertTrue(self.flags.match(appversion='3.5')) michael@0: self.assertTrue(self.flags.match(appversion='3.10')) michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: mozunit.main()