Пример #1
0
    def test_attributes(self):
        # ARRANGE #
        cases = [
            NameAndValue(
                'plain string',
                'the plain string'
            ),
            NameAndValue(
                'to-string object',
                str_constructor.FormatPositional('template {}', 'value')
            ),
        ]
        for string_is_line_ended in [False, True]:
            for case in cases:
                with self.subTest(string_is_line_ended=string_is_line_ended,
                                  string=case.name):
                    detail = sut.PreFormattedStringDetail(case.value, string_is_line_ended)

                    # ACT & ASSERT #

                    self.assertIs(case.value,
                                  detail.object_with_to_string,
                                  'object_with_to_string')

                    self.assertIs(string_is_line_ended,
                                  detail.string_is_line_ended,
                                  'string_is_line_ended')
Пример #2
0
 def _range_expr_must_not_be_empty(self):
     if self._range_expr == '' or self._range_expr.isspace():
         msg = text_docs.single_pre_formatted_line_object(
             str_constructor.FormatPositional(
                 'Empty {}: {}',
                 names.RANGE_EXPR_SED_NAME,
                 repr(self._range_expr))
         )
         raise ValidationErrorException(msg)
Пример #3
0
 def _details(self) -> Sequence[tree.Detail]:
     renderer = custom_details.actual__custom(
         str_constructor.FormatPositional(
             'Actual contents ({} files)',
             len(self._actual_contents),
         ),
         custom_details.string_list(self._dir_contents_err_msg_lines()),
     )
     return renderer.render()
Пример #4
0
 def _no_match(self, tb: TraceBuilder,
               tot_num_elements: int) -> MatchingResult:
     explanation = str_constructor.FormatPositional(
         'No {} matches ({} tested)',
         self._conf.setup.rendering.type_name,
         tot_num_elements,
     )
     return (tb.append_details(
         self._explanation_when_no_element_matcher_trace(
             explanation)).build_result(False))
Пример #5
0
 def render(self) -> Sequence[Detail]:
     line = self._line
     return [
         tree.StringDetail(
             str_constructor.FormatPositional(
                 'Line {}. {}',
                 line[0],
                 repr(line[1]),
             ))
     ]
Пример #6
0
 def _all_match(self, tb: TraceBuilder,
                tot_num_elements: int) -> MatchingResult:
     actual = details.String(
         str_constructor.FormatPositional(
             'Every {} matches ({} tested)',
             self._conf.setup.rendering.type_name,
             tot_num_elements,
         ))
     expected = custom_details.TreeStructure(
         self._conf.predicate.structure())
     return (tb.append_details(
         custom_details.expected(expected)).append_details(
             custom_details.actual(actual)).build_result(True))
Пример #7
0
 def _compile_and_set_pattern(self,
                              regex_pattern: str) -> Optional[TextRenderer]:
     try:
         flags = 0
         if self._is_ignore_case:
             flags = re.IGNORECASE
         self.pattern = re.compile(regex_pattern, flags)
         return None
     except Exception as ex:
         return text_docs.single_line(
             str_constructor.FormatPositional(
                 "Invalid {}: '{}'",
                 syntax_elements.REGEX_SYNTAX_ELEMENT.singular_name,
                 ex,
             ))
Пример #8
0
def evaluate(py_expr: str) -> int:
    """
    :raises ValidationErrorException
    :return: Evaluated value
    """
    try:
        return python_evaluate(py_expr)
    except NotAnIntegerException as ex:
        py_ex_str = ('' if ex.python_exception_message is None else
                     '\n\nPython evaluation error:\n' +
                     ex.python_exception_message)
        msg = text_docs.single_pre_formatted_line_object(
            str_constructor.FormatPositional(
                'Value must be an integer: `{}\'{}', ex.value_string,
                py_ex_str))
        raise ValidationErrorException(msg)
