Skip to content

Commit 37de5a6

Browse files
authored
Fix where (str seq) was printing seq string items without " (#892)
Hi, could you please consider patch to have the `str` function on seq string items surrounding them with quotation marks. It fixes #891 The fix simply resets the `human_readable` kw argument for inner elements to False for seqs and maps. I have included a `test_str` test in `lrepr_test.py` to test the same. This is based on `test_lstr` above it. It appears to me as if `test_lstr` is inaccurately named. Despite being labeled `lstr`, it employes `pr-str` which differs from invoking `lstr` directly with its arguments, Therefore, I find naming it `test_lstr` a little misleading; it rather seems as to be only capturing the output of `pr`. I could be wrong though 😅 Thanks --------- Co-authored-by: ikappaki <[email protected]>
1 parent 3ab59a2 commit 37de5a6

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
* Fix a bug where the compiler would throw an exception partially macroexpanding forms with `recur` forms provided as arguments (#856)
3333
* Fix a bug where the original `(var ...)` form is not retained during analysis, causing it to be lost in calls to `macroexpand` (#888)
3434
* Fix issue with the reader var macro failing in syntax quote when unquoting a symbol, e.g. `(#'~symbol) (#889)
35+
* Fix issue where `(str seq)` was printing seq string items without quotation marks (#891)
3536

3637
## [v0.1.0b1]
3738
### Added

src/basilisp/lang/obj.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,12 @@ def map_lrepr(
9797

9898
kwargs = _process_kwargs(**kwargs)
9999

100+
kw_items = kwargs.copy()
101+
kw_items["human_readable"] = False
102+
100103
def entry_reprs():
101104
for k, v in entries():
102-
yield f"{lrepr(k, **kwargs)} {lrepr(v, **kwargs)}"
105+
yield f"{lrepr(k, **kw_items)} {lrepr(v, **kw_items)}"
103106

104107
trailer = []
105108
print_dup = kwargs["print_dup"]
@@ -148,7 +151,9 @@ def seq_lrepr(
148151
else:
149152
items = iterable # type: ignore
150153

151-
items = list(map(lambda o: lrepr(o, **kwargs), items))
154+
kw_items = kwargs.copy()
155+
kw_items["human_readable"] = False
156+
items = list(map(lambda o: lrepr(o, **kw_items), items))
152157
seq_lrepr = PRINT_SEPARATOR.join(items + trailer)
153158

154159
print_meta = kwargs["print_meta"]

tests/basilisp/cli_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def test_version(run_cli):
343343
"args,ret",
344344
[
345345
([], b"nil\n"),
346-
(["1", "hi", "yes"], b"[1 hi yes]\n"),
346+
(["1", "hi", "yes"], b'["1" "hi" "yes"]\n'),
347347
],
348348
)
349349
def test_run_script(tmp_path: pathlib.Path, args: List[str], ret: bytes):

tests/basilisp/importer_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ def test_execute_module_correctly(
440440
code = importer.BasilispImporter().get_code("package.module")
441441
exec(code)
442442
captured = capsys.readouterr()
443-
assert captured.out == "package.module\n[1 2 3]\n"
443+
assert captured.out == 'package.module\n["1" "2" "3"]\n'
444444

445445

446446
@pytest.fixture
@@ -475,7 +475,7 @@ def bootstrap_file() -> pathlib.Path:
475475
"args,ret",
476476
[
477477
([], b"package.test-run-ns-as-pymodule\nnil\n"),
478-
(["1", "hi", "yes"], b"package.test-run-ns-as-pymodule\n[1 hi yes]\n"),
478+
(["1", "hi", "yes"], b'package.test-run-ns-as-pymodule\n["1" "hi" "yes"]\n'),
479479
],
480480
)
481481
def test_run_namespace_as_python_module(

tests/basilisp/lrepr_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ def test_print_readably(lcompile: CompileFn):
178178
r'#b "\x7fELF\x01\x01\x01\x00"',
179179
r'(pr-str #b "\x7f\x45\x4c\x46\x01\x01\x01\x00")',
180180
),
181+
(
182+
'#uuid "81f35603-0408-4b3d-bbc0-462e3702747f"',
183+
'(pr-str #uuid "81f35603-0408-4b3d-bbc0-462e3702747f")',
184+
),
181185
('#"\\s"', '(pr-str #"\\s")'),
182186
(
183187
'#inst "2018-11-28T12:43:25.477000+00:00"',
@@ -295,3 +299,55 @@ def test_lrepr_round_trip_special_cases(lcompile: CompileFn):
295299
)
296300
def test_lstr(lcompile: CompileFn, s: str, code: str):
297301
assert s == lcompile(code)
302+
303+
304+
@pytest.mark.parametrize(
305+
"s,code",
306+
[
307+
("true", "(str true)"),
308+
("false", "(str false)"),
309+
("", "(str nil)"),
310+
("4J", "(str 4J)"),
311+
("37.8J", "(str 37.8J)"),
312+
("37.8J", "(str 37.8J)"),
313+
("8837", "(str 8837)"),
314+
("0.64", "(str 0.64)"),
315+
("3.14", "(str 3.14M)"),
316+
("22/7", "(str 22/7)"),
317+
("##NaN", "(str ##NaN)"),
318+
("##Inf", "(str ##Inf)"),
319+
("##-Inf", "(str ##-Inf)"),
320+
("hi", '(str "hi")'),
321+
("Hello\nworld!", '(str "Hello\nworld!")'),
322+
('#b ""', '(str #b "")'),
323+
(
324+
r'#b "\x7fELF\x01\x01\x01\x00"',
325+
r'(str #b "\x7f\x45\x4c\x46\x01\x01\x01\x00")',
326+
),
327+
(
328+
"81f35603-0408-4b3d-bbc0-462e3702747f",
329+
'(str #uuid "81f35603-0408-4b3d-bbc0-462e3702747f")',
330+
),
331+
('#"\\s"', '(str #"\\s")'),
332+
(
333+
'#inst "2018-11-28T12:43:25.477000+00:00"',
334+
'(str #inst "2018-11-28T12:43:25.477-00:00")',
335+
),
336+
('#py ["a" 1 false]', '(str #py ["a" 1 false])'),
337+
('#py ("a" 1 false)', '(str #py ("a" 1 false))'),
338+
('#py {"a" "b"}', '(str #py {"a" "b"})'),
339+
("#py #{}", "(str #py #{})"),
340+
("abc", '(str "abc")'),
341+
("false", "(str false)"),
342+
("123", "(str 123)"),
343+
('[:ab 2 "xyz"]', '(str [:ab 2 "xyz"])'),
344+
('#{"xyz"}', '(str #{"xyz"})'),
345+
('{"a" "b"}', '(str {"a" "b"})'),
346+
(
347+
'{"a" ["b" :c 1 false ("x") {"y" "z"}]}',
348+
'(str {"a" ["b" :c 1 false (seq ["x"]) {"y" "z"}]})',
349+
),
350+
],
351+
)
352+
def test_str(lcompile: CompileFn, s: str, code: str):
353+
assert s == lcompile(code)

0 commit comments

Comments
 (0)