"""Tests for utility functions.""" from devtoolbelt.utils import ( format_bytes, format_duration, parse_key_value, pluralize, truncate, ) class TestFormatBytes: """Tests for format_bytes function.""" def test_bytes(self): assert format_bytes(500) == "500.00 B" def test_kilobytes(self): assert format_bytes(2048) == "2.00 KB" def test_megabytes(self): assert format_bytes(1048576) == "1.00 MB" def test_gigabytes(self): assert format_bytes(1073741824) == "1.00 GB" class TestFormatDuration: """Tests for format_duration function.""" def test_milliseconds(self): assert format_duration(0.5) == "500ms" def test_seconds(self): assert format_duration(30) == "30.0s" def test_minutes(self): assert format_duration(120) == "2.0m" def test_hours(self): assert format_duration(7200) == "2.0h" def test_days(self): assert format_duration(172800) == "2.0d" class TestParseKeyValue: """Tests for parse_key_value function.""" def test_empty_string(self): assert parse_key_value("") == {} def test_single_pair(self): result = parse_key_value("key=value") assert result == {"key": "value"} def test_multiple_pairs(self): result = parse_key_value("key1=value1,key2=value2,key3=value3") assert result == { "key1": "value1", "key2": "value2", "key3": "value3" } def test_values_with_spaces(self): result = parse_key_value("name=John Doe,role=Admin") assert result == {"name": "John Doe", "role": "Admin"} def test_missing_value(self): result = parse_key_value("key") assert result == {} class TestPluralize: """Tests for pluralize function.""" def test_singular(self): assert pluralize(1, "item") == "item" def test_plural(self): assert pluralize(5, "item") == "items" def test_custom_plural(self): assert pluralize(1, "child", "children") == "child" assert pluralize(3, "child", "children") == "children" class TestTruncate: """Tests for truncate function.""" def test_no_truncation_needed(self): assert truncate("hello", 10) == "hello" def test_truncation(self): assert truncate("hello world", 8) == "hello..." def test_custom_suffix(self): assert truncate("hello world", 8, suffix="***") == "hello***" def test_exact_length(self): assert truncate("hello", 5) == "hello"