How to use test_file_pattern method in green

Best Python code snippet using green

strict_enum_value_checker_test.py

Source:strict_enum_value_checker_test.py Github

copy

Full Screen

1#!/usr/bin/env python2# Copyright 2014 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import difflib6import os7import re8import unittest9from strict_enum_value_checker import StrictEnumValueChecker10class MockLogging(object):11 def __init__(self):12 self.lines = []13 def info(self, message):14 self.lines.append(message)15 def debug(self, message):16 self.lines.append(message)17class MockInputApi(object):18 def __init__(self):19 self.re = re20 self.os_path = os.path21 self.files = []22 self.is_committing = False23 self.logging = MockLogging()24 def AffectedFiles(self, include_deletes=None):25 return self.files26class MockOutputApi(object):27 class PresubmitResult(object):28 def __init__(self, message, items=None, long_text=""):29 self.message = message30 self.items = items31 self.long_text = long_text32 class PresubmitError(PresubmitResult):33 def __init__(self, message, items, long_text=""):34 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)35 self.type = "error"36 class PresubmitPromptWarning(PresubmitResult):37 def __init__(self, message, items, long_text=""):38 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)39 self.type = "warning"40 class PresubmitNotifyResult(PresubmitResult):41 def __init__(self, message, items, long_text=""):42 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)43 self.type = "notify"44class MockFile(object):45 def __init__(self, local_path, old_contents, new_contents):46 self._local_path = local_path47 self._new_contents = new_contents48 self._old_contents = old_contents49 self._cached_changed_contents = None50 def ChangedContents(self):51 return self._changed_contents52 def NewContents(self):53 return self._new_contents54 def LocalPath(self):55 return self._local_path56 def IsDirectory(self):57 return False58 def GenerateScmDiff(self):59 result = ""60 for line in difflib.unified_diff(self._old_contents, self._new_contents,61 self._local_path, self._local_path):62 result += line63 return result64 # NOTE: This method is a copy of ChangeContents method of AffectedFile in65 # presubmit_support.py66 def ChangedContents(self):67 """Returns a list of tuples (line number, line text) of all new lines.68 This relies on the scm diff output describing each changed code section69 with a line of the form70 ^@@ <old line num>,<old size> <new line num>,<new size> @@$71 """72 if self._cached_changed_contents is not None:73 return self._cached_changed_contents[:]74 self._cached_changed_contents = []75 line_num = 076 if self.IsDirectory():77 return []78 for line in self.GenerateScmDiff().splitlines():79 m = re.match(r"^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@", line)80 if m:81 line_num = int(m.groups(1)[0])82 continue83 if line.startswith("+") and not line.startswith("++"):84 self._cached_changed_contents.append((line_num, line[1:]))85 if not line.startswith("-"):86 line_num += 187 return self._cached_changed_contents[:]88class MockChange(object):89 def __init__(self, changed_files):90 self._changed_files = changed_files91 def LocalPaths(self):92 return self._changed_files93class StrictEnumValueCheckerTest(unittest.TestCase):94 TEST_FILE_PATTERN = "changed_file_%s.h"95 MOCK_FILE_LOCAL_PATH = "mock_enum.h"96 START_MARKER = "enum MockEnum {"97 END_MARKER = " mBoundary"98 def _ReadTextFileContents(self, path):99 """Given a path, returns a list of strings corresponding to the text lines100 in the file. Reads files in text format.101 """102 fo = open(path, "r")103 try:104 contents = fo.readlines()105 finally:106 fo.close()107 return contents108 def _ReadInputFile(self):109 return self._ReadTextFileContents("mock_enum.h")110 def _PrepareTest(self, new_file_path):111 old_contents = self._ReadInputFile()112 if not new_file_path:113 new_contents = []114 else:115 new_contents = self._ReadTextFileContents(new_file_path)116 input_api = MockInputApi()117 mock_file = MockFile(self.MOCK_FILE_LOCAL_PATH,118 old_contents,119 new_contents)120 input_api.files.append(mock_file)121 output_api = MockOutputApi()122 return input_api, output_api123 def _RunTest(self, new_file_path):124 input_api, output_api = self._PrepareTest(new_file_path)125 checker = StrictEnumValueChecker(input_api, output_api, self.START_MARKER,126 self.END_MARKER, self.MOCK_FILE_LOCAL_PATH)127 results = checker.Run()128 return results129 def testDeleteFile(self):130 results = self._RunTest(new_file_path=None)131 # TODO(rpaquay) How to check it's the expected warning?'132 self.assertEquals(1, len(results),133 "We should get a single warning about file deletion.")134 def testSimpleValidEdit(self):135 results = self._RunTest(self.TEST_FILE_PATTERN % "1")136 # TODO(rpaquay) How to check it's the expected warning?'137 self.assertEquals(0, len(results),138 "We should get no warning for simple edits.")139 def testSingleDeletionOfEntry(self):140 results = self._RunTest(self.TEST_FILE_PATTERN % "2")141 # TODO(rpaquay) How to check it's the expected warning?'142 self.assertEquals(1, len(results),143 "We should get a warning for an entry deletion.")144 def testSingleRenameOfEntry(self):145 results = self._RunTest(self.TEST_FILE_PATTERN % "3")146 # TODO(rpaquay) How to check it's the expected warning?'147 self.assertEquals(1, len(results),148 "We should get a warning for an entry rename, even "149 "though it is not optimal.")150 def testMissingEnumStartOfEntry(self):151 results = self._RunTest(self.TEST_FILE_PATTERN % "4")152 # TODO(rpaquay) How to check it's the expected warning?'153 self.assertEquals(1, len(results),154 "We should get a warning for a missing enum marker.")155 def testMissingEnumEndOfEntry(self):156 results = self._RunTest(self.TEST_FILE_PATTERN % "5")157 # TODO(rpaquay) How to check it's the expected warning?'158 self.assertEquals(1, len(results),159 "We should get a warning for a missing enum marker.")160 def testInvertedEnumMarkersOfEntry(self):161 results = self._RunTest(self.TEST_FILE_PATTERN % "6")162 # TODO(rpaquay) How to check it's the expected warning?'163 self.assertEquals(1, len(results),164 "We should get a warning for inverted enum markers.")165 def testMultipleInvalidEdits(self):166 results = self._RunTest(self.TEST_FILE_PATTERN % "7")167 # TODO(rpaquay) How to check it's the expected warning?'168 self.assertEquals(3, len(results),169 "We should get 3 warnings (one per edit).")170 def testSingleInvalidInserts(self):171 results = self._RunTest(self.TEST_FILE_PATTERN % "8")172 # TODO(rpaquay) How to check it's the expected warning?'173 self.assertEquals(1, len(results),174 "We should get a warning for a single invalid "175 "insertion inside the enum.")176 def testMulitpleValidInserts(self):177 results = self._RunTest(self.TEST_FILE_PATTERN % "9")178 # TODO(rpaquay) How to check it's the expected warning?'179 self.assertEquals(0, len(results),180 "We should not get a warning mulitple valid edits")181 def testSingleValidDeleteOutsideOfEnum(self):182 results = self._RunTest(self.TEST_FILE_PATTERN % "10")183 # TODO(rpaquay) How to check it's the expected warning?'184 self.assertEquals(0, len(results),185 "We should not get a warning for a deletion outside of "186 "the enum")187if __name__ == '__main__':...

