def complex_field_nested_within_a_hyperlink_complex_field_is_wrapped_with_the_hyperlink(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ xml_text(' AUTHOR "John Doe"') ]), self._SEPARATE_COMPLEX_FIELD, _run_element_with_text("John Doe"), self._END_COMPLEX_FIELD, self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("John Doe"), )), self._is_empty_hyperlinked_run, is_empty_run, )))
def test_can_parse_single_diffdoc_block_without_content(): content = _load_elements(dedent(""" .. diff-doc:: start example """)) assert_that(content, is_sequence( is_diffdoc_block( arguments=is_sequence("start", "example"), options={}, content="", ), ))
def _is_hyperlinked_run(self, **kwargs): return is_run(children=is_sequence( is_hyperlink( href=self._URI, **kwargs ), ))
def test_can_parse_diffdoc_block_without_options_and_content_followed_by_text(): content = _load_elements(dedent(""" .. diff-doc:: start example Text """)) assert_that(content, is_sequence( is_diffdoc_block( arguments=is_sequence("start", "example"), options={}, content="", ), is_text("\n"), is_text("Text"), ))
def elements_are_coerced_to_matchers(): matcher = is_sequence("apple", "banana") assert_equal( "iterable containing in order:\n 0: 'apple'\n 1: 'banana'", matcher.describe() )
def mismatches_when_items_are_in_wrong_order(): matcher = is_sequence(equal_to("apple"), equal_to("banana")) assert_equal( unmatched("element at index 0 mismatched:\n * was 'banana'"), matcher.match(["banana", "apple"]) )
def description_contains_descriptions_of_submatchers(): matcher = is_sequence(equal_to("apple"), equal_to("banana")) assert_equal( "iterable containing in order:\n 0: 'apple'\n 1: 'banana'", matcher.describe() )
def when_there_are_zero_submatchers_then_description_is_of_empty_iterable(): matcher = is_sequence() assert_equal( "empty iterable", matcher.describe() )
def mismatches_when_contains_extra_item(): matcher = is_sequence(equal_to("apple")) assert_equal( unmatched("had extra elements:\n * 'coconut'"), matcher.match(["apple", "coconut"]) )
def mismatches_when_item_is_expected_but_iterable_is_empty(): matcher = is_sequence(equal_to("apple")) assert_equal( unmatched("iterable was empty"), matcher.match([]) )
def when_empty_iterable_is_expected_then_empty_iterable_matches(): matcher = is_sequence() assert_equal( matched(), matcher.match([]) )
def test_when_field_value_in_input_object_in_list_is_not_set_then_default_is_used(): Input = schema.InputObjectType( "Input", fields=( schema.input_field("field0", type=schema.Int, default=42), ), ) Root = g.ObjectType( "Root", fields=( g.field("one", type=g.Int, params=[ g.param("arg", type=g.ListType(Input)), ]), ), ) graphql_query = """ query { one(arg: [{}]) } """ object_query = _document_text_to_graph_query(graphql_query, query_type=Root) assert_that(object_query.field_queries[0].args.arg, is_sequence( has_attrs( field0=42, ), ))
def mismatches_when_item_is_missing(): matcher = is_sequence(equal_to("apple"), equal_to("banana"), equal_to("coconut")) assert_equal( unmatched("element at index 2 was missing"), matcher.match(["apple", "banana"]) )
def mismatches_when_actual_is_not_iterable(): matcher = is_sequence(equal_to("apple")) assert_equal( unmatched("was not iterable\nwas 0"), matcher.match(0) )
def test_diff_with_render_false_renders_nothing(self): element = parser.Diff( name="example", content=dedent(""" --- old +++ new @@ -1,2 +1,2 @@ -x = 1 +x = 2 print(x) """), render=False, ) state = { "example": compiler.Code( language="python", content="x = 1\nprint(x)\n", pending_lines=(), ), } new_state, new_element = _execute(state, element) assert_that(new_element, is_empty_element) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence("x = 2")))
def test_diff_with_render_true_renders_content(self): # TODO: handle rendering when last_render is out of date content = dedent(""" --- old +++ new @@ -1,2 +1,2 @@ -x = 1 +x = 2 print(x) """) element = parser.Diff( name="example", content=content, render=True, ) state = { "example": compiler.Code( language="python", content="x = 1\nprint(x)\n", pending_lines=(), ), } new_state, new_element = _execute(state, element) assert_that(new_element, is_literal_block(content=content)) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence()))
def test_parsing_rst_splits_file_into_text_and_diffdoc_blocks(): content = rst.loads(dedent(""" Text one .. diff-doc:: start example :language: python :render: True Example 1 Example 2 Text two Text three .. diff-doc:: replace example Example 3 Example 4 Text four """)) assert_that(content, is_sequence( is_tuple(1, is_text("Text one\n")), is_tuple(2, is_text("\n")), is_tuple(3, is_diffdoc_block( arguments=is_sequence("start", "example"), options={"language": "python", "render": "True"}, content="Example 1\n\nExample 2\n", )), is_tuple(10, is_text("\n")), is_tuple(11, is_text("Text two\n")), is_tuple(12, is_text("\n")), is_tuple(13, is_text("Text three\n")), is_tuple(14, is_text("\n")), is_tuple(15, is_diffdoc_block( arguments=is_sequence("replace", "example"), options={}, content="Example 3\n\nExample 4\n", )), is_tuple(20, is_text("\n")), is_tuple(21, is_text("Text four")), ))
def runs_after_a_complex_field_for_hyperlinks_are_not_read_as_hyperlinks(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._END_COMPLEX_FIELD, _run_element_with_text("this will not be a hyperlink"), ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, is_empty_run, is_run(children=is_sequence( is_text("this will not be a hyperlink"), )), )))
def test_can_parse_diffdoc_block_with_options_and_without_content_followed_by_text(): content = _load_elements(dedent(""" .. diff-doc:: start example :language: python :render: True Text """)) assert_that(content, is_sequence( is_diffdoc_block( arguments=is_sequence("start", "example"), options={"language": "python", "render": "True"}, content="", ), is_text("\n"), is_text("Text"), ))
def test_start_with_render_false_renders_nothing(self): element = parser.Start( name="example", language="python", content="x = 1\nprint(x)", render=False, ) state = {} new_state, new_element = _execute(state, element) assert_that(new_element, is_empty_element) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence("x = 1", "print(x)")))
def can_handle_split_instr_text_elements(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [xml_text(" HYPE")]), xml_element("w:instrText", {}, [ xml_text('RLINK "{0}"'.format(self._URI)), ]), self._SEPARATE_COMPLEX_FIELD, _run_element_with_text("this is a hyperlink"), self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that( paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run( children=is_sequence(is_text("this is a hyperlink"), )), is_empty_run, )))
def field_without_separate_fld_char_is_ignored(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, self._HYPERLINK_INSTRTEXT, self._SEPARATE_COMPLEX_FIELD, self._BEGIN_COMPLEX_FIELD, self._END_COMPLEX_FIELD, _run_element_with_text("this is a hyperlink"), self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("this is a hyperlink"), )), is_empty_run, )))
def can_handle_split_instr_text_elements(self): element = xml_element("w:p", {}, [ self._BEGIN_COMPLEX_FIELD, xml_element("w:instrText", {}, [ xml_text(" HYPE") ]), xml_element("w:instrText", {}, [ xml_text('RLINK "{0}"'.format(self._URI)), ]), self._SEPARATE_COMPLEX_FIELD, _run_element_with_text("this is a hyperlink"), self._END_COMPLEX_FIELD, ]) paragraph = _read_and_get_document_xml_element(element) assert_that(paragraph, is_paragraph(children=is_sequence( is_empty_run, self._is_empty_hyperlinked_run, self._is_hyperlinked_run(children=is_sequence( is_text("this is a hyperlink"), )), is_empty_run, )))
def tbl_header_marks_table_row_as_header(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:trPr", {}, [xml_element("w:tblHeader")]), ]), xml_element("w:tr"), ]) table = _read_and_get_document_xml_element(element) assert_that( table, is_table(children=is_sequence( is_row(is_header=True), is_row(is_header=False), ), ))
def test_start_with_render_true_renders_content(self): element = parser.Start( name="example", language="python", content="x = 1", render=True, ) state = {} new_state, new_element = _execute(state, element) assert_that(new_element, is_code_block( language="python", content="x = 1", )) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence()))
def test_indentation_of_rendered_lines_can_differ_from_content(self): element = parser.Render( name="example", content=" print(x)", ) state = { "example": _create_code( language="python", content=" x = 1\n print(x)", pending_lines=(" x = 1", " print(x)"), ), } new_state, new_element = _execute(state, element) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence(" x = 1")))
def tbl_header_marks_table_row_as_header(self): element = xml_element("w:tbl", {}, [ xml_element("w:tr", {}, [ xml_element("w:trPr", {}, [ xml_element("w:tblHeader") ]), ]), xml_element("w:tr"), ]) table = _read_and_get_document_xml_element(element) assert_that(table, is_table( children=is_sequence( is_row(is_header=True), is_row(is_header=False), ), ))
def test_converting_from_diff_to_replace_generates_replace_block(self): start = parser.Start( name="example", language="python", render=True, content=dedent(""" x = 1 print(x) """), ) diff = parser.Diff( name="example", render=False, content=dedent(""" --- old +++ new @@ -1,2 +1,2 @@ -x = 1 +x = 2 print(x) """), ) output = compiler.convert_block( source=( (1, start), (2, diff), ), line_number=2, block_type="replace", ) assert_that(output, is_sequence( is_start(), is_replace( name="example", render=False, content=dedent(""" x = 2 print(x) """) ), ))
def test_replace_with_render_false_renders_nothing(self): element = parser.Replace( name="example", content="x = 2\nprint(x)\n", render=False, ) state = { "example": _create_code( language="python", content="x = 1\nprint(x)\n", pending_lines=(), ), } new_state, new_element = _execute(state, element) assert_that(new_element, is_empty_element) assert_that(new_state["example"], has_attrs(pending_lines=is_sequence("x = 2")))
def test_cbow(): input = list(range(1, 10)) y, X = cbow(input) assert_that(y, is_sequence(3, 4, 5, 6, 7)) assert_that( X, is_sequence( is_sequence(1, 2, 4, 5), is_sequence(2, 3, 5, 6), is_sequence(3, 4, 6, 7), is_sequence(4, 5, 7, 8), is_sequence(5, 6, 8, 9), ))
def test_given_no_mutation_type_is_defined_when_operation_is_mutation_then_error_is_raised(): QueryRoot = g.ObjectType( "Query", ( g.field("query_value", type=g.Int), ), ) graphql_query = """ mutation { queryValue } """ error = pytest.raises( GraphQLError, lambda: _document_text_to_graph_query(graphql_query, query_type=QueryRoot), ) assert_that(error.value.message, equal_to("unsupported operation: mutation")) assert_that(error.value.nodes, is_sequence(has_attrs(operation=has_attrs(value="mutation"))))
def is_query(query): if isinstance(query, schema.ScalarQuery): return is_instance(schema.ScalarQuery) elif isinstance(query, schema.EnumQuery): return is_instance(schema.EnumQuery) elif isinstance(query, schema.FieldQuery): return has_attrs( key=query.key, field=query.field, type_query=is_query(query.type_query), args=has_attrs(_values=is_mapping(query.args._values)), ) elif isinstance(query, schema.ListQuery): return has_attrs( type=query.type, element_query=is_query(query.element_query), ) elif isinstance(query, schema.NullableQuery): return has_attrs( type=query.type, element_query=is_query(query.element_query), ) elif isinstance(query, schema.ObjectQuery): return has_attrs( type=query.type, field_queries=is_sequence( * [is_query(field_query) for field_query in query.field_queries]), ) else: raise Exception("Unhandled query type: {}".format(type(query)))
def matches_when_all_submatchers_match_one_item_with_no_items_leftover(): matcher = is_sequence(equal_to("apple"), equal_to("banana")) assert_equal(matched(), matcher.match(["apple", "banana"]))
def mismatches_when_contains_extra_item(): matcher = is_sequence(equal_to("apple")) assert_equal(unmatched("had extra elements:\n * 'coconut'"), matcher.match(["apple", "coconut"]))
def mismatches_when_items_are_in_wrong_order(): matcher = is_sequence(equal_to("apple"), equal_to("banana")) assert_equal(unmatched("element at index 0 mismatched:\n * was 'banana'"), matcher.match(["banana", "apple"]))
def is_result(state, element): return is_sequence( state, element )
def close_to_can_be_used_in_composite_matcher(): matcher = is_sequence("a", "b", close_to(42, 1)) assert_equal(matched(), matcher.match(("a", "b", 42)))
def elements_are_coerced_to_matchers(): matcher = is_sequence("apple", "banana") assert_equal("iterable containing in order:\n 0: 'apple'\n 1: 'banana'", matcher.describe())
def description_contains_descriptions_of_submatchers(): matcher = is_sequence(equal_to("apple"), equal_to("banana")) assert_equal("iterable containing in order:\n 0: 'apple'\n 1: 'banana'", matcher.describe())
def when_there_are_zero_submatchers_then_description_is_of_empty_iterable(): matcher = is_sequence() assert_equal("empty iterable", matcher.describe())
def mismatches_when_item_is_missing(): matcher = is_sequence(equal_to("apple"), equal_to("banana"), equal_to("coconut")) assert_equal(unmatched("element at index 2 was missing"), matcher.match(["apple", "banana"]))