rails_zen 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ require 'rails_zen/given_model_gen'
2
+
3
+ RSpec.describe RailsZen::GivenModelGen do
4
+
5
+ let(:given_model_gen) {
6
+ RailsZen::GivenModelGen.new("user", "name:string email:string phone:integer")
7
+ }
8
+
9
+ it "#attrs returns just attributes' name without types " do
10
+ expect(given_model_gen.attrs).to eq ["name", "email", "phone"]
11
+ end
12
+
13
+ describe "#chosen_attrs" do
14
+
15
+ context "when no simple attributes is given by user" do
16
+ it "returns attrs(object) that needs validations, extra specifications" do
17
+
18
+ expect(given_model_gen.chosen_attrs).to match_array([
19
+ an_object_having_attributes(class: RailsZen::ChosenAttr, name:"name", type: "string"),
20
+ an_object_having_attributes(class: RailsZen::ChosenAttr, name:"email", type: "string"),
21
+ an_object_having_attributes(class: RailsZen::ChosenAttr, name:"phone", type: "integer")
22
+ ])
23
+ end
24
+ end
25
+ context "when no simple attributes is given by user" do
26
+
27
+ it "returns attrs(object) that needs validations, extra specifications except simple attributes" do
28
+
29
+ given_model_gen.simple_attributes = "0".split
30
+ expect(given_model_gen.chosen_attrs).to match_array([
31
+ an_object_having_attributes(class: RailsZen::ChosenAttr, name:"email", type: "string"),
32
+ an_object_having_attributes(class: RailsZen::ChosenAttr, name:"phone", type: "integer")
33
+ ])
34
+ end
35
+ end
36
+ end
37
+ describe "#ask_for_has_many_relations" do
38
+
39
+ before do
40
+ @loc = double('file')
41
+ set_stream(StringIO.new("posts\n"))
42
+ @file = String.new
43
+ allow(File).to receive(:open).with(any_args) { @loc }
44
+ allow(File).to receive(:binread).with(any_args) { @file }
45
+ allow_any_instance_of(RailsZen::WriteToModel).to receive(:file_name) { '/randompath/yo'}
46
+
47
+ end
48
+ context "when a hasmany relation is given" do
49
+ it "writes to the model file" do
50
+ @file << "class User < ActiveRecord::Base\nend\n"
51
+ given_model_gen.ask_for_has_many_relations
52
+ expect(@file).to include "has_many :posts"
53
+ end
54
+ it "writes to the model spec file" do
55
+ @file << "RSpec.describe User, type: :model do\n pending '....'\nend"
56
+ given_model_gen.ask_for_has_many_relations
57
+ expect(@file).to include "it { is_expected.to have_many(:posts) }"
58
+ end
59
+ end
60
+ context "when no hasmany relation is given" do
61
+ before do
62
+ set_stream(StringIO.new("\n"))
63
+ end
64
+ it "does not write to the model file" do
65
+ @file << "class User < ActiveRecord::Base\nend\n"
66
+ given_model_gen.ask_for_has_many_relations
67
+ expect(@file).not_to include "has_many :posts"
68
+ end
69
+ it "does not write to the model spec file" do
70
+ @file << "RSpec.describe User, type: :model do\n pending '....'\nend"
71
+ given_model_gen.ask_for_has_many_relations
72
+ expect(@file).not_to include "it { is_expected.to have_many(:posts) }"
73
+ end
74
+ end
75
+
76
+ end
77
+ end
78
+
79
+ def set_stream(input)
80
+ $terminal.instance_variable_set(:@input, input)
81
+ end
@@ -0,0 +1,81 @@
1
+ require 'rails_zen/write_to_files/write_to_model'
2
+ require 'rails_zen/write_to_files/write_to_spec'
3
+ require 'rails_zen/model_action'
4
+ require 'highline/import'
5
+
6
+ RSpec.describe RailsZen::ModelAction do
7
+
8
+
9
+ let(:model_action){
10
+ RailsZen::ModelAction.new("sum", false, "calculator")
11
+ }
12
+
13
+ describe "#write!" do
14
+ before do
15
+ @loc = double('file')
16
+ set_stream(StringIO.new("returns sum of two numbers\na,b\n1,2\n3"))
17
+ @file = String.new
18
+ allow(File).to receive(:open).with(any_args) { @loc }
19
+ allow(File).to receive(:binread).with(any_args) { @file }
20
+ allow_any_instance_of(RailsZen::WriteToModel).to receive(:file_name) { '/randompath/yo'}
21
+
22
+ allow(File).to receive(:foreach).with(any_args).and_return(@loc)
23
+ allow(@loc).to receive(:grep).with(any_args).and_return([1, 2])
24
+ end
25
+
26
+ context "when user wants to write an instance method" do
27
+
28
+ it "writes to the model file" do
29
+ @file << "class Calculator < ActiveRecord::Base\nend\n"
30
+
31
+ model_action.write!
32
+ expect(@file).to include "sum(a, b)"
33
+ end
34
+ it "writes to the model spec file" do
35
+ @file << "RSpec.describe Calculator, type: :model do\n pending '....'\nend"
36
+
37
+ model_action.write!
38
+ expect(@file).to include "describe \"#sum\""
39
+ end
40
+
41
+ it "writes expectation in the model spec file" do
42
+ @file << "RSpec.describe Calculator, type: :model do\n pending '....'\nend"
43
+
44
+ model_action.write!
45
+ expect(@file).to include "expect(calculator.sum(1, 2)).to eq \"3\""
46
+ end
47
+ end
48
+ context "when user wants to write a class method" do
49
+
50
+ before do
51
+ model_action.is_class_action = true
52
+ @file << "RSpec.describe Calculator, type: :model do\n pending '....'\nend"
53
+ end
54
+
55
+ it "writes to the model file" do
56
+ @file << "class Calculator < ActiveRecord::Base\nend\n"
57
+ model_action.write!
58
+ expect(@file).to include "self.sum(a, b)"
59
+ end
60
+ it "writes to the model spec file" do
61
+ model_action.write!
62
+ expect(@file).to include "describe \".sum\""
63
+ end
64
+
65
+ it "writes expectation in the model spec file" do
66
+ model_action.write!
67
+ expect(@file).to include "expect(Calculator.sum(1, 2)).to eq \"3\""
68
+ end
69
+
70
+ it "write factory method" do
71
+ allow(@loc).to receive(:grep).with(any_args).and_return([])
72
+ model_action.write!
73
+ expect(@file).to include "FactoryGirl.create(:calculator)"
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def set_stream(input)
80
+ $terminal.instance_variable_set(:@input, input)
81
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails_zen/write_to_files'
2
+
3
+ module Common
4
+ def build_file_attr(type, scope=nil, validator="validates_presence_of")
5
+ attr = RailsZen::ChosenAttr.new("email", "string").tap do |c|
6
+ c.validator = validator
7
+ c.type_based_validators = nil
8
+ c.scope_attr = scope
9
+ end
10
+ RailsZen::WriteToFiles.new(attr, 'user').build_file_writers(type)
11
+ end
12
+ end
@@ -0,0 +1,93 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # The `.rspec` file also contains a few flags that are not defaults but that
16
+ # users commonly want.
17
+ #
18
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+ Dir.glob(::File.expand_path('../support/*.rb', __FILE__)).each { |f| require_relative f }
20
+
21
+ RSpec.configure do |config|
22
+ # rspec-expectations config goes here. You can use an alternate
23
+ # assertion/expectation library such as wrong or the stdlib/minitest
24
+ # assertions if you prefer.
25
+ config.expect_with :rspec do |expectations|
26
+ # This option will default to `true` in RSpec 4. It makes the `description`
27
+ # and `failure_message` of custom matchers include text for helper methods
28
+ # defined using `chain`, e.g.:
29
+ # be_bigger_than(2).and_smaller_than(4).description
30
+ # # => "be bigger than 2 and smaller than 4"
31
+ # ...rather than:
32
+ # # => "be bigger than 2"
33
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
34
+ end
35
+
36
+ # rspec-mocks config goes here. You can use an alternate test double
37
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
38
+ config.mock_with :rspec do |mocks|
39
+ # Prevents you from mocking or stubbing a method that does not exist on
40
+ # a real object. This is generally recommended, and will default to
41
+ # `true` in RSpec 4.
42
+ mocks.verify_partial_doubles = true
43
+ end
44
+
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ =begin
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Limits the available syntax to the non-monkey patched syntax that is
56
+ # recommended. For more details, see:
57
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
58
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
59
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
60
+ config.disable_monkey_patching!
61
+
62
+ # This setting enables warnings. It's recommended, but in some cases may
63
+ # be too noisy due to issues in dependencies.
64
+ config.warnings = true
65
+
66
+ # Many RSpec users commonly either run the entire suite or an individual
67
+ # file, and it's useful to allow more verbose output when running an
68
+ # individual spec file.
69
+ if config.files_to_run.one?
70
+ # Use the documentation formatter for detailed output,
71
+ # unless a formatter has already been configured
72
+ # (e.g. via a command-line flag).
73
+ config.default_formatter = 'doc'
74
+ end
75
+
76
+ # Print the 10 slowest examples and example groups at the
77
+ # end of the spec run, to help surface which specs are running
78
+ # particularly slow.
79
+ config.profile_examples = 10
80
+
81
+ # Run specs in random order to surface order dependencies. If you find an
82
+ # order dependency and want to debug it, you can fix the order by providing
83
+ # the seed, which is printed after each run.
84
+ # --seed 1234
85
+ config.order = :random
86
+
87
+ # Seed global randomization in this process using the `--seed` CLI option.
88
+ # Setting this allows you to use `--seed` to deterministically reproduce
89
+ # test failures related to randomization by passing the same `--seed` value
90
+ # as the one that triggered the failure.
91
+ Kernel.srand config.seed
92
+ =end
93
+ end
@@ -0,0 +1,11 @@
1
+ require 'aruba/api'
2
+ require 'aruba/reporting'
3
+
4
+ RSpec.configure do |config|
5
+ config.include Aruba::Api
6
+
7
+ config.before(:each) do
8
+ restore_env
9
+ clean_current_dir
10
+ end
11
+ end
@@ -0,0 +1,50 @@
1
+ require 'rails_zen/write_to_files/write_to_migration'
2
+ require 'shared/build_file_attr'
3
+
4
+ include Common
5
+
6
+ RSpec.describe RailsZen::WriteToMigration do
7
+
8
+ before do
9
+ @write_to_migration = build_file_attr("WriteToMigration")
10
+ end
11
+
12
+ let(:file) {
13
+ str = %{
14
+ class CreateUsers < ActiveRecord::Migration
15
+ def change
16
+ create_table :developers do |t|
17
+ t.string :email
18
+ t.timestamps null: false
19
+ end
20
+ end
21
+ end
22
+ }
23
+ String.new(str)
24
+ }
25
+
26
+ it { expect(@write_to_migration).to respond_to :write!}
27
+
28
+ describe "#write!" do
29
+
30
+ before do
31
+ loc = double('file')
32
+ allow(File).to receive(:open).with(any_args) { loc }
33
+ allow(File).to receive(:binread).with(any_args) { file }
34
+ allow(Dir).to receive(:glob).with(any_args) {["2023_create_users.rb"]}
35
+ end
36
+ it "appends to the line" do
37
+
38
+ @write_to_migration.scope_attr = []
39
+ @write_to_migration.write!
40
+ expect(file).to include("t.string :email, required: true, null: false")
41
+ end
42
+ fit "inserts an index line to the file" do
43
+
44
+ @write_to_migration.scope_attr = [:post_id, :comment_id]
45
+ @write_to_migration.write!
46
+
47
+ expect(file).to include("t.index [:post_id, :comment_id]")
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,57 @@
1
+ require 'rails_zen/write_to_files/write_to_model'
2
+ require 'shared/build_file_attr'
3
+
4
+ include Common
5
+
6
+ RSpec.describe RailsZen::WriteToModel do
7
+
8
+ before do
9
+ @write_to_model = build_file_attr("WriteToModel")
10
+ end
11
+
12
+ let(:file) { String.new("class User < ActiveRecord::Base\nend\n") }
13
+
14
+ it { expect(@write_to_model).to respond_to :write!}
15
+
16
+ describe "#write!" do
17
+
18
+ before do
19
+ loc = double('file')
20
+ allow(File).to receive(:open).with(any_args) { loc }
21
+ allow(File).to receive(:binread).with(any_args) { file }
22
+ end
23
+ it "appends to the file" do
24
+
25
+ allow(@write_to_model).to receive(:file_name) { '/randompath/yo'}
26
+
27
+ @write_to_model.write!
28
+ expect(file).to include("validates :email, presence: true")
29
+ end
30
+ context "when one scope" do
31
+
32
+ before do
33
+ @write = build_file_attr("WriteToModel", [:post_id, :comment_id], "validates_uniqueness_scoped_to")
34
+ end
35
+
36
+ fit "adds a scope to uniqueness" do
37
+ allow(@write).to receive(:file_name) { '/randompath/yo'}
38
+ @write.write!
39
+
40
+ expect(file).to include("validates_uniqueness_of :post_id, scope: :comment_id")
41
+ end
42
+ end
43
+ context "when multiple scope" do
44
+
45
+ before do
46
+ @write = build_file_attr("WriteToModel", [:post_id, :comment_id, :score_id], "validates_uniqueness_scoped_to")
47
+ end
48
+
49
+ fit "adds an array of scope to uniqueness" do
50
+ allow(@write).to receive(:file_name) { '/randompath/yo'}
51
+ @write.write!
52
+
53
+ expect(file).to include("validates_uniqueness_of :post_id, scope: [:comment_id, :score_id]")
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,63 @@
1
+ require 'rails_zen/write_to_files/write_to_spec'
2
+ require 'shared/build_file_attr'
3
+
4
+ include Common
5
+
6
+ RSpec.describe RailsZen::WriteToSpec do
7
+
8
+ before do
9
+ @write_to_spec = build_file_attr("WriteToSpec")
10
+ end
11
+
12
+ let(:file) { String.new("RSpec.describe User, type: :model do\n pending '....'\nend") }
13
+
14
+ it { expect(@write_to_spec).to respond_to :write!}
15
+
16
+ describe "#write!" do
17
+
18
+ before do
19
+
20
+ @loc = double('file')
21
+ allow(File).to receive(:open).with(any_args) { @loc }
22
+ allow(File).to receive(:binread).with(any_args) { file }
23
+ allow(File).to receive(:foreach).with(any_args).and_return(@loc)
24
+ allow(@loc).to receive(:grep).with(any_args).and_return([])
25
+ end
26
+ it "inserts factory girl instance" do
27
+
28
+ @write_to_spec.write!
29
+ expect(file).to include("FactoryGirl.create(:user)")
30
+ end
31
+ it "appends to the file" do
32
+
33
+ @write_to_spec.write!
34
+ expect(file).to include "it { user; is_expected.to validate_presence_of(:email)}"
35
+
36
+ end
37
+ context "when one scope" do
38
+
39
+ before do
40
+ @write = build_file_attr("WriteToSpec", [:post_id, :comment_id], "validates_uniqueness_scoped_to")
41
+ end
42
+
43
+ fit "adds a scope to uniqueness" do
44
+ @write.write!
45
+
46
+ expect(file).to include("it { user; is_expected.to validate_uniqueness_of(:post_id).scoped_to(:comment_id)}")
47
+ end
48
+ end
49
+
50
+ context "when multiple scopes" do
51
+
52
+ before do
53
+ @write = build_file_attr("WriteToSpec", [:post_id, :comment_id, :score], "validates_uniqueness_scoped_to")
54
+ end
55
+
56
+ fit "adds a scope to uniqueness" do
57
+ @write.write!
58
+
59
+ expect(file).to include("it { user; is_expected.to validate_uniqueness_of(:post_id).scoped_to([:comment_id, :score])}")
60
+ end
61
+ end
62
+ end
63
+ end