Full Screen

Full Screen

PRESUBMIT_test.py

Source:PRESUBMIT_test.py Github

copy

Full Screen

1#!/usr/bin/env python2# Copyright (c) 2012 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5import difflib6import os7import re8import unittest9import PRESUBMIT10class MockLogging(object):11 def __init__(self):12 self.lines = []13 def info(self, message):14 self.lines.append(message)15 def debug(self, message):16 self.lines.append(message)17class MockInputApi(object):18 def __init__(self):19 self.re = re20 self.os_path = os.path21 self.files = []22 self.is_committing = False23 self.logging = MockLogging()24 def AffectedFiles(self, include_deletes=None):25 return self.files26class MockOutputApi(object):27 class PresubmitResult(object):28 def __init__(self, message, items=None, long_text=''):29 self.message = message30 self.items = items31 self.long_text = long_text32 class PresubmitError(PresubmitResult):33 def __init__(self, message, items, long_text=''):34 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)35 self.type = 'error'36 class PresubmitPromptWarning(PresubmitResult):37 def __init__(self, message, items, long_text=''):38 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)39 self.type = 'warning'40 class PresubmitNotifyResult(PresubmitResult):41 def __init__(self, message, items, long_text=''):42 MockOutputApi.PresubmitResult.__init__(self, message, items, long_text)43 self.type = 'notify'44class MockFile(object):45 def __init__(self, local_path, old_contents, new_contents):46 self._local_path = local_path47 self._new_contents = new_contents48 self._old_contents = old_contents49 self._cached_changed_contents = None50 def ChangedContents(self):51 return self._changed_contents52 def NewContents(self):53 return self._new_contents54 def LocalPath(self):55 return self._local_path56 def IsDirectory(self):57 return False58 def GenerateScmDiff(self):59 result = ""60 for line in difflib.unified_diff(self._old_contents, self._new_contents,61 self._local_path, self._local_path):62 result += line63 return result64 # NOTE: This method is a copy of ChangeContents method of AffectedFile in65 # presubmit_support.py66 def ChangedContents(self):67 """Returns a list of tuples (line number, line text) of all new lines.68 This relies on the scm diff output describing each changed code section69 with a line of the form70 ^@@ <old line num>,<old size> <new line num>,<new size> @@$71 """72 if self._cached_changed_contents is not None:73 return self._cached_changed_contents[:]74 self._cached_changed_contents = []75 line_num = 076 if self.IsDirectory():77 return []78 for line in self.GenerateScmDiff().splitlines():79 m = re.match(r'^@@ [0-9\,\+\-]+ \+([0-9]+)\,[0-9]+ @@', line)80 if m:81 line_num = int(m.groups(1)[0])82 continue83 if line.startswith('+') and not line.startswith('++'):84 self._cached_changed_contents.append((line_num, line[1:]))85 if not line.startswith('-'):86 line_num += 187 return self._cached_changed_contents[:]88class MockChange(object):89 def __init__(self, changed_files):90 self._changed_files = changed_files91 def LocalPaths(self):92 return self._changed_files93class HistogramValueCheckerTest(unittest.TestCase):94 TEST_FILE_PATTERN = "PRESUBMIT_test_new_file_%s.txt"95 def _ReadTextFileContents(self, path):96 """Given a path, returns a list of strings corresponding to the text lines97 in the file. Reads files in text format.98 """99 fo = open(path, 'r')100 try:101 contents = fo.readlines()102 finally:103 fo.close()104 return contents105 def _ReadInputFile(self):106 return self._ReadTextFileContents("PRESUBMIT_test_old_file.txt")107 def _PrepareTest(self, new_file_path):108 old_contents = self._ReadInputFile()109 if not new_file_path:110 new_contents = []111 else:112 new_contents = self._ReadTextFileContents(new_file_path)113 input_api = MockInputApi()114 mock_file = MockFile(PRESUBMIT.HistogramValueChecker.LOCAL_PATH,115 old_contents,116 new_contents)117 input_api.files.append(mock_file)118 output_api = MockOutputApi()119 return input_api, output_api120 def _RunTest(self, new_file_path):121 input_api, output_api = self._PrepareTest(new_file_path)122 checker = PRESUBMIT.HistogramValueChecker(input_api, output_api)123 results = checker.Run()124 return results125 def testDeleteFile(self):126 results = self._RunTest(new_file_path=None)127 # TODO(rpaquay) How to check it's the expected warning?'128 self.assertEquals(1, len(results),129 "We hould get a single warning about file deletion.")130 def testSimpleValidEdit(self):131 results = self._RunTest(self.TEST_FILE_PATTERN % "1")132 # TODO(rpaquay) How to check it's the expected warning?'133 self.assertEquals(0, len(results),134 "We should get no warning for simple edits.")135 def testSingleDeletionOfEntry(self):136 results = self._RunTest(self.TEST_FILE_PATTERN % "2")137 # TODO(rpaquay) How to check it's the expected warning?'138 self.assertEquals(1, len(results),139 "We should get a warning for an entry deletion.")140 def testSingleRenameOfEntry(self):141 results = self._RunTest(self.TEST_FILE_PATTERN % "3")142 # TODO(rpaquay) How to check it's the expected warning?'143 self.assertEquals(1, len(results),144 "We should get a warning for an entry rename, even "145 "though it is not optimal.")146 def testMissingEnumStartOfEntry(self):147 results = self._RunTest(self.TEST_FILE_PATTERN % "4")148 # TODO(rpaquay) How to check it's the expected warning?'149 self.assertEquals(1, len(results),150 "We should get a warning for a missing enum marker.")151 def testMissingEnumEndOfEntry(self):152 results = self._RunTest(self.TEST_FILE_PATTERN % "5")153 # TODO(rpaquay) How to check it's the expected warning?'154 self.assertEquals(1, len(results),155 "We should get a warning for a missing enum marker.")156 def testInvertedEnumMarkersOfEntry(self):157 results = self._RunTest(self.TEST_FILE_PATTERN % "6")158 # TODO(rpaquay) How to check it's the expected warning?'159 self.assertEquals(1, len(results),160 "We should get a warning for inverted enum markers.")161 def testMultipleInvalidEdits(self):162 results = self._RunTest(self.TEST_FILE_PATTERN % "7")163 # TODO(rpaquay) How to check it's the expected warning?'164 self.assertEquals(3, len(results),165 "We should get 3 warnings (one per edit).")166 def testSingleInvalidInserts(self):167 results = self._RunTest(self.TEST_FILE_PATTERN % "8")168 # TODO(rpaquay) How to check it's the expected warning?'169 self.assertEquals(1, len(results),170 "We should get a warning for a single invalid "171 "insertion inside the enum.")172 def testMulitpleValidInserts(self):173 results = self._RunTest(self.TEST_FILE_PATTERN % "9")174 # TODO(rpaquay) How to check it's the expected warning?'175 self.assertEquals(0, len(results),176 "We should not get a warning mulitple valid edits")177 def testSingleValidDeleteOutsideOfEnum(self):178 results = self._RunTest(self.TEST_FILE_PATTERN % "10")179 # TODO(rpaquay) How to check it's the expected warning?'180 self.assertEquals(0, len(results),181 "We should not get a warning for a deletion outside of "182 "the enum")183if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run green automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful