spec/form_spec.rb in rasti-form-3.0.0 vs spec/form_spec.rb in rasti-form-3.1.0
- old
+ new
@@ -6,11 +6,11 @@
let(:point_subclass) { Class.new point_class }
def build_form(&block)
Class.new(Rasti::Form) do
- class_eval &block
+ class_eval(&block)
end
end
describe 'Initialization' do
@@ -139,32 +139,83 @@
end
describe 'Validations' do
+ it 'Not error' do
+ form = build_form do
+ attribute :text, Rasti::Form::Types::String
+
+ def validate
+ assert_not_error :text do
+ raise 'Invalid text' if text.nil?
+ end
+ end
+ end
+
+ proc { form.new text: 'text' }.must_be_silent
+
+ error = proc { form.new }.must_raise Rasti::Form::ValidationError
+ error.message.must_equal 'Validation error: #<Rasti::Form[]> {"text":["Invalid text"]}'
+ end
+
it 'Required' do
form = build_form do
attribute :text, Rasti::Form::Types::String
def validate
assert_present :text
end
end
+ proc { form.new text: 'text' }.must_be_silent
+
error = proc { form.new }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[]> {"text":["not present"]}'
end
+ it 'Required when cast failed' do
+ form = build_form do
+ attribute :number, Rasti::Form::Types::Integer
+
+ def validate
+ assert_present :number
+ end
+ end
+
+ proc { form.new number: 1 }.must_be_silent
+
+ error = proc { form.new number: 'text' }.must_raise Rasti::Form::ValidationError
+ error.message.must_equal "Validation error: #<Rasti::Form[]> {\"number\":[\"Invalid cast: 'text' -> Rasti::Form::Types::Integer\"]}"
+ end
+
+ it 'Not required' do
+ form = build_form do
+ attribute :text, Rasti::Form::Types::String
+
+ def validate
+ assert_not_present :text
+ end
+ end
+
+ proc { form.new }.must_be_silent
+
+ error = proc { form.new text: 'text' }.must_raise Rasti::Form::ValidationError
+ error.message.must_equal 'Validation error: #<Rasti::Form[text: "text"]> {"text":["is present"]}'
+ end
+
it 'Not empty string' do
form = build_form do
attribute :text, Rasti::Form::Types::String
def validate
assert_not_empty :text
end
end
+ proc { form.new text: 'text' }.must_be_silent
+
error = proc { form.new text: ' ' }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[text: " "]> {"text":["is empty"]}'
end
it 'Not empty array' do
@@ -174,10 +225,12 @@
def validate
assert_not_empty :array
end
end
+ proc { form.new array: ['text'] }.must_be_silent
+
error = proc { form.new array: [] }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[array: []]> {"array":["is empty"]}'
end
it 'Included in values list' do
@@ -187,10 +240,12 @@
def validate
assert_included_in :text, %w(value_1 value_2)
end
end
+ proc { form.new text: 'value_1' }.must_be_silent
+
error = proc { form.new text: 'xyz' }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[text: "xyz"]> {"text":["not included in \'value_1\', \'value_2\'"]}'
end
it 'Time range' do
@@ -201,15 +256,17 @@
def validate
assert_time_range :from, :to
end
end
- from = '2018-01-01 15:30:00 +0000'
- to = '2018-01-01 03:10:00 +0000'
+ from = Time.parse '2018-01-01 03:10:00'
+ to = Time.parse '2018-01-01 15:30:00'
- error = proc { form.new from: from, to: to }.must_raise Rasti::Form::ValidationError
- error.message.must_equal "Validation error: #<Rasti::Form[from: #{from}, to: #{to}]> {\"from\":[\"invalid time range\"]}"
+ proc { form.new from: from, to: to }.must_be_silent
+
+ error = proc { form.new from: to.to_s, to: from.to_s }.must_raise Rasti::Form::ValidationError
+ error.message.must_equal "Validation error: #<Rasti::Form[from: #{to}, to: #{from}]> {\"from\":[\"invalid time range\"]}"
end
it 'Nested form' do
form = build_form do
attribute :range, Rasti::Form::Types::Form[min: Rasti::Form::Types::Integer, max: Rasti::Form::Types::Integer]
@@ -218,10 +275,12 @@
assert_present 'range.min'
assert_present 'range.max'
end
end
+ proc { form.new range: {min: 1, max: 2} }.must_be_silent
+
error = proc { form.new }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["not present"],"range.max":["not present"]}'
end
it 'Nested validation' do
@@ -234,10 +293,12 @@
end
end
form = build_form do
attribute :range, Rasti::Form::Types::Form[range]
- end
+ end
+
+ proc { form.new range: {min: 1, max: 2} }.must_be_silent
error = proc { form.new range: {min: 2, max: 1} }.must_raise Rasti::Form::ValidationError
error.message.must_equal 'Validation error: #<Rasti::Form[]> {"range.min":["Min must be less than Max"]}'
end
\ No newline at end of file