fluidattacks-blends-domain 0.40.0

Blends functional core: pure AST graph to syntax graph (no_std)
Documentation
use super::build_array_expression;
use crate::ast::node_utils::AstNodeAndId;
use crate::syntax::_node_builders::core::SyntaxNodeBuildError;
use crate::syntax::_node_builders::languages::rust::tests_utils::{find_node, rust_ast_graph};
use crate::syntax::core_nodes::{
    ArrayExpression, Comment, CoreSyntaxNode, CoreSyntaxNodeWithId, NumberLiteral,
};
use crate::NodeId;
use alloc::borrow::ToOwned;

const LIST_SOURCE: &str = include_str!("array_expression.test.rs");

fn mock_node(nid: NodeId, kind: &str) -> CoreSyntaxNodeWithId {
    if kind == "block_comment" || kind == "line_comment" {
        return (
            CoreSyntaxNode::Comment(Comment {
                value: "block".to_owned(),
            }),
            nid,
        );
    }
    // Do not mock every child as a comment because the builder will remove them
    // NumberLiteral was chosen arbitrarily only to avoid mapping into a Comment
    (
        CoreSyntaxNode::NumberLiteral(NumberLiteral {
            value: kind.to_owned(),
        }),
        nid,
    )
}

fn mock_build((node, n_id): AstNodeAndId) -> CoreSyntaxNodeWithId {
    mock_node(n_id, &node.kind)
}

#[test]
fn builds_every_element_in_order() {
    let graph = rust_ast_graph(LIST_SOURCE);
    let node = find_node(&graph, "array_expression");
    let built = build_array_expression(&graph, node, |p| Ok(mock_build(p))).unwrap();
    let expected = ArrayExpression {
        elements: vec![
            mock_node(NodeId(16), "reference_expression"),
            mock_node(NodeId(23), "integer_literal"),
            mock_node(NodeId(27), "integer_literal"),
        ],
    };
    assert_eq!(built, expected);
}

#[test]
fn fails_when_an_element_cannot_be_built() {
    let graph = rust_ast_graph(LIST_SOURCE);
    let node = find_node(&graph, "array_expression");
    let error = SyntaxNodeBuildError::UnsupportedNode("integer_literal".to_owned());
    assert_eq!(
        build_array_expression(&graph, node, |_| Err(error.clone())),
        Err(error)
    );
}