factory_strategist 0.2.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83f33524068913a52ed377f0ba677ddf8e669598a0d2a679ee939ee91d540120
4
- data.tar.gz: 0c5b55ccc8d6799c2f46f3cdb30630aa68e60300104e9b4499dd5317c8e0e88a
3
+ metadata.gz: b07999650c6bcc6ae0df9f0bf95dcf750133bfd89749a42a3d41e369454c543a
4
+ data.tar.gz: 7d023e6035a66b3080a8d50fa76a25db8a545e1e88b7575366fbaa75b601c8b8
5
5
  SHA512:
6
- metadata.gz: f65c98e670a0e64af242e1c7ba4bf4e3c4ebf16a08457bcad97bf657f1b311ecae196f758e9b03587161d6ff26e72dacdbec1147d328425362495284788af743
7
- data.tar.gz: d545034207063763fcf086b92b2dccafa1aeaf1faf4ce736f31d6a50ff84341fa92b58ce551563c2b6284111761122248bbba777cb298380476ce565d7bae4bc
6
+ metadata.gz: 4c72a9e9712a09a5c93fcb81ccfcdcde921250d341deac535bb2d71bbaa07f517e42065ef664d4f153b15f09d06fbc6b35dd9abe390a319680cbf19079d7d366
7
+ data.tar.gz: 406c76322557328fcce027c0caa62e600c3c2c0b87756cfd1572460953f8714211d1cebeb24d8393f3774b556bc2bb442fb4bee4a96233f1f90cd19613462ff9
data/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2021-10-23
4
+
5
+ ### Fixed
6
+
7
+ - Fix env name, `FATORYSTRATEGIST` to `FACTORY_STRATEGIST` by @okuramasafumi
8
+
9
+ ## [0.5.0] - 2021-10-22
10
+
11
+ ### Changed
12
+
13
+ - `build` now takes precedence over `build_stubbed`
14
+
15
+ ## [0.4.0] - 2021-10-20
16
+
17
+ ### Changed
18
+
19
+ - Rewrite `create` to `build` or `build_stubbed` and create new example block
20
+
21
+ ## [0.3.0] - 2021-10-03
22
+
23
+ - Rewrite main codes to make them simple(no specs!)
24
+
3
25
  ## [0.2.0] - 2021-09-26
4
26
 
5
27
  - Add environment variable: `FATORYSTRATEGIST` to apply configuration this gem expands
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_strategist (0.1.0)
4
+ factory_strategist (0.6.0)
5
5
  factory_bot
6
6
  rspec
7
7
 
@@ -62,9 +62,10 @@ GEM
62
62
  tzinfo (2.0.4)
63
63
  concurrent-ruby (~> 1.0)
64
64
  unicode-display_width (2.0.0)
65
- zeitwerk (2.4.2)
65
+ zeitwerk (2.5.1)
66
66
 
67
67
  PLATFORMS
68
+ x86_64-darwin-19
68
69
  x86_64-darwin-20
69
70
  x86_64-linux
70
71
 
@@ -77,4 +78,4 @@ DEPENDENCIES
77
78
  rubocop-rspec
78
79
 
79
80
  BUNDLED WITH
80
- 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.
@@ -40,7 +30,7 @@ As for usability, it is like a kind of linter.
40
30
  When your application has `spec/models/user_spec.rb` and run it with FactoryStrategist.
41
31
 
42
32
  ```shell
43
- $ FATORYSTRATEGIST=1 bundle exec rspec spec/models/user_spec.rb
33
+ $ FACTORY_STRATEGIST=1 bundle exec rspec spec/models/user_spec.rb
44
34
  ```
45
35
 
46
36
  In the case which you choose best strategies, the commands returns nothing.
@@ -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).
@@ -2,22 +2,48 @@
2
2
 
3
3
  require "rspec"
4
4
  require "factory_bot"
5
+ require_relative "./proc_parser"
6
+
7
+ using ProcParser
5
8
  module FactoryStrategist
6
9
  # Settings to see which method is best
7
10
  module Configure
8
11
  RSpec.configure do |config|
9
- config.before(:suite) do |_ex|
10
- FactoryBot::Syntax::Methods.alias_method :create, :build_stubbed
11
- end
12
-
13
- config.after(:example) do |ex|
14
- case ex.exception
15
- when RSpec::Expectations::ExpectationNotMetError
16
- # no-op
17
- when nil
18
- p "#{ex.location} create can be replaced to build_stubbed"
19
- end
12
+ config.around(:example) do |ex|
13
+ detect_optimal_strategy_at(ex)
20
14
  end
21
15
  end
22
16
  end
23
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.2.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "factory_strategist/version"
4
- require_relative "factory_strategist/configure" if ENV["FATORYSTRATEGIST"]
4
+ require_relative "factory_strategist/configure" if ENV["FACTORY_STRATEGIST"]
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.2.0
4
+ version: 0.6.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-26 00:00:00.000000000 Z
11
+ date: 2021-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot
@@ -89,6 +89,7 @@ files:
89
89
  - factory_strategist.gemspec
90
90
  - lib/factory_strategist.rb
91
91
  - lib/factory_strategist/configure.rb
92
+ - lib/factory_strategist/proc_parser.rb
92
93
  - lib/factory_strategist/version.rb
93
94
  homepage: https://github.com/neko314/factory_strategist
94
95
  licenses: