Source code for jasy.test.script.deadcode

#!/usr/bin/env python3

import sys, os, unittest, logging

# Extend PYTHONPATH with local 'lib' folder
if __name__ == "__main__":
    jasyroot = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir, os.pardir, os.pardir))
    sys.path.insert(0, jasyroot)
    print("Running from %s..." % jasyroot)

import jasy.script.parse.Parser as Parser
import jasy.script.parse.ScopeScanner as ScopeScanner
import jasy.script.output.Compressor as Compressor
import jasy.script.clean.Unused as Unused
import jasy.script.clean.DeadCode as DeadCode


[docs]class Tests(unittest.TestCase):
[docs] def process(self, code): node = Parser.parse(code) DeadCode.cleanup(node) return Compressor.Compressor().compress(node)
[docs] def test_if_trueish(self): self.assertEqual(self.process('if (true) x++;'), 'x++;')
[docs] def test_if_falsy(self): self.assertEqual(self.process('if (false) x++;'), '')
[docs] def test_if_equal_true(self): self.assertEqual(self.process('if (2==2) x++;'), 'x++;')
[docs] def test_if_equal_false(self): self.assertEqual(self.process('if (2==3) x++;'), '')
[docs] def test_if_identical_true(self): self.assertEqual(self.process('if (2===2) x++;'), 'x++;')
[docs] def test_if_identical_false(self): self.assertEqual(self.process('if (2===3) x++;'), '')
[docs] def test_if_not_trueish(self): self.assertEqual(self.process('if (!true) x++;'), '')
[docs] def test_if_not_falsy(self): self.assertEqual(self.process('if (!false) x++;'), 'x++;')
[docs] def test_if_trueish_and_trueish(self): self.assertEqual(self.process('if (true && true) x++;'), 'x++;')
[docs] def test_if_falsy_and_falsy(self): self.assertEqual(self.process('if (false && false) x++;'), '')
[docs] def test_if_trueish_and_falsy(self): self.assertEqual(self.process('if (true && false) x++;'), '')
[docs] def test_if_falsy_and_trueish(self): self.assertEqual(self.process('if (false && true) x++;'), '')
[docs] def test_if_unknown_and_falsy(self): self.assertEqual(self.process('if (x && false) x++;'), '')
[docs] def test_if_unknown_and_trueish(self): self.assertEqual(self.process('if (x && true) x++;'), 'if(x&&true)x++;')
[docs] def test_if_falsy_and_unknown(self): self.assertEqual(self.process('if (false && x) x++;'), '')
[docs] def test_if_trueish_and_unknown(self): self.assertEqual(self.process('if (true && x) x++;'), 'if(true&&x)x++;')
[docs] def test_if_trueish_or_trueish(self): self.assertEqual(self.process('if (true || true) x++;'), 'x++;')
[docs] def test_if_falsy_or_falsy(self): self.assertEqual(self.process('if (false || false) x++;'), '')
[docs] def test_if_trueish_or_falsy(self): self.assertEqual(self.process('if (true || false) x++;'), 'x++;')
[docs] def test_if_falsy_or_trueish(self): self.assertEqual(self.process('if (false || true) x++;'), 'x++;')
[docs] def test_if_unknown_or_falsy(self): self.assertEqual(self.process('if (x || false) x++;'), 'if(x||false)x++;')
[docs] def test_if_unknown_or_trueish(self): self.assertEqual(self.process('if (x || true) x++;'), 'if(x||true)x++;')
[docs] def test_if_falsy_or_unknown(self): self.assertEqual(self.process('if (false || x) x++;'), 'if(false||x)x++;')
[docs] def test_if_trueish_or_unknown(self): self.assertEqual(self.process('if (true || x) x++;'), 'if(true||x)x++;')
if __name__ == '__main__': logging.getLogger().setLevel(logging.ERROR) suite = unittest.TestLoader().loadTestsFromTestCase(Tests) unittest.TextTestRunner(verbosity=2).run(suite)