branchout 0.1.0 → 0.2.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 +4 -4
- data/README.ja.md +102 -6
- data/README.md +72 -7
- data/lib/branchout/cli.rb +11 -3
- data/lib/branchout/emitters/minitest.rb +22 -6
- data/lib/branchout/emitters/rspec.rb +25 -2
- data/lib/branchout/emitters/support.rb +68 -7
- data/lib/branchout/emitters.rb +19 -6
- data/lib/branchout/engine.rb +15 -6
- data/lib/branchout/explore.rb +2 -2
- data/lib/branchout/input.rb +38 -5
- data/lib/branchout/input_file.rb +38 -7
- data/lib/branchout/report.rb +142 -14
- data/lib/branchout/runner.rb +70 -13
- data/lib/branchout/target.rb +22 -0
- data/lib/branchout/version.rb +1 -1
- data/skills/branchout/SKILL.md +12 -11
- metadata +1 -1
|
@@ -156,8 +156,7 @@ module Branchout
|
|
|
156
156
|
end
|
|
157
157
|
receiver = input.receiver.is_a?(Input) ? project_paths(input.receiver, destination) : input.receiver
|
|
158
158
|
setup = input.setup.map { |call| Input::SetupCall.new(call.method_name, project_paths(call.arguments, destination)) }
|
|
159
|
-
|
|
160
|
-
pin_keys: input.pin_keys, block: input.block, before: input.before)
|
|
159
|
+
input.with(args: rewrite.(input.args), kwargs: rewrite.(input.kwargs), receiver: receiver, setup: setup)
|
|
161
160
|
end
|
|
162
161
|
|
|
163
162
|
# +value+, a returned value or an error message, with each String
|
|
@@ -203,7 +202,8 @@ module Branchout
|
|
|
203
202
|
# directory, that the Strings of +value+ hold anywhere in their text.
|
|
204
203
|
def machine_paths_in(value)
|
|
205
204
|
homes = machine_homes.map { |home| Regexp.escape(home) }.join("|")
|
|
206
|
-
|
|
205
|
+
# A sentence's period or colon after a path ends the sentence, not the path.
|
|
206
|
+
strings(value).flat_map { |text| text.scan(%r{(?:#{homes})[^\s"'`(),;]+}) }.map { |path| path.sub(/[.:]+\z/, "") }
|
|
207
207
|
end
|
|
208
208
|
|
|
209
209
|
def machine_homes
|
|
@@ -243,12 +243,15 @@ module Branchout
|
|
|
243
243
|
end
|
|
244
244
|
call = ->(on) { with_block(call_text(on, target.method_name, argument_list(input, thawed: thawed), private_calls), input.block) }
|
|
245
245
|
before = input.before ? input.before.source.lines(chomp: true) : []
|
|
246
|
-
return [before, call.(receiver)] if input.setup.empty? && !input.receiver.is_a?(Code)
|
|
246
|
+
return [before, call.(receiver)] if target.singleton || (input.setup.empty? && !input.receiver.is_a?(Code) && input.after.nil?)
|
|
247
247
|
|
|
248
|
-
name
|
|
248
|
+
# The name explore gave the receiver, so a setup block, an argument,
|
|
249
|
+
# or the after that reads it reads the same object here.
|
|
250
|
+
name = built_receiver_name(target)
|
|
249
251
|
statements = [*before, "#{name} = #{receiver}"]
|
|
250
252
|
input.setup.each do |setup_call|
|
|
251
|
-
statements << call_text(name, setup_call.method_name, argument_list(setup_call.arguments, thawed: thawed), [])
|
|
253
|
+
statements << with_block(call_text(name, setup_call.method_name, argument_list(setup_call.arguments, thawed: thawed), []),
|
|
254
|
+
setup_call.arguments.block)
|
|
252
255
|
end
|
|
253
256
|
[statements, call.(name)]
|
|
254
257
|
end
|
|
@@ -426,7 +429,7 @@ module Branchout
|
|
|
426
429
|
roomy = !differing.empty? || (!pin.error && mixed_array?(pin.value))
|
|
427
430
|
clip(outcome_words(pin, differing), roomy ? OUTCOME_LIMIT : 30)
|
|
428
431
|
end
|
|
429
|
-
names = pins.each_with_index.map { |pin, index| name_of(
|
|
432
|
+
names = pins.each_with_index.map { |pin, index| name_of(named_input_words(pin.input), outcomes[index]) }
|
|
430
433
|
names.each_index.group_by { |index| names[index] }.each_value do |siblings|
|
|
431
434
|
next if siblings.one?
|
|
432
435
|
|
|
@@ -540,6 +543,7 @@ module Branchout
|
|
|
540
543
|
end
|
|
541
544
|
out << [[:block], input.block] if input.block
|
|
542
545
|
out << [[:before], input.before] if input.before
|
|
546
|
+
out << [[:after], input.after] if input.after
|
|
543
547
|
out
|
|
544
548
|
end
|
|
545
549
|
|
|
@@ -596,6 +600,29 @@ module Branchout
|
|
|
596
600
|
chosen.empty? ? stable : chosen
|
|
597
601
|
end
|
|
598
602
|
|
|
603
|
+
# What +pin+'s after read, as a pin of its own whose value is that
|
|
604
|
+
# read, so a report and a test pin it as they pin a returned value.
|
|
605
|
+
# The input's pin_keys name keys of the returned Hash, not of this
|
|
606
|
+
# one. nil for a pin with no after read.
|
|
607
|
+
def after_pin(pin)
|
|
608
|
+
return nil unless pin.observed
|
|
609
|
+
|
|
610
|
+
pin.dup.tap do |copy|
|
|
611
|
+
copy.input = pin.input.with(pin_keys: nil)
|
|
612
|
+
copy.value = pin.observed.value
|
|
613
|
+
copy.varying = pin.observed.varying
|
|
614
|
+
copy.error = nil
|
|
615
|
+
copy.observed = nil
|
|
616
|
+
end
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
# +source+, an expression, as the receiver of a method call: in
|
|
620
|
+
# parentheses unless it is a chain of names and calls, where a call
|
|
621
|
+
# after it binds to the whole.
|
|
622
|
+
def receiver_form(source)
|
|
623
|
+
source.match?(/\A[A-Za-z_@$][\w@$]*(?:(?:\.|::)[A-Za-z_]\w*[?!]?(?:\([^()]*\))?)*\z/) ? source : "(#{source})"
|
|
624
|
+
end
|
|
625
|
+
|
|
599
626
|
def outcome_text(pin, differing, short: false)
|
|
600
627
|
return "raises #{pin.error.class_name}" if pin.error
|
|
601
628
|
|
|
@@ -999,6 +1026,15 @@ module Branchout
|
|
|
999
1026
|
end
|
|
1000
1027
|
end
|
|
1001
1028
|
|
|
1029
|
+
# An input with a note is named by the note, which says why the input
|
|
1030
|
+
# is there in the words of its author; the values, with their escapes
|
|
1031
|
+
# and their Ruby, say it less well. A note with no Latin letter or
|
|
1032
|
+
# digit gives no words, so its input is named by its values.
|
|
1033
|
+
def named_input_words(input)
|
|
1034
|
+
words = input.note ? tokens(input.note) : []
|
|
1035
|
+
words.empty? ? input_words(input) : words
|
|
1036
|
+
end
|
|
1037
|
+
|
|
1002
1038
|
def input_words(input)
|
|
1003
1039
|
words = input.args.flat_map { |arg| argument_words(arg) }
|
|
1004
1040
|
words += input.kwargs.flat_map { |key, value| [key.to_s, *argument_words(value)] }
|
|
@@ -1150,6 +1186,31 @@ module Branchout
|
|
|
1150
1186
|
text.gsub(%r{(?:#{homes})(?:[^\s"\x27`(),;/]+/)*}, "")
|
|
1151
1187
|
end
|
|
1152
1188
|
|
|
1189
|
+
# +value+ as a report shows it: each path under the temporary or the
|
|
1190
|
+
# home directory starts with <tmp>/ or <home>/, and .../ stands for
|
|
1191
|
+
# its directories below that. The report stays the same on every
|
|
1192
|
+
# machine, and a reader still sees where the value held a path, which
|
|
1193
|
+
# the file alone of a test name would hide.
|
|
1194
|
+
def marked(value)
|
|
1195
|
+
case value
|
|
1196
|
+
when String then with_machine_marks(value)
|
|
1197
|
+
when Array then value.map { |item| marked(item) }
|
|
1198
|
+
when Hash then value.transform_values { |item| marked(item) }
|
|
1199
|
+
else value
|
|
1200
|
+
end
|
|
1201
|
+
end
|
|
1202
|
+
|
|
1203
|
+
def with_machine_marks(text)
|
|
1204
|
+
labels = [[Dir.tmpdir, "<tmp>"], [Dir.home, "<home>"]].flat_map { |dir, label| [["#{dir}/", label], ["#{File.realpath(dir)}/", label]] }.uniq
|
|
1205
|
+
# The longest first, so a home under the temporary directory is a home.
|
|
1206
|
+
labels = labels.sort_by { |dir, _| -dir.length }
|
|
1207
|
+
found = Regexp.union(labels.map(&:first))
|
|
1208
|
+
text.gsub(%r{(#{found})((?:[^\s"\x27`(),;/]+/)*)}) do
|
|
1209
|
+
label = labels.find { |dir, _| dir == Regexp.last_match(1) }.last
|
|
1210
|
+
"#{label}/#{".../" unless Regexp.last_match(2).empty?}"
|
|
1211
|
+
end
|
|
1212
|
+
end
|
|
1213
|
+
|
|
1153
1214
|
# A number longer than LONG_NUMBER digits fills a name, so it keeps
|
|
1154
1215
|
# its first three digits and its power of ten: 123e46.
|
|
1155
1216
|
def number_word(value)
|
data/lib/branchout/emitters.rb
CHANGED
|
@@ -30,11 +30,15 @@ module Branchout
|
|
|
30
30
|
def write(format, result, target, destination, require_by_name: false, pin_keys: [])
|
|
31
31
|
pins = project_pins(result.pins, destination)
|
|
32
32
|
Support.write_file(destination, render(format, result, target, destination, require_by_name: require_by_name, pin_keys: pin_keys))
|
|
33
|
-
varying = result.pins.select(&:varying).map { |pin| varying_warning(pin, pin_keys) }
|
|
33
|
+
varying = result.pins.select(&:varying).map { |pin| varying_warning(pin, pin_keys) } +
|
|
34
|
+
result.pins.filter_map { |pin| Support.after_pin(pin) }.select(&:varying).map { |pin| after_varying_warning(pin) }
|
|
34
35
|
passed = pins.flat_map { |pin| Support.machine_paths(pin.input) }.uniq
|
|
35
36
|
pinned = pins.flat_map do |pin|
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
# A pattern pins the rest of its message as written, paths too.
|
|
38
|
+
message = pin.error.exact_message || pin.error.message if pin.error&.stable_message || pin.error&.message_pattern
|
|
39
|
+
after = Support.after_pin(pin)
|
|
40
|
+
read = Support.pinned_value(after.value, Support.keys_for(after, [])) if after && after.varying != :whole
|
|
41
|
+
Support.machine_paths_in([Support.pinned_value(pin.value, Support.keys_for(pin, pin_keys)), message, read])
|
|
38
42
|
end
|
|
39
43
|
existing, missing = passed.partition { |path| File.exist?(path) }
|
|
40
44
|
outside = Support.required_outside_project(result.requires, (target.path unless result.target_required), destination,
|
|
@@ -50,8 +54,8 @@ module Branchout
|
|
|
50
54
|
"branchout: the emitted test passes #{path}, a path of this machine outside the test's project that does not exist, " \
|
|
51
55
|
"so the test holds this machine's directories. Put the path under the project, and the test names it from its own directory."
|
|
52
56
|
end + (pinned.uniq - existing).map do |path|
|
|
53
|
-
"branchout: the emitted test pins #{path}, a path of this machine outside the test's project, in a returned value " \
|
|
54
|
-
"
|
|
57
|
+
"branchout: the emitted test pins #{path}, a path of this machine outside the test's project, in a returned value, " \
|
|
58
|
+
"an error message, or what an after read, so the test passes only here. Give #{target.entry} a path under the project."
|
|
55
59
|
end
|
|
56
60
|
end
|
|
57
61
|
|
|
@@ -73,14 +77,23 @@ module Branchout
|
|
|
73
77
|
"so its test pins only #{Support.keys_for(pin, pin_keys).join(", ")}."
|
|
74
78
|
end
|
|
75
79
|
|
|
80
|
+
def after_varying_warning(pin)
|
|
81
|
+
return "branchout: the after #{pin.input.after.source} of #{pin.input.text} read a value that differed between two runs, so its test does not check it." if pin.varying == :whole
|
|
82
|
+
|
|
83
|
+
"branchout: the after #{pin.input.after.source} of #{pin.input.text} read a Hash whose #{pin.varying.join(", ")} differed between two runs, " \
|
|
84
|
+
"so its test pins only #{Support.keys_for(pin, []).join(", ")}."
|
|
85
|
+
end
|
|
86
|
+
|
|
76
87
|
# Each pin with the paths under the test's project in its input, its
|
|
77
|
-
# returned value,
|
|
88
|
+
# returned value, its error message, and what its after read written
|
|
89
|
+
# from the test's file.
|
|
78
90
|
# A pin whose value differed as a whole between two runs is left out.
|
|
79
91
|
def project_pins(pins, destination)
|
|
80
92
|
pins.reject { |pin| pin.varying == :whole }.map do |pin|
|
|
81
93
|
pin.dup.tap do |copy|
|
|
82
94
|
copy.input = Support.project_paths(pin.input, destination)
|
|
83
95
|
copy.value = Support.project_value(pin.value, destination)
|
|
96
|
+
copy.observed = pin.observed.dup.tap { |read| read.value = Support.project_value(read.value, destination) } if pin.observed
|
|
84
97
|
next unless pin.error
|
|
85
98
|
|
|
86
99
|
copy.error = pin.error.dup.tap do |error|
|
data/lib/branchout/engine.rb
CHANGED
|
@@ -8,7 +8,7 @@ module Branchout
|
|
|
8
8
|
:command, :target, :entry, :seed, :strategies, :random, :mutate_cap, :timeout,
|
|
9
9
|
:inputs_ran, :mutate_ran, :random_ran, :executions,
|
|
10
10
|
:covered, :frontier, :exceptions, :pins, :requires, :load_paths, :target_required, :private_calls, :const_expression, :repeated_inputs, :not_counted,
|
|
11
|
-
:operators, :memos, :boundaries,
|
|
11
|
+
:operators, :memos, :defined_memos, :boundaries,
|
|
12
12
|
:test_class, :spec_type,
|
|
13
13
|
keyword_init: true
|
|
14
14
|
)
|
|
@@ -26,22 +26,28 @@ module Branchout
|
|
|
26
26
|
# +varying+ is Runner::Execution's: what differed between two runs.
|
|
27
27
|
# +listed_at+ is the input's index in the inputs file, or nil for an
|
|
28
28
|
# input branchout made.
|
|
29
|
-
|
|
29
|
+
# +observed+ is the Runner::Observation of what the input's after read,
|
|
30
|
+
# or nil when the input has no after or its call timed out.
|
|
31
|
+
Pin = Struct.new(:input, :value, :error, :covers, :thawed, :varying, :listed_at, :observed, keyword_init: true)
|
|
30
32
|
Cover = Struct.new(:line, :column, :text, keyword_init: true)
|
|
31
33
|
Finding = Struct.new(:class_name, :message, :loc, :input, :stable_message, keyword_init: true)
|
|
32
34
|
|
|
33
35
|
def self.explore(target:, entry:, user_inputs: [], type_overrides: nil, seed: 0, strategies: STRATEGY_ORDER,
|
|
34
|
-
random: 16, mutate_cap: 256, timeout: 1, command: "explore", requires: [], load_paths: [], host: nil)
|
|
36
|
+
random: 16, mutate_cap: 256, timeout: 1, command: "explore", requires: [], load_paths: [], host: nil, after: nil)
|
|
35
37
|
new(
|
|
36
38
|
target: target, entry: entry, user_inputs: user_inputs, type_overrides: type_overrides,
|
|
37
39
|
seed: seed, strategies: strategies, random: random, mutate_cap: mutate_cap,
|
|
38
|
-
timeout: timeout, command: command, requires: requires, load_paths: load_paths, host: host
|
|
40
|
+
timeout: timeout, command: command, requires: requires, load_paths: load_paths, host: host, after: after
|
|
39
41
|
).explore
|
|
40
42
|
end
|
|
41
43
|
|
|
44
|
+
# +after+ is the inputs file's own after, the Code that reads what each
|
|
45
|
+
# call left. The listed inputs have it already; the inputs that mutate
|
|
46
|
+
# and random make get it here, so every call of the run reads the same.
|
|
42
47
|
def initialize(target:, entry:, user_inputs:, type_overrides:, seed:, strategies:, random:, mutate_cap:, timeout:, command:,
|
|
43
|
-
requires:, load_paths:, host:)
|
|
48
|
+
requires:, load_paths:, host:, after: nil)
|
|
44
49
|
@host = host
|
|
50
|
+
@after = after
|
|
45
51
|
@path = target
|
|
46
52
|
@load_paths = load_paths.map { |dir| File.expand_path(dir) }
|
|
47
53
|
@requires = requires.map { |name| required_file(name) or raise Error, require_not_found(name) }
|
|
@@ -61,6 +67,7 @@ module Branchout
|
|
|
61
67
|
user = @strategies.include?("inputs") ? @user_inputs : []
|
|
62
68
|
mutated = @strategies.include?("mutate") ? Mutator.new(target, cap: @mutate_cap).inputs : []
|
|
63
69
|
randoms = @strategies.include?("random") ? Sampler.new(target, seed: @seed, count: @random).inputs : []
|
|
70
|
+
mutated, randoms = [mutated, randoms].map { |list| list.map { |input| input.with(after: @after) } } if @after
|
|
64
71
|
|
|
65
72
|
counts = {inputs: 0, mutate: 0, random: 0}
|
|
66
73
|
seen = {}
|
|
@@ -149,6 +156,7 @@ module Branchout
|
|
|
149
156
|
not_counted: runner.not_counted,
|
|
150
157
|
operators: target.operators,
|
|
151
158
|
memos: target.memos,
|
|
159
|
+
defined_memos: target.defined_memos,
|
|
152
160
|
boundaries: target.boundaries,
|
|
153
161
|
test_class: @host&.test_class,
|
|
154
162
|
spec_type: @host&.spec_type
|
|
@@ -232,7 +240,8 @@ module Branchout
|
|
|
232
240
|
error: execution.error,
|
|
233
241
|
covers: covers_of(target, execution),
|
|
234
242
|
thawed: execution.thawed,
|
|
235
|
-
varying: execution.varying
|
|
243
|
+
varying: execution.varying,
|
|
244
|
+
observed: execution.observed
|
|
236
245
|
)
|
|
237
246
|
end
|
|
238
247
|
end
|
data/lib/branchout/explore.rb
CHANGED
|
@@ -64,7 +64,7 @@ module Branchout
|
|
|
64
64
|
raise Error, "verbose: is only valid with the text report; json: true holds every arm and the run settings"
|
|
65
65
|
end
|
|
66
66
|
target ||= defining_file(entry)
|
|
67
|
-
user_inputs, type_overrides = inputs.is_a?(String) ? InputFile.load(inputs) : [parse_inputs(inputs), nil]
|
|
67
|
+
user_inputs, type_overrides, after = inputs.is_a?(String) ? InputFile.load(inputs) : [parse_inputs(inputs), nil, nil]
|
|
68
68
|
requires = requires.nil? ? [loaded_helper].compact : Array(requires)
|
|
69
69
|
format = Emitters.resolve(format&.to_s, emit_tests) if emit_tests
|
|
70
70
|
if emit_tests && requires.empty?
|
|
@@ -75,7 +75,7 @@ module Branchout
|
|
|
75
75
|
test_class: within.class.superclass.name, spec_type: spec_type_of(within))
|
|
76
76
|
result = Engine.explore(
|
|
77
77
|
target: target, entry: entry, user_inputs: user_inputs, type_overrides: type_overrides, seed: seed,
|
|
78
|
-
strategies: strategies, random: random, mutate_cap: mutate_cap, timeout: timeout, requires: requires, host: host
|
|
78
|
+
strategies: strategies, random: random, mutate_cap: mutate_cap, timeout: timeout, requires: requires, host: host, after: after
|
|
79
79
|
)
|
|
80
80
|
if emit_tests
|
|
81
81
|
warnings = Emitters.write(format, result, Target.parse(target, entry, type_overrides: type_overrides),
|
data/lib/branchout/input.rb
CHANGED
|
@@ -15,6 +15,8 @@ module Branchout
|
|
|
15
15
|
# A block as a call takes it, written with braces so it binds to the
|
|
16
16
|
# call in an emitted assertion as it did in explore.
|
|
17
17
|
def self.block(source)
|
|
18
|
+
raise Error, "block #{source.inspect} is not a block such as \"{ |item| item.price > 2 }\"" unless source.is_a?(String)
|
|
19
|
+
|
|
18
20
|
body = Prism.parse("m #{source}").value.statements.body
|
|
19
21
|
unless source.start_with?("{") && body.length == 1 && body.first.respond_to?(:block) && body.first.block
|
|
20
22
|
raise Error, "block #{source.inspect} is not a block such as \"{ |item| item.price > 2 }\""
|
|
@@ -23,6 +25,20 @@ module Branchout
|
|
|
23
25
|
new(-source)
|
|
24
26
|
end
|
|
25
27
|
|
|
28
|
+
# One expression, as an emitted test passes it to assert_equal or
|
|
29
|
+
# expect: a modifier if or two statements would not parse there.
|
|
30
|
+
def self.expression(source, example)
|
|
31
|
+
raise Error, "Ruby #{source.inspect} is not one expression such as #{example.inspect}" unless source.is_a?(String)
|
|
32
|
+
|
|
33
|
+
code = parse(source)
|
|
34
|
+
unless Prism.parse(source).value.statements.body.length == 1 && Prism.parse("m(#{source})").errors.empty?
|
|
35
|
+
raise Error, "Ruby #{source.inspect} is not one expression such as #{example.inspect}; " \
|
|
36
|
+
"read several values as one Array or Hash"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
code
|
|
40
|
+
end
|
|
41
|
+
|
|
26
42
|
def to_s = source
|
|
27
43
|
end
|
|
28
44
|
|
|
@@ -73,9 +89,16 @@ module Branchout
|
|
|
73
89
|
# not in to_h. +block+ is the Code of the block the entry gets, or nil.
|
|
74
90
|
# +before+ is the Code that makes what the call reads, such as the rows
|
|
75
91
|
# a class method looks up, run first in the input's savepoint, or nil.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
92
|
+
# +note+ is the reason the inputs file gives for the input, which names
|
|
93
|
+
# it in the report and in an emitted test. Like +pin_keys+, it is not
|
|
94
|
+
# part of the call, so it is not in to_h.
|
|
95
|
+
# +after+ is the Code of one expression that reads what the call left,
|
|
96
|
+
# such as the receiver's state, once the call returned or raised, or
|
|
97
|
+
# nil. Its value is pinned beside the call's, so it is in to_h: two
|
|
98
|
+
# inputs that read different things are two tests.
|
|
99
|
+
attr_reader :args, :kwargs, :receiver, :setup, :pin_keys, :block, :before, :note, :after
|
|
100
|
+
|
|
101
|
+
def initialize(args:, kwargs: {}, receiver: nil, setup: [], pin_keys: nil, block: nil, before: nil, note: nil, after: nil)
|
|
79
102
|
@args = deep_freeze(normalize(args))
|
|
80
103
|
unless @args.is_a?(Array)
|
|
81
104
|
raise Error, "input args must be an array"
|
|
@@ -94,9 +117,17 @@ module Branchout
|
|
|
94
117
|
@pin_keys = pin_keys&.map { |key| -key }.freeze
|
|
95
118
|
@block = block
|
|
96
119
|
@before = before
|
|
120
|
+
@note = note && -note
|
|
121
|
+
@after = after
|
|
97
122
|
freeze
|
|
98
123
|
end
|
|
99
124
|
|
|
125
|
+
# This input with +changes+ in place of its own parts.
|
|
126
|
+
def with(**changes)
|
|
127
|
+
parts = {args: args, kwargs: kwargs, receiver: receiver, setup: setup, pin_keys: pin_keys, block: block, before: before, note: note, after: after}
|
|
128
|
+
Input.new(**parts, **changes)
|
|
129
|
+
end
|
|
130
|
+
|
|
100
131
|
def to_h
|
|
101
132
|
hash = {"args" => args, "kwargs" => kwargs}
|
|
102
133
|
if receiver.is_a?(Code)
|
|
@@ -107,6 +138,7 @@ module Branchout
|
|
|
107
138
|
hash["setup"] = setup.map(&:to_h) unless setup.empty?
|
|
108
139
|
hash["block"] = block.source if block
|
|
109
140
|
hash["before"] = before if before
|
|
141
|
+
hash["after"] = after if after
|
|
110
142
|
hash
|
|
111
143
|
end
|
|
112
144
|
|
|
@@ -118,7 +150,7 @@ module Branchout
|
|
|
118
150
|
# the args array so an agent can paste it back into inputs.json. The
|
|
119
151
|
# entry form leaves out empty kwargs, which an inputs file may omit.
|
|
120
152
|
def text
|
|
121
|
-
return Canon.json(args) if kwargs.empty? && receiver.nil? && setup.empty? && block.nil? && before.nil?
|
|
153
|
+
return Canon.json(args) if kwargs.empty? && receiver.nil? && setup.empty? && block.nil? && before.nil? && after.nil?
|
|
122
154
|
|
|
123
155
|
Canon.json(short_h)
|
|
124
156
|
end
|
|
@@ -139,7 +171,8 @@ module Branchout
|
|
|
139
171
|
self.class.weight_of(receiver) +
|
|
140
172
|
setup.sum { |call| 1 + call.arguments.weight } +
|
|
141
173
|
(block ? block.source.length : 0) +
|
|
142
|
-
(before ? before.source.length : 0)
|
|
174
|
+
(before ? before.source.length : 0) +
|
|
175
|
+
(after ? after.source.length : 0)
|
|
143
176
|
end
|
|
144
177
|
|
|
145
178
|
def self.weight_of(value)
|
data/lib/branchout/input_file.rb
CHANGED
|
@@ -8,17 +8,22 @@ module Branchout
|
|
|
8
8
|
raise Error, "inputs file not found: #{path}" unless File.file?(path)
|
|
9
9
|
|
|
10
10
|
begin
|
|
11
|
-
|
|
11
|
+
# JSON is UTF-8 whatever the locale says, and under LANG=C a note in
|
|
12
|
+
# another script would not read.
|
|
13
|
+
data = JSON.parse(File.read(path, encoding: "UTF-8"))
|
|
12
14
|
rescue JSON::ParserError => e
|
|
13
15
|
raise Error, "invalid JSON in #{path}: #{e.message}"
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
types = nil
|
|
19
|
+
after = nil
|
|
17
20
|
entries = data
|
|
18
21
|
if data.is_a?(Hash)
|
|
19
22
|
types = data["types"]
|
|
20
23
|
entries = data["inputs"]
|
|
21
24
|
raise Error, "#{path} is missing \"inputs\"" if entries.nil?
|
|
25
|
+
|
|
26
|
+
after = parse_after(data["after"], path) if data.key?("after")
|
|
22
27
|
end
|
|
23
28
|
raise Error, "#{path} inputs must be an array" unless entries.is_a?(Array)
|
|
24
29
|
if types && !types.is_a?(Array)
|
|
@@ -29,15 +34,32 @@ module Branchout
|
|
|
29
34
|
end
|
|
30
35
|
|
|
31
36
|
inputs = entries.each_with_index.map { |entry, index| parse_entry(entry, "#{path} inputs[#{index}]") }
|
|
32
|
-
|
|
37
|
+
# The file's after reads what every call left, unless an input reads
|
|
38
|
+
# something of its own.
|
|
39
|
+
inputs = inputs.map { |input| input.after ? input : input.with(after: after) } if after
|
|
40
|
+
[inputs, types, after]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
AFTER_EXAMPLE = "{\"ruby\": \"cart.items\"}"
|
|
44
|
+
|
|
45
|
+
# The Code of an after: one expression, which an emitted test pins as
|
|
46
|
+
# it pins a returned value.
|
|
47
|
+
def parse_after(value, label)
|
|
48
|
+
raise Error, "#{label} after must be Ruby such as #{AFTER_EXAMPLE}" unless value.is_a?(Hash) && code?(value)
|
|
49
|
+
|
|
50
|
+
Code.expression(value["ruby"], "cart.items")
|
|
51
|
+
rescue Error => e
|
|
52
|
+
raise if e.message.start_with?(label)
|
|
53
|
+
|
|
54
|
+
raise Error, "#{label} after #{e.message.sub(/\ARuby /, "")}"
|
|
33
55
|
end
|
|
34
56
|
|
|
35
57
|
# Keys that say how the entry is called, so args may be left out for none.
|
|
36
|
-
CALL_KEYS = %w[new receiver setup kwargs block before].freeze
|
|
58
|
+
CALL_KEYS = %w[new receiver setup kwargs block before after].freeze
|
|
37
59
|
|
|
38
60
|
METHOD_NAME = /\A(?:[A-Za-z_][A-Za-z0-9_]*[?!=]?|<<|>>|\[\]=?|[-+*\/%&|^<>]|\*\*|<=>|==|!=|<=|>=|=~)\z/
|
|
39
61
|
|
|
40
|
-
def parse_entry(entry, label, keys = %w[args kwargs new receiver setup note pin_keys block before])
|
|
62
|
+
def parse_entry(entry, label, keys = %w[args kwargs new receiver setup note pin_keys block before after])
|
|
41
63
|
case entry
|
|
42
64
|
when Array
|
|
43
65
|
Input.new(args: entry)
|
|
@@ -71,8 +93,12 @@ module Branchout
|
|
|
71
93
|
|
|
72
94
|
before = Code.parse(entry["before"]["ruby"])
|
|
73
95
|
end
|
|
96
|
+
# A note that is not a String, which an earlier branchout ignored, is
|
|
97
|
+
# still ignored.
|
|
98
|
+
note = entry["note"] if entry["note"].is_a?(String)
|
|
99
|
+
after = parse_after(entry["after"], label) if entry.key?("after")
|
|
74
100
|
Input.new(args: entry.fetch("args", []), kwargs: entry["kwargs"] || {}, receiver: receiver, setup: setup, pin_keys: pin_keys, block: block,
|
|
75
|
-
before: before)
|
|
101
|
+
before: before, note: (note unless note&.strip&.empty?), after: after)
|
|
76
102
|
else
|
|
77
103
|
raise Error, "#{label} is #{JSON.generate(entry)}, not a call; write each call as an array of its arguments, such as #{JSON.generate([entry])}"
|
|
78
104
|
end
|
|
@@ -111,14 +137,19 @@ module Branchout
|
|
|
111
137
|
step_label = "#{label}[#{index}]"
|
|
112
138
|
raise Error, "#{step_label} must be an object" unless step.is_a?(Hash)
|
|
113
139
|
|
|
114
|
-
unknown = step.keys - %w[method args kwargs]
|
|
140
|
+
unknown = step.keys - %w[method args kwargs block]
|
|
115
141
|
raise Error, "#{step_label} has unknown keys: #{unknown.join(", ")}" unless unknown.empty?
|
|
116
142
|
raise Error, "#{step_label} is missing method" unless step.key?("method")
|
|
117
143
|
|
|
118
144
|
name = step["method"]
|
|
119
145
|
raise Error, "#{step_label} method #{name.inspect} is not a method name" unless name.is_a?(String) && METHOD_NAME.match?(name)
|
|
120
146
|
|
|
121
|
-
|
|
147
|
+
begin
|
|
148
|
+
block = Code.block(step["block"]) if step.key?("block")
|
|
149
|
+
rescue Error => e
|
|
150
|
+
raise Error, "#{step_label} #{e.message}"
|
|
151
|
+
end
|
|
152
|
+
Input::SetupCall.new(name, Input.new(args: step.fetch("args", []), kwargs: step["kwargs"] || {}, block: block))
|
|
122
153
|
end
|
|
123
154
|
end
|
|
124
155
|
end
|