test_element.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Tests of classes in element.py.
  2. The really big classes -- Tag, PageElement, and NavigableString --
  3. are tested in separate files.
  4. """
  5. from bs4.element import (
  6. CharsetMetaAttributeValue,
  7. ContentMetaAttributeValue,
  8. NamespacedAttribute,
  9. )
  10. from . import SoupTest
  11. class TestNamedspacedAttribute(object):
  12. def test_name_may_be_none_or_missing(self):
  13. a = NamespacedAttribute("xmlns", None)
  14. assert a == "xmlns"
  15. a = NamespacedAttribute("xmlns", "")
  16. assert a == "xmlns"
  17. a = NamespacedAttribute("xmlns")
  18. assert a == "xmlns"
  19. def test_namespace_may_be_none_or_missing(self):
  20. a = NamespacedAttribute(None, "tag")
  21. assert a == "tag"
  22. a = NamespacedAttribute("", "tag")
  23. assert a == "tag"
  24. def test_attribute_is_equivalent_to_colon_separated_string(self):
  25. a = NamespacedAttribute("a", "b")
  26. assert "a:b" == a
  27. def test_attributes_are_equivalent_if_prefix_and_name_identical(self):
  28. a = NamespacedAttribute("a", "b", "c")
  29. b = NamespacedAttribute("a", "b", "c")
  30. assert a == b
  31. # The actual namespace is not considered.
  32. c = NamespacedAttribute("a", "b", None)
  33. assert a == c
  34. # But name and prefix are important.
  35. d = NamespacedAttribute("a", "z", "c")
  36. assert a != d
  37. e = NamespacedAttribute("z", "b", "c")
  38. assert a != e
  39. class TestAttributeValueWithCharsetSubstitution(object):
  40. """Certain attributes are designed to have the charset of the
  41. final document substituted into their value.
  42. """
  43. def test_content_meta_attribute_value(self):
  44. # The value of a CharsetMetaAttributeValue is whatever
  45. # encoding the string is in.
  46. value = CharsetMetaAttributeValue("euc-jp")
  47. assert "euc-jp" == value
  48. assert "euc-jp" == value.original_value
  49. assert "utf8" == value.encode("utf8")
  50. assert "ascii" == value.encode("ascii")
  51. def test_content_meta_attribute_value(self):
  52. value = ContentMetaAttributeValue("text/html; charset=euc-jp")
  53. assert "text/html; charset=euc-jp" == value
  54. assert "text/html; charset=euc-jp" == value.original_value
  55. assert "text/html; charset=utf8" == value.encode("utf8")
  56. assert "text/html; charset=ascii" == value.encode("ascii")