Пример #9
0
    def _split_into_valid_number_of_parts(self) -> List[str]:
        self._range_expr_must_not_be_empty()

        limit_parts = self._range_expr.strip().split(names.LINE_NUMBERS_FILTER__LIMIT_SEPARATOR)

        if len(limit_parts) > 2:
            msg = text_docs.single_pre_formatted_line_object(
                str_constructor.FormatPositional(
                    'Invalid {} (found more than one "{}"): {}',
                    names.RANGE_EXPR_SED_NAME,
                    names.LINE_NUMBERS_FILTER__LIMIT_SEPARATOR,
                    repr(self._range_expr))
            )
            raise ValidationErrorException(msg)

        return limit_parts
Пример #10
0
 def main(self,
          environment: InstructionEnvironmentForPostSdsStep,
          settings: InstructionSettings,
          os_services: OsServices) -> pfh.PassOrFailOrHardError:
     actual_contents = environment.sds.result.stdout_file.read_text()
     if actual_contents == self.expected:
         return pfh.new_pfh_pass()
     else:
         return pfh.new_pfh_fail(
             new_pre_formatted_str_for_test(
                 str_constructor.FormatPositional(
                     'Expected: {}\nActual  : {}',
                     self.expected, actual_contents,
                 )
             )
         )
Пример #11
0
 def test_attributes(self):
     # ARRANGE #
     cases = [
         NameAndValue(
             'plain string',
             'the plain string'
         ),
         NameAndValue(
             'to-string object',
             str_constructor.FormatPositional('template {}', 'value')
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT && ASSERT #
             detail = sut.StringDetail(case.value)
             self.assertIs(case.value,
                           detail.string)
Пример #12
0
    def _get_reference_instruction_arguments(
            self, lines: Sequence[str]) -> List[str]:
        ret_val = []
        for line in lines:
            if not line or line.isspace():
                continue
            parts = line.split(maxsplit=1)
            if len(parts
                   ) == 2 and parts[0] == self._reference_instruction_name:
                ret_val.append(parts[1])
            else:
                err_msg = new_pre_formatted_str_for_test(
                    str_constructor.FormatPositional(
                        'Invalid act phase instruction: {}\nExpecting: {}',
                        line, self._reference_instruction_name))
                raise ParseException.of_str(err_msg)

        return ret_val
Пример #13
0
    def validate_pre_sds(self, environment: PathResolvingEnvironmentPreSds):

        try:
            resolved_value = self._int_sdv.resolve(environment)
        except NotAnIntegerException as ex:
            py_ex_str = (
                ''
                if ex.python_exception_message is None
                else
                '\n\nPython evaluation error:\n' + ex.python_exception_message
            )
            msg = text_docs.single_pre_formatted_line_object(
                str_constructor.FormatPositional(
                    'Argument must be an integer: `{}\'{}',
                    ex.value_string,
                    py_ex_str)
            )
            raise svh_exception.SvhValidationException(msg)

        self._validate_custom(resolved_value)
Пример #14
0
def matches_detail_properties(
    depth: int,
    text_style: TextStyle = TEXT_STYLE__NEUTRAL
) -> Assertion[s.ElementProperties]:
    return asrt_struct.equals_element_properties(
        expected_detail_properties(depth, text_style))


STRING_OBJECT_CASES = [
    NameAndValue(
        'constant',
        'a string constant',
    ),
    NameAndValue(
        'must apply str',
        str_constructor.FormatPositional(
            '{}',
            'a string that is generated',
        ),
    ),
]

_HEADER_PROPERTIES_FOR_F = ElementProperties(
    INDENTATION__NEUTRAL,
    TextStyle(color=ForegroundColor.RED, font_style=FontStyle.BOLD),
)
_HEADER_PROPERTIES_FOR_T = ElementProperties(
    INDENTATION__NEUTRAL,
    TextStyle(color=ForegroundColor.GREEN),
)
Пример #15
0
 def _non_matching_element_header(self) -> ToStringObject:
     return str_constructor.FormatPositional(
         'At least 1 {} does not match',
         self._conf.setup.rendering.type_name)
Пример #16
0
 def require_has_valid_head_token(self, syntax_element: str):
     if self.token_stream.look_ahead_state is LookAheadState.NULL:
         self.error_plain(_MISSING_START + syntax_element)
     self._require_head_token_has_valid_syntax(
         str_constructor.FormatPositional('Invalid syntax of {}:\n',
                                          syntax_element))