Skip to content

Commit

Permalink
add a couple golden files
Browse files Browse the repository at this point in the history
  • Loading branch information
brady.ouren committed Dec 18, 2024
1 parent 8b3ff87 commit 5736d15
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 30 deletions.
57 changes: 27 additions & 30 deletions components/clarinet-format/src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub fn format_source_exprs(
// (Tuple [(PSE)])
NativeFunctions::TupleCons => {
println!("tuple cons");
format_tuple_cons(settings, list)
format_tuple_cons(settings, list, previous_indentation)
}
NativeFunctions::ListCons => {
println!("list cons");
Expand Down Expand Up @@ -149,7 +149,6 @@ pub fn format_source_exprs(
}
}
} else {
println!("else");
format!(
"({})",
format_source_exprs(
Expand Down Expand Up @@ -446,29 +445,20 @@ fn format_match(
let indentation = &settings.indentation.to_string();
let space = format!("{}{}", previous_indentation, indentation);

acc.push_str(&display_pse(settings, &exprs[1], "", false).to_string());
// first branch. some or ok binding
acc.push_str(&format!(
"\n{}{}\n{}{}",
space,
display_pse(settings, &exprs[2], &space, false),
space,
format_source_exprs(settings, &[exprs[3].clone()], &space, None, "")
// value to match on
acc.push_str(&format_source_exprs(
settings,
&[exprs[1].clone()],
previous_indentation,
None,
"",
));
// second branch. none or err binding
if let Some(some_branch) = exprs[4].match_list() {
// branches evenly spaced
for branch in exprs[2..].iter() {
acc.push_str(&format!(
"\n{}({})",
space,
format_source_exprs(settings, some_branch, previous_indentation, None, "")
));
} else {
acc.push_str(&format!(
"\n{}{}\n{}{}",
space,
display_pse(settings, &exprs[4], &space, false),
"\n{}{}",
space,
format_source_exprs(settings, &[exprs[5].clone()], &space, None, "")
format_source_exprs(settings, &[branch.clone()], &space, None, "")
));
}
acc.push_str(&format!("\n{})", previous_indentation));
Expand Down Expand Up @@ -615,13 +605,18 @@ fn format_key_value(
));
}
}
acc.push_str(previous_indentation);
acc.push('}');
acc.to_string()
}
fn format_tuple_cons(settings: &Settings, exprs: &[PreSymbolicExpression]) -> String {
fn format_tuple_cons(
settings: &Settings,
exprs: &[PreSymbolicExpression],
previous_indentation: &str,
) -> String {
// if the kv map is defined with (tuple (c 1)) then we have to strip the
// ClarityName("tuple") out first
format_key_value(settings, &exprs[1..], "")
format_key_value(settings, &exprs[1..], previous_indentation)
}

// This should panic on most things besides atoms and values. Added this to help
Expand Down Expand Up @@ -895,6 +890,7 @@ mod tests_formatter {
)"#;
assert_eq!(result, expected);
}

#[test]
fn test_response_match() {
let src = "(match x value (ok (+ to-add value)) err-value (err err-value))";
Expand Down Expand Up @@ -948,12 +944,13 @@ mod tests_formatter {
assert_eq!(result, "(begin\n (ok true)\n)\n");
}

// #[test]
// fn test_ignore_formatting() {
// let src = ";; @format-ignore\n( begin ( ok true ))";
// let result = format_with(&String::from(src), Settings::new(Indentation::Space(4), 80));
// assert_eq!(src, result);
// }
#[test]
#[ignore]
fn test_ignore_formatting() {
let src = ";; @format-ignore\n( begin ( ok true ))";
let result = format_with(&String::from(src), Settings::new(Indentation::Space(4), 80));
assert_eq!(src, result);
}

#[test]
#[ignore]
Expand Down
32 changes: 32 additions & 0 deletions components/clarinet-format/tests/golden-intended/match-or.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;; Determines if a character is a vowel (a, e, i, o, u, and y).
(define-private (is-vowel
(char (buff 1))
)
(or
(is-eq char 0x61) ;; a
(is-eq char 0x65) ;; e
(is-eq char 0x69) ;; i
(is-eq char 0x6f) ;; o
(is-eq char 0x75) ;; u
(is-eq char 0x79) ;; y
)
)

;; pre comment
(define-private (something)
(match opt
value
(ok (handle-new-value value))
(ok 1)
)
)

(define-read-only (is-borroweable-isolated
(asset principal)
)
(match (index-of? (contract-call? pool-reserve-data get-borroweable-isolated-read) asset)
res
true
false
)
)
17 changes: 17 additions & 0 deletions components/clarinet-format/tests/golden-intended/tuple.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(define-public (set-user-reserve
(user principal)
(asset principal)
(state {
principal-borrow-balance: uint,
last-variable-borrow-cumulative-index: uint,
origination-fee: uint,
stable-borrow-rate: uint,
last-updated-block: uint,
use-as-collateral: bool
})
)
(begin
(asserts! (is-lending-pool contract-caller) ERR_UNAUTHORIZED)
(contract-call? pool-reserve-data set-user-reserve-data user asset state)
)
)
21 changes: 21 additions & 0 deletions components/clarinet-format/tests/golden/match-or.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
;; Determines if a character is a vowel (a, e, i, o, u, and y).
(define-private (is-vowel (char (buff 1)))
(or
(is-eq char 0x61) ;; a
(is-eq char 0x65) ;; e
(is-eq char 0x69) ;; i
(is-eq char 0x6f) ;; o
(is-eq char 0x75) ;; u
(is-eq char 0x79) ;; y
)
)

;; pre comment
(define-private (something)
(match opt value (ok (handle-new-value value)) (ok 1))
)

(define-read-only (is-borroweable-isolated (asset principal))
(match (index-of? (contract-call? .pool-reserve-data get-borroweable-isolated-read) asset)
res true
false))
18 changes: 18 additions & 0 deletions components/clarinet-format/tests/golden/tuple.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(define-public (set-user-reserve
(user principal)
(asset principal)
(state
(tuple
(principal-borrow-balance uint)
(last-variable-borrow-cumulative-index uint)
(origination-fee uint)
(stable-borrow-rate uint)
(last-updated-block uint)
(use-as-collateral bool)
)
))
(begin
(asserts! (is-lending-pool contract-caller) ERR_UNAUTHORIZED)
(contract-call? .pool-reserve-data set-user-reserve-data user asset state)
)
)

0 comments on commit 5736d15

Please sign in to comment.