Skip to content

Commit

Permalink
fix: Don't add an extra indentation for testcases (#4878)
Browse files Browse the repository at this point in the history
* fix: Don't add an extra indentation for testcases

* chore: Re-format testcases to remove unneeded indentation

* fix: Don't lose indentation on function bodies

Figured out how to remove this regression. This does increase the indentation of the arguments instead, however I would argue that this looks better correct.

* chore: make generate
  • Loading branch information
Markus Westerlind authored Jun 21, 2022
1 parent 3fb53d6 commit 3ba6a9b
Show file tree
Hide file tree
Showing 68 changed files with 2,103 additions and 2,057 deletions.
28 changes: 18 additions & 10 deletions libflux/flux-core/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@ impl<'doc> Formatter<'doc> {
affixes: vec![affixes(
docs![arena, self.format_comments(&n.lbrace), "{"],
docs![arena, arena.hardline(), "}"],
)
.nest()],
)],
body: docs![
arena,
arena.hardline(),
Expand Down Expand Up @@ -689,10 +688,17 @@ impl<'doc> Formatter<'doc> {
let comment = self.format_comments(&n.base.comments);
let prefix = docs![
arena,
"testcase ",
"testcase",
arena.line(),
self.format_identifier(&n.id),
if let Some(extends) = &n.extends {
docs![arena, " extends ", self.format_string_literal(extends),]
docs![
arena,
arena.line(),
"extends",
arena.line(),
self.format_string_literal(extends),
]
} else {
arena.nil()
},
Expand Down Expand Up @@ -867,9 +873,9 @@ impl<'doc> Formatter<'doc> {
let line = self.multiline(multiline);
let line_ = self.multiline_(multiline);

let prefix1 = self.format_comments(&n.lparen);
let lparen_comments = self.format_comments(&n.lparen);

let prefix2 = docs![
let args = docs![
arena,
"(",
docs![
Expand Down Expand Up @@ -914,8 +920,8 @@ impl<'doc> Formatter<'doc> {
let b = strip_parens(b);
HangDoc {
affixes: vec![
affixes(prefix2, arena.nil()),
affixes(prefix1, arena.nil()).nest(),
affixes(args, arena.nil()),
affixes(lparen_comments, arena.nil()).nest(),
],
body: docs![
arena,
Expand All @@ -936,8 +942,10 @@ impl<'doc> Formatter<'doc> {
ast::FunctionBody::Block(b) => {
let mut hang_doc = self.format_block(b);
hang_doc.add_prefix(arena.line());
hang_doc.affixes.push(affixes(prefix2, arena.nil()));
hang_doc.affixes.push(affixes(prefix1, arena.nil()).nest());
hang_doc.affixes.push(affixes(args, arena.nil()).nest());
hang_doc
.affixes
.push(affixes(lparen_comments, arena.nil()).nest());
hang_doc
}
}
Expand Down
116 changes: 77 additions & 39 deletions libflux/flux-core/src/formatter/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,10 +745,10 @@ fn comments2() {

assert_unchanged(
r"fn = (a) =>
//comment
{
return a
}",
//comment
{
return a
}",
);
// Comments around braces needs some work.
assert_unchanged(
Expand Down Expand Up @@ -894,7 +894,40 @@ testcase x
testcase x {
x = 1
}"#,
)
);
}

#[test]
fn testcase_indentation() {
assert_unchanged(
r#"testcase basic {
inData =
"
#datatype,string,long,dateTime:RFC3339,long,string,string,string
#group,false,false,false,false,true,true,true
#default,_result,,,,,,
,result,table,_time,_value,_field,_measurement,host
,,0,2018-05-22T19:53:26Z,100,load1,system,host.local
"
outData =
"
#datatype,string,long,double
#group,false,false,false
#default,_result,,
,result,table,newValue
,,0,100.0
"
got =
csv.from(csv: inData)
|> testing.load()
|> range(start: 2018-05-22T19:53:26Z)
|> map(fn: (r) => ({newValue: float(v: r._value)}))
want = csv.from(csv: outData)
testing.diff(want: want, got: got) |> yield()
}"#,
);
}

#[test]
Expand Down Expand Up @@ -1069,30 +1102,43 @@ fn preserve_multiline_test() {

assert_unchanged(
r#"event = (
url,
username,
password,
action="EventsRouter",
methods="add_event",
type="rpc",
tid=1,
summary="",
device="",
component="",
severity,
eventClass="",
eventClassKey="",
collector="",
message="",
) =>
url,
username,
password,
action="EventsRouter",
methods="add_event",
type="rpc",
tid=1,
summary="",
device="",
component="",
severity,
eventClass="",
eventClassKey="",
collector="",
message="",
) =>
{
body = json.encode(v: payload)
return http.post(headers: headers, url: url, data: body)
}"#,
);

//Checks that a method with <= 4 params does not get reformatted
assert_unchanged(
r#"event = (url, message="") => {
body = json.encode(v: payload)
return http.post(headers: headers, url: url, data: body)
}"#,
);
}

#[test]
fn preserve_multiline_test_2() {
//Checks that a method with >4 params gets expanded correctly
assert_format(
expect_format(
r#"selectWindow = (column="_value", fn, as, every, defaultValue, tables=<-) => {
_column = column
_as = as
Expand All @@ -1101,14 +1147,14 @@ fn preserve_multiline_test() {
|> aggregateWindow(every: every, fn: fn, column: _column, createEmpty: true)
|> fill(column: _column, value: defaultValue)
|> rename(fn: (column) => if column == _column then _as else column)}"#,
r#"selectWindow = (
column="_value",
fn,
as,
every,
defaultValue,
tables=<-,
) =>
expect![[r#"selectWindow = (
column="_value",
fn,
as,
every,
defaultValue,
tables=<-,
) =>
{
_column = column
_as = as
Expand All @@ -1118,15 +1164,7 @@ fn preserve_multiline_test() {
|> aggregateWindow(every: every, fn: fn, column: _column, createEmpty: true)
|> fill(column: _column, value: defaultValue)
|> rename(fn: (column) => if column == _column then _as else column)
}"#,
);
//Checks that a method with <= 4 params does not get reformatted
assert_unchanged(
r#"event = (url, message="") => {
body = json.encode(v: payload)
return http.post(headers: headers, url: url, data: body)
}"#,
}"#]],
);
}

Expand Down
Loading

0 comments on commit 3ba6a9b

Please sign in to comment.