Skip to content

Commit

Permalink
feat: add is_valid_license_plate (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioduarte authored Oct 9, 2023
1 parent a252da6 commit e0d0396
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Utilitário `is_valid_license_plate` [#237](https://github.com/brazilian-utils/brutils-python/pull/237)
- Utilitário `remove_symbols_pis` [#236](https://github.com/brazilian-utils/brutils-python/pull/236)
- Utilitário `format_processo_juridico` [#210](https://github.com/brazilian-utils/brutils-python/pull/210)
- Utilitário `generate_pis` [#218](https://github.com/brazilian-utils/brutils-python/pull/218)
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ False
- [Email](#email)
- [is_valid_email](#is_valid_email)
- [License Plate](#license_plate)
- [is_valid_license_plate](#is_valid_license_plate)
- [is_valid_license_plate_old_format](#is_valid_license_plate_old_format)
- [is_valid_license_plate_mercosul](#is_valid_license_plate_mercosul)
- [convert_license_plate_to_mercosul](#convert_license_plate_to_mercosul)
Expand Down Expand Up @@ -279,6 +280,22 @@ False

## Placa de Carro

### is_valid_license_plate

Verifica se uma placa veicular é válida. Suporta padrão antigo e padrão Mercosul.

```python
>>> from brutils import is_valid_license_plate
>>> is_valid_license_plate('ABC1234')
True
>>> is_valid_license_plate('def5678')
True
>>> is_valid_license_plate('ABC4E67')
True
>>> is_valid_license_plate('GHI-4567')
False
```

### is_valid_license_plate_old_format

Verifica se é uma Placa de Veículo no antigo padrão utilizado no Brasil. Recebe como parâmetro uma string devendo conter somente caracteres alfanuméricos(letras e números) e retorna um valor booleano. ***Exemplo: 'abc1234' resulta em True.***
Expand Down
17 changes: 17 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ False
- [Email](#email)
- [is_valid_email](#is_valid_email)
- [License_Plate](#license_plate)
- [is_valid_license_plate](#is_valid_license_plate)
- [is_valid_license_plate_old_format](#is_valid_license_plate_old_format)
- [is_valid_license_plate_mercosul](#is_valid_license_plate_mercosul)
- [convert_license_plate_to_mercosul](#convert_license_plate_to_mercosul)
Expand Down Expand Up @@ -272,6 +273,22 @@ False

## License Plate

### is_valid_license_plate

Checks whether a Brazilian license plate is valid. It supports the old format as well as the Mercosul one.

```python
>>> from brutils import is_valid_license_plate
>>> is_valid_license_plate('ABC1234')
True
>>> is_valid_license_plate('def5678')
True
>>> is_valid_license_plate('ABC4E67')
True
>>> is_valid_license_plate('GHI-4567')
False
```

### is_valid_license_plate_old_format

Checks if it is a License Plate in the old format used in Brazil. Receives as a parameter a string that should contain only alphanumeric characters (letters and numbers) and returns a boolean value. ***Example: 'abc1234' results in True.***
Expand Down
1 change: 1 addition & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from brutils.license_plate import (
is_valid_mercosul as is_valid_license_plate_mercosul,
is_valid_license_plate_old_format,
is_valid as is_valid_license_plate,
convert_to_mercosul as convert_license_plate_to_mercosul,
format as format_license_plate,
remove_symbols as remove_symbols_license_plate,
Expand Down
10 changes: 10 additions & 0 deletions brutils/license_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ def format(license_plate):
############


def is_valid(license_plate: str) -> bool:
"""
Checks wheter license plate is valid according to the old format and
the Mercosul one.
"""
return is_valid_license_plate_old_format(
license_plate
) or is_valid_mercosul(license_plate)


def is_valid_license_plate_old_format(plate: str) -> bool:
"""
Checks whether a string matches the old format of Brazilian license plate.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_license_plate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
remove_symbols,
is_valid_license_plate_old_format,
is_valid_mercosul,
is_valid,
convert_to_mercosul,
format,
)
Expand All @@ -19,6 +20,23 @@ def test_remove_symbols(self):
self.assertEqual(remove_symbols("@---#"), "@#")
self.assertEqual(remove_symbols("---"), "")

def test_is_valid(self):
# Invalid old license plate
self.assertFalse(is_valid(123456))
self.assertFalse(is_valid("123-456"))

# Invalid mercosul license plate
self.assertFalse(is_valid("ABCDEFG"))
self.assertFalse(is_valid("ABC-123"))

# Valid old license plate
self.assertTrue(is_valid("ABC1234"))
self.assertTrue(is_valid("abc1234"))

# Valid mercosul license plate
self.assertTrue(is_valid("ABC4E67"))
self.assertTrue(is_valid("XXX9X99"))

def test_is_valid_license_old_format(self):
# When license plate is valid, returns True
self.assertTrue(is_valid_license_plate_old_format("ABC1234"))
Expand Down

0 comments on commit e0d0396

Please sign in to comment.