Skip to content

Commit

Permalink
feat: add Currency class
Browse files Browse the repository at this point in the history
  • Loading branch information
tdgrunenwald committed Dec 5, 2024
1 parent f409869 commit 7651bdd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 5 additions & 3 deletions features/steps/hello.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from behave import given, when, then
from ddd_sample.domain.currency import USD, Currency


@given('a currency item in {denomination}')
def _(context, denomination: str):
context.denomination = denomination
assert denomination == 'USD-cents', f"Denomination '{denomination}' is currently unsupported"
context.format = USD


@given('value of {value:d}')
def _(context, value: int):
context.value = value
context.currency = Currency(value=value, format=context.format)


@when('the currency is stringified')
def _(context):
context.string = ''
context.string = str(context.currency)


@then('it matches {pattern}')
Expand Down
18 changes: 18 additions & 0 deletions src/ddd_sample/domain/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from typing import Callable


CurrencyFormatMixin = Callable[[int], str]


def USD(value: int) -> str:
return "${:,}.{:02d}".format(value // 100, value % 100)


class Currency:
def __init__(self, value: int, format: CurrencyFormatMixin):
super().__init__()
self.value = value
self.format = format

def __str__(self) -> str:
return self.format(self.value)

0 comments on commit 7651bdd

Please sign in to comment.