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

Adding a spec to verify that settings values are only instantiates once #84

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Changes from all 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
36 changes: 36 additions & 0 deletions spec/habitat_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ class Fruit(R)
include SuperMod
end

class SpecialClient
class_property connection_count : Int32 = 0
class_property call_count : Int32 = 0

def initialize
SpecialClient.connection_count += 1
end

def call
SpecialClient.call_count += 1
"done"
end
end

class MemoizedSettings
Habitat.create do
setting connection : SpecialClient = SpecialClient.new
end
end

class ASettingForEverything
alias Dot = Int32
Habitat.create do
Expand Down Expand Up @@ -291,6 +311,22 @@ describe Habitat do
it "doesn't conflict with Habitat Settings" do
Settings::Index.settings.parent_setting.should eq true
end

it "verifies a setting can be called multiple times without creating a new object" do
SpecialClient.connection_count.should eq(1)
SpecialClient.call_count.should eq(0)
obj_id = MemoizedSettings.settings.connection.object_id

MemoizedSettings.settings.connection.call.should eq("done")
MemoizedSettings.settings.connection.object_id.should eq(obj_id)
SpecialClient.connection_count.should eq(1)
SpecialClient.call_count.should eq(1)

MemoizedSettings.settings.connection.call.should eq("done")
MemoizedSettings.settings.connection.object_id.should eq(obj_id)
SpecialClient.connection_count.should eq(1)
SpecialClient.call_count.should eq(2)
end
end

private def setup_server(port = 8080,
Expand Down
Loading