assert_generator 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/assert_generator.rb +31 -9
  3. metadata +20 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6a39a9e34de7b0029e30b7f60040d90ff67291cd25dc86db0ec6e8be9012c5e
4
- data.tar.gz: 12b754a1426058a33f4ebd6433ce4374ef1e435740aa97f8000f7db1ca85132e
3
+ metadata.gz: ccd94537b7e844452c6b6cedd7322dce6dbe867566f4e7a6cb61c6bf489b451a
4
+ data.tar.gz: d776744f117e30458eb37e417f7d88c102772fb21776c2051df539fef379a560
5
5
  SHA512:
6
- metadata.gz: 58a6244d7e2e978d9a1962f1a1ffd82d44e201403176fdb8e0c77347b207f63619db90bd9d36462bfe6b5fecc14238f0ae66e387ec22f24c1d07925daae74ba4
7
- data.tar.gz: 66ba172a33d01d294da78df6c53e5f8e07dae7ee52f5767ddc8d1a7b73a39631572167db95f586a50f743b481d3d445cb2a7fc6c6ff5d39705bedc004ba50cd3
6
+ metadata.gz: 92de98db446541f10e121582584635776dbb059a30ce51160455ad593840ffcc4745ac8a62a54c4eb5e5993e14cf7a0e381ad9a1ca95f2d5d8e88a8520a66ffd
7
+ data.tar.gz: 459757fe20cd990c11db825ae5e821e75c6172d2cde093f4dd994a94e4cb69b33a19552dc02fdd4e5852117b31a9bb07f14f008604c57df5c19530530264daa1
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'date'
4
+ require 'binding_of_caller'
4
5
 
5
6
  module AssertGenerator
6
7
  # Generate asserts based on some actual output from an object under test
@@ -19,16 +20,18 @@ module AssertGenerator
19
20
  # to access the object
20
21
  # @param relative_dates [String] adjust dates to be relative to the supplied date as a string expression
21
22
  # - use with date dependent fixtures
23
+ # @param numeric_precision [Integer] compare BigDecimal and Float using this level of precision
24
+ # in `assert_in_delta, set to nil for an `assert_equal` compare
22
25
  # @yield a block which when evaluated returns the source object
23
26
  #
24
- def self.generate_asserts(source = nil, source_expr = nil, relative_dates: nil, &block)
25
- AssertGenerator::Klass.new.send(:generate_asserts, source, source_expr, relative_dates, block)
27
+ def self.generate_asserts(source = nil, source_expr = nil, relative_dates: nil, numeric_precision: 2, &block)
28
+ AssertGenerator::Klass.new.send(:generate_asserts, source, source_expr, relative_dates, numeric_precision, block)
26
29
  end
27
30
 
28
31
  class Klass
29
- attr_accessor :relative_dates, :relative_date_today
32
+ attr_accessor :relative_dates, :relative_date_today, :numeric_precision
30
33
 
31
- def generate_asserts(source, source_expr, relative_dates, block)
34
+ def generate_asserts(source, source_expr, relative_dates, numeric_precision, block)
32
35
  if block
33
36
  unless !defined?(Rails) || Rails.env.test?
34
37
  raise 'AssertGenerator must only be used in the test context'
@@ -45,14 +48,14 @@ module AssertGenerator
45
48
  end
46
49
 
47
50
  if relative_dates
48
- raise 'relative_dates needs block syntax' unless block
49
-
50
51
  self.relative_dates = relative_dates
51
52
  # rubocop:disable Security/Eval
52
- self.relative_date_today = eval(relative_dates, block.binding)
53
+ self.relative_date_today = eval(relative_dates, block&.binding || binding.of_caller(1))
53
54
  # rubocop:enable Security/Eval
54
55
  end
55
56
 
57
+ self.numeric_precision = numeric_precision
58
+
56
59
  if source.is_a?(Hash)
57
60
  generate_asserts_hash(source, source_expr)
58
61
  return self
@@ -111,7 +114,7 @@ module AssertGenerator
111
114
  accessor = make_accessor.call(*accessor_params)
112
115
 
113
116
  if drillable_object(v)
114
- return generate_asserts(v, accessor, nil, nil)
117
+ return generate_asserts(v, accessor, relative_dates, numeric_precision, nil)
115
118
  end
116
119
 
117
120
  if v.nil?
@@ -121,10 +124,12 @@ module AssertGenerator
121
124
  elsif v.is_a?(false.class)
122
125
  out "refute #{accessor}"
123
126
 
124
- elsif v.is_a?(DateTime) || (defined?(ActiveSupport::TimeWithZone) && v.is_a?(ActiveSupport::TimeWithZone))
127
+ elsif is_timey?(v)
125
128
  generate_date_time_assert(v, accessor)
126
129
  elsif v.is_a?(Date)
127
130
  generate_date_assert(v, accessor)
131
+ elsif v.is_a?(Float) || v.is_a?(BigDecimal)
132
+ generate_numeric_assert(v, accessor)
128
133
  else
129
134
  out "assert_equal #{v.inspect}, #{make_accessor.call(*accessor_params)}"
130
135
  end
@@ -155,5 +160,22 @@ module AssertGenerator
155
160
  out "assert_equal DateTime.new(#{v.year}, #{v.month}, #{v.day}, " \
156
161
  + "#{v.hour}, #{v.min}, #{v.sec}, '#{v.zone}'), #{accessor}"
157
162
  end
163
+
164
+ def generate_numeric_assert(v, accessor)
165
+ if numeric_precision
166
+ delta = 10.0 ** -numeric_precision
167
+ assert_code = "assert_in_delta #{v.to_f.round(numeric_precision)}, #{accessor}, #{delta}"
168
+ else
169
+ assert_code = "assert_equal #{v}, #{accessor}"
170
+ end
171
+
172
+ out assert_code
173
+ end
174
+
175
+ private
176
+
177
+ def is_timey?(v)
178
+ v.is_a?(DateTime) || (defined?(ActiveSupport::TimeWithZone) && v.is_a?(ActiveSupport::TimeWithZone))
179
+ end
158
180
  end
159
181
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Parratt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-27 00:00:00.000000000 Z
11
+ date: 2020-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '10.0'
61
+ version: '13.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '10.0'
68
+ version: '13.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: shoulda
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '3.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: binding_of_caller
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement