Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
"""Tests for scadfmt.formatter: one case per rule."""
import re
import pytest
from scadfmt import formatter
from scadfmt.formatter import FormatError, format_source
@pytest.mark.parametrize(
("source", "expected"),
[
# Operators and `=`
("x=1+2*3/4-5%6^7;", "x = 1 + 2 * 3 / 4 - 5 % 6 ^ 7;"),
("x=a<b&&c>=d||e!=f;", "x = a < b && c >= d || e != f;"),
("x=[5&3,5|3,~5,1<<4,16>>2];", "x = [5 & 3, 5 | 3, ~5, 1 << 4, 16 >> 2];"),
("cube(size=10,center=true);", "cube(size = 10, center = true);"),
("module m(a=1,b=[1,2]){}", "module m(a = 1, b = [1, 2]) {}"),
# Unary operators
("x=[-1,+a,!b,-(c)];", "x = [-1, +a, !b, -(c)];"),
("x=- -a;", "x = - -a;"),
("-a;", "-a;"),
("x=a- -b;", "x = a - -b;"),
("x=f(a)-b;", "x = f(a) - b;"),
("x=[for(i=v)if(c)i else -i];", "x = [for (i = v) if (c) i else -i];"),
("f=function(x)-x;", "f = function (x) -x;"),
("y=let(a=1)-a;", "y = let(a = 1) -a;"),
("z=[for(i=v)-i];", "z = [for (i = v) -i];"),
("w=assert(c)-1;", "w = assert(c) -1;"),
("x=[each-v];", "x = [each -v];"),
("x=!-a;", "x = !-a;"),
("x=+ +a;", "x = + +a;"),
("h=0xFF&0x0f;", "h = 0xFF & 0x0f;"),
# Ternary vs range colon
("x=a?b:c;", "x = a ? b : c;"),
("x=[a?b:c,0:2];", "x = [a ? b : c, 0:2];"),
("r=[ 0 : 2 : -10 ];", "r = [0:2:-10];"),
("x=a?b?c:d:e;", "x = a ? b ? c : d : e;"),
# Calls, indexing, members, keywords
("x=f (a)[0].y;", "x = f(a)[0].y;"),
("if(a)b();", "if (a) b();"),
("for(i=[0:2])b();", "for (i = [0:2]) b();"),
("intersection_for(i=v)b();", "intersection_for (i = v) b();"),
("f=function(x)x;", "f = function (x) x;"),
("x=let(a=1)assert(a)echo(a)a;", "x = let(a = 1) assert(a) echo(a) a;"),
("x=[each[1,2]];", "x = [each [1, 2]];"),
("x=g(1)(2);", "x = g(1)(2);"),
("x=m[0][1];", "x = m[0][1];"),
("x=f(a,[1,2],(3));", "x = f(a, [1, 2], (3));"),
# Modifiers
("#a();%b();*c();!d();", "#a();\n%b();\n*c();\n!d();"),
("translate(v) # cube();", "translate(v) #cube();"),
("if(a)b();else *c();", "if (a) b();\nelse *c();"),
("x=(a)*b;", "x = (a) * b;"),
("x=1;#a();", "x = 1;\n#a();"),
("module m(){x=1;#a();}", "module m() {\n x = 1;\n #a();\n}"),
("!#a();", "!#a();"),
("a*b;", "a * b;"),
("1*b;", "1 * b;"),
("module m(){*a();}", "module m() {\n *a();\n}"),
("if(a){b();}*c();", "if (a) {\n b();\n}\n*c();"),
# Braces
("module m(){a();b();}", "module m() {\n a();\n b();\n}"),
("if(a){b();}else{c();}", "if (a) {\n b();\n} else {\n c();\n}"),
("if(a){b();}else if(c){d();}", "if (a) {\n b();\n} else if (c) {\n d();\n}"),
("module m(){}", "module m() {}"),
("module m(){a();}m();", "module m() {\n a();\n}\n\nm();"),
("if(a){b();};", "if (a) {\n b();\n};"),
("module m(){a();/* c */}", "module m() {\n a();\n /* c */\n}"),
("x=[for(i=0;i<3;i=i+1)i];", "x = [for (i = 0; i < 3; i = i + 1) i];"),
("module m(){// note\na();}", "module m() { // note\n a();\n}"),
("a();// one\nb();c();// two", "a(); // one\nb();\nc(); // two"),
# Includes
("include<a/b.scad>", "include <a/b.scad>"),
("use <a.scad>\nx=1;", "use <a.scad>\n\nx = 1;"),
# Comments
("/* a */x=1;/* b */", "/* a */ x = 1;\n/* b */"),
("x=1;// c ", "x = 1; // c"),
],
)
def test_single_line_rules(source, expected):
assert format_source(source) == expected + "\n"
def test_block_indentation():
source = "module m() {\nif (a) {\nb();\n}\n}\n"
assert format_source(source) == "module m() {\n if (a) {\n b();\n }\n}\n"
def test_multi_line_brackets_indent_once_per_line():
source = "x = f([\n1,\n2\n]);\n"
assert format_source(source) == "x = f([\n 1,\n 2\n]);\n"
def test_multi_line_signature_body_stays_one_level_in():
source = "module m(a,\nb) {\nc();\n}\n"
assert format_source(source) == "module m(a,\n b) {\n c();\n}\n"
def test_module_chain_nests_per_line():
source = "translate(v)\nrotate(r)\ncube();\nnext();\n"
assert format_source(source) == "translate(v)\n rotate(r)\n cube();\nnext();\n"
def test_comment_lines_inside_a_chain_do_not_nest():
source = "translate(v)\n// line\n/* block */\ncube();\n"
assert format_source(source) == "translate(v)\n // line\n /* block */\n cube();\n"
def test_else_binds_to_the_nearest_unbraced_if():
source = "if (outer)\nif (inner)\na();\nelse\nb();\nelse\nc();\nnext();\n"
expected = "if (outer)\n if (inner)\n a();\n else\n b();\nelse\n c();\nnext();\n"
assert format_source(source) == expected
def test_else_after_a_finished_if_chain():
source = "if (a) b();\nelse if (c) d();\nelse e();\nf();\n"
assert format_source(source) == source
def test_expression_continuation_stays_one_level_in():
source = "function f(m) =\nm == 1\n? a\n: b;\n"
assert format_source(source) == "function f(m) =\n m == 1\n ? a\n : b;\n"
@pytest.mark.parametrize(
("source", "expected"),
[
("x=a\n*b;\n", "x = a\n * b;\n"),
("x = (a)\n*b;\n", "x = (a)\n * b;\n"),
("module m() {\nx = (a)\n* b;\n}\n", "module m() {\n x = (a)\n * b;\n}\n"),
("a+*b;\n", "a + * b;\n"),
],
)
def test_operator_starting_a_continuation_is_not_a_modifier(source, expected):
assert format_source(source) == expected
def test_multi_line_call_arguments_indent_once():
assert format_source("x = f(\n1\n);\n") == "x = f(\n 1\n);\n"
def test_else_if_chain_inside_unbraced_if():
source = "if (o)\nif (a)\nx();\nelse if (b)\ny();\nelse\nz();\n"
assert format_source(source) == "if (o)\n if (a)\n x();\n else if (b)\n y();\n else\n z();\n"
def test_comment_between_if_and_else_keeps_the_match():
source = "if (outer)\nif (inner)\na();\n// note\nelse\nb();\n"
assert format_source(source) == "if (outer)\n if (inner)\n a();\n// note\n else\n b();\n"
def test_chain_ending_in_block():
source = "rotate(r)\ndifference() {\na();\n}\n"
assert format_source(source) == "rotate(r)\n difference() {\n a();\n }\n"
def test_brace_on_own_line_is_not_a_continuation():
source = "module m()\n{\na();\n}\n"
assert format_source(source) == "module m()\n{\n a();\n}\n"
def test_statement_after_include_is_not_a_continuation():
assert format_source("include <a.scad>\nx = 1;\n") == "include <a.scad>\n\nx = 1;\n"
def test_imports_form_one_block_followed_by_one_blank_line():
source = "include <a.scad>\n\n\nuse <b.scad>\n// why c\n\ninclude <c.scad>\n\n\n\nx = 1;\n"
expected = "include <a.scad>\nuse <b.scad>\n// why c\ninclude <c.scad>\n\nx = 1;\n"
assert format_source(source) == expected
def test_comment_ending_the_file_after_imports_is_separated():
assert format_source("include <a.scad>\n// end\n") == "include <a.scad>\n\n// end\n"
def test_comment_after_import_block_is_separated():
source = "include <a.scad>\n// section\nx = 1;\n"
assert format_source(source) == "include <a.scad>\n\n// section\nx = 1;\n"
def test_definitions_get_one_blank_line_around_them():
source = (
"x = 1;\n// doc for f\nfunction f(x) = x;\nfunction g(x) =\n x;\n\n\nmodule m() {\n"
"module inner() {\ncube();\n}\ninner();\n}\nm();\n"
)
expected = (
"x = 1;\n\n// doc for f\nfunction f(x) = x;\n\nfunction g(x) =\n x;\n\nmodule m() {\n"
" module inner() {\n cube();\n }\n\n inner();\n}\n\nm();\n"
)
assert format_source(source) == expected
@pytest.mark.parametrize(
("source", "expected"),
[
("function f() = 1;\nx = 2;\n", "function f() = 1;\n\nx = 2;\n"),
("module m() {}\n#cube();\n", "module m() {}\n\n#cube();\n"),
("x = 1;\nmodule m\n() {}\n", "x = 1;\n\nmodule m\n () {}\n"),
("module\nm() {}\n", "module\n m() {}\n"),
("module m() {}\nx = 1;\n// end\n", "module m() {}\n\nx = 1;\n// end\n"),
],
)
def test_definition_boundaries(source, expected):
assert format_source(source) == expected
def test_definition_closing_a_block_gets_no_blank_line_before_the_brace():
source = "module m() {\nfunction f() = 1;\n}\n"
assert format_source(source) == "module m() {\n function f() = 1;\n}\n"
def test_comments_directly_above_belong_to_the_next_line():
source = (
"include <a.scad>\n// section\nx = 1;\n/**\n * Doc.\n */\nmodule m() {}\n// about n\nmodule n() {}\n"
"/* [Hidden] */\ny = 2;\n"
)
expected = (
"include <a.scad>\n\n// section\nx = 1;\n\n/**\n * Doc.\n */\nmodule m() {}\n\n// about n\n"
"module n() {}\n\n/* [Hidden] */\ny = 2;\n"
)
assert format_source(source) == expected
def test_function_literal_is_not_a_definition():
assert format_source("x = 1;\nf = function (x) x;\ny = 2;\n") == "x = 1;\nf = function (x) x;\ny = 2;\n"
def test_fmt_off_region_gets_no_added_breaks():
source = "// fmt: off\n// note\nmodule m() { a(); b(); }\nx=1; /* a\n b */\n// fmt: on\nmodule n() { c(); }\n"
expected = (
"// fmt: off\n// note\nmodule m() { a(); b(); }\nx=1; /* a\n b */\n// fmt: on\n\nmodule n() {\n c();\n}\n"
)
assert format_source(source) == expected
def test_comment_lines_follow_code_indent():
source = "module m() {\n// inside\na();\n}\n"
assert format_source(source) == "module m() {\n // inside\n a();\n}\n"
def test_trailing_comments_align_per_run():
source = "a = 1; // one\nlong_name = 2; // two\n\nb = 3; // alone\n"
expected = "a = 1; // one\nlong_name = 2; // two\n\nb = 3; // alone\n"
assert format_source(source) == expected
def test_comment_only_line_ends_alignment_run():
source = "a = 1; // x\n// break\nlong_name = 2; // y\n"
assert format_source(source) == "a = 1; // x\n// break\nlong_name = 2; // y\n"
def test_blank_lines_are_capped():
source = "\n\na = 1;\n\n\n\n\nb = 2;\nif (c) {\n\n\n\nd();\n\n\n}\nx =\n\n\n1;\n\n\n"
expected = "a = 1;\n\n\nb = 2;\nif (c) {\n\n d();\n\n}\nx =\n\n 1;\n"
assert format_source(source) == expected
def test_fmt_off_keeps_lines_verbatim():
source = "x=1;\n// fmt: off\nm = [\n 1, 0,\n\n\n\n 0, 1 ];\n// fmt: on\ny=2;\n"
expected = "x = 1;\n// fmt: off\nm = [\n 1, 0,\n\n\n\n 0, 1 ];\n// fmt: on\ny = 2;\n"
assert format_source(source) == expected
def test_block_comment_keeps_inner_lines():
source = "module m() {\n/**\n * Doc. \n */\na();\n}\n"
assert format_source(source) == "module m() {\n /**\n * Doc.\n */\n a();\n}\n"
def test_crlf_inside_a_string_is_kept():
source = 'x = "a\r\nb";\r\n'
assert format_source(source) == source
@pytest.mark.parametrize(
("source", "expected"),
[
("x=1;\r\ny=2;\r\n", "x = 1;\r\ny = 2;\r\n"),
("x=1;\ry=2;", "x = 1;\ry = 2;\r"),
("x=1;\ny=2;\r\n", "x = 1;\ny = 2;\n"),
("x=1;", "x = 1;\n"),
("X=1;\ny=2;", "X = 1;\ny = 2;\n"),
("// fmt: off\nm=[1, 2];\rn=[3, 4];\n// fmt: on\n", "// fmt: off\nm=[1, 2];\nn=[3, 4];\n// fmt: on\n"),
(
"/* a\r\n b */\r\n// fmt: off\r\nm=[1, 2];\r\n// fmt: on\r\n",
"/* a\r\n b */\r\n// fmt: off\r\nm=[1, 2];\r\n// fmt: on\r\n",
),
],
)
def test_line_endings_follow_the_first_one(source, expected):
assert format_source(source) == expected
def test_empty_input():
assert format_source("") == ""
assert format_source("\n\n") == ""
@pytest.mark.parametrize(
("source", "message"),
[
("x = @;", "unexpected character"),
("a());", "unexpected ')'"),
("a(];", "unexpected ']'"),
("module m() {", "unclosed '{'"),
("module m() { x = (", "unclosed '('"),
],
)
def test_errors(source, message):
with pytest.raises(FormatError, match=re.escape(message)):
format_source(source)
def test_output_with_different_tokens_is_rejected(monkeypatch):
monkeypatch.setattr(formatter, "_render", lambda *args: "x = 2;\n")
with pytest.raises(FormatError, match="would change the code"):
format_source("x = 1;")
4121c9fb7This file is pinned to the selected release. Download the original to open it in your design tool.