Skip to content

Commit

Permalink
Fix specs & add file/line information to constraint validator test ca…
Browse files Browse the repository at this point in the history
…se methods
  • Loading branch information
Blacksmoke16 committed Sep 25, 2020
1 parent 2a720f6 commit 8fabecf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
8 changes: 4 additions & 4 deletions spec/metadata/class_metadata_spec.cr
Expand Up @@ -8,11 +8,11 @@ describe AVD::Metadata::ClassMetadata do
it "#add_property_constraint" do
metadata = AVD::Metadata::ClassMetadata(Entity).new Entity
metadata.add_property_constraint(
AVD::Metadata::PropertyMetadata(String, Entity).new("name"),
AVD::Metadata::PropertyMetadata(Entity).new("name"),
CustomConstraint.new ""
)
metadata.add_property_constraint(
AVD::Metadata::PropertyMetadata(Int32, Entity).new("age"),
AVD::Metadata::PropertyMetadata(Entity).new("age"),
CustomConstraint.new ""
)

Expand All @@ -22,7 +22,7 @@ describe AVD::Metadata::ClassMetadata do
it "#has_property_metadata?" do
metadata = AVD::Metadata::ClassMetadata(Entity).new Entity
metadata.add_property_constraint(
AVD::Metadata::PropertyMetadata(String, Entity).new("name"),
AVD::Metadata::PropertyMetadata(Entity).new("name"),
CustomConstraint.new ""
)

Expand All @@ -33,7 +33,7 @@ describe AVD::Metadata::ClassMetadata do
it "#property_metadata" do
metadata = AVD::Metadata::ClassMetadata(Entity).new Entity

name_metadata = AVD::Metadata::PropertyMetadata(String, Entity).new("name")
name_metadata = AVD::Metadata::PropertyMetadata(Entity).new("name")

metadata.add_property_constraint(name_metadata, CustomConstraint.new(""))

Expand Down
22 changes: 18 additions & 4 deletions spec/spec_helper.cr
Expand Up @@ -34,8 +34,12 @@ struct TestClassCallback
class_setter class_callback : AVD::Constraints::Callback::CallbackProc? = nil
class_setter group : Array(String)? = ["group"]

def self.load_metadata(class_metadata : AVD::Metadata::ClassMetadataBase) : Nil
def self.validation_class_metadata : AVD::Metadata::ClassMetadata
class_metadata = AVD::Metadata::ClassMetadata(TestPropertyCallback).new self

class_metadata.add_constraint AVD::Constraints::Callback.new callback: @@class_callback, groups: @@group

class_metadata
end
end

Expand All @@ -59,12 +63,22 @@ struct TestPropertyCallback
class_setter callback : AVD::Constraints::Callback::CallbackProc?
class_setter callback2 : AVD::Constraints::Callback::CallbackProc? = nil

def self.load_metadata(class_metadata : AVD::Metadata::ClassMetadataBase) : Nil
class_metadata.add_property_constraint "name", AVD::Constraints::Callback.new callback: @@callback, groups: [@@group1]
def self.validation_class_metadata : AVD::Metadata::ClassMetadata
class_metadata = AVD::Metadata::ClassMetadata(TestPropertyCallback).new self

class_metadata.add_property_constraint(
AVD::Metadata::PropertyMetadata(TestPropertyCallback).new("name"),
AVD::Constraints::Callback.new callback: @@callback, groups: [@@group1]
)

if callback2 = @@callback2
class_metadata.add_property_constraint "age", AVD::Constraints::Callback.new callback: callback2, groups: [@@group2]
class_metadata.add_property_constraint(
AVD::Metadata::PropertyMetadata(TestPropertyCallback).new("age"),
AVD::Constraints::Callback.new callback: callback2, groups: [@@group2]
)
end

class_metadata
end

property name : String
Expand Down
59 changes: 33 additions & 26 deletions src/athena-validator.cr
Expand Up @@ -46,41 +46,48 @@ module Athena::Validator
end
end

class Address
include AVD::Validatable
# class Address
# include AVD::Validatable

def initialize(@street : String, @zip_code : String); end
# def initialize(@street : String, @zip_code : String); end

@[Assert::NotBlank]
@street : String
# @[Assert::NotBlank(message: "You cannot have an empty street")]
# @street : String

@[Assert::NotBlank]
@[Assert::Size(..5)]
@zip_code : String
end
# @[Assert::NotBlank]
# @[Assert::Size(..5)]
# @zip_code : String
# end

class Author
include AVD::Validatable
# class Author
# include AVD::Validatable

def initialize(@first_name : String, @last_name : String, @address : Address); end
# def initialize(@first_name : String, @last_name : String, @address : Address); end

@[Assert::NotBlank]
@first_name : String
# @[Assert::Callback]
# def validate(context : AVD::ExecutionContextInterface, payload : Hash(String, String)?) : Nil
# # Do stuff here
# end

@[Assert::NotBlank]
@[Assert::Size(4..)]
@last_name : String
# def self.load_metadata(class_metadata : AVD::Metadata::ClassMetadataBase) : Nil
# class_metadata.add_property_constraint "first_name", AVD::Constraints::NotBlank.new
# end

@[Assert::Valid]
@address : Address
end
# @first_name : String

address = Address.new "", "15061"
author = Author.new "Jim", "Bobb", address
# @[Assert::NotBlank]
# @[Assert::Size((4..))]
# @last_name : String

validator = AVD.validator
# @[Assert::Valid]
# @address : Address
# end

pp validator.validate author
# Object(Author).address.street:
# This value should not be blank. (code: 0d0c3254-3642-4cb0-9882-46ee5918e6e3)
# address = Address.new "", "15061"
# author = Author.new "", "Bobb", address

# validator = AVD.validator

# puts validator.validate author
# Object(Author).address.street:
# You cannot have an empty street (code: 0d0c3254-3642-4cb0-9882-46ee5918e6e3)
8 changes: 4 additions & 4 deletions src/spec.cr
Expand Up @@ -41,9 +41,9 @@ module Athena::Validator::Spec
self.constraint_class.new **args
end

def assert_no_violation : Nil
def assert_no_violation(*, file : String = __FILE__, line : Int32 = __LINE__) : Nil
unless (violation_count = self.context.violations.size).zero?
fail "0 violations expected but got #{violation_count}."
fail "0 violations expected but got #{violation_count}.", file, line
end
end

Expand Down Expand Up @@ -179,15 +179,15 @@ module Athena::Validator::Spec
self
end

def assert_violation : Nil
def assert_violation(*, file : String = __FILE__, line : Int32 = __LINE__) : Nil
expected_violations = [self.get_violation] of AVD::Violation::ConstraintViolationInterface

violations = @context.violations

violations.size.should eq 1

expected_violations.each_with_index do |violation, idx|
violation.should eq violations[idx]
violation.should eq(violations[idx]), file: file, line: line
end
end

Expand Down

0 comments on commit 8fabecf

Please sign in to comment.