Skip to content

Commit

Permalink
Strip surrounding whitespace before parsing (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodal committed Aug 20, 2020
1 parent b6e66af commit b4402b2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ v0.3.0 (UNRELEASED)
with both amounts payable and currency. This field is only set if the
optional dependency ``py-moneyed`` is installed.

- Strip surrounding whitespace before parsing.

:mod:`biip.gtin`

- Detect Restricted Circulation Numbers (RCN) and return a subclass of
Expand All @@ -38,6 +40,8 @@ v0.3.0 (UNRELEASED)

- Add RCN rules for the Baltics, Great Britain, Norway, and Sweden.

- Strip surrounding whitespace before parsing.

- Bug fix: Keep all leading zeros in GTIN-8.

- Bug fix: Convert GTIN-8 to GTIN-12 before extracting GS1 Prefix.
Expand Down
1 change: 1 addition & 0 deletions src/biip/gs1/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def parse(
Raises:
ParseError: If a fixed-length field ends with a separator character.
"""
value = value.strip()
element_strings = []
rest = value[:]

Expand Down
2 changes: 2 additions & 0 deletions src/biip/gtin/_gtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def parse(
"""
from biip.gtin import Rcn

value = value.strip()

if len(value) not in (8, 12, 13, 14):
raise ParseError(
f"Failed parsing {value!r} as GTIN: "
Expand Down
6 changes: 6 additions & 0 deletions tests/gs1/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def test_parse_fails_if_fixed_length_field_ends_with_separator_char() -> None:
)


def test_parse_strips_surrounding_whitespace() -> None:
message = GS1Message.parse(" \t 800370713240010220085952 \n ")

assert message.value == "800370713240010220085952"


@pytest.mark.parametrize(
"value, expected",
[
Expand Down
6 changes: 6 additions & 0 deletions tests/gtin/test_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_parse_gtin_13_with_invalid_check_digit() -> None:
)


def test_parse_strips_surrounding_whitespace() -> None:
gtin = Gtin.parse(" \t 5901234123457 \n ")

assert gtin.value == "5901234123457"


@pytest.mark.parametrize(
"value",
[
Expand Down

0 comments on commit b4402b2

Please sign in to comment.