verbose error with wrong options

This commit is contained in:
Daniil Fajnberg 2022-01-01 17:21:36 +01:00
parent fe3dad39b5
commit c6dbcda947
2 changed files with 14 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[metadata]
name = yamlhttpforms
version = 0.0.3
version = 0.0.4
author = Daniil F.
author_email = mail@placeholder123.to
description = HTTP forms defined in YAML

View File

@ -45,9 +45,19 @@ def check_select_field_options(field_tag: Tag, field_interface: 'FormField', che
`FieldInterfaceWrong`
if the `default` is not equal to the value of the <option> tag which has the `selected` attribute
"""
options = {tag[VALUE]: tag.get_text(strip=True) for tag in field_tag.find_all(OPTION)}
if options != field_interface.options:
raise SelectOptionsWrong(f"Wrong options in {field_interface}")
html_options = {(tag[VALUE], tag.get_text(strip=True)) for tag in field_tag.find_all(OPTION)}
interface_options = set(field_interface.options.items())
missing_in_interface = html_options - interface_options
not_in_html = interface_options - html_options
s = ""
if missing_in_interface:
s += "\nThe following <options> HTML tags were not found in the interface:\n"
s += "\n".join(str(tup) for tup in missing_in_interface)
if not_in_html:
s += "\nThe following options were defined, but are not present in the HTML:\n"
s += "\n".join(str(tup) for tup in not_in_html)
if s:
raise SelectOptionsWrong(f"Wrong options in field '{field_interface.name}'." + s)
if check_defaults:
default = field_tag.find(lambda tag: tag.name == OPTION and tag.has_attr(SELECTED))[VALUE]
if default != field_interface.default: