Skip to content

Commit

Permalink
Use strings instead of symbols in #finalize specs (crystal-lang#11619)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored and rdp committed Jan 19, 2022
1 parent 66eeb7b commit e8f63fd
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 76 deletions.
10 changes: 2 additions & 8 deletions spec/std/object_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,7 @@ private class DelegatedTestObject
end

private class TestObjectWithFinalize
property key : Symbol?

def finalize
if key = self.key
State.inc(key)
end
end
include FinalizeCounter

def_clone
end
Expand Down Expand Up @@ -494,7 +488,7 @@ describe Object do

it "calls #finalize on #clone'd objects" do
obj = TestObjectWithFinalize.new
assert_finalizes(:clone) { obj.clone }
assert_finalizes("clone") { obj.clone }
end

describe "def_hash" do
Expand Down
14 changes: 3 additions & 11 deletions spec/std/openssl/ssl/context_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ require "openssl"
require "../../../support/finalize"

class OpenSSL::SSL::Context
property key : Symbol?

def finalize
if key = self.key
State.inc(key)
end

previous_def
end
include FinalizeCounter
end

describe OpenSSL::SSL::Context do
Expand Down Expand Up @@ -211,11 +203,11 @@ describe OpenSSL::SSL::Context do
{% end %}

it "calls #finalize on insecure client context" do
assert_finalizes(:insecure_client_ctx) { OpenSSL::SSL::Context::Client.insecure }
assert_finalizes("insecure_client_ctx") { OpenSSL::SSL::Context::Client.insecure }
end

it "calls #finalize on insecure server context" do
assert_finalizes(:insecure_server_ctx) { OpenSSL::SSL::Context::Server.insecure }
assert_finalizes("insecure_server_ctx") { OpenSSL::SSL::Context::Server.insecure }
end

describe ".from_hash" do
Expand Down
10 changes: 2 additions & 8 deletions spec/std/reference_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ private module ReferenceSpec
end

class TestClassWithFinalize
property key : Symbol?

def finalize
if key = self.key
State.inc(key)
end
end
include FinalizeCounter
end
end

Expand Down Expand Up @@ -144,6 +138,6 @@ describe "Reference" do

it "calls #finalize on #dup'ed objects" do
obj = ReferenceSpec::TestClassWithFinalize.new
assert_finalizes(:clone) { obj.dup }
assert_finalizes("dup") { obj.dup }
end
end
49 changes: 16 additions & 33 deletions spec/std/weak_ref_spec.cr
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
require "spec"
require "weak_ref"

private class State
@@count = {} of Symbol => Int64

def self.inc(key)
@@count[key] = @@count.fetch(key, 0i64) + 1
end

def self.count(key)
@@count.fetch(key, 0i64)
end

def self.reset
@@count.clear
end
end
require "../support/finalize"

private class Foo
def initialize(@key : Symbol)
end
include FinalizeCounter

def finalize
State.inc @key
def initialize(@key : String)
end
end

describe WeakRef do
it "should get dereferenced object" do
foo = Foo.new :foo
foo = Foo.new("foo")
ref = WeakRef.new(foo)
ref.should_not be_nil
ref.value.should be(foo)
Expand All @@ -46,37 +29,37 @@ describe WeakRef do
GC.collect
end

it "State counts released objects" do
State.reset
State.count(:foo).should eq 0
it "FinalizeState counts released objects" do
FinalizeState.reset
FinalizeState.count("foo").should eq 0
10.times do
Foo.new(:foo)
Foo.new("foo")
end
GC.collect
State.count(:foo).should be > 0
FinalizeState.count("foo").should be > 0
end

it "Referenced object should not be released" do
State.reset
FinalizeState.reset
instances = [] of Foo
State.count(:strong_foo_ref).should eq 0
FinalizeState.count("strong_foo_ref").should eq 0
10.times do
instances << Foo.new(:strong_foo_ref)
instances << Foo.new("strong_foo_ref")
end
GC.collect
State.count(:strong_foo_ref).should eq 0
FinalizeState.count("strong_foo_ref").should eq 0
end

it "Weak referenced object should be released if no other reference" do
State.reset
FinalizeState.reset
instances = [] of WeakRef(Foo)
last = nil
10.times do
last = Foo.new(:weak_foo_ref)
last = Foo.new("weak_foo_ref")
instances << WeakRef.new(last)
end
GC.collect
State.count(:weak_foo_ref).should be > 0
FinalizeState.count("weak_foo_ref").should be > 0
instances.select { |wr| wr.value.nil? }.size.should be > 0
instances[-1].value.should_not be_nil

Expand Down
12 changes: 4 additions & 8 deletions spec/std/yaml/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,12 @@ end

private class YAMLAttrWithFinalize
include YAML::Serializable
include FinalizeCounter

property value : YAML::Any

@[YAML::Field(ignore: true)]
property key : Symbol?

def finalize
if key = self.key
State.inc(key)
end
end
property key : String?
end

module YAMLAttrModule
Expand Down Expand Up @@ -900,7 +896,7 @@ describe "YAML::Serializable" do
end

it "calls #finalize" do
assert_finalizes(:yaml) { YAMLAttrWithFinalize.from_yaml("---\nvalue: 1\n") }
assert_finalizes("yaml") { YAMLAttrWithFinalize.from_yaml("---\nvalue: 1\n") }
end

describe "work with module and inheritance" do
Expand Down
32 changes: 24 additions & 8 deletions spec/support/finalize.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class State
@@count = {} of Symbol => Int64
module FinalizeState
@@count = {} of String => Int64

def self.inc(key)
def self.inc(key : String)
@@count[key] = @@count.fetch(key, 0i64) + 1
end

def self.count(key)
def self.count(key : String)
@@count.fetch(key, 0i64)
end

Expand All @@ -14,9 +14,25 @@ class State
end
end

def assert_finalizes(key)
State.reset
State.count(key).should eq(0)
module FinalizeCounter
macro included
property key : String?

def finalize
if key = @key
FinalizeState.inc(key)
end

{% if @type.has_method?(:finalize) %}
previous_def
{% end %}
end
end
end

def assert_finalizes(key : String)
FinalizeState.reset
FinalizeState.count(key).should eq(0)

10.times do
obj = yield
Expand All @@ -25,5 +41,5 @@ def assert_finalizes(key)

GC.collect

State.count(key).should be > 0
FinalizeState.count(key).should be > 0
end

0 comments on commit e8f63fd

Please sign in to comment.