factory_strategist 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72156c528e7c26354b741bcd6e9774a9115b55098fb66b4cbcab6df0395b3235
4
- data.tar.gz: e76bb500a3e4ee0828a1347f6282262ffe39a38e8ffe40e9b42596c38116736c
3
+ metadata.gz: d95eb767d76d8721c3b79c5bc8540d6c1bfc1e605d8a89605892005538fb68a9
4
+ data.tar.gz: 40d5e549acf43c3479576ba3f9c4845428699d94c33bdbbfda4a14fdbb1b2ca7
5
5
  SHA512:
6
- metadata.gz: 50e11e52cfbf2337ffba95aa6287ac0c32067a77476802ec0ec6cbe98035b443acb6fbcbfb311a658100ea71392bd19c65cdbdcf8693b6e6b196f7612bf4ac04
7
- data.tar.gz: 6d7ba24c1bc541e824d66f588a73ef295ea46506b433ff3363f06d5c7d239cbd382a189386d3729f7ca13e7b4d0675c4f29fb206dc914a682713788676f8d452
6
+ metadata.gz: db525a1cc9609c04e6dd60b8e5279d607b9737f5f34110650eabb0c3c57c8c2f447026f3bed650cdbf135be128a9740ad4ba26796008d6415addf0afa60a08d5
7
+ data.tar.gz: cf947466ada2d3f73bccdd0c3adb6ec07a6729be057425e2a972cb4fe63c354300b66e1f9fbdff159d31ca1c80ce6c039f254cdb85adcb0146736575acf146ee
data/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.1.0] - 2021-08-22
3
+ ## [0.5.0] - 2021-10-22
4
4
 
5
- - Initial release
5
+ ### Changed
6
+
7
+ - `build` now takes precedence over `build_stubbed`
8
+
9
+ ## [0.4.0] - 2021-10-20
10
+
11
+ ### Changed
12
+
13
+ - Rewrite `create` to `build` or `build_stubbed` and create new example block
14
+
15
+ ## [0.3.0] - 2021-10-03
16
+
17
+ - Rewrite main codes to make them simple(no specs!)
18
+
19
+ ## [0.2.0] - 2021-09-26
20
+
21
+ - Add environment variable: `FATORYSTRATEGIST` to apply configuration this gem expands
22
+
23
+ ## [0.1.0] - 2021-09-24
24
+
25
+ - Verify if called `FactoryBot.create` is replacable to `FactoryBot.build_stubbed` by alias
data/Gemfile.lock CHANGED
@@ -1,24 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_strategist (0.1.0)
4
+ factory_strategist (0.5.0)
5
+ factory_bot
6
+ rspec
5
7
 
6
8
  GEM
7
9
  remote: https://rubygems.org/
8
10
  specs:
9
- actionpack (6.1.4.1)
10
- actionview (= 6.1.4.1)
11
- activesupport (= 6.1.4.1)
12
- rack (~> 2.0, >= 2.0.9)
13
- rack-test (>= 0.6.3)
14
- rails-dom-testing (~> 2.0)
15
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
16
- actionview (6.1.4.1)
17
- activesupport (= 6.1.4.1)
18
- builder (~> 3.1)
19
- erubi (~> 1.4)
20
- rails-dom-testing (~> 2.0)
21
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
22
11
  activesupport (6.1.4.1)
23
12
  concurrent-ruby (~> 1.0, >= 1.0.2)
24
13
  i18n (>= 1.6, < 2)
@@ -26,42 +15,16 @@ GEM
26
15
  tzinfo (~> 2.0)
27
16
  zeitwerk (~> 2.3)
28
17
  ast (2.4.2)
29
- builder (3.2.4)
30
18
  concurrent-ruby (1.1.9)
31
- crass (1.0.6)
32
19
  diff-lcs (1.4.4)
33
- erubi (1.10.0)
34
20
  factory_bot (6.2.0)
35
21
  activesupport (>= 5.0.0)
36
22
  i18n (1.8.10)
37
23
  concurrent-ruby (~> 1.0)
38
- loofah (2.12.0)
39
- crass (~> 1.0.2)
40
- nokogiri (>= 1.5.9)
41
- method_source (1.0.0)
42
24
  minitest (5.14.4)
43
- nokogiri (1.12.4-x86_64-darwin)
44
- racc (~> 1.4)
45
- nokogiri (1.12.4-x86_64-linux)
46
- racc (~> 1.4)
47
25
  parallel (1.20.1)
48
26
  parser (3.0.2.0)
