assert_generator 0.1.2 → 0.1.3

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 +33 -20
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 053f7053120b5b24d0f18500bf92830d3148f34f183bf725f46a72f25fe0044f
4
- data.tar.gz: c68c7498bd4c2155c8d059a1d29ec9b1cb7da6e0bf8d29d027dc1fd665a76dd1
3
+ metadata.gz: e6a39a9e34de7b0029e30b7f60040d90ff67291cd25dc86db0ec6e8be9012c5e
4
+ data.tar.gz: 12b754a1426058a33f4ebd6433ce4374ef1e435740aa97f8000f7db1ca85132e
5
5
  SHA512:
6
- metadata.gz: 7f75eea249badbeac33e539f6ddc3a406319adec57d1f2e64d2fe9c4a5c52456e3c2a53d7618956049e19b52a7e1837ac574eb993126824dde7238197af889d2
7
- data.tar.gz: 166520d9d86b9d56b83d4692c177c272f224bc31c2879b5b5139b59fb31fc6d4f8add9747c98a1998e92cd74020a73a418f901607a3fea5bc0bf3909253b9b7b
6
+ metadata.gz: 58a6244d7e2e978d9a1962f1a1ffd82d44e201403176fdb8e0c77347b207f63619db90bd9d36462bfe6b5fecc14238f0ae66e387ec22f24c1d07925daae74ba4
7
+ data.tar.gz: 66ba172a33d01d294da78df6c53e5f8e07dae7ee52f5767ddc8d1a7b73a39631572167db95f586a50f743b481d3d445cb2a7fc6c6ff5d39705bedc004ba50cd3
@@ -58,8 +58,14 @@ module AssertGenerator
58
58
  return self
59
59
  end
60
60
 
61
- if defined?(ActiveRecord::Base) && source.is_a?(ActiveRecord::Base)
62
- generate_asserts_ar(source, source_expr)
61
+ if source.respond_to?(:attributes)
62
+ # AR or something that quacks like it
63
+ generate_asserts_attributes(source, source_expr)
64
+ return self
65
+ end
66
+
67
+ if source.is_a?(Range)
68
+ generate_asserts_range(source, source_expr)
63
69
  return self
64
70
  end
65
71
 
@@ -72,7 +78,8 @@ module AssertGenerator
72
78
  self
73
79
  end
74
80
 
75
- def generate_asserts_ar(h, p)
81
+ # Active Record, or anything else with an attributes hash
82
+ def generate_asserts_attributes(h, p)
76
83
  h.attributes.each do |k, v|
77
84
  generate_assert_drillable_item(v, ->(meth) { "#{p}.#{meth}" }, k)
78
85
  end
@@ -95,25 +102,31 @@ module AssertGenerator
95
102
  end
96
103
  end
97
104
 
105
+ def generate_asserts_range(a, p)
106
+ generate_assert_drillable_item(a.first, ->(_x) { "#{p}.first" }, p)
107
+ generate_assert_drillable_item(a.last, ->(_x) { "#{p}.last" }, p)
108
+ end
109
+
98
110
  def generate_assert_drillable_item(v, make_accessor, *accessor_params)
99
111
  accessor = make_accessor.call(*accessor_params)
112
+
100
113
  if drillable_object(v)
101
- generate_asserts(v, accessor, nil, nil)
114
+ return generate_asserts(v, accessor, nil, nil)
115
+ end
116
+
117
+ if v.nil?
118
+ out "assert_nil #{accessor}"
119
+ elsif v.is_a?(true.class)
120
+ out "assert #{accessor}"
121
+ elsif v.is_a?(false.class)
122
+ out "refute #{accessor}"
123
+
124
+ elsif v.is_a?(DateTime) || (defined?(ActiveSupport::TimeWithZone) && v.is_a?(ActiveSupport::TimeWithZone))
125
+ generate_date_time_assert(v, accessor)
126
+ elsif v.is_a?(Date)
127
+ generate_date_assert(v, accessor)
102
128
  else
103
- if v.nil?
104
- out "assert_nil #{accessor}"
105
- elsif v.is_a?(true.class)
106
- out "assert #{accessor}"
107
- elsif v.is_a?(false.class)
108
- out "refute #{accessor}"
109
-
110
- elsif v.is_a?(DateTime) || (defined?(ActiveSupport::TimeWithZone) && v.is_a?(ActiveSupport::TimeWithZone))
111
- generate_date_time_assert(v, accessor)
112
- elsif v.is_a?(Date)
113
- generate_date_assert(v, accessor)
114
- else
115
- out "assert_equal #{v.inspect}, #{make_accessor.call(*accessor_params)}"
116
- end
129
+ out "assert_equal #{v.inspect}, #{make_accessor.call(*accessor_params)}"
117
130
  end
118
131
  end
119
132
 
@@ -122,7 +135,7 @@ module AssertGenerator
122
135
  end
123
136
 
124
137
  def drillable_object(v)
125
- v.is_a?(Enumerable) || (defined?(ActiveRecord::Base) && v.is_a?(ActiveRecord::Base))
138
+ v.is_a?(Enumerable) || v.respond_to?(:attributes)
126
139
  end
127
140
 
128
141
  def generate_date_assert(v, accessor)
@@ -131,7 +144,7 @@ module AssertGenerator
131
144
  if date_diff.to_i == 0
132
145
  out "assert_equal #{relative_dates}, #{accessor}"
133
146
  else
134
- out "assert_equal #{relative_dates} + #{date_diff}.days, #{accessor}"
147
+ out "assert_equal #{relative_dates} #{date_diff.to_i < 0 ? '-' : '+'} #{date_diff.to_i.abs}.days, #{accessor}"
135
148
  end
136
149
  else
137
150
  out "assert_equal Date.new(#{v.year}, #{v.month}, #{v.day}), #{accessor}"
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.2
4
+ version: 0.1.3
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-02-03 00:00:00.000000000 Z
11
+ date: 2020-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler