From bf8061bb3bfe784fefeee6f114a44f1412770b40 Mon Sep 17 00:00:00 2001 From: jnichols0 Date: Thu, 3 Mar 2022 01:40:10 -0500 Subject: [PATCH] FEATURE: signed HexInt --- hitch/story/scalar-hexadecimal-integer.story | 12 +++++++++--- strictyaml/utils.py | 8 +++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/hitch/story/scalar-hexadecimal-integer.story b/hitch/story/scalar-hexadecimal-integer.story index fa8fe3e..dfc7c1c 100644 --- a/hitch/story/scalar-hexadecimal-integer.story +++ b/hitch/story/scalar-hexadecimal-integer.story @@ -7,11 +7,15 @@ 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) @@ -19,8 +23,10 @@ Hexadecimal Integers (HexInt): 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: diff --git a/strictyaml/utils.py b/strictyaml/utils.py index c653fe8..c9ffd50 100644 --- a/strictyaml/utils.py +++ b/strictyaml/utils.py @@ -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 @@ -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):