Pass format tests
This commit is contained in:
parent
7d7b96d76f
commit
f89e94cc33
@ -1,6 +0,0 @@
|
||||
const fs = require('node:fs');
|
||||
const data = fs.readFileSync("examples/assets/jq_data.json");
|
||||
const items = JSON.parse(data);
|
||||
const output = items.map((item) => item:commit:committer:name);
|
||||
|
||||
console.log(output)
|
@ -4,7 +4,7 @@ use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tree_sitter::Node;
|
||||
|
||||
use crate::{AbstractTree, Error, Expression, Format, Map, Result, Statement, Type, Value};
|
||||
use crate::{AbstractTree, Error, Format, Map, Result, Statement, Type, Value};
|
||||
|
||||
/// Abstract representation of a block.
|
||||
///
|
||||
@ -136,26 +136,12 @@ impl Format for Block {
|
||||
output.push_str("{\n");
|
||||
}
|
||||
|
||||
let mut previous: Option<&Statement> = None;
|
||||
|
||||
for next_statement in &self.statements {
|
||||
if let Some(previous_statement) = &previous {
|
||||
match previous_statement {
|
||||
Statement::While(_) | Statement::Expression(Expression::FunctionCall(_)) => {
|
||||
for (index, statement) in self.statements.iter().enumerate() {
|
||||
if index > 0 {
|
||||
output.push('\n');
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
match next_statement {
|
||||
Statement::Block(_) | Statement::While(_) => output.push('\n'),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
next_statement.format(output, indent_level + 1);
|
||||
|
||||
previous = Some(next_statement);
|
||||
statement.format(output, indent_level + 1);
|
||||
}
|
||||
|
||||
output.push('\n');
|
||||
|
@ -124,7 +124,7 @@ impl AbstractTree for Root {
|
||||
impl Format for Root {
|
||||
fn format(&self, output: &mut String, indent_level: u8) {
|
||||
for (index, statement) in self.statements.iter().enumerate() {
|
||||
if index > 1 {
|
||||
if index > 0 {
|
||||
output.push('\n');
|
||||
}
|
||||
statement.format(output, indent_level);
|
||||
|
@ -332,24 +332,27 @@ impl Format for ValueNode {
|
||||
}
|
||||
}
|
||||
ValueNode::Map(nodes) => {
|
||||
output.push('{');
|
||||
output.push_str("{\n");
|
||||
|
||||
for (key, (statement, type_option)) in nodes {
|
||||
if let Some(r#type) = type_option {
|
||||
output.push_str(" ");
|
||||
ValueNode::indent(output, indent_level + 1);
|
||||
output.push_str(key);
|
||||
output.push_str(" <");
|
||||
r#type.format(output, indent_level);
|
||||
r#type.format(output, 0);
|
||||
output.push_str("> = ");
|
||||
statement.format(output, indent_level);
|
||||
statement.format(output, 0);
|
||||
} else {
|
||||
output.push_str(" ");
|
||||
ValueNode::indent(output, indent_level + 1);
|
||||
output.push_str(key);
|
||||
output.push_str(" = ");
|
||||
statement.format(output, indent_level);
|
||||
}
|
||||
statement.format(output, 0);
|
||||
}
|
||||
|
||||
output.push('\n');
|
||||
}
|
||||
|
||||
ValueNode::indent(output, indent_level);
|
||||
output.push('}');
|
||||
}
|
||||
ValueNode::BuiltInValue(built_in_value) => built_in_value.format(output, indent_level),
|
||||
|
@ -44,8 +44,6 @@ impl Format for Function {
|
||||
Function::BuiltIn(built_in_function) => built_in_function.format(output, indent_level),
|
||||
Function::ContextDefined(function_node) => function_node.format(output, indent_level),
|
||||
}
|
||||
|
||||
output.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,14 +6,15 @@ fn format_simple_program() {
|
||||
|
||||
interpreter.run("x=1").unwrap();
|
||||
|
||||
assert_eq!(interpreter.format(), "x = 1");
|
||||
assert_eq!(interpreter.format(), "x = 1\n");
|
||||
}
|
||||
|
||||
const FORMATTED_BLOCK: &str = "{
|
||||
1
|
||||
2
|
||||
3
|
||||
}";
|
||||
}
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn format_block() {
|
||||
@ -24,24 +25,27 @@ fn format_block() {
|
||||
assert_eq!(FORMATTED_BLOCK, interpreter.format());
|
||||
}
|
||||
|
||||
const FORMATTED_NESTED_BLOCK: &str = "{
|
||||
const FORMATTED_MAP: &str = "{
|
||||
{
|
||||
x = 1
|
||||
y <int> = 2
|
||||
}
|
||||
}";
|
||||
}
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn format_nested_block() {
|
||||
fn format_map() {
|
||||
let mut interpreter = Interpreter::new(Map::new());
|
||||
|
||||
interpreter.run("{{x=1}}").unwrap();
|
||||
interpreter.run("{{x=1 y <int> = 2}}").unwrap();
|
||||
|
||||
assert_eq!(FORMATTED_NESTED_BLOCK, interpreter.format());
|
||||
assert_eq!(FORMATTED_MAP, interpreter.format());
|
||||
}
|
||||
|
||||
const FORMATTED_FUNCTION: &str = "(x <int>) <num> {
|
||||
x / 2
|
||||
}";
|
||||
}
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn format_function() {
|
||||
|
Loading…
Reference in New Issue
Block a user