11# encoding: utf-8
2- require_relative '../../spec_helper'
32require 'annotate/annotate_models'
43require 'annotate/active_record_patch'
54require 'active_support/core_ext/string'
87
98RSpec . describe AnnotateModels do
109 include AnnotateTestHelpers
11-
12- MAGIC_COMMENTS = [
13- '# encoding: UTF-8' ,
14- '# coding: UTF-8' ,
15- '# -*- coding: UTF-8 -*-' ,
16- '#encoding: utf-8' ,
17- '# encoding: utf-8' ,
18- '# -*- encoding : utf-8 -*-' ,
19- "# encoding: utf-8\n # frozen_string_literal: true" ,
20- "# frozen_string_literal: true\n # encoding: utf-8" ,
21- '# frozen_string_literal: true' ,
22- '#frozen_string_literal: false' ,
23- '# -*- frozen_string_literal : true -*-'
24- ] . freeze
10+ include AnnotateTestConstants
2511
2612 describe 'annotating a file' do
2713 before do
@@ -41,36 +27,80 @@ class User < ActiveRecord::Base
4127 Annotate ::Helpers . reset_options ( Annotate ::Constants ::ALL_ANNOTATE_OPTIONS )
4228 end
4329
44- def write_model ( file_name , file_content )
45- fname = File . join ( @model_dir , file_name )
46- FileUtils . mkdir_p ( File . dirname ( fname ) )
47- File . open ( fname , 'wb' ) { |f | f . write file_content }
30+ context "with 'before'" do
31+ let ( :position ) { 'before' }
32+
33+ it "should put annotation before class if :position == 'before'" do
34+ annotate_one_file position : position
35+ expect ( File . read ( @model_file_name ) )
36+ . to eq ( "#{ @schema_info } #{ @file_content } " )
37+ end
38+ end
39+
40+ context "with :before" do
41+ let ( :position ) { :before }
4842
49- [ fname , file_content ]
43+ it "should put annotation before class if :position == :before" do
44+ annotate_one_file position : position
45+ expect ( File . read ( @model_file_name ) )
46+ . to eq ( "#{ @schema_info } #{ @file_content } " )
47+ end
5048 end
5149
52- def annotate_one_file ( options = { } )
53- Annotate . set_defaults ( options )
54- options = Annotate . setup_options ( options )
55- AnnotateModels . annotate_one_file ( @model_file_name , @schema_info , :position_in_class , options )
50+ context "with 'top'" do
51+ let ( :position ) { 'top' }
5652
57- # Wipe settings so the next call will pick up new values...
58- Annotate . instance_variable_set ( '@has_set_defaults' , false )
59- Annotate :: Constants :: POSITION_OPTIONS . each { | key | ENV [ key . to_s ] = '' }
60- Annotate :: Constants :: FLAG_OPTIONS . each { | key | ENV [ key . to_s ] = '' }
61- Annotate :: Constants :: PATH_OPTIONS . each { | key | ENV [ key . to_s ] = '' }
53+ it "should put annotation before class if :position == 'top'" do
54+ annotate_one_file position : position
55+ expect ( File . read ( @model_file_name ) )
56+ . to eq ( " #{ @schema_info } #{ @file_content } " )
57+ end
6258 end
6359
64- [ 'before' , :before , 'top' , :top ] . each do |position |
65- it "should put annotation before class if :position == #{ position } " do
60+ context "with :top" do
61+ let ( :position ) { :top }
62+
63+ it "should put annotation before class if :position == :top" do
6664 annotate_one_file position : position
6765 expect ( File . read ( @model_file_name ) )
6866 . to eq ( "#{ @schema_info } #{ @file_content } " )
6967 end
7068 end
7169
72- [ 'after' , :after , 'bottom' , :bottom ] . each do |position |
73- it "should put annotation after class if position: #{ position } " do
70+ context "with 'after'" do
71+ let ( :position ) { 'after' }
72+
73+ it "should put annotation after class if position: 'after'" do
74+ annotate_one_file position : position
75+ expect ( File . read ( @model_file_name ) )
76+ . to eq ( "#{ @file_content } \n #{ @schema_info } " )
77+ end
78+ end
79+
80+ context "with :after" do
81+ let ( :position ) { :after }
82+
83+ it "should put annotation after class if position: :after" do
84+ annotate_one_file position : position
85+ expect ( File . read ( @model_file_name ) )
86+ . to eq ( "#{ @file_content } \n #{ @schema_info } " )
87+ end
88+ end
89+
90+ context "with 'bottom'" do
91+ let ( :position ) { 'bottom' }
92+
93+ it "should put annotation after class if position: 'bottom'" do
94+ annotate_one_file position : position
95+ expect ( File . read ( @model_file_name ) )
96+ . to eq ( "#{ @file_content } \n #{ @schema_info } " )
97+ end
98+ end
99+
100+ context "with :bottom" do
101+ let ( :position ) { :bottom }
102+
103+ it "should put annotation after class if position: :bottom" do
74104 annotate_one_file position : position
75105 expect ( File . read ( @model_file_name ) )
76106 . to eq ( "#{ @file_content } \n #{ @schema_info } " )
@@ -196,7 +226,7 @@ class Foo::User < ActiveRecord::Base
196226 end
197227
198228 it 'should not touch magic comments' do
199- MAGIC_COMMENTS . each do |magic_comment |
229+ AnnotateTestConstants :: MAGIC_COMMENTS . each do |magic_comment |
200230 write_model 'user.rb' , <<~EOS
201231 #{ magic_comment }
202232 class User < ActiveRecord::Base
@@ -216,7 +246,7 @@ class User < ActiveRecord::Base
216246
217247 it 'adds an empty line between magic comments and annotation (position :before)' do
218248 content = "class User < ActiveRecord::Base\n end\n "
219- MAGIC_COMMENTS . each do |magic_comment |
249+ AnnotateTestConstants :: MAGIC_COMMENTS . each do |magic_comment |
220250 model_file_name , = write_model 'user.rb' , "#{ magic_comment } \n #{ content } "
221251
222252 annotate_one_file position : :before
@@ -228,7 +258,7 @@ class User < ActiveRecord::Base
228258
229259 it 'only keeps a single empty line around the annotation (position :before)' do
230260 content = "class User < ActiveRecord::Base\n end\n "
231- MAGIC_COMMENTS . each do |magic_comment |
261+ AnnotateTestConstants :: MAGIC_COMMENTS . each do |magic_comment |
232262 schema_info = AnnotateModels ::SchemaInfo . generate ( @klass , '== Schema Info' )
233263 model_file_name , = write_model 'user.rb' , "#{ magic_comment } \n \n \n \n #{ content } "
234264
@@ -240,7 +270,7 @@ class User < ActiveRecord::Base
240270
241271 it 'does not change whitespace between magic comments and model file content (position :after)' do
242272 content = "class User < ActiveRecord::Base\n end\n "
243- MAGIC_COMMENTS . each do |magic_comment |
273+ AnnotateTestConstants :: MAGIC_COMMENTS . each do |magic_comment |
244274 model_file_name , = write_model 'user.rb' , "#{ magic_comment } \n #{ content } "
245275
246276 annotate_one_file position : :after
0 commit comments