factory_strategist 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +1 -1
- data/README.md +0 -10
- data/lib/factory_strategist/configure.rb +18 -7
- data/lib/factory_strategist/proc_parser.rb +55 -0
- data/lib/factory_strategist/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b696ae5076bfcd930027df307103f00ec6c7267d660c0eba3bca872f94f3e5c
|
4
|
+
data.tar.gz: c7c51ce9ec33f16d774fd9a4453d4a47faadd03a60a2ca6b83a6166f6d2b7d66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 374037f3c92595ce7ff909951f9a2533aed5eb3b9d8cd3d096a8ad56f638b50c5f137b3de1c00455f7c540fee678c6408746059f51eb7d196956a5c2a87627cd
|
7
|
+
data.tar.gz: 3b5ee739ffb8f8d14db07fba26ac735da669f016d7209b5925ec600bc41d9364940996d199e7534a83bbd236ead5dd5c27dc475cfdc4a88aa784016dc255cec4
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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?(
|
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?(
|
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.
|
28
|
-
|
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?(
|
36
|
-
|
37
|
-
run_successfully?(
|
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
|
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.
|
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-
|
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:
|