From 5a9f8ad523069e30d4ab00ea88fe7c4661790330 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Sat, 1 Jun 2024 14:23:45 +0200 Subject: [PATCH] Add a pretty-printer for Runic.Node. --- src/Runic.jl | 7 +++++++ test/runtests.jl | 13 ++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/Runic.jl b/src/Runic.jl index ccba35c..054c1fd 100644 --- a/src/Runic.jl +++ b/src/Runic.jl @@ -39,6 +39,13 @@ function Node(node::JuliaSyntax.GreenNode) ) end +function Base.show(io::IO, node::Node) + print(io, "Node({head: {kind: ") + show(io, kind(node)) + print(io, ", flags: \"$(flags(node))\"}, span: $(span(node))})") + return nothing +end + # Defining these allow using many duck-typed methods in JuliaSyntax directly without having # to re-package a Node as a GreenNode. JuliaSyntax.head(node::Node) = head(node) diff --git a/test/runtests.jl b/test/runtests.jl index 0f58540..1a8d213 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,10 +7,11 @@ using Test: using JuliaSyntax: JuliaSyntax -@testset "Chisels" begin - # Type stability of verified_kids +@testset "Node" begin node = Runic.Node(JuliaSyntax.parseall(JuliaSyntax.GreenNode, "a = 1 + b\n")) - @test typeof(@inferred Runic.verified_kids(node)) === Vector{Runic.Node} + + # Pretty-printing + @test sprint(show, node) == "Node({head: {kind: K\"toplevel\", flags: \"0\"}, span: 10})" # JuliaSyntax duck-typing for n in (node, Runic.verified_kids(node)...,) @@ -19,6 +20,12 @@ using JuliaSyntax: @test Runic.flags(n) === JuliaSyntax.flags(n) === n.head.flags @test Runic.span(n) === JuliaSyntax.span(n) === n.span end +end + +@testset "Chisels" begin + # Type stability of verified_kids + node = Runic.Node(JuliaSyntax.parseall(JuliaSyntax.GreenNode, "a = 1 + b\n")) + @test typeof(@inferred Runic.verified_kids(node)) === Vector{Runic.Node} # replace_bytes!: insert larger io = IOBuffer(); write(io, "abc"); seek(io, 1)