Skip to content

Commit

Permalink
Test string_utils module (#362)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Feb 8, 2022
1 parent b95c76f commit 2bd7ece
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/whitenoise/string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def decode_if_byte_string(s, force_text=False):
if isinstance(s, bytes):
s = s.decode("utf-8")
s = s.decode()
if force_text and not isinstance(s, str):
s = str(s)
return s
Expand Down
38 changes: 38 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations

from whitenoise.string_utils import decode_if_byte_string, ensure_leading_trailing_slash


class DecodeIfByteStringTests:
def test_bytes(self):
assert decode_if_byte_string(b"abc") == "abc"

def test_unforced(self):
x = object()
assert decode_if_byte_string(x) is x

def test_forced(self):
x = object()
result = decode_if_byte_string(x, force_text=True)
assert isinstance(result, str)
assert result.startswith("<object object at ")


class EnsureLeadingTrailingSlashTests:
def test_none(self):
assert ensure_leading_trailing_slash(None) == "/"

def test_empty(self):
assert ensure_leading_trailing_slash("") == "/"

def test_slash(self):
assert ensure_leading_trailing_slash("/") == "/"

def test_contents(self):
assert ensure_leading_trailing_slash("/foo/") == "/foo/"

def test_leading(self):
assert ensure_leading_trailing_slash("/foo") == "/foo"

def test_trailing(self):
assert ensure_leading_trailing_slash("foo/") == "/foo"

0 comments on commit 2bd7ece

Please sign in to comment.