Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: signed HexInt #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions hitch/story/scalar-hexadecimal-integer.story
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ Hexadecimal Integers (HexInt):
given:
yaml_snippet: |
x: 0x1a
y: -0x1a
z: +0x1a
setup: |
from strictyaml import Map, HexInt, load
from ensure import Ensure

schema = Map({"x": HexInt()})
schema = Map({"x": HexInt(),
"y": HexInt(),
"z": HexInt()})

parsed = load(yaml_snippet, schema)

variations:
Parsed correctly:
steps:
- Run: |
Ensure(parsed).equals({"x": 26})
Ensure(parsed.as_yaml()).equals("x: 0x1a\n")
Ensure(parsed).equals({"x": 26,
"y": -26,
"z": 26})
Ensure(parsed.as_yaml()).equals("x: 0x1a\ny: -0x1a\nz: +0x1a")

Uppercase:
given:
Expand Down
8 changes: 7 additions & 1 deletion strictyaml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ def is_hexadecimal(value):
"""
Is a string a string of a hexademcial integer?

>>> is_hexadecimal("+0xa1")
True

>>> is_hexadecimal("-0xa1")
True

>>> is_hexadecimal("0xa1")
True

Expand All @@ -101,7 +107,7 @@ def is_hexadecimal(value):
>>> is_hexadecimal("1")
False
"""
return compile(r"^0[xX]+[a-fA-F0-9]+$").match(value) is not None
return compile(r"^[\-\+]?0[xX]+[a-fA-F0-9]+$").match(value) is not None


def is_decimal(value):
Expand Down