bparity 0.1.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.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +61 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +146 -0
  5. data/Rakefile +36 -0
  6. data/docs/application_example.md +25 -0
  7. data/docs/formal_assurance_limits.md +21 -0
  8. data/exe/bparity +7 -0
  9. data/fixtures/scenarios/01_pure_function/adapter.rb +13 -0
  10. data/fixtures/scenarios/01_pure_function/boundary.rb +17 -0
  11. data/fixtures/scenarios/01_pure_function/legacy/dead_gem.rb +9 -0
  12. data/fixtures/scenarios/01_pure_function/legacy/slugifier.rb +17 -0
  13. data/fixtures/scenarios/01_pure_function/replacement/broken.rb +15 -0
  14. data/fixtures/scenarios/01_pure_function/replacement/good.rb +19 -0
  15. data/fixtures/scenarios/01_pure_function/spec/slugifier_spec.rb +20 -0
  16. data/fixtures/scenarios/01_pure_function/test/slugifier_test.rb +10 -0
  17. data/fixtures/scenarios/02_stateful_client/adapter.rb +19 -0
  18. data/fixtures/scenarios/02_stateful_client/boundary.rb +10 -0
  19. data/fixtures/scenarios/02_stateful_client/legacy/client.rb +29 -0
  20. data/fixtures/scenarios/02_stateful_client/replacement/broken.rb +14 -0
  21. data/fixtures/scenarios/02_stateful_client/replacement/good.rb +23 -0
  22. data/fixtures/scenarios/02_stateful_client/spec/client_spec.rb +31 -0
  23. data/fixtures/scenarios/03_external_boundary/adapter.rb +13 -0
  24. data/fixtures/scenarios/03_external_boundary/boundary.rb +11 -0
  25. data/fixtures/scenarios/03_external_boundary/legacy/dead_formatter.rb +7 -0
  26. data/fixtures/scenarios/03_external_boundary/legacy/receipt.rb +11 -0
  27. data/fixtures/scenarios/03_external_boundary/replacement/broken.rb +11 -0
  28. data/fixtures/scenarios/03_external_boundary/replacement/good.rb +11 -0
  29. data/fixtures/scenarios/03_external_boundary/spec/receipt_spec.rb +10 -0
  30. data/fixtures/scenarios/04_intentional_divergence/adapter.rb +12 -0
  31. data/fixtures/scenarios/04_intentional_divergence/adapter_unwaived.rb +10 -0
  32. data/fixtures/scenarios/04_intentional_divergence/boundary.rb +8 -0
  33. data/fixtures/scenarios/04_intentional_divergence/legacy/dead_identity.rb +7 -0
  34. data/fixtures/scenarios/04_intentional_divergence/legacy/identity.rb +9 -0
  35. data/fixtures/scenarios/04_intentional_divergence/replacement/broken.rb +9 -0
  36. data/fixtures/scenarios/04_intentional_divergence/replacement/good.rb +9 -0
  37. data/fixtures/scenarios/04_intentional_divergence/spec/identity_spec.rb +10 -0
  38. data/fixtures/scenarios/05_formal_negative/adapter.rb +15 -0
  39. data/fixtures/scenarios/05_formal_negative/boundary.rb +16 -0
  40. data/fixtures/scenarios/05_formal_negative/legacy/dead_lock.rb +5 -0
  41. data/fixtures/scenarios/05_formal_negative/legacy/turnstile.rb +24 -0
  42. data/fixtures/scenarios/05_formal_negative/replacement/broken.rb +18 -0
  43. data/fixtures/scenarios/05_formal_negative/replacement/formal_broken.rb +24 -0
  44. data/fixtures/scenarios/05_formal_negative/replacement/good.rb +24 -0
  45. data/fixtures/scenarios/05_formal_negative/spec/turnstile_spec.rb +21 -0
  46. data/lib/bparity/adapter.rb +115 -0
  47. data/lib/bparity/adequacy.rb +78 -0
  48. data/lib/bparity/boundary.rb +97 -0
  49. data/lib/bparity/cli/formal_commands.rb +428 -0
  50. data/lib/bparity/cli/verification_commands.rb +242 -0
  51. data/lib/bparity/cli.rb +262 -0
  52. data/lib/bparity/corpus.rb +45 -0
  53. data/lib/bparity/errors.rb +12 -0
  54. data/lib/bparity/formal/assumptions.rb +131 -0
  55. data/lib/bparity/formal/bounded.rb +543 -0
  56. data/lib/bparity/formal/contract.rb +81 -0
  57. data/lib/bparity/formal/deductive.rb +481 -0
  58. data/lib/bparity/formal/lts.rb +344 -0
  59. data/lib/bparity/formal/result.rb +50 -0
  60. data/lib/bparity/formal.rb +8 -0
  61. data/lib/bparity/recording.rb +412 -0
  62. data/lib/bparity/reporting.rb +128 -0
  63. data/lib/bparity/spec_bundle.rb +208 -0
  64. data/lib/bparity/synthesis.rb +487 -0
  65. data/lib/bparity/verification.rb +310 -0
  66. data/lib/bparity/version.rb +5 -0
  67. data/lib/bparity.rb +42 -0
  68. metadata +125 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7b9f47440ae17ece5214893bb3f2e012f069fba252bced3580da8dae06e9a808
