Skip to content

Commit 88e2ca1

Browse files
committed
New wrapper testing and adjustmenr for Chain and Distinct.
1 parent 8dfc712 commit 88e2ca1

6 files changed

Lines changed: 50 additions & 77 deletions

File tree

.github/workflows/rename_project.yml

Lines changed: 0 additions & 42 deletions
This file was deleted.

javascript/wrapper.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ function hex2a(hex)
1616
// Require the secrets package and assign it to a variable
1717
const secrets = require('../node_modules/secrets.js-grempe/secrets.js');
1818

19+
// Extend secrets package to cause a JSONDecodeError for coverage tests
20+
secrets.fail = () => {
21+
console.log('Oh no, this is a string. Not JSON.');
22+
};
23+
1924
// If the script is executed directly, call the appropriate function based on arguments
2025
if (require.main === module) {
2126
const commands = hex2a(process.argv[2]);

js2pysecrets/decorators.py

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,23 @@ def __init__(self, func, test=False, distinct=False):
1111

1212
def __call__(self, *args, test=False, distinct=False, **kwargs):
1313
def wrapped_func(*args, **kwargs):
14-
args_str = ", ".join(repr(arg) for arg in args)
15-
16-
return f"{self.func.__name__}({args_str})"
14+
if args:
15+
args_str = ", ".join(repr(arg) for arg in args)
16+
return f"{self.func.__name__}({args_str})"
17+
else:
18+
if self.distinct or distinct:
19+
return f"{self.func.__name__}()"
20+
else:
21+
return f"{self.func.__name__}()"
1722

1823
if distinct or self.distinct:
19-
return (
20-
wrapped_func(*args, **kwargs)
21-
if args
22-
else self.func(*args, **kwargs)
23-
)
24+
return wrapped_func(*args, **kwargs)
2425
else:
25-
2626
data = []
27-
28-
# DO NOT REMOVE THIS
2927
if test or self.test:
3028
data.append("setRNG('testRandom')")
3129

32-
data.append(
33-
wrapped_func(*args, **kwargs)
34-
if args
35-
else self.func(*args, **kwargs)
36-
)
37-
38-
# Break this section out into a stand-alone function
39-
# json_data = json.dumps(data, indent=None).replace("'", "`")
40-
# commands = json_data.encode().hex()
41-
# results = wrapper(commands)
42-
# End of section
30+
data.append(wrapped_func(*args, **kwargs))
4331

4432
return get_last_element_or_string(chain(data))
4533

js2pysecrets/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def random(*args, **kwargs):
1818

1919

2020
@JsFunction
21-
def foobar(*args, **kwargs):
21+
def setRNG(*args, **kwargs):
2222
pass # pragma: no cover
2323

2424

js2pysecrets/wrapper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def wrapper(input_data):
7979
print(
8080
"Error: Node.js is required. Please install Node.js to continue."
8181
) # pragma: no cover
82-
exit(1) # pragma: no cover
82+
# exit(1) # pragma: no cover
8383
except subprocess.CalledProcessError as e:
8484
# Print the error from the JavaScript script
8585
js_error = e.stderr.strip() # Use e.stderr instead of result.stderr

tests/test_wrapper.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from js2pysecrets.decorators import JsFunction, jsNeedless
2-
from js2pysecrets.wrapper import wrapper
2+
from js2pysecrets.wrapper import wrapper, chain
33

44
import js2pysecrets.node as node
55
import pytest
@@ -25,6 +25,11 @@ def invalidFunction(*args, **kwargs):
2525
pass
2626

2727

28+
@JsFunction
29+
def fail(*args, **kwargs):
30+
pass
31+
32+
2833
@jsNeedless
2934
def needless(*args, **kwargs):
3035
pass # pragma: no cover
@@ -34,13 +39,30 @@ def test_JsFunction():
3439
assert node.random(128) != node.random(32)
3540

3641

42+
def test_noArgs():
43+
assert node.init() == None
44+
45+
3746
def test_jsNeedless():
3847
assert not_supported(lambda: needless(33, "blue"))
3948

4049

4150
def test_Distinct():
42-
command = node.share("aabb", 6, 3, distinct=True)
43-
assert command == "share('aabb', 6, 3)"
51+
assert node.share("aabb", 6, 3, distinct=True) == "share('aabb', 6, 3)"
52+
assert node.init(distinct=True) == "init()"
53+
54+
55+
def test_Chain():
56+
data = []
57+
data.append(node.setRNG("testRandom", distinct=True))
58+
data.append(node.share("aabb", 6, 3, distinct=True))
59+
data.append(node.share("aabb", 6, 3, distinct=True))
60+
data.append(node.init(16, distinct=True))
61+
data.append(node.share("aabb", 6, 3, distinct=True))
62+
data.append(node.share("aabb", 6, 3, distinct=True))
63+
results = chain(data)
64+
assert results[1][4] == results[2][4]
65+
assert results[4][5] != results[5][5]
4466

4567

4668
def test_Randomness():
@@ -87,14 +109,14 @@ def test_CalledProcessError():
87109
assert "Invalid hex character" in str(caught_warnings[0].message)
88110

89111

90-
# @pytest.mark.filterwarnings("ignore:fail")
91-
# def test_JSONDecodeError():
92-
# with warnings.catch_warnings(record=True) as caught_warnings:
93-
# node.fail()
94-
# # Check if any warnings were raised
95-
# assert len(caught_warnings) == 1
96-
# assert issubclass(caught_warnings[0].category, Warning)
97-
# assert "error decoding JSON" in str(caught_warnings[0].message)
112+
@pytest.mark.filterwarnings("ignore:fail")
113+
def test_JSONDecodeError():
114+
with warnings.catch_warnings(record=True) as caught_warnings:
115+
fail(123)
116+
# Check if any warnings were raised
117+
assert len(caught_warnings) == 1
118+
assert issubclass(caught_warnings[0].category, Warning)
119+
assert "error decoding JSON" in str(caught_warnings[0].message)
98120

99121

100122
# @pytest.mark.filterwarnings("ignore:wrapper")

0 commit comments

Comments
 (0)