Write formatting tests; Improve formatting output
This commit is contained in:
parent
a52b17930e
commit
731bf1cb98
@ -134,15 +134,15 @@ impl AbstractTree for Block {
|
|||||||
impl Display for Block {
|
impl Display for Block {
|
||||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
if self.is_async {
|
if self.is_async {
|
||||||
write!(f, "async {{")?;
|
writeln!(f, "async {{")?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{{")?;
|
writeln!(f, "{{")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
for statement in &self.statements {
|
for statement in &self.statements {
|
||||||
write!(f, " {statement}")?;
|
writeln!(f, " {statement}")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(f, "}}")
|
writeln!(f, "}}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::Node;
|
use tree_sitter::Node;
|
||||||
|
|
||||||
@ -143,3 +145,32 @@ impl AbstractTree for FunctionNode {
|
|||||||
Ok(self.r#type().clone())
|
Ok(self.r#type().clone())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for FunctionNode {
|
||||||
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||||
|
let FunctionNode {
|
||||||
|
parameters,
|
||||||
|
body,
|
||||||
|
r#type,
|
||||||
|
..
|
||||||
|
} = self;
|
||||||
|
|
||||||
|
let (parameter_types, return_type) = if let Type::Function {
|
||||||
|
parameter_types,
|
||||||
|
return_type,
|
||||||
|
} = r#type
|
||||||
|
{
|
||||||
|
(parameter_types, return_type)
|
||||||
|
} else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
|
||||||
|
write!(f, "(")?;
|
||||||
|
|
||||||
|
for (identifier, r#type) in parameters.iter().zip(parameter_types.iter()) {
|
||||||
|
write!(f, "{identifier} <{}>", r#type)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(f, ") <{return_type}> {body}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -96,7 +96,7 @@ impl Display for MathOperator {
|
|||||||
MathOperator::Add => write!(f, "+"),
|
MathOperator::Add => write!(f, "+"),
|
||||||
MathOperator::Subtract => write!(f, "-"),
|
MathOperator::Subtract => write!(f, "-"),
|
||||||
MathOperator::Multiply => write!(f, "*"),
|
MathOperator::Multiply => write!(f, "*"),
|
||||||
MathOperator::Divide => write!(f, "\\"),
|
MathOperator::Divide => write!(f, "/"),
|
||||||
MathOperator::Modulo => write!(f, "%"),
|
MathOperator::Modulo => write!(f, "%"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,8 +15,8 @@ impl Display for Function {
|
|||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Function::BuiltIn(built_in_function) => write!(f, "{}", built_in_function.r#type()),
|
Function::BuiltIn(built_in_function) => write!(f, "{}", built_in_function.r#type()),
|
||||||
Function::ContextDefined(context_defined_function) => {
|
Function::ContextDefined(function_node) => {
|
||||||
write!(f, "{}", context_defined_function.r#type())
|
write!(f, "{}", function_node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
40
tests/format.rs
Normal file
40
tests/format.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
use dust_lang::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn format_simple_program() {
|
||||||
|
let mut interpreter = Interpreter::new(Map::new());
|
||||||
|
|
||||||
|
interpreter.run("x=1").unwrap();
|
||||||
|
|
||||||
|
assert_eq!(interpreter.format(), "x = 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
const FORMATTED_BLOCK: &str = "{
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn format_block() {
|
||||||
|
let mut interpreter = Interpreter::new(Map::new());
|
||||||
|
|
||||||
|
interpreter.run("{1 2 3}").unwrap();
|
||||||
|
|
||||||
|
assert_eq!(FORMATTED_BLOCK, interpreter.format());
|
||||||
|
}
|
||||||
|
|
||||||
|
const FORMATTED_FUNCTION: &str = "(x <int>) <num> {
|
||||||
|
x / 2
|
||||||
|
}
|
||||||
|
";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn format_function() {
|
||||||
|
let mut interpreter = Interpreter::new(Map::new());
|
||||||
|
|
||||||
|
interpreter.run("( x< int > )<num>{x/2}").unwrap();
|
||||||
|
|
||||||
|
assert_eq!(FORMATTED_FUNCTION, interpreter.format());
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user