betamax 0.1.1 → 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/CHANGELOG.md +5 -0
- data/README.md +51 -3
- data/Rakefile +9 -4
- data/lib/betamax/errors.rb +2 -2
- data/lib/betamax/minitest.rb +27 -0
- data/lib/betamax/player.rb +2 -0
- data/lib/betamax/rspec.rb +1 -1
- data/lib/betamax/tape.rb +8 -8
- data/lib/betamax/version.rb +1 -1
- data/lib/betamax.rb +14 -13
- metadata +4 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b13224e3fa5f820802a68dc000dbabab4a023eff18250e3b0af5e9fb36f98092
|
|
4
|
+
data.tar.gz: 70fb0889db8f8b4df8b7b60527895f27d806bda46164990670cec559e5e0b29c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0698af118ee8b4a7900e8fa490548b9967e4c91c5fa1cb7d7543b5f55aaf71ab1993a8b6efdfa80467c830e86f24227be5f42e3b0509bb628657914c49f01003'
|
|
7
|
+
data.tar.gz: ea48915b0269d20c9810d7a65a8519c0b9a6b84cdab6dd918822ba92da084fbd427b0d2145ce7f27757a5bacf39d9476a7646e0fcf2fbbbcbff2a26d64db51e6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Betamax
|
|
2
2
|
|
|
3
|
-
Betamax allows for the recording and playback of arbitrary Ruby objects to simplify testing external dependencies.
|
|
3
|
+
Betamax allows for the recording and playback of arbitrary Ruby objects to simplify testing external dependencies. Think of it like the [VCR](https://github.com/vcr/vcr) gem, but for any complicated object, not just HTTP interactions. This gem was originally developed to write a test suite for a MiniDisc NetMD Walkman library, where the recording and playback of real USB traffic was desired.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -18,11 +18,59 @@ gem install betamax
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
Currently this gem supports testing with RSpec. First, require the gem in your spec helper:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# File: spec/spec_helper.rb
|
|
25
|
+
|
|
26
|
+
require 'betamax'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Then, record an object. The first time you run your test, a recording will be created in `spec/betamax_tapes/`. Subsequent test runs will playback from your recording. Make sure to tag your example as `:betamax` to enable recording and playback.
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
RSpec.describe 'a MiniDisc device' do
|
|
33
|
+
let(:walkman) do
|
|
34
|
+
usb_device = LibUSB::Context.devices(idProduct: 0x0084).first
|
|
35
|
+
|
|
36
|
+
MiniDisc::Device.new usb_device: Betamax.record(usb_device)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'has a tracklist', :betamax do
|
|
40
|
+
walkman.open do |disc|
|
|
41
|
+
disc.tracks.first.title.should == 'Song of the Highwire Shrimper'
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
In the above example, the test will continue to work even if the USB device is no longer present. If for some reason changes to your code change the interaction with your recorded object, your test will fail. This could be because methods were called in a different order, block arguments changed, method calls were expected but did not take place, etc.
|
|
49
|
+
|
|
50
|
+
### Minitest
|
|
51
|
+
|
|
52
|
+
Include `Betamax::Minitest` in your test class and surround the recorded interaction with `with_betamax`. Tapes are named after the test class and method and stored in `test/betamax_tapes/` (override with `tapes_folder:`).
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class WalkmanTest < Minitest::Test
|
|
56
|
+
include Betamax::Minitest
|
|
57
|
+
|
|
58
|
+
def test_tracklist
|
|
59
|
+
with_betamax do
|
|
60
|
+
usb_device = LibUSB::Context.devices(idProduct: 0x0084).first
|
|
61
|
+
walkman = MiniDisc::Device.new usb_device: Betamax.record(usb_device)
|
|
62
|
+
|
|
63
|
+
walkman.open do |disc|
|
|
64
|
+
assert_equal 'Song of the Highwire Shrimper', disc.tracks.first.title
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
```
|
|
22
70
|
|
|
23
71
|
## Development
|
|
24
72
|
|
|
25
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake
|
|
73
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the RSpec and Minitest suites and RuboCop. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
26
74
|
|
|
27
75
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
28
76
|
|
data/Rakefile
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require 'bundler/gem_tasks'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'rspec/core/rake_task'
|
|
4
|
+
require 'rubocop/rake_task'
|
|
3
5
|
|
|
4
6
|
RSpec::Core::RakeTask.new :spec
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
Rake::TestTask.new :test do |task|
|
|
9
|
+
task.libs << 'lib'
|
|
10
|
+
task.pattern = 'test/**/*_test.rb'
|
|
11
|
+
end
|
|
7
12
|
|
|
8
13
|
RuboCop::RakeTask.new
|
|
9
14
|
|
|
10
|
-
task default: %i[spec rubocop]
|
|
15
|
+
task default: %i[spec test rubocop]
|
data/lib/betamax/errors.rb
CHANGED
|
@@ -3,7 +3,7 @@ module Betamax
|
|
|
3
3
|
class Error < StandardError; end
|
|
4
4
|
|
|
5
5
|
class NoTapeInserted < Error
|
|
6
|
-
def initialize message =
|
|
6
|
+
def initialize message = 'No tape inserted'
|
|
7
7
|
super
|
|
8
8
|
end
|
|
9
9
|
end
|
|
@@ -51,7 +51,7 @@ module Betamax
|
|
|
51
51
|
|
|
52
52
|
class UnusedRecordings < PlaybackError
|
|
53
53
|
def initialize unused_methods
|
|
54
|
-
method_names = unused_methods.map(&:method_name).join(
|
|
54
|
+
method_names = unused_methods.map(&:method_name).join(', ')
|
|
55
55
|
super "Recording has #{unused_methods.size} unused method(s): #{method_names}"
|
|
56
56
|
end
|
|
57
57
|
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Betamax
|
|
2
|
+
module Minitest
|
|
3
|
+
DEFAULT_TAPES_FOLDER = Pathname.new 'test/betamax_tapes/'
|
|
4
|
+
|
|
5
|
+
# Adapts a Minitest test and block to the example interface Player expects.
|
|
6
|
+
class Example
|
|
7
|
+
attr_reader :metadata
|
|
8
|
+
|
|
9
|
+
def initialize test, &block
|
|
10
|
+
@metadata = { description: test.name, example_group: { description: test.class.name } }
|
|
11
|
+
@block = block
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def run
|
|
15
|
+
@block.call
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def exception
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def with_betamax(tapes_folder: DEFAULT_TAPES_FOLDER, &)
|
|
24
|
+
Player.new(Example.new(self, &), tapes_folder: Pathname.new(tapes_folder)).play
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/lib/betamax/player.rb
CHANGED
data/lib/betamax/rspec.rb
CHANGED
data/lib/betamax/tape.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
require
|
|
2
|
-
require
|
|
1
|
+
require 'fileutils'
|
|
2
|
+
require 'yaml'
|
|
3
3
|
|
|
4
4
|
module Betamax
|
|
5
5
|
class Tape
|
|
@@ -20,11 +20,11 @@ module Betamax
|
|
|
20
20
|
|
|
21
21
|
def self.from_rspec_example example, tapes_folder:
|
|
22
22
|
name = cassette_name_for example.metadata
|
|
23
|
-
name.gsub!
|
|
24
|
-
name.gsub!(/[\s-]+/,
|
|
25
|
-
name.gsub! %r{[^a-zA-Z0-9_/]},
|
|
23
|
+
name.gsub! '::', '_'
|
|
24
|
+
name.gsub!(/[\s-]+/, '_')
|
|
25
|
+
name.gsub! %r{[^a-zA-Z0-9_/]}, ''
|
|
26
26
|
|
|
27
|
-
filename = Pathname.new(name).sub_ext
|
|
27
|
+
filename = Pathname.new(name).sub_ext '.yaml'
|
|
28
28
|
full_path = tapes_folder / filename
|
|
29
29
|
|
|
30
30
|
new full_path
|
|
@@ -44,7 +44,7 @@ module Betamax
|
|
|
44
44
|
current = current[:parent_example_group]
|
|
45
45
|
end
|
|
46
46
|
|
|
47
|
-
description_parts.join
|
|
47
|
+
description_parts.join '/'
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
def load
|
|
@@ -66,7 +66,7 @@ module Betamax
|
|
|
66
66
|
version: Recording::VERSION,
|
|
67
67
|
objects: { default: proxy }
|
|
68
68
|
|
|
69
|
-
File.open @path,
|
|
69
|
+
File.open @path, 'w' do |file|
|
|
70
70
|
file.puts recording.to_yaml
|
|
71
71
|
end
|
|
72
72
|
end
|
data/lib/betamax/version.rb
CHANGED
data/lib/betamax.rb
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
require
|
|
2
|
-
|
|
3
|
-
require_relative
|
|
4
|
-
require_relative
|
|
5
|
-
require_relative
|
|
6
|
-
require_relative
|
|
7
|
-
require_relative
|
|
8
|
-
require_relative
|
|
9
|
-
require_relative
|
|
10
|
-
require_relative
|
|
11
|
-
require_relative
|
|
12
|
-
require_relative
|
|
13
|
-
require_relative
|
|
1
|
+
require 'pathname'
|
|
2
|
+
|
|
3
|
+
require_relative 'betamax/errors'
|
|
4
|
+
require_relative 'betamax/method_player'
|
|
5
|
+
require_relative 'betamax/method_recorder'
|
|
6
|
+
require_relative 'betamax/minitest'
|
|
7
|
+
require_relative 'betamax/player'
|
|
8
|
+
require_relative 'betamax/recorded_method'
|
|
9
|
+
require_relative 'betamax/recorded_object'
|
|
10
|
+
require_relative 'betamax/recorded_yielding'
|
|
11
|
+
require_relative 'betamax/recording'
|
|
12
|
+
require_relative 'betamax/rspec'
|
|
13
|
+
require_relative 'betamax/tape'
|
|
14
|
+
require_relative 'betamax/version'
|
|
14
15
|
|
|
15
16
|
module Betamax
|
|
16
17
|
extend RSpec
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: betamax
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John DeSilva
|
|
@@ -25,6 +25,7 @@ files:
|
|
|
25
25
|
- lib/betamax/errors.rb
|
|
26
26
|
- lib/betamax/method_player.rb
|
|
27
27
|
- lib/betamax/method_recorder.rb
|
|
28
|
+
- lib/betamax/minitest.rb
|
|
28
29
|
- lib/betamax/player.rb
|
|
29
30
|
- lib/betamax/recorded_method.rb
|
|
30
31
|
- lib/betamax/recorded_object.rb
|
|
@@ -48,14 +49,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
48
49
|
requirements:
|
|
49
50
|
- - ">="
|
|
50
51
|
- !ruby/object:Gem::Version
|
|
51
|
-
version: 3.
|
|
52
|
+
version: '3.3'
|
|
52
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
54
|
requirements:
|
|
54
55
|
- - ">="
|
|
55
56
|
- !ruby/object:Gem::Version
|
|
56
57
|
version: '0'
|
|
57
58
|
requirements: []
|
|
58
|
-
rubygems_version: 4.0.
|
|
59
|
+
rubygems_version: 4.0.16
|
|
59
60
|
specification_version: 4
|
|
60
61
|
summary: Record and playback of arbitrary Ruby objects
|
|
61
62
|
test_files: []
|