Permalink
Browse files

Check for missing specs

  • Loading branch information...
paulcsmith committed Jun 2, 2017
1 parent d8b2a38 commit 6b5afc508c2e210ff764b5f30fe0e8c267c39eca
Showing with 29 additions and 1 deletion.
  1. +13 −1 spec/habitat_spec.cr
  2. +16 −0 src/habitat.cr
@@ -1,8 +1,9 @@
require "./spec_helper"
private class FakeServer
class FakeServer
Habitat.create do
setting port : Int32
setting this_is_missing : String

This comment has been minimized.

@Sija

Sija Nov 2, 2017

Contributor

Allowing to pass required: true as an option would be nice :)

This comment has been minimized.

@paulcsmith

paulcsmith Nov 2, 2017

Member

Thanks for the suggestion! This setting is required, but all settings start out as nil.

Near the end of your program you call raise_if_missing_settings and it will raise if you forgot to set one. You can see an example in the next spec.

Does that help explain things?

This comment has been minimized.

@Sija

Sija Nov 2, 2017

Contributor

It does, yet I was thinking about making it optional - giving ability to control it.

class Foo
  Habitat.create do
    setting this_is_required : String, required: true
    setting this_is_not : Int32
  end
end

Foo.configure do
  settings.this_is_required = "bar"
end

Foo.missing_settings?.should be_false

This comment has been minimized.

@paulcsmith

paulcsmith Nov 2, 2017

Member

Oh I see. There is an issue for this about handling Nil. The way to do it is to use a nilable type. Then habitat will not check it and will allow leaving it as nil or explicitly setting it as nil

This comment has been minimized.

@Sija

Sija Nov 2, 2017

Contributor

That sounds simpler and more "native" than using options, gr8!

setting debug_errors : Bool = true
end
@@ -22,4 +23,15 @@ describe Habitat do
FakeServer.settings.debug_errors.should eq true
FakeServer.new.available_in_instance_methods.should eq 8080
end
it "can check for missing settings" do
# Because this_is_missing was never set
Habitat.missing_settings?.should be_true
FakeServer.configure do
settings.this_is_missing = "Not anymore"
end
Habitat.missing_settings?.should be_false
end
end
@@ -1,6 +1,17 @@
require "./habitat/*"
class Habitat
REQUIRED_SETTINGS = [] of String
macro finished
def self.missing_settings?
{% for setting in REQUIRED_SETTINGS %}
return true if {{ setting.id }}.nil?
{% end %}
false
end
end
macro create
include Habitat::SettingHelpers
@@ -26,6 +37,7 @@ class Habitat
macro setting(decl)
class Settings
@@{{ decl.var }} : {{decl.type}} | Nil {% if decl.value %} = {{ decl.value }}{% end %}
{% Habitat::REQUIRED_SETTINGS << "#{@type}.settings.#{decl.var}?" %}
def self.{{ decl.var }}=(value : {{ decl.type }})
@@{{ decl.var }} = value
@@ -34,6 +46,10 @@ class Habitat
def self.{{ decl.var }}
@@{{ decl.var }}.not_nil!
end
def self.{{ decl.var }}?
@@{{ decl.var }}
end
end
end
end

0 comments on commit 6b5afc5

Please sign in to comment.