Skip to content

Commit

Permalink
Refactor hierarchy printers (#10791)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Jul 24, 2021
1 parent 4a54929 commit 78b6cfb
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 118 deletions.
67 changes: 67 additions & 0 deletions spec/compiler/crystal/tools/hierarchy_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "../../../spec_helper"

describe Crystal::TextHierarchyPrinter do
it "works" do
program = semantic(%(
class Foo
end
class Bar < Foo
end
), inject_primitives: false).program

output = String.build { |io| Crystal.print_hierarchy(program, io, "ar$", "text") }
output.should eq(<<-EOS)
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Foo (4 bytes)
|
+- class Bar (4 bytes)\n
EOS
end
end

describe Crystal::JSONHierarchyPrinter do
it "works" do
program = semantic(%(
class Foo
end
class Bar < Foo
end
), inject_primitives: false).program

output = String.build { |io| Crystal.print_hierarchy(program, io, "ar$", "json") }
JSON.parse(output).should eq(JSON.parse(<<-EOS))
{
"name": "Object",
"kind": "class",
"size_in_bytes": 4,
"sub_types": [
{
"name": "Reference",
"kind": "class",
"size_in_bytes": 4,
"sub_types": [
{
"name": "Foo",
"kind": "class",
"size_in_bytes": 4,
"sub_types": [
{
"name": "Bar",
"kind": "class",
"size_in_bytes": 4,
"sub_types": []
}
]
}
]
}
]
}
EOS
end
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Crystal::Command
private def hierarchy
config, result = compile_no_codegen "tool hierarchy", hierarchy: true, top_level: true
@progress_tracker.stage("Tool (hierarchy)") do
Crystal.print_hierarchy result.program, config.hierarchy_exp, config.output_format
Crystal.print_hierarchy result.program, STDOUT, config.hierarchy_exp, config.output_format
end
end

Expand Down
Loading

0 comments on commit 78b6cfb

Please sign in to comment.