Check Tag Values
Now we know how to check a tag present in the code. But what if we want to check the values or attributes of a tag?
has_
functions are responsible for verifying the presence of different kind of values in a given tag.
This functions always return the state that they were intially passed and are recommended to use at the ‘end’ of a chain.
- has_equal_attr(state, attrs: Optional[Union[List[str], Tuple[str]]] = None, missing_msg: str = 'Expected attribute `{{attr}}` not found.', incorrect_msg: str = 'Expected attribute `{{attr}}` to be `"{{sol}}"`, but found `"{{stu}}"`.', check_values: bool = True, append: bool = True, **kwargs)
Check whether the student code have the same attributes as the solution code.
This function first checks for the presence of attributes in the student code, and if found then compares their values with the solution code.
- Parameters:
state (object) – State instance describing student and solution code. Can be omitted if used with
Ex()
.attrs (List[str] | Tuple[str] | None, optional) – The list or tuple of attributes of the solution to check. If
None
, all attributes in the solution code will be checked.missing_msg (str, optional) – Message to display if any attribute is missing in student code.
incorrect_msg (str, optional) – Message to display if any attribute with the incorret value is found.
check_values (bool, optional) – Whether to check the attribute values for equality. If
False
, only the presence of attributes will be checked.append (bool, optional) – Whether to append the message to the existing report. If
False
, only the message of this function will display.kwargs – Additional keyword arguments to pass into
missing_msg
orincorrect_msg
jinja template.
- Returns:
The same State object with updatted messages. And hence recommended to use at the end of the chain.
- Return type:
State
- Raises:
InstructorError – If an attribute specified in
attrs
is missing in the solution code.TestFail – If an attribute specified in
attrs
is missing in the student code or has an incorrect value. (aka feedback)
- Example:
>>> from htmlwhat.State import State >>> from htmlwhat.checks import check_body, has_equal_attr >>> student_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... </head> ... <body> ... </body> ... </html> ... """ >>> solution_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... </head> ... <body width="40px" class="example" > ... </body> ... </html> ... """ >>> state = State(student_code=student_code, solution_code=solution_code) >>> body_state = check_body(state) >>> has_equal_attr(body_state) Traceback (most recent call last): ... protowhat.failure.TestFail: Inspect the `<body>` tag. Expected attribute `width` not found.
Let’s modify
student_code
->>> student_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... </head> ... <body width="40px" class="hello"> ... </body> ... </html> ... """
And run the code again,
Inspect the `<body>` tag. Expected attribute `class` to be `"example"`, but found `"hello"`.
- has_equal_text(state, incorrect_msg: str = 'Expected text not found.', show_text: bool = False, append: bool = True, **kwargs)
Check whether the student and solution code have identical text. Keep in mind that this function get text of child tags as well.
Hence in the code
<body>Hello<p>this is paragraph</p>World</body>
, text of body is “Hello this is paragraph World” and not only “Hello World”.- Parameters:
state (object) – State instance describing student and solution code. Can be omitted if used with
Ex()
.incorrect_msg (str, optional) – The message to display if student text not equal to solution text.
show_text (bool, optional) – Whether to include the actual text in the error message. Default is False.
append (bool, optional) – Whether to append the message to the existing report. If
False
, only the message of this function will display.kwargs – Additional keyword arguments to pass into
missing_msg
orexpand_msg
jinja template.
- Returns:
The same State object with updatted messages. And hence recommended to use at the end of the chain.
- Return type:
State
- Raises:
TestFail – If the student text is not strictly equal to the solution text. (aka feedback)
- Example:
>>> from htmlwhat.State import State >>> from htmlwhat.checks import check_head, check_tag, has_equal_text >>> student_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... <title>412-52-2524</title> ... </head> ... </html> ... """ >>> solution_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... <title>412-52-2222</title> ... </head> ... </html> ... """ >>> state = State(student_code=student_code, solution_code=solution_code) >>> head_state = check_head(state) >>> title_state = check_tag(head_state, "title") >>> has_equal_text(title_state, show_text=True) Traceback (most recent call last): ... protowhat.failure.TestFail: Check the `title` tag with in `head`. Expected text `412-52-2222` but found `412-52-2524`
- has_code(state, text: str, incorrect_msg: str = "Didn't find {{text}} in your code.", fixed: bool = True, append=True, **kwargs)
Check whether the student code contains a specific text or follows a regex pattern. This function is a primary designed for regex pattern matching and is solution and AST independent, hence not required solution to contain the
text
.- Parameters:
state (object) – State instance describing student and solution code. Can be omitted if used with
Ex()
.text (str) – The text or regex pattern to search for in the student code.
incorrect_msg (str, optional) – The message to display if the text or pattern is not found in student code.
fixed (bool, optional) – Fixed should be
False
for regextext
. Default is True, for checking fixed text present in the student code.append (bool, optional) – Whether to append the message to the existing report. If
False
, only the message of this function will display.kwargs – Additional keyword arguments to pass into
missing_msg
orexpand_msg
jinja template.
- Returns:
The same State object with updatted messages. And hence recommended to use at the end of the chain.
- Return type:
State
- Raises:
TestFail – If the given
text
is not found in student code. (aka feedback)- Example:
>>> from htmlwhat.State import State >>> from htmlwhat.checks import has_code >>> student_code = """ ... <!DOCTYPE html> ... <html> ... <head> ... <title>12-52-2524</title> ... </head> ... </html> ... """ >>> solution_code = """ """ >>> state = State(student_code=student_code, solution_code=solution_code) >>> has_code(state, text=".*\d{3}-\d{2}-\d{4}.*", fixed=False) Traceback (most recent call last): ... protowhat.failure.TestFail: Didn't find the pattern `.*\d{3}-\d{2}-\d{4}.*` in your code.