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