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
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167

ios / PRESUBMIT_test.py [blame]

#!/usr/bin/env python
# Copyright 2017 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import os
import sys
import unittest

import PRESUBMIT

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import PRESUBMIT_test_mocks

class CheckTODOFormatTest(unittest.TestCase):
    """Test the _CheckBugInToDo presubmit check."""

    def testTODOs(self):
        # All instances of the "TO DO" string in the following test cases are
        # broken by line breaks, because this file is run through the PRESUBIT
        # that it tests, so incorrectly formatted items in the test fixture
        # will trigger errors.
        bad_lines = [
            'TO'
            'DO(ldap): fix this', 'TO'
            'DO(ladp): see crbug.com/8675309', 'TO'
            'DO(8675309): fix this', 'TO'
            'DO(http://crbug.com/8675309): fix this', 'TO'
            'DO( crbug.com/8675309): fix this', 'TO'
            'DO(crbug/8675309): fix this', 'TO'
            'DO(crbug.com): fix this', 'TO'
            'DO(inccrbug.com): fix this',
        ]
        deprecated_lines = [
            'TO'
            'DO(b/12345): fix this'
        ]
        good_lines = [
            'TO'
            'DO(crbug.com/8675309): fix this', 'TO'
            'DO(crbug.com/8675309): fix this (please)'
        ]
        mock_input = PRESUBMIT_test_mocks.MockInputApi()
        lines = bad_lines + deprecated_lines + good_lines
        mock_input.files = [
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm', lines)
        ]
        mock_output = PRESUBMIT_test_mocks.MockOutputApi()
        results = PRESUBMIT._CheckBugInToDo(mock_input, mock_output)
        # Expect one error result and one warning result.
        self.assertEqual(len(results), 2)
        self.assertEqual('error', results[0].type)
        self.assertEqual('warning', results[1].type)
        self.assertTrue('without bug numbers' in results[0].message)
        self.assertTrue('with a deprecated bug link' in results[1].message)
        error_lines = results[0].message.split('\n')
        self.assertEqual(len(error_lines), len(bad_lines) + 2)
        warning_lines = results[1].message.split('\n')
        self.assertEqual(len(warning_lines), len(deprecated_lines) + 2)

class CheckHasNoIncludeDirectivesTest(unittest.TestCase):
    """Test the _CheckHasNoIncludeDirectives presubmit check."""

    def testFindsIncludeDirectives(self):
        good_lines = [
            '#import <system>', '#import "my/path/my/header.h"',
            '#import "my/path/my/source.mm"', '#import "my/path/my/source.m"'
        ]
        bad_lines = [
            '#include <system>', '#import <system>',
            '#include "my/path/my/header.h"',
            '#include "my/path/my/source.mm"', '#import "my/path/my/header.h"'
            '#include "my/path/my/source.m"'
        ]
        mock_input = PRESUBMIT_test_mocks.MockInputApi()
        mock_input.files = [
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm',
                                          bad_lines),
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller_2.mm',
                                          good_lines),
            PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.h',
                                          bad_lines),
            PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.m',
                                          bad_lines),
            PRESUBMIT_test_mocks.MockFile('ios/path/bar_controller.cc',
                                          bad_lines),
            PRESUBMIT_test_mocks.MockFile('chrome/path/foo_controller.mm',
                                          bad_lines),
        ]
        mock_output = PRESUBMIT_test_mocks.MockOutputApi()
        errors = PRESUBMIT._CheckHasNoIncludeDirectives(
            mock_input, mock_output)
        self.assertEqual(len(errors), 1)
        self.assertEqual('error', errors[0].type)
        self.assertTrue('ios/path/foo_controller.mm:1' in errors[0].message)
        self.assertTrue('ios/path/foo_controller.mm:3' in errors[0].message)
        self.assertTrue('ios/path/foo_controller.mm:4' in errors[0].message)


class CheckHasNoPipeInCommentTest(unittest.TestCase):
    """Test the _CheckHasNoPipeInComment presubmit check."""

    def testFindsIncludeDirectives(self):
        good_lines = [
            '#if !defined(a) || !defined(b)',
            '// This does A || B', '// `MySymbol` is correct',
            'bitVariable1 | bitVariable2'
        ]
        bad_lines = [
            '// |MySymbol| is wrong', '// What is wrong is: |MySymbol|'
        ]
        mock_input = PRESUBMIT_test_mocks.MockInputApi()
        mock_input.files = [
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.mm',
                                          good_lines + bad_lines),
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_controller.h',
                                          bad_lines + good_lines),
        ]
        mock_output = PRESUBMIT_test_mocks.MockOutputApi()
        errors = PRESUBMIT._CheckHasNoPipeInComment(mock_input, mock_output)
        self.assertEqual(len(errors), 1)
        self.assertEqual('warning', errors[0].type)
        self.assertTrue('ios/path/foo_controller.mm:5' in errors[0].message)
        self.assertTrue('ios/path/foo_controller.mm:6' in errors[0].message)
        self.assertTrue('ios/path/foo_controller.h:1' in errors[0].message)
        self.assertTrue('ios/path/foo_controller.h:2' in errors[0].message)
        error_lines = errors[0].message.split('\n')
        self.assertEqual(len(error_lines), len(bad_lines) * 2 + 3)

class _CheckCanImproveTestUsingExpectNSEQ(unittest.TestCase):
    """Test the _CheckCanImproveTestUsingExpectNSEQ presubmit. """

    def testFindImprovableTestUsingExpectNSEQ(self):
        good_lines = [
          'EXPECT_TRUE(a == b);',
          'if (a isEqualToString:b) {',
          'if (a isEqualToData:b) {'
          ]
        bad_lines = [
          'EXPECT_TRUE(a ',
          ' isEqualToString:b);',
          'EXPECT_TRUE(@"example" isEqualToString:@"example");',
          'EXPECT_FALSE(@"example" isEqualToData:@"example");',
          'EXPECT_TRUE(@"example" isEqualToArray:@"example");'
        ]
        mock_input = PRESUBMIT_test_mocks.MockInputApi()
        mock_input.files = [
            PRESUBMIT_test_mocks.MockFile('ios/path/foo_unittest.mm',
                                          good_lines + bad_lines),
        ]
        mock_output = PRESUBMIT_test_mocks.MockOutputApi()
        errors = PRESUBMIT._CheckCanImproveTestUsingExpectNSEQ(
            mock_input, mock_output)
        self.assertEqual(len(errors), 1)
        self.assertEqual('warning', errors[0].type)
        self.assertFalse('ios/path/foo_unittest.mm:1' in errors[0].message)
        self.assertFalse('ios/path/foo_unittest.mm:2' in errors[0].message)
        self.assertFalse('ios/path/foo_unittest.mm:3' in errors[0].message)
        self.assertTrue('ios/path/foo_unittest.mm:4' in errors[0].message)
        self.assertFalse('ios/path/foo_unittest.mm:5' in errors[0].message)
        self.assertTrue('ios/path/foo_unittest.mm:6' in errors[0].message)
        self.assertTrue('ios/path/foo_unittest.mm:7' in errors[0].message)
        self.assertTrue('ios/path/foo_unittest.mm:8' in errors[0].message)


if __name__ == '__main__':
    unittest.main()