diff --git a/fixit/cli.py b/fixit/cli.py index 52d4ffb..6ed5030 100644 --- a/fixit/cli.py +++ b/fixit/cli.py @@ -12,7 +12,9 @@ CONVERSIONS = { "c2f": convert.celsius_to_fahrenheit, "f2c": convert.fahrenheit_to_celsius, + "kg2lb": convert.kg_to_lbs, "km2mi": convert.km_to_miles, + "lb2kg": convert.lbs_to_kg, "mi2km": convert.miles_to_km, } diff --git a/fixit/convert.py b/fixit/convert.py index 5ca0ce0..0eaffab 100644 --- a/fixit/convert.py +++ b/fixit/convert.py @@ -1,6 +1,7 @@ """Unit conversions.""" KM_PER_MILE = 1.609344 +LBS_PER_KG = 2.20462 def celsius_to_fahrenheit(c: float) -> float: @@ -37,3 +38,22 @@ def miles_to_km(miles: float) -> float: miles_to_km(1) returns approximately 1.6093. """ return miles * KM_PER_MILE + + +def kg_to_lbs(kg: float) -> float: + """Convert kilograms to pounds. + + Example: + kg_to_lbs(1) returns approximately 2.20462. + """ + return kg * LBS_PER_KG + + +def lbs_to_kg(lbs: float) -> float: + """Convert pounds to kilograms. + + Example: + lbs_to_kg(2.20462) returns approximately 1.0. + """ + return lbs / LBS_PER_KG + diff --git a/tests/test_cli.py b/tests/test_cli.py index 532b78e..c90db12 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,5 @@ +import pytest + from fixit.cli import main @@ -43,3 +45,14 @@ def test_top_words_command_defaults_to_three(capsys): assert result == 0 assert capsys.readouterr().out == "one: 1\ntwo: 1\nthree: 1\n" + + +def test_convert_command_weight(capsys): + result = main(["convert", "1", "kg2lb"]) + assert result == 0 + assert float(capsys.readouterr().out.strip()) == pytest.approx(2.20462) + + result = main(["convert", "2.20462", "lb2kg"]) + assert result == 0 + assert float(capsys.readouterr().out.strip()) == pytest.approx(1.0) + diff --git a/tests/test_convert.py b/tests/test_convert.py index ddd3410..89f0506 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -3,7 +3,9 @@ from fixit.convert import ( celsius_to_fahrenheit, fahrenheit_to_celsius, + kg_to_lbs, km_to_miles, + lbs_to_kg, miles_to_km, ) @@ -38,3 +40,14 @@ def test_absolute_zero(): def test_float_km_to_miles(): assert km_to_miles(1.5) == pytest.approx(0.932056788) + + +def test_kg_to_lbs(): + assert kg_to_lbs(1) == pytest.approx(2.20462) + assert kg_to_lbs(10) == pytest.approx(22.0462) + + +def test_lbs_to_kg(): + assert lbs_to_kg(2.20462) == pytest.approx(1.0) + assert lbs_to_kg(10) == pytest.approx(4.53592, rel=1e-3) +