4
+ data.tar.gz: b529651ce97730707485285311831a8f1d2b17fc14150f86c7bf15c4a74d953d
5
+ SHA512:
6
+ metadata.gz: 78738e2d8f3c8d972fb26e723f311f7710fe1beb8c3d982054882b76ffbcdff5196d4824e9f3f721279c84093d73adf364c5f4a34196f4e8257f9bba9ee8e5a4
7
+ data.tar.gz: ac3460337046a7f8b6a794ae238d0deb67c80eb7d0373526982fb3af56a1d73085c387f9de156e4166a81f4eebe9c4f37f7f383f23c340bd1e3cca376d678cdd
data/.rubocop.yml ADDED
@@ -0,0 +1,61 @@
1
+ plugins:
2
+ - rubocop-rspec
3
+
4
+ AllCops:
5
+ CacheRootDirectory: tmp/rubocop_cache
6
+ NewCops: enable
7
+ SuggestExtensions: false
8
+ TargetRubyVersion: 3.1
9
+ Exclude:
10
+ - ".idea/**/*"
11
+ - "fixtures/**/*"
12
+ - "tmp/**/*"
13
+ - "vendor/**/*"
14
+
15
+ Style/StringLiterals:
16
+ EnforcedStyle: double_quotes
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Metrics/AbcSize:
22
+ Max: 55
23
+
24
+ Metrics/BlockLength:
25
+ Exclude:
26
+ - "spec/**/*"
27
+ - "*.gemspec"
28
+ - "Rakefile"
29
+
30
+ Metrics/ClassLength:
31
+ Max: 300
32
+
33
+ Metrics/CyclomaticComplexity:
34
+ Max: 25
35
+
36
+ Metrics/MethodLength:
37
+ Max: 35
38
+
39
+ Metrics/ModuleLength:
40
+ Max: 250
41
+
42
+ Metrics/ParameterLists:
43
+ Max: 12
44
+
45
+ Metrics/PerceivedComplexity:
46
+ Max: 15
47
+
48
+ RSpec/ExampleLength:
49
+ Max: 30
50
+
51
+ RSpec/MultipleExpectations:
52
+ Max: 8
53
+
54
+ RSpec/SpecFilePathFormat:
55
+ Enabled: false
56
+
57
+ RSpec/VariableDefinition:
58
+ Enabled: false
59
+
60
+ RSpec/VariableName:
61
+ Enabled: false
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Yudai Takada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # bparity
2
+
3
+ `bparity` captures the observable behavior of a legacy Ruby API, stores it as a portable specification bundle, and checks a structurally different replacement through a small adapter. The legacy dependency is not needed during verification.
4
+
5
+ ## Preserve the legacy environment first
6
+
7
+ If the old dependency can still run, preserve it before installing anything else:
8
+
9
+ ```bash
10
+ bundle package --all
11
+ bundle exec bparity init --timecapsule
12
+ ```
13
+
14
+ Commit `vendor/cache`, the generated time-capsule Dockerfile, the behavior corpus, and the Specification Bundle. The replacement-side verifier does not load the legacy dependency.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ bundle add bparity
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Get an initial boundary proposal from calls made by an exercise script. Discovery observes Ruby calls only; C calls are intentionally excluded:
25
+
26
+ ```bash
27
+ bundle exec bparity discover --require lib/legacy.rb --target Legacy::Slugifier script/exercise_legacy.rb
28
+ ```
29
+
30
+ Create `.bparity/boundary.rb` in the legacy environment:
31
+
32
+ ```ruby
33
+ Bparity.boundary do
34
+ observe "Legacy::Slugifier" do
35
+ methods :call
36
+ end
37
+ driver :rspec, files: "spec/**/*_spec.rb"
38
+ end
39
+ ```
40
+
41
+ Record and synthesize a checked specification bundle while the legacy environment still works:
42
+
43
+ ```bash
44
+ bundle exec bparity record --require lib/legacy.rb
45
+ bundle exec bparity synthesize --coverage .bparity/coverage.json \
46
+ --tests 'spec/**/*_spec.rb' --source 'lib/**/*.rb'
47
+ ```
48
+
49
+ If the legacy runtime is already unavailable, extract only executable static assertions and mark the missing runtime evidence as a gap:
50
+
51
+ ```bash
52
+ bundle exec bparity synthesize --static-only --tests 'spec/**/*_spec.rb' --source 'lib/**/*.rb'
53
+ ```
54
+
55
+ Commit `.bparity/spec_bundle.yml`. In the replacement environment, map its abstract operations to the new API:
56
+
57
+ ```ruby
58
+ Bparity.adapter(spec: ".bparity/spec_bundle.yml") do
59
+ subject "Slugifier" do
60
+ construct { NewSlugService }
61
+ operation "#call" do
62
+ invoke { |service, args, _kwargs| service.generate(text: args.fetch(0)) }
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ The verification step requires only the bundle, adapter, and replacement:
69
+
70
+ ```bash
71
+ bundle exec bparity verify --require lib/new_slug_service.rb \
72
+ --runners replay,property,model --fail-under 100
73
+ ```
74
+
75
+ `replay` checks recorded examples, `property` generates boundary inputs for declared contracts, and `model` checks a stateful replacement against the learned LTS. `differential` additionally requires `--old-command` and `--new-command`; both commands read one JSON input from stdin and print one JSON observation. For same-named classes and public methods, omit `--adapter` to use direct replay. An explicitly supplied but missing adapter is an error.
76
+
77
+ Reports support `markdown`, `json`, `junit`, and `html`. A changed bundle checksum is rejected; intentional incompatibilities must be declared with `waive` in the adapter. Skipped model checks do not count toward `--fail-under`, and a run with only skipped checks fails.
78
+
79
+ Generate an adapter skeleton when the bundle is ready:
80
+
81
+ ```bash
82
+ bundle exec bparity init --from-spec .bparity/spec_bundle.yml
83
+ ```
84
+
85
+ Run a bounded exhaustive comparison while both implementations are available:
86
+
87
+ ```bash
88
+ bundle exec bparity prove --level f2 --scope size=3,depth=2 \
89
+ --require lib/legacy.rb --require lib/replacement.rb \
90
+ --counterexample-out spec/f2_counterexample_spec.rb --promote-invariants
91
+ ```
92
+
93
+ The result uses `no_difference_found`, `difference_found`, or `inconclusive`; it always includes the enumerated case count, scope, assumptions, and excluded inputs.
94
+ If the legacy class is unavailable, F2 checks the replacement against declared postconditions and invariants instead. It refuses to run when neither legacy execution nor contracts are available. Domain generation is bounded by `--max-cases`; any truncation is reported as `inconclusive`, never exhaustive.
95
+
96
+ For a stateful subject with `state` projections in both the boundary and adapter, compare learned finite models:
97
+
98
+ ```bash
99
+ bundle exec bparity prove --level f3 --equivalence trace \
100
+ --require lib/replacement.rb --export-lts tmp/client
101
+ ```
102
+
103
+ F3 reports both model sizes, the learned alphabet, whether exploration was exact, and the shortest distinguishing sequence. `--counterexample-out spec/f3_counterexample_spec.rb` writes that sequence as a regression example. The claim applies only to the projected models.
104
+
105
+ For an explicitly selected pure Ruby fragment, F4 translates both methods to SMT-LIB 2 and invokes Z3:
106
+
107
+ ```bash
108
+ bundle exec bparity prove --level f4 --solver z3 --validate-translation \
109
+ --old-source lib/legacy_math.rb --old-method double \
110
+ --new-source lib/new_math.rb --new-method twice --types Integer \
111
+ --require lib/legacy_math.rb --require lib/new_math.rb \
112
+ --counterexample-out spec/f4_counterexample_spec.rb
113
+ ```
114
+
115
+ Translation validation is mandatory. Unsupported Ruby, a translation mismatch, a missing Z3 executable, `unknown`, and solver failures all produce `inconclusive`, never PASS. The implemented fragment covers pure Integer/Boolean/String expressions and conditionals.
116
+
117
+ Run the five-point adequacy assessment, optionally with the soft `mutant` integration:
118
+
119
+ ```bash
120
+ bundle exec bparity adequacy --require lib/replacement.rb
121
+ bundle exec bparity adequacy --require lib/replacement.rb --mutant
122
+ ```
123
+
124
+ ## Assurance limits
125
+
126
+ A successful replay means no difference was found for the recorded examples (F0). It does not prove complete program equivalence. Formal checks always report their bounded scope, assumptions, and unverified areas; solver `unknown` and timeouts are inconclusive, never PASS. See [Formal assurance limits](docs/formal_assurance_limits.md).
127
+
128
+ ## Reproduce the fixtures
129
+
130
+ The acceptance suite records, synthesizes, and verifies all five scenarios. Each has a structurally different correct replacement and an intentionally broken replacement:
131
+
132
+ ```bash
133
+ bundle exec rspec spec/acceptance_spec.rb
134
+ ```
135
+
136
+ ## Development
137
+
138
+ After checking out the repository, run `bundle install` and `bundle exec rake`.
139
+
140
+ ## Contributing
141
+
142
+ Bug reports and pull requests are welcome at https://github.com/ydah/bparity.
143
+
144
+ ## License
145
+
146
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+ require "tmpdir"
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+ RuboCop::RakeTask.new
10
+
11
+ task default: %i[spec rubocop]
12
+
13
+ desc "Measure recording overhead against the pure-function fixture"
14
+ task :benchmark_recording do
15
+ plain = [RbConfig.ruby, "-Ilib", "-r./fixtures/scenarios/01_pure_function/legacy/slugifier.rb",
16
+ "-S", "rspec", "fixtures/scenarios/01_pure_function/spec/slugifier_spec.rb"]
17
+ Dir.mktmpdir do |directory|
18
+ recorded = [RbConfig.ruby, "-Ilib", "exe/bparity", "record", "--boundary",
19
+ "fixtures/scenarios/01_pure_function/boundary.rb", "--out", File.join(directory, "corpus.jsonl"),
20
+ "--coverage", File.join(directory, "coverage.json"), "--require",
21
+ "fixtures/scenarios/01_pure_function/legacy/slugifier.rb"]
22
+ median = lambda do |command|
23
+ 5.times.map do
24
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
25
+ abort "Benchmark command failed: #{command.join(' ')}" unless system(*command, out: File::NULL, err: File::NULL)
26
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
27
+ end.sort[2]
28
+ end
29
+ baseline = median.call(plain)
30
+ instrumented = median.call(recorded)
31
+ overhead = ((instrumented / baseline) - 1) * 100
32
+ puts format("Recording overhead: %<overhead>.1f%% (plain %<plain>.3fs, recorded %<recorded>.3fs)",
33
+ overhead:, plain: baseline, recorded: instrumented)
34
+ abort "Recording overhead exceeds 50%" if overhead > 50
35
+ end
36
+ end
@@ -0,0 +1,25 @@
1
+ # Application example
2
+
3
+ The repository's five fixture scenarios are the reference application:
4
+
5
+ 1. `01_pure_function` records a slugifier that calls a retired transliterator and exercises replay and F2.
6
+ 2. `02_stateful_client` maps a stateful legacy client to a differently shaped session API.
7
+ 3. `03_external_boundary` verifies the replacement's interaction with an external boundary, not only its return value.
8
+ 4. `04_intentional_divergence` demonstrates that an undeclared change fails and an explicit adapter waiver is reported as `WAIVED`.
9
+ 5. `05_formal_negative` changes only a state transition; replay and F3 detect it and F3 emits a distinguishing sequence.
10
+
11
+ Run the complete application check with:
12
+
13
+ ```bash
14
+ bundle exec rspec spec/acceptance_spec.rb
15
+ ```
16
+
17
+ The acceptance process launches each CLI phase in a fresh Ruby process. This also demonstrates that verification needs the Specification Bundle, adapter, and replacement only; the legacy dependency is loaded solely while recording.
18
+
19
+ Recording overhead is checked separately against the pure-function fixture:
20
+
21
+ ```bash
22
+ bundle exec rake benchmark_recording
23
+ ```
24
+
25
+ The task uses five runs and fails when median end-to-end overhead exceeds 50%.
@@ -0,0 +1,21 @@
1
+ # Formal assurance limits
2
+
3
+ `bparity` never reports unrestricted program equivalence. Every formal result has one of three verdicts: `no_difference_found`, `difference_found`, or `inconclusive`. It also carries its scope, assumptions, excluded behavior, and any counterexample.
4
+
5
+ ## Levels
6
+
7
+ - F0 checks only recorded calls. Unrecorded inputs and operation sequences remain unverified.
8
+ - F1 executes declared first-order contracts. A passing result applies only to executed paths.
9
+ - F2 enumerates every value in the reported finite domain. Case, time, or domain-construction limits make the result `inconclusive`; they are never described as exhaustive. When the legacy implementation is unavailable, the claim is limited to the declared postconditions and invariants.
10
+ - F3 compares projected finite-state models. The report includes both model sizes, the learned alphabet, whether exploration completed, and a reminder that this is a model claim rather than a whole-implementation claim.
11
+ - F4 applies only to the declared pure fragment. It requires bounded translation validation before Z3 is consulted. Unsupported Ruby, translation disagreement, solver absence, timeout, or `unknown` invalidates the F4 claim.
12
+
13
+ ## Assumptions
14
+
15
+ Formal output names its assumptions. H1 freezes the verified class definitions, H3 excludes dynamic evaluation from the verified fragment, H6 models exceptions as outputs, and H7 currently declares single-threaded execution without runtime enforcement. A detected assumption violation invalidates the result.
16
+
17
+ ## Reading a successful result
18
+
19
+ `no_difference_found` means no counterexample exists in the exact reported scope under the listed assumptions. It says nothing about larger F2 values, unprojected F3 state, operations outside the learned alphabet, unsupported F4 Ruby, concurrency, process effects, timing, GC, or object identity.
20
+
21
+ Use the five-point assessment alongside the verdict: specification coverage, generated checks, formal reach, mutation constraint strength, and residual risks. Waivers remain visible and require explicit adapter declarations.
data/exe/bparity ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bparity"
5
+ require "bparity/cli"
6
+
7
+ exit Bparity::CLI.start
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.adapter do
4
+ external "DeadGem::Transliterator" => "Replacement::Transliterator"
5
+
6
+ subject "Slugifier" do
7
+ construct { Replacement::Slugs }
8
+ operation "#call" do
9
+ invoke { |subject, args, _kwargs| subject.generate(text: args.fetch(0)) }
10
+ map_error { |error| { class: error.class.name, message: error.message, cause: error.cause&.class&.name } }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.boundary do
4
+ observe "Legacy::Slugifier" do
5
+ methods :call
6
+ end
7
+
8
+ external "DeadGem::Transliterator" do
9
+ methods :transliterate
10
+ end
11
+
12
+ driver :rspec, files: "fixtures/scenarios/01_pure_function/spec/**/*_spec.rb"
13
+ formal do
14
+ pure_fragment "Legacy::Slugifier#call"
15
+ bounded_scope size: 3, depth: 2
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DeadGem
4
+ class Transliterator
5
+ def transliterate(value)
6
+ value.encode("ASCII", invalid: :replace, undef: :replace, replace: "")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dead_gem"
4
+
5
+ module Legacy
6
+ class Slugifier
7
+ def initialize
8
+ @transliterator = DeadGem::Transliterator.new
9
+ end
10
+
11
+ def call(value)
12
+ raise ArgumentError, "blank" if value.nil? || value.strip.empty?
13
+
14
+ @transliterator.transliterate(value).downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-|\-\z/, "")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Transliterator
5
+ def transliterate(value) = value
6
+ end
7
+
8
+ module Slugs
9
+ module_function
10
+
11
+ def generate(text:)
12
+ Transliterator.new.transliterate(text).downcase.gsub(/[^a-z0-9]+/, "-")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Transliterator
5
+ def transliterate(value)
6
+ value.encode("ASCII", invalid: :replace, undef: :replace, replace: "")
7
+ end
8
+ end
9
+
10
+ module Slugs
11
+ module_function
12
+
13
+ def generate(text:)
14
+ raise ArgumentError, "blank" if text.nil? || text.strip.empty?
15
+
16
+ Transliterator.new.transliterate(text).downcase.gsub(/[^a-z0-9]+/, "-").gsub(/\A-|\-\z/, "")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec"
4
+ require_relative "../legacy/slugifier"
5
+
6
+ RSpec.describe Legacy::Slugifier do
7
+ subject(:slugifier) { described_class.new }
8
+
9
+ it "converts spaces to hyphens" do
10
+ expect(slugifier.call("Hello World")).to eq("hello-world")
11
+ end
12
+
13
+ it "removes punctuation at both ends" do
14
+ expect(slugifier.call(" Hi! ")).to eq("hi")
15
+ end
16
+
17
+ it "rejects blank input" do
18
+ expect { slugifier.call("") }.to raise_error(ArgumentError, "blank")
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "minitest/test"
4
+ require_relative "../legacy/slugifier"
5
+
6
+ class SlugifierTest < Minitest::Test
7
+ def test_normalizes_a_title
8
+ assert_equal "hello-world", Legacy::Slugifier.new.call("Hello World")
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.adapter do
4
+ subject "Client" do
5
+ construct { Replacement::Session.new }
6
+ state { |session| { open: session.phase == :ready } }
7
+
8
+ operation("#connect") do
9
+ invoke { |session, _args, _kwargs| session.start; true }
10
+ end
11
+ operation("#fetch") do
12
+ invoke { |session, _args, _kwargs| session.query }
13
+ map_error { |error| { class: "Legacy::NotConnectedError", message: error.message } }
14
+ end
15
+ operation("#close") do
16
+ invoke { |session, _args, _kwargs| session.shutdown; nil }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.boundary do
4
+ observe "Legacy::Client" do
5
+ methods :connect, :fetch, :close
6
+ state { |client| { open: client.status == :open } }
7
+ end
8
+ driver :rspec, files: "fixtures/scenarios/02_stateful_client/spec/**/*_spec.rb"
9
+ formal { lts_learning :active }
10
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legacy
4
+ class NotConnectedError < StandardError; end
5
+
6
+ class Client
7
+ attr_reader :status
8
+
9
+ def initialize
10
+ @status = :closed
11
+ end
12
+
13
+ def connect
14
+ @status = :open
15
+ true
16
+ end
17
+
18
+ def fetch
19
+ raise NotConnectedError, "client is closed" unless status == :open
20
+
21
+ :data
22
+ end
23
+
24
+ def close
25
+ @status = :closed
26
+ nil
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Disconnected < StandardError; end
5
+
6
+ class Session
7
+ attr_reader :phase
8
+
9
+ def initialize = @phase = :down
10
+ def start = @phase = :ready
11
+ def query = :data
12
+ def shutdown = nil
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Disconnected < StandardError; end
5
+
6
+ class Session
7
+ attr_reader :phase
8
+
9
+ def initialize
10
+ @phase = :down
11
+ end
12
+
13
+ def start = @phase = :ready
14
+
15
+ def query
16
+ raise Disconnected, "client is closed" unless phase == :ready
17
+
18
+ :data
19
+ end
20
+
21
+ def shutdown = @phase = :down
22
+ end
23
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec"
4
+ require_relative "../legacy/client"
5
+
6
+ RSpec.describe Legacy::Client do
7
+ subject(:client) { described_class.new }
8
+
9
+ it "fetches after connecting" do
10
+ expect(client.connect).to be(true)
11
+ expect(client.fetch).to eq(:data)
12
+ end
13
+
14
+ it "rejects fetch while closed" do
15
+ expect { client.fetch }.to raise_error(Legacy::NotConnectedError)
16
+ end
17
+
18
+ it "closes an open client" do
19
+ client.connect
20
+ expect(client.close).to be_nil
21
+ end
22
+
23
+ it "allows repeated connect" do
24
+ client.connect
25
+ expect(client.connect).to be(true)
26
+ end
27
+
28
+ it "allows close while already closed" do
29
+ expect(client.close).to be_nil
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.adapter do
4
+ external "RetiredFormatter::Money" => "Replacement::Currency" do
5
+ map_call(:render) { |call| call.merge("method" => "format") }
6
+ end
7
+ subject "Receipt" do
8
+ construct { Replacement::ReceiptView.new }
9
+ operation "#print" do
10
+ invoke { |view, args, _kwargs| view.render(total: args.fetch(0)) }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bparity.boundary do
4
+ observe "Legacy::Receipt" do
5
+ methods :print
6
+ end
7
+ external "RetiredFormatter::Money" do
8
+ methods :format
9
+ end
10
+ driver :rspec, files: "fixtures/scenarios/03_external_boundary/spec/**/*_spec.rb"
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RetiredFormatter
4
+ class Money
5
+ def format(cents) = "$#{cents / 100}"
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dead_formatter"
4
+
5
+ module Legacy
6
+ class Receipt
7
+ def print(cents)
8
+ "Total: #{RetiredFormatter::Money.new.format(cents)}"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Currency
5
+ def render(cents) = "$#{cents / 100}"
6
+ end
7
+
8
+ class ReceiptView
9
+ def render(total:) = "Total: $#{total / 100}"
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Replacement
4
+ class Currency
5
+ def render(cents) = "$#{cents / 100}"
6
+ end
7
+
8
+ class ReceiptView
9
+ def render(total:) = "Total: #{Currency.new.render(total)}"
10
+ end
11
+ end