FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_codegen.py at main · pythonnative/pythonnative · GitHub
pythonnative
pythonnative
Repository navigation
Code
Issues
11
(11)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_codegen.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
116 lines (98 loc) · 5.04 KB
Breadcrumbs
pythonnative
/
tests
/
test_codegen.py
Copy path
File metadata and controls
116 lines (98 loc) · 5.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
"""Generated native contracts match a clean Python interpreter."""
import
os
import
re
import
runpy
import
shutil
import
subprocess
import
sys
from
pathlib
import
Path
from
typing
import
Literal
import
pytest
from
pythonnative
.
sdk
.
native_types
import
NativeTypes
,
quoted
def
test_checked_native_contracts_match_python_definitions
(
tmp_path
:
Path
)
->
None
:
subprocess
.
run
(
[
sys
.
executable
,
"-c"
,
"import pythonnative; from pythonnative.sdk.codegen import generate; "
"import sys; generate(sys.argv[1])"
,
str
(
tmp_path
),
],
check
=
True
,
)
root
=
Path
(
__file__
).
parents
[
1
]
/
"src/pythonnative/native"
for
extension
,
folder
in
(
(
"swift"
,
root
/
"ios/Sources/PythonNativeKit/Generated"
),
(
"kt"
,
root
/
"android/src/main/java/com/pythonnative/generated"
),
):
for
name
in
(
"PNContracts"
,
"NativeProps"
,
"NativeModules"
,
"NativeValues"
):
assert
(
tmp_path
/
f"
{
name
}
.
{
extension
}
"
).
read_bytes
()
==
(
folder
/
f"
{
name
}
.
{
extension
}
"
).
read_bytes
()
for
name
in
(
"components.py"
,
"modules.py"
):
compile
((
tmp_path
/
name
).
read_text
(),
str
(
tmp_path
/
name
),
"exec"
)
def
test_extension_manifests_keep_builtin_type_names
(
tmp_path
:
Path
)
->
None
:
# A plugin build loads extension manifests from JSON, which turns tuples
# into lists. An extension component that redeclares View's props must
# not rename the built-in types that hand-written native code spells.
script
=
(
"import json, sys; import pythonnative; from pythonnative.sdk import schema; "
"from pythonnative.sdk.codegen import generate; "
"badge = json.loads(json.dumps(schema.manifest()['components']['View'], default=str)); "
"badge['name'] = 'AaaBadge'; "
"schema.load_manifest({'protocol': 4, 'yoga': '3.2.1', 'components': {'AaaBadge': badge}}); "
"generate(sys.argv[1])"
)
subprocess
.
run
([
sys
.
executable
,
"-c"
,
script
,
str
(
tmp_path
)],
check
=
True
)
root
=
Path
(
__file__
).
parents
[
1
]
/
"src/pythonnative/native"
declaration
=
re
.
compile
(
r"^(?:sealed class|data class|enum class|public enum|public struct) (PN\w+)"
,
re
.
M
)
for
extension
,
folder
in
(
(
"swift"
,
root
/
"ios/Sources/PythonNativeKit/Generated"
),
(
"kt"
,
root
/
"android/src/main/java/com/pythonnative/generated"
),
):
checked
=
set
(
declaration
.
findall
((
folder
/
f"NativeValues.
{
extension
}
"
).
read_text
()))
assert
checked
<=
set
(
declaration
.
findall
((
tmp_path
/
f"NativeValues.
{
extension
}
"
).
read_text
()))
def
base
(
text
:
str
)
->
str
:
return
text
.
split
(
"open class PNViewProps"
,
1
)[
1
].
split
(
"
\n
}
\n
"
,
1
)[
0
]
assert
base
((
tmp_path
/
f"NativeProps.
{
extension
}
"
).
read_text
())
==
base
(
(
folder
/
f"NativeProps.
{
extension
}
"
).
read_text
()
)
def
test_example_extension_contracts_match_the_builtins
(
tmp_path
:
Path
)
->
None
:
# The inbox extension's manifest copies View's props; regenerate it in a
# scratch copy so a built-in change can't leave it stale unnoticed.
source
=
Path
(
__file__
).
parents
[
1
]
/
"examples/inbox-extension"
copy
=
tmp_path
/
"inbox-extension"
shutil
.
copytree
(
source
/
"src"
,
copy
/
"src"
)
shutil
.
copy2
(
source
/
"generate_contracts.py"
,
copy
/
"generate_contracts.py"
)
subprocess
.
run
(
[
sys
.
executable
,
str
(
copy
/
"generate_contracts.py"
)],
check
=
True
,
env
=
{
**
os
.
environ
,
"PYTHONPATH"
:
str
(
copy
/
"src"
)},
)
for
relative
in
(
"inbox_extension/native/schema.json"
,
"inbox_extension/api.py"
):
assert
(
copy
/
"src"
/
relative
).
read_text
()
==
(
source
/
"src"
/
relative
).
read_text
(),
relative
def
test_generated_literals_preserve_interpolation_and_control_characters
()
->
None
:
value
=
'$total "quoted"
\\
path
\n
\x00
\b
'
assert
quoted
(
value
)
==
r'"$total \"quoted\" \\path\n\u{0}\u{8}"'
assert
quoted
(
value
,
kotlin
=
True
)
==
r'"\$total \"quoted\" \\path\n\u0000\u0008"'
types
=
NativeTypes
()
types
.
types
({
"enum"
: [
"$total"
,
"
\x00
"
]},
"EscapedLiteral"
)
assert
r'"\$total"'
in
"
\n
"
.
join
(
types
.
kotlin
)
assert
r'"\u{0}"'
in
"
\n
"
.
join
(
types
.
swift
)
def
test_checked_native_record_fixtures_match_python_annotations
()
->
None
:
fixtures
=
runpy
.
run_path
(
"scripts/generate-native-contracts.py"
)[
"record_fixtures"
]()
for
path
,
expected
in
fixtures
.
items
():
assert
path
.
read_text
()
==
expected
def
test_codegen_escapes_kotlin_literals_once
(
tmp_path
:
Path
,
monkeypatch
:
pytest
.
MonkeyPatch
)
->
None
:
from
pythonnative
.
sdk
.
codegen
import
generate
from
pythonnative
.
sdk
.
schema
import
COMPONENTS
,
ComponentSchema
from
pythonnative
.
sdk
.
types
import
type_schema
monkeypatch
.
setitem
(
COMPONENTS
,
"LiteralFixture"
,
ComponentSchema
(
"LiteralFixture"
, {
"choice"
:
type_schema
(
Literal
[
"$total"
])})
)
generate
(
tmp_path
)
kotlin
=
(
tmp_path
/
"NativeValues.kt"
).
read_text
()
assert
r'"\$total"'
in
kotlin
assert
r'"\\$total"'
not
in
kotlin
assert
(
(
tmp_path
/
"modules.py"
)
.
read_text
()
.
startswith
(
'"""Generated Python interfaces. Regenerate from the declarations."""
\n
\n
'
)
)
Back
|
FazBrowse Home
|
New Git URL