Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style |
| 3 | # license that can be found in the LICENSE file or at |
| 4 | # https://developers.google.com/open-source/licenses/bsd |
| 5 | |
| 6 | """Unittest for the prettify module.""" |
| 7 | from __future__ import print_function |
| 8 | from __future__ import division |
| 9 | from __future__ import absolute_import |
| 10 | |
| 11 | import unittest |
| 12 | |
| 13 | import ezt |
| 14 | |
| 15 | from features import prettify |
| 16 | |
| 17 | |
| 18 | class SourceBrowseTest(unittest.TestCase): |
| 19 | |
| 20 | def testPrepareSourceLinesForHighlighting(self): |
| 21 | # String representing an empty source file |
| 22 | src = '' |
| 23 | |
| 24 | file_lines = prettify.PrepareSourceLinesForHighlighting(src) |
| 25 | self.assertEqual(len(file_lines), 0) |
| 26 | |
| 27 | def testPrepareSourceLinesForHighlightingNoBreaks(self): |
| 28 | # seven lines of text with no blank lines |
| 29 | src = ' 1\n 2\n 3\n 4\n 5\n 6\n 7' |
| 30 | |
| 31 | file_lines = prettify.PrepareSourceLinesForHighlighting(src) |
| 32 | self.assertEqual(len(file_lines), 7) |
| 33 | out_lines = [fl.line for fl in file_lines] |
| 34 | self.assertEqual('\n'.join(out_lines), src) |
| 35 | |
| 36 | file_lines = prettify.PrepareSourceLinesForHighlighting(src) |
| 37 | self.assertEqual(len(file_lines), 7) |
| 38 | |
| 39 | def testPrepareSourceLinesForHighlightingWithBreaks(self): |
| 40 | # seven lines of text with line 5 being blank |
| 41 | src = ' 1\n 2\n 3\n 4\n\n 6\n 7' |
| 42 | |
| 43 | file_lines = prettify.PrepareSourceLinesForHighlighting(src) |
| 44 | self.assertEqual(len(file_lines), 7) |
| 45 | |
| 46 | |
| 47 | class BuildPrettifyDataTest(unittest.TestCase): |
| 48 | |
| 49 | def testNonSourceFile(self): |
| 50 | prettify_data = prettify.BuildPrettifyData(0, '/dev/null') |
| 51 | self.assertDictEqual( |
| 52 | dict(should_prettify=ezt.boolean(False), |
| 53 | prettify_class=None), |
| 54 | prettify_data) |
| 55 | |
| 56 | prettify_data = prettify.BuildPrettifyData(10, 'readme.txt') |
| 57 | self.assertDictEqual( |
| 58 | dict(should_prettify=ezt.boolean(False), |
| 59 | prettify_class=None), |
| 60 | prettify_data) |
| 61 | |
| 62 | def testGenericLanguage(self): |
| 63 | prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/hello.php') |
| 64 | self.assertDictEqual( |
| 65 | dict(should_prettify=ezt.boolean(True), |
| 66 | prettify_class=''), |
| 67 | prettify_data) |
| 68 | |
| 69 | def testSpecificLanguage(self): |
| 70 | prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/hello.java') |
| 71 | self.assertDictEqual( |
| 72 | dict(should_prettify=ezt.boolean(True), |
| 73 | prettify_class='lang-java'), |
| 74 | prettify_data) |
| 75 | |
| 76 | def testThirdPartyExtensionLanguages(self): |
| 77 | for ext in ['apollo', 'agc', 'aea', 'el', 'scm', 'cl', 'lisp', |
| 78 | 'go', 'hs', 'lua', 'fs', 'ml', 'proto', 'scala', |
| 79 | 'sql', 'vb', 'vbs', 'vhdl', 'vhd', 'wiki', 'yaml', |
| 80 | 'yml', 'clj']: |
| 81 | prettify_data = prettify.BuildPrettifyData(123, '/trunk/src/hello.' + ext) |
| 82 | self.assertDictEqual( |
| 83 | dict(should_prettify=ezt.boolean(True), |
| 84 | prettify_class='lang-' + ext), |
| 85 | prettify_data) |
| 86 | |
| 87 | def testExactFilename(self): |
| 88 | prettify_data = prettify.BuildPrettifyData(123, 'trunk/src/Makefile') |
| 89 | self.assertDictEqual( |
| 90 | dict(should_prettify=ezt.boolean(True), |
| 91 | prettify_class='lang-sh'), |
| 92 | prettify_data) |