2024-08-13 17:14:57 +01:00
import pytest
from textual . _binary_encode import DecodeError , dump , load
@pytest.mark.parametrize (
" data " ,
[
2024-08-13 21:11:45 +01:00
None ,
2024-08-13 17:14:57 +01:00
False ,
True ,
2024-08-13 21:00:32 +01:00
- 10 ,
- 1 ,
2024-08-13 17:14:57 +01:00
0 ,
1 ,
100 ,
" " ,
2024-08-20 16:51:02 +01:00
" 💩 " ,
2024-08-13 17:14:57 +01:00
" Hello " ,
b " World " ,
b " " ,
[ ] ,
( ) ,
2024-08-13 21:11:45 +01:00
[ None ] ,
2024-08-13 17:14:57 +01:00
[ 1 , 2 , 3 ] ,
2024-08-20 16:51:02 +01:00
( 0 , " foo " ) ,
( 1 , " foo " ) ,
( 0 , " " ) ,
( " " , " 💩 " , " 💩💩 " ) ,
( " " ) ,
2024-08-13 17:14:57 +01:00
[ " hello " , " world " ] ,
[ " hello " , b " world " ] ,
( " hello " , " world " ) ,
( " hello " , b " world " ) ,
2024-08-20 16:51:02 +01:00
( " foo " , " bar " , " baz " ) ,
( " foo " * 1000 , " bar " * 100 , " baz " * 500 ) ,
( 1 , " foo " , " bar " , " baz " ) ,
2024-08-13 17:14:57 +01:00
{ } ,
{ " foo " : " bar " } ,
{ " foo " : " bar " , b " egg " : b " baz " } ,
{ " foo " : " bar " , b " egg " : b " baz " , " list_of_things " : [ 1 , 2 , 3 , " Paul " , " Jessica " ] } ,
[ { } ] ,
[ [ 1 ] ] ,
[ ( 1 , 2 ) , ( 3 , 4 ) ] ,
] ,
)
def test_round_trip ( data : object ) - > None :
""" Test the data may be encoded then decoded """
encoded = dump ( data )
assert isinstance ( encoded , bytes )
decoded = load ( encoded )
assert data == decoded
@pytest.mark.parametrize (
" data " ,
[
2024-08-13 21:03:33 +01:00
b " " ,
2024-08-13 17:14:57 +01:00
b " 100:hello " ,
b " i " ,
b " i1 " ,
b " i10 " ,
b " li1e " ,
2024-08-13 21:01:30 +01:00
b " x100 " ,
2024-08-13 17:14:57 +01:00
] ,
)
def test_bad_encoding ( data : bytes ) - > None :
with pytest . raises ( DecodeError ) :
load ( data )
2024-08-13 21:03:33 +01:00
2024-08-13 21:18:37 +01:00
@pytest.mark.parametrize (
" data " ,
[
set ( ) ,
float ,
. . . ,
[ float ] ,
] ,
)
def test_dump_invalid_type ( data ) :
with pytest . raises ( TypeError ) :
dump ( data )
2024-08-13 21:03:33 +01:00
def test_load_wrong_type ( ) :
with pytest . raises ( TypeError ) :
load ( None )
with pytest . raises ( TypeError ) :
load ( " foo " )