preprocess_if_expr_test.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2020 The Chromium Authors. All rights reserved.
  2. # Use of this source code is governed by a BSD-style license that can be
  3. # found in the LICENSE file.
  4. import os
  5. import shutil
  6. import tempfile
  7. import unittest
  8. import preprocess_if_expr
  9. _HERE_DIR = os.path.dirname(__file__)
  10. class PreprocessIfExprTest(unittest.TestCase):
  11. def setUp(self):
  12. self._out_folder = None
  13. def tearDown(self):
  14. if self._out_folder:
  15. shutil.rmtree(self._out_folder)
  16. def _read_out_file(self, file_name):
  17. assert self._out_folder
  18. with open(os.path.join(self._out_folder, file_name), 'r') as f:
  19. return f.read()
  20. def _run_test(self, additional_options, file_name, expected_file_name):
  21. assert not self._out_folder
  22. self._out_folder = tempfile.mkdtemp(dir=_HERE_DIR)
  23. preprocess_if_expr.main([
  24. '--in-folder',
  25. os.path.join(_HERE_DIR, 'preprocess_tests'),
  26. '--out-folder',
  27. self._out_folder,
  28. '--in-files',
  29. file_name,
  30. ] + additional_options)
  31. actual = self._read_out_file(file_name)
  32. with open(os.path.join(_HERE_DIR, 'preprocess_tests', expected_file_name),
  33. 'r') as f:
  34. expected = f.read()
  35. self.assertMultiLineEqual(expected, actual)
  36. def testPreprocess(self):
  37. self._run_test(
  38. ['-D', 'foo', '-D', 'bar', '-D', 'apple=false', '-D', 'orange=false'],
  39. 'test_with_ifexpr.js', 'test_with_ifexpr_expected.js')
  40. def testPreprocessWithComments(self):
  41. self._run_test([
  42. '-D', 'foo', '-D', 'bar', '-D', 'apple=false', '-D', 'orange=false',
  43. '--enable_removal_comments'
  44. ], 'test_with_ifexpr.js', 'test_with_ifexpr_expected_comments.js')
  45. def testPreprocessTypescriptWithComments(self):
  46. self._run_test([
  47. '-D', 'foo', '-D', 'bar', '-D', 'orange=false',
  48. '--enable_removal_comments'
  49. ], 'test_with_ifexpr.ts', 'test_with_ifexpr_expected.ts')
  50. def testPreprocessHtmlWithComments(self):
  51. self._run_test(
  52. ['-D', 'foo', '-D', 'orange=false', '--enable_removal_comments'],
  53. 'test_with_ifexpr.html', 'test_with_ifexpr_expected.html')
  54. def testPreprocessJavaScriptHtmlTemplateWithComments(self):
  55. self._run_test(
  56. ['-D', 'foo', '-D', 'bar=false', '--enable_removal_comments'],
  57. 'test_with_ifexpr.html.js', 'test_with_ifexpr_expected.html.js')
  58. def testPreprocessTypeScriptHtmlTemplateWithComments(self):
  59. self._run_test(
  60. ['-D', 'foo', '-D', 'bar=false', '--enable_removal_comments'],
  61. 'test_with_ifexpr.html.ts', 'test_with_ifexpr_expected.html.ts')
  62. if __name__ == '__main__':
  63. unittest.main()