1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
# -*- coding: utf-8 -*-
# pylint: disable=bad-continuation
# pylint: disable=too-many-lines
from __future__ import unicode_literals
import io
import os
import unittest
from cmakelang import configuration
from cmakelang.command_tests import assert_format, TestBase
class TestMiscFormatting(TestBase):
"""
Ensure that various inputs format the way we want them to
"""
kExpectNumSidecarTests = 86
def test_config_hashruler_minlength(self):
# make sure changing hashruler_min_length works correctly
# self.config.enable_markup = False
for min_width in {3, 5, 7, 9}:
self.config.markup.hashruler_min_length = min_width
# NOTE(josh): these tests use short rulers that wont be picked up by
# the default pattern
self.config.markup.ruler_pattern = (r'#{%d}#*' % (min_width - 1))
just_shy = '#' * (min_width - 1)
just_right = '#' * min_width
longer = '#' * (min_width + 2)
full_line = '#' * (self.config.format.line_width - 2)
assert_format(self, """
# A comment: min_width={min_width}, just_shy
{just_shy}
""".format(min_width=min_width, just_shy=just_shy),
"""\
# A comment: min_width={min_width}, just_shy
#
""".format(min_width=min_width))
assert_format(self, """
# A comment: min_width={min_width}, just_right
{just_right}
""".format(min_width=min_width, just_right=just_right),
"""\
# A comment: min_width={min_width}, just_right
# {full_line}
""".format(min_width=min_width, full_line=full_line))
assert_format(self, """
# A comment: min_width={min_width}, longer
{longer}
""".format(min_width=min_width, longer=longer),
"""\
# A comment: min_width={min_width}, longer
# {full_line}
""".format(min_width=min_width, full_line=full_line))
def test_windows_line_endings_input(self):
self.source_str = (
"#[[*********************************************\r\n"
"* Information line 1\r\n"
"* Information line 2\r\n"
"************************************************]]\r\n")
self.expect_format = """\
#[[*********************************************
* Information line 1
* Information line 2
************************************************]]
"""
def test_windows_line_endings_output(self):
config_dict = self.config.as_dict()
config_dict['line_ending'] = 'windows'
self.config = configuration.Configuration(**config_dict)
self.source_str = """\
#[[*********************************************
* Information line 1
* Information line 2
************************************************]]"""
self.expect_format = (
"#[[*********************************************\r\n"
"* Information line 1\r\n"
"* Information line 2\r\n"
"************************************************]]\r\n")
def test_auto_line_endings(self):
config_dict = self.config.as_dict()
config_dict['line_ending'] = 'auto'
self.config = configuration.Configuration(**config_dict)
self.source_str = (
"#[[*********************************************\r\n"
"* Information line 1\r\n"
"* Information line 2\r\n"
"************************************************]]\r\n")
self.expect_format = (
"#[[*********************************************\r\n"
"* Information line 1\r\n"
"* Information line 2\r\n"
"************************************************]]\r\n")
def test_byte_order_mark(self):
self.source_str = """\
\ufeffcmake_minimum_required(VERSION 2.8.11)
project(cmakelang_test)
"""
self.expect_format = """\
cmake_minimum_required(VERSION 2.8.11)
project(cmakelang_test)
"""
self.config.encode.emit_byteorder_mark = True
self.source_str = """\
cmake_minimum_required(VERSION 2.8.11)
project(cmakelang_test)
"""
self.expect_format = """\
\ufeffcmake_minimum_required(VERSION 2.8.11)
project(cmakelang_test)
"""
def test_example_file(self):
thisdir = os.path.dirname(__file__)
base_override = os.getenv("PYBUILD_TEST_BASE_OVERRIDE")
if base_override is not None:
thisdir = thisdir.replace(os.environ['PWD'], base_override)
infile_path = os.path.join(
thisdir, '..', 'format/testdata', 'test_in.cmake')
outfile_path = os.path.join(
thisdir, '..', 'format/testdata', 'test_out.cmake')
with io.open(infile_path, 'r', encoding='utf8') as infile:
infile_text = infile.read()
with io.open(outfile_path, 'r', encoding='utf8') as outfile:
outfile_text = outfile.read()
self.source_str = infile_text
self.expect_format = outfile_text
if __name__ == '__main__':
unittest.main()
|