factory_strategist 0.3.0 → 0.4.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: a0ce7f02ee00b07efad3acb9b46edec6e555e6a271cb8cf380f4a7da0d447780
4
- data.tar.gz: 3fefcdb5cdad957f34c19c5cb4cb36106bc3df444e373478dbc697be37277780
3
+ metadata.gz: 1b696ae5076bfcd930027df307103f00ec6c7267d660c0eba3bca872f94f3e5c
4
+ data.tar.gz: c7c51ce9ec33f16d774fd9a4453d4a47faadd03a60a2ca6b83a6166f6d2b7d66
5
5
  SHA512:
6
- metadata.gz: 603fa59303b82d120b1afe8d2de49e4c5e5f10e063bef151570351ec1e11396ed82da2b52dcdae881b0722d4c9e5d9a409c4b8bdca4bbefc67aa9773ffd90532
7
- data.tar.gz: 6e4d85f95b0d63eddb6d5eb6a730d439a807ffb726eabad15d4c3dbd31e2c6d6f2ddb51383ba9b43efccfee1eeb9f6b007debb471f65877688775a1444de9cdb
6
+ metadata.gz: 374037f3c92595ce7ff909951f9a2533aed5eb3b9d8cd3d096a8ad56f638b50c5f137b3de1c00455f7c540fee678c6408746059f51eb7d196956a5c2a87627cd
7
+ data.tar.gz: 3b5ee739ffb8f8d14db07fba26ac735da669f016d7209b5925ec600bc41d9364940996d199e7534a83bbd236ead5dd5c27dc475cfdc4a88aa784016dc255cec4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2021-10-20
4
+
5
+ ### Changed
6
+
7
+ - Rewrite `create` to `build` or `build_stubbed` and create new example block
8
+
3
9
  ## [0.3.0] - 2021-10-03
4
10
 
5
11
  - Rewrite main codes to make them simple(no specs!)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- factory_strategist (0.3.0)
4
+ factory_strategist (0.4.0)
5
5
  factory_bot
6
6
  rspec
7
7
 
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.
@@ -2,6 +2,9 @@
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
@@ -18,21 +21,29 @@ private
18
21
  def detect_optimal_strategy_at(example)
19
22
  return unless run_successfully?(example) # when spec fails with create, no-op
20
23
 
21
- return put_best_strategy_at(example, :build_stubbed) if run_successfully_with?(:build_stubbed, example)
24
+ return put_best_strategy_at(example, :build_stubbed) if run_successfully_with?("build_stubbed", example)
22
25
 
23
- put_best_strategy_at(example, :build) if run_successfully_with?(:build, example)
26
+ put_best_strategy_at(example, :build) if run_successfully_with?("build", example)
24
27
  end
25
28
 
26
29
  def run_successfully?(example)
27
- example.run
28
- !example.exception
30
+ example.call
31
+ true
32
+ rescue StandardError
33
+ false
29
34
  end
30
35
 
31
36
  def put_best_strategy_at(example, method)
32
37
  p "#{example.location} create can be replaced to #{method}"
33
38
  end
34
39
 
35
- def run_successfully_with?(method, example)
36
- FactoryBot::Syntax::Methods.alias_method :create, method
37
- run_successfully?(example)
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
38
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.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
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.3.0
4
+ version: 0.4.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-10-03 00:00:00.000000000 Z
11
+ date: 2021-10-20 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: