Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generic types in crystal tool hierarchy #13715

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 149 additions & 56 deletions spec/compiler/crystal/tools/hierarchy_spec.cr
Original file line number Diff line number Diff line change
@@ -1,87 +1,180 @@
require "../../../spec_helper"

private def assert_text_hierarchy(source, filter, expected, *, file = __FILE__, line = __LINE__)
program = semantic(source).program
output = String.build { |io| Crystal.print_hierarchy(program, io, filter, "text") }
output.should eq(expected), file: file, line: line
end

private def assert_json_hierarchy(source, filter, expected, *, file = __FILE__, line = __LINE__)
program = semantic(source).program
output = String.build { |io| Crystal.print_hierarchy(program, io, filter, "json") }
JSON.parse(output).should eq(JSON.parse(expected)), file: file, line: line
end

describe Crystal::TextHierarchyPrinter do
it "works" do
program = semantic(<<-CRYSTAL).program
assert_text_hierarchy <<-CRYSTAL, "ar$", <<-EOS
class Foo
end

class Bar < Foo
end
CRYSTAL

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
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Foo (4 bytes)
|
+- class Bar (4 bytes)\n
EOS
end

it "shows correct size for Bool member" do
program = semantic(<<-CRYSTAL).program
assert_text_hierarchy <<-CRYSTAL, "Foo", <<-EOS
struct Foo
@x = true
end
CRYSTAL
- class Object (4 bytes)
|
+- struct Value (0 bytes)
|
+- struct Struct (0 bytes)
|
+- struct Foo (1 bytes)
@x : Bool (1 bytes)\n
EOS
end

it "shows correct size for members with bound types" do
assert_text_hierarchy <<-CRYSTAL, "Foo", <<-EOS
struct Bar1(T)
@x = uninitialized T
end

class Bar2(T)
@x = uninitialized T
end

module Bar3(T)
struct I(T)
include Bar3(T)

@x = uninitialized T
end
end

module Bar4(T)
class I(T)
include Bar3(T)

output = String.build { |io| Crystal.print_hierarchy(program, io, "Foo", "text") }
output.should eq(<<-EOS)
- class Object (4 bytes)
|
+- struct Value (0 bytes)
|
+- struct Struct (0 bytes)
|
+- struct Foo (1 bytes)
@x : Bool (1 bytes)\n
EOS
@x = uninitialized T
end
end

class Foo(T)
@a = uninitialized T*
@b = uninitialized T
@c = uninitialized T[4]
@d = uninitialized Int32[T]
@e = uninitialized T ->
@f = uninitialized T?
@g = uninitialized {T}
@h = uninitialized {x: T}
@i = uninitialized Bar1(T)
@j = uninitialized Bar2(T)
@k = uninitialized Bar3(T)
@l = uninitialized Bar4(T)
end
CRYSTAL
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Foo(T)
@a : Pointer(T) ( 8 bytes)
@b : T
@c : StaticArray(T, 4)
@d : StaticArray(Int32, T)
@e : Proc(T, Nil) (16 bytes)
@f : (T | Nil)
@g : Tuple(T)
@h : NamedTuple(x: T)
@i : Bar1(T)
@j : Bar2(T) ( 8 bytes)
@k : Bar3(T)
@l : Bar4(T) ( 8 bytes)\n
EOS
end

it "shows correct total size of generic class if known" do
assert_text_hierarchy <<-CRYSTAL, "Foo", <<-EOS
class Bar1(T)
@x = uninitialized T
end

class Bar2(T)
@x = uninitialized T
end

class Foo(T)
@a = uninitialized T*
@b : Bar1(T) | Bar2(T)?
@c = uninitialized T*[6]
@d = uninitialized Int64
end
CRYSTAL
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Foo(T) (80 bytes)
@a : Pointer(T) ( 8 bytes)
@b : (Bar1(T) | Bar2(T) | Nil) ( 8 bytes)
@c : StaticArray(Pointer(T), 6) (48 bytes)
@d : Int64 ( 8 bytes)\n
EOS
end
end

describe Crystal::JSONHierarchyPrinter do
it "works" do
program = semantic(<<-CRYSTAL).program
assert_json_hierarchy <<-CRYSTAL, "ar$", <<-JSON
class Foo
end

class Bar < Foo
end
CRYSTAL

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
{
"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": []
}
]
}
]
}
]
}
JSON
end
end
Loading
Loading