49
27
  ast (~> 2.4.1)
50
- racc (1.5.2)
51
- rack (2.2.3)
52
- rack-test (1.1.0)
53
- rack (>= 1.0, < 3)
54
- rails-dom-testing (2.0.3)
55
- activesupport (>= 4.2.0)
56
- nokogiri (>= 1.6)
57
- rails-html-sanitizer (1.4.2)
58
- loofah (~> 2.3)
59
- railties (6.1.4.1)
60
- actionpack (= 6.1.4.1)
61
- activesupport (= 6.1.4.1)
62
- method_source
63
- rake (>= 0.13)
64
- thor (~> 1.0)
65
28
  rainbow (3.0.0)
66
29
  rake (13.0.6)
67
30
  regexp_parser (2.1.1)
@@ -78,14 +41,6 @@ GEM
78
41
  rspec-mocks (3.10.2)
79
42
  diff-lcs (>= 1.2.0, < 2.0)
80
43
  rspec-support (~> 3.10.0)
81
- rspec-rails (5.0.2)
82
- actionpack (>= 5.2)
83
- activesupport (>= 5.2)
84
- railties (>= 5.2)
85
- rspec-core (~> 3.10)
86
- rspec-expectations (~> 3.10)
87
- rspec-mocks (~> 3.10)
88
- rspec-support (~> 3.10)
89
44
  rspec-support (3.10.2)
90
45
  rubocop (1.20.0)
91
46
  parallel (~> 1.10)
@@ -104,25 +59,23 @@ GEM
104
59
  rubocop (~> 1.0)
105
60
  rubocop-ast (>= 1.1.0)
106
61
  ruby-progressbar (1.11.0)
107
- thor (1.1.0)
108
62
  tzinfo (2.0.4)
109
63
  concurrent-ruby (~> 1.0)
110
64
  unicode-display_width (2.0.0)
111
- zeitwerk (2.4.2)
65
+ zeitwerk (2.5.1)
112
66
 
113
67
  PLATFORMS
68
+ x86_64-darwin-19
114
69
  x86_64-darwin-20
115
70
  x86_64-linux
116
71
 
117
72
  DEPENDENCIES
118
- factory_bot
119
73
  factory_strategist!
120
74
  rake (~> 13.0)
121
75
  rspec (~> 3.0)
122
- rspec-rails
123
76
  rubocop (~> 1.7)
124
77
  rubocop-rake
125
78
  rubocop-rspec
126
79
 
127
80
  BUNDLED WITH
128
- 2.2.26
81
+ 2.2.28
data/README.md CHANGED
@@ -21,16 +21,6 @@ Or install it yourself as:
21
21
 
22
22
  $ gem install factory_strategist
23
23
 
24
- At last, include module at in spec/rails_heper of your rails app:
25
-
26
- ```ruby
27
- require 'factory_strategist'
28
-
29
- RSpec.configure do |config|
30
- config.include FactoryStrategist
31
- end
32
- ```
33
-
34
24
  ## Usage
35
25
 
36
26
  As for usability, it is like a kind of linter.
@@ -58,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
58
48
 
59
49
  ## Contributing
60
50
 
61
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/factory_strategist. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/factory_strategist/blob/main/CODE_OF_CONDUCT.md).
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/neko314/factory_strategist. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/neko314/factory_strategist/blob/main/CODE_OF_CONDUCT.md).
62
52
 
63
53
  ## License
64
54
 
@@ -66,4 +56,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
66
56
 
67
57
  ## Code of Conduct
68
58
 
69
- Everyone interacting in the FactoryStrategist project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/factory_strategist/blob/main/CODE_OF_CONDUCT.md).
59
+ Everyone interacting in the FactoryStrategist project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/neko314/factory_strategist/blob/main/CODE_OF_CONDUCT.md).
@@ -32,8 +32,8 @@ Gem::Specification.new do |spec|
32
32
 
33
33
  # Uncomment to register a new dependency of your gem
34
34
  # spec.add_dependency "example-gem", "~> 1.0"
35
- spec.add_development_dependency "factory_bot"
36
- spec.add_development_dependency "rspec-rails"
35
+ spec.add_dependency "factory_bot"
36
+ spec.add_dependency "rspec"
37
37
  spec.add_development_dependency "rubocop-rake"
38
38
  spec.add_development_dependency "rubocop-rspec"
