-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor in order to add support for class variables
- Loading branch information
Showing
10 changed files
with
129 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
## Main | ||
|
||
Nothing so far | ||
#### Additions | ||
* Refactor to support class variables as well | ||
|
||
## 0.1.0 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module Minitest | ||
module Substitute | ||
class Substitutor | ||
|
||
EVAL_METHODS = [:instance_eval, :instance_eval, :class_eval].freeze | ||
|
||
def initialize(variable, substitute, on:) | ||
@variable, @substitute, @on = variable, substitute, on | ||
@original = get | ||
end | ||
|
||
def commit | ||
set @substitute | ||
end | ||
|
||
def rollback | ||
set @original | ||
end | ||
|
||
private | ||
|
||
def eval_method | ||
EVAL_METHODS[@variable.count('@')] | ||
end | ||
|
||
def get | ||
@on.send(eval_method, @variable.to_s) | ||
end | ||
|
||
def set(value) | ||
@on.send(eval_method, "#{@variable} = value") | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../spec_helper' | ||
require_relative '../../../factory' | ||
|
||
describe :eval_method do | ||
subject do | ||
Minitest::Substitute::Substitutor | ||
end | ||
|
||
it "returns instance_eval for instance variables" do | ||
_(subject.new('@version', nil, on: $_spec_config_instance).send(:eval_method)).must_equal :instance_eval | ||
end | ||
|
||
it "returns class_eval for class variables" do | ||
_(subject.new('@@counter', nil, on: Config).send(:eval_method)).must_equal :class_eval | ||
end | ||
|
||
it "returns instance_eval for global variables" do | ||
_(subject.new('$foobar', nil, on: Object).send(:eval_method)).must_equal :instance_eval | ||
end | ||
|
||
it "returns instance_eval for global accessors" do | ||
_(subject.new('ENV', nil, on: Object).send(:eval_method)).must_equal :instance_eval | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters