Skip to content

Commit 19cca98

Browse files
authored
test ruby 2.7 and remove memory leak (#505)
1 parent 6407afb commit 19cca98

12 files changed

+73
-70
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
.config
55
.yardoc
66
Gemfile.lock
7-
gemfiles/*.lock
87
InstalledFiles
98
_yardoc
109
coverage
@@ -18,3 +17,6 @@ test/version_tmp
1817
tmp
1918
.rubocop-https*
2019
.byebug_history
20+
.idea
21+
*.iml
22+
gemfiles/*.gemfile.lock

.travis.yml

+5-23
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,14 @@ cache: bundler
33
bundler_args: --without benchmarks tools
44
rvm:
55
- ruby-head
6+
- 2.7
7+
- 2.6
68
- 2.5
79
- 2.4
8-
env:
9-
- "DRY_VALIDATION='~> 1.5.0'"
10-
- "DRY_VALIDATION='~> 1.4.0'"
11-
- "DRY_VALIDATION='~> 1.3.0'"
12-
- "DRY_VALIDATION='~> 1.2.0'"
13-
- "DRY_VALIDATION='~> 1.1.0'"
14-
- "DRY_VALIDATION='~> 1.0.0'"
15-
- "DRY_VALIDATION='~> 0.13.0'"
16-
- "DRY_VALIDATION='~> 0.12.0'"
10+
gemfile:
11+
- gemfiles/1.5.0.gemfile
12+
- gemfiles/0.13.0.gemfile
1713
matrix:
1814
fast_finish: true
1915
allow_failures:
2016
- rvm: ruby-head
21-
include:
22-
- { rvm: 2.2, env: "DRY_VALIDATION='~> 0.11.0'" }
23-
- { rvm: 2.3, env: "DRY_VALIDATION='~> 0.12.0'" }
24-
- { rvm: 2.3, env: "DRY_VALIDATION='~> 0.13.0'" }
25-
26-
27-
# maybe move this to slack in a private channel
28-
# notifications:
29-
# webhooks:
30-
# urls:
31-
# - https://webhooks.gitter.im/e/680e86d98056f2ae2fd7
32-
# on_success: change # options: [always|never|change] default: always
33-
# on_failure: always # options: [always|never|change] default: always
34-
# on_start: never # options: [always|never|change] default: always

Appraisals

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
appraise "1.5.0" do
2+
gem 'dry-monads', "1.3.5"
3+
gem 'dry-validation', '~> 1.5.0'
4+
end
5+
6+
appraise "0.13.0" do
7+
gem 'dry-validation', '~> 0.13.0'
8+
end

Gemfile

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@ source "https://rubygems.org"
22

33
gemspec
44

5-
# gem "disposable", path: "../disposable"
6-
7-
# just trying to add `dry-monads` correct version in base on dry-validation
8-
dry_v_version = ENV.fetch('DRY_VALIDATION', '~> 0.13.0').gsub("~>", "").to_f
9-
gem 'dry-monads', "~> #{[dry_v_version, 1.3].min}" if dry_v_version >= 1
10-
gem 'dry-validation', ENV.fetch('DRY_VALIDATION', '~> 0.13.0')
5+
gem "appraisal", "~> 2.2"

LICENSE.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2013 - 2016 Nick Sutterer
1+
Copyright (c) 2013 - 2020 Nick Sutterer
22

33
MIT License
44

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "bundler/gem_tasks"
22
require "rake/testtask"
33
require "rubocop/rake_task"
4+
require "dry/types/version"
45

56
task default: %i[test]
67

@@ -10,8 +11,7 @@ TEST_WITH_OLD_AND_NEW_API = %w[
1011
].freeze
1112

1213
def dry_v_test_files
13-
dry_v_version = ENV.fetch("DRY_VALIDATION", "~> 0.13.0")
14-
api = dry_v_version.gsub("~>", "").to_f >= 1.0 ? "new" : "old"
14+
api = Gem::Version.new(Dry::Types::VERSION).to_s.split('.').first.to_i >= 1 ? "new" : "old"
1515
TEST_WITH_OLD_AND_NEW_API.map { |file| "test/#{file}_#{api}_api.rb" }
1616
end
1717

gemfiles/0.13.0.gemfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "appraisal", "~> 2.2"
6+
gem "dry-validation", "~> 0.13.0"
7+
8+
gemspec path: "../"

gemfiles/1.5.0.gemfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "appraisal", "~> 2.2"
6+
gem "dry-monads", "1.3.5"
7+
gem "dry-validation", "~> 1.5.0"
8+
9+
gemspec path: "../"

lib/reform/form/dry/new_api.rb

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
::Dry::Validation.load_extensions(:hints)
2+
13
module Reform::Form::Dry
24
module NewApi
3-
class Contract < Dry::Validation::Contract
5+
6+
class Contract < ::Dry::Validation::Contract
47
end
58

69
module Validations
@@ -18,39 +21,25 @@ class Group
1821
include InputHash
1922

2023
def initialize(options = {})
21-
options ||= {}
22-
@validator = options[:schema] || Reform::Form::Dry::NewApi::Contract
23-
24-
@schema_inject_params = options[:with] || {}
24+
@validator = options.fetch(:schema, Contract)
25+
@schema_inject_params = options.fetch(:with, {})
2526
end
2627

2728
def instance_exec(&block)
28-
Dry::Validation.load_extensions(:hints)
2929
@block = block
3030
end
3131

3232
def call(form)
33-
dynamic_options = {}
34-
dynamic_options[:form] = form if @schema_inject_params[:form]
35-
inject_options = @schema_inject_params.merge(dynamic_options)
36-
37-
Dry::Schema::DSL.class_eval do
38-
inject_options.each do |key, value|
39-
define_method(key) { value }
40-
end
41-
end
42-
4333
# when passing options[:schema] the class instance is already created so we just need to call
4434
# "call"
45-
@validator = @validator.build(&@block) if @validator == Reform::Form::Dry::NewApi::Contract
46-
47-
# TODO: only pass submitted values to Schema#call?
48-
dry_result = @validator.call(input_hash(form))
49-
# dry_messages = dry_result.messages
50-
51-
return dry_result
35+
if @validator.is_a?(Class) && @validator <= ::Dry::Validation::Contract
36+
dynamic_options = {}
37+
dynamic_options[:form] = form if @schema_inject_params[:form]
38+
inject_options = @schema_inject_params.merge(dynamic_options)
39+
@validator = @validator.build(inject_options, &@block)
40+
end
5241

53-
_reform_errors = Reform::Contract::Errors.new(dry_result) # TODO: dry should be merged here.
42+
@validator.call(input_hash(form))
5443
end
5544
end
5645
end

reform.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency "bundler"
2525
spec.add_development_dependency "minitest"
2626
spec.add_development_dependency "minitest-line"
27-
spec.add_development_dependency "byebug"
27+
spec.add_development_dependency "pry-byebug"
2828
spec.add_development_dependency "multi_json"
2929
spec.add_development_dependency "rake"
3030
spec.add_development_dependency "rubocop"

test/test_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require "representable/debug"
44
require "declarative/testing"
55
require "pp"
6-
require "byebug"
6+
require "pry-byebug"
77

88
require "reform/form/dry"
99

test/validation/dry_validation_new_api.rb

+21-11
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ class AlbumForm < TestForm
7373
form.to_result.hints.must_equal(title: ["size cannot be less than 2"])
7474
form.artist.to_result.errors.must_equal(email: ["must be filled"])
7575
form.artist.to_result.messages.must_equal(email: ["must be filled"])
76-
form.artist.to_result.hints.must_equal(email: [])
76+
form.artist.to_result.hints.must_equal({})
7777
form.artist.label.to_result.errors.must_equal(location: ["must be filled"])
7878
form.artist.label.to_result.messages.must_equal(location: ["must be filled"])
79-
form.artist.label.to_result.hints.must_equal(location: [])
79+
form.artist.label.to_result.hints.must_equal({})
8080
form.songs[0].to_result.errors.must_equal({})
8181
form.songs[0].to_result.messages.must_equal({})
8282
form.songs[0].to_result.hints.must_equal({})
8383
form.songs[1].to_result.errors.must_equal(title: ["must be filled"])
8484
form.songs[1].to_result.messages.must_equal(title: ["must be filled"])
85-
form.songs[1].to_result.hints.must_equal(title: [])
85+
form.songs[1].to_result.hints.must_equal({})
8686
form.songs[1].to_result.errors(locale: :de).must_equal(title: ["muss abgefüllt sein"])
8787
# seems like dry-v when calling Dry::Schema::Result#messages locale option is ignored
8888
# started a topic in their forum https://discourse.dry-rb.org/t/dry-result-messages-ignore-locale-option/910
8989
# form.songs[1].to_result.messages(locale: :de).must_equal(title: ["muss abgefüllt sein"])
90-
form.songs[1].to_result.hints(locale: :de).must_equal(title: [])
90+
form.songs[1].to_result.hints(locale: :de).must_equal({})
9191
end
9292

9393
it "only nested property is invalid." do
@@ -196,7 +196,13 @@ class SessionForm < TestForm
196196
end
197197

198198
validation name: :dynamic_args, with: {form: true} do
199-
params { required(:color).maybe(included_in?: form.colors) }
199+
option :form
200+
params { optional(:color) }
201+
rule(:color) do
202+
if value
203+
key.failure("must be one of: #{form.colors}") unless form.colors.include? value
204+
end
205+
end
200206
end
201207

202208
def colors
@@ -208,15 +214,15 @@ def colors
208214

209215
# valid.
210216
it do
211-
form.validate(
217+
assert form.validate(
212218
username: "Helloween",
213219
email: "yep",
214220
starts_at: "01/01/2000 - 11:00",
215221
active: "true",
216222
confirm_password: "pA55w0rd"
217-
).must_equal true
218-
form.active.must_equal true
219-
form.errors.messages.inspect.must_equal "{}"
223+
)
224+
assert form.active
225+
assert_equal "{}", form.errors.messages.inspect
220226
end
221227

222228
it "invalid" do
@@ -309,8 +315,12 @@ class SessionForm < TestForm
309315
property :username
310316

311317
validation name: :default, with: {user: OpenStruct.new(name: "Nick")} do
318+
option :user
312319
params do
313-
required(:username).filled(eql?: user.name)
320+
required(:username).filled
321+
end
322+
rule(:username) do
323+
key.failure("must be equal to #{user.name}") unless user.name == value
314324
end
315325
end
316326
end
@@ -471,7 +481,7 @@ class AlbumForm < TestForm
471481
# TODO: use the same form structure as the top one and do the same test against messages, errors and hints.
472482
form.producers[0].to_result.errors.must_equal(name: ["must be filled"])
473483
form.producers[0].to_result.messages.must_equal(name: ["must be filled"])
474-
form.producers[0].to_result.hints.must_equal(name: [])
484+
form.producers[0].to_result.hints.must_equal({})
475485
end
476486

477487
# FIXME: fix the "must be filled error"

0 commit comments

Comments
 (0)