39
39
 
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec"
4
+ require "factory_bot"
5
+ require_relative "./proc_parser"
6
+
7
+ using ProcParser
8
+ module FactoryStrategist
9
+ # Settings to see which method is best
10
+ module Configure
11
+ RSpec.configure do |config|
12
+ config.around(:example) do |ex|
13
+ detect_optimal_strategy_at(ex)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def detect_optimal_strategy_at(example)
22
+ return unless run_successfully?(example) # when spec fails with create, no-op
23
+
24
+ return put_best_strategy_at(example, :build) if run_successfully_with?("build", example)
25
+
26
+ put_best_strategy_at(example, :build_stubbed) if run_successfully_with?("build_stubbed", example)
27
+ end
28
+
29
+ def run_successfully?(example)
30
+ example.call
31
+ true
32
+ rescue StandardError
33
+ false
34
+ end
35
+
36
+ def put_best_strategy_at(example, method)
37
+ p "#{example.location} create can be replaced to #{method}"
38
+ end
39
+
40
+ def run_successfully_with?(method_name, example)
41
+ ex = example_replaced_from_create_to(method_name, example)
42
+ run_successfully?(ex)
43
+ end
44
+
45
+ def example_replaced_from_create_to(method_name, example)
46
+ block_body = example.example.metadata[:block].body
47
+ new_body = "Proc.new{ #{block_body.gsub("create", method_name.to_s)} }"
48
+ eval(new_body) # rubocop:disable Security/Eval
49
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # module to get parsed proc as string
4
+ # the codes in this file are based on https://secret-garden.hatenablog.com/entry/2019/09/08/145037 by @osyo-manga
5
+ module ProcParser
6
+ ELEMENTS = %i[
7
+ magic
8
+ major_version
9
+ minor_version
10
+ format_type
11
+ misc
12
+ label
13
+ path
14
+ absolute_path
15
+ first_lineno
16
+ type
17
+ locals
18
+ args
19
+ catch_table
20
+ bytecode
21
+ ].freeze
22
+
23
+ refine RubyVM::InstructionSequence do
24
+ def to_h
25
+ ELEMENTS.zip(to_a).to_h
26
+ end
27
+ end
28
+
29
+ using self
30
+
31
+ refine Proc do
32
+ def body # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
33
+ path, (start_lnum, start_col, end_lnum, end_col) = code_location
34
+
35
+ raise "Unsupported file" if path.nil?
36
+
37
+ File.readlines(path).then do |lines|
38
+ start_line = lines[start_lnum - 1]
39
+ end_line = lines[end_lnum - 1]
40
+ if start_lnum == end_lnum
41
+ start_line[(start_col + 1)...(end_col - 1)]
42
+ elsif end_lnum - start_lnum == 1
43
+ start_line[(start_col + 1)...] + end_line[...(end_col - 1)]
44
+ else
45
+ lines[start_lnum...(end_lnum - 1)].join
46
+ end
47
+ end
48
+ end
49
+
50
+ def code_location
51
+ RubyVM::InstructionSequence.of(self).to_h
52
+ .then { |iseq| [iseq[:absolute_path], iseq.dig(:misc, :code_location)] }
53
+ end
54
+ end
55
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FactoryStrategist
4
- VERSION = "0.1.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -1,23 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "factory_strategist/version"
4
- require "rspec"
5
- require "factory_bot"
6
-
7
- # Settings to see which method is best
8
- module FactoryStrategist
9
- RSpec.configure do |config|
10
- config.before(:suite) do |_ex|
11
- FactoryBot::Syntax::Methods.alias_method :create, :build_stubbed
12
- end
13
-
14
- config.after(:example) do |ex|
15
- case ex.exception
16
- when RSpec::Expectations::ExpectationNotMetError
17
- # no-op
18
- when nil
19
- p "#{ex.location} create can be replaced to build_stubbed"
20
- end
21
- end
22
- end
23
- end
4
+ require_relative "factory_strategist/configure" if ENV["FATORYSTRATEGIST"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: factory_strategist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keiko Kaneko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-24 00:00:00.000000000 Z
11
+ date: 2021-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot
@@ -17,7 +17,7 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :development
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -25,13 +25,13 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec-rails
28
+ name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :development
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
@@ -88,6 +88,8 @@ files:
88
88
  - bin/setup
89
89
  - factory_strategist.gemspec
90
90
  - lib/factory_strategist.rb
91
+ - lib/factory_strategist/configure.rb
92
+ - lib/factory_strategist/proc_parser.rb
91
93
  - lib/factory_strategist/version.rb
92
94
  homepage: https://github.com/neko314/factory_strategist
93
95
  licenses: