Skip to content

Commit

Permalink
Add constant MAGIC_COMMENTS
Browse files Browse the repository at this point in the history
  • Loading branch information
nard-tech committed Jan 12, 2020
1 parent d265dca commit a0f37e5
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
require 'files'

describe AnnotateModels do # rubocop:disable Metrics/BlockLength
MAGIC_COMMENTS = [
'# encoding: UTF-8',
'# coding: UTF-8',
'# -*- coding: UTF-8 -*-',
'#encoding: utf-8',
'# encoding: utf-8',
'# -*- encoding : utf-8 -*-',
"# encoding: utf-8\n# frozen_string_literal: true",
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-'
].freeze

def mock_index(name, params = {})
double('IndexKeyDefinition',
name: name,
Expand Down Expand Up @@ -1955,22 +1969,6 @@ def annotate_one_file(options = {})
Annotate::Constants::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
end

def magic_comments_list_each
[
'# encoding: UTF-8',
'# coding: UTF-8',
'# -*- coding: UTF-8 -*-',
'#encoding: utf-8',
'# encoding: utf-8',
'# -*- encoding : utf-8 -*-',
"# encoding: utf-8\n# frozen_string_literal: true",
"# frozen_string_literal: true\n# encoding: utf-8",
'# frozen_string_literal: true',
'#frozen_string_literal: false',
'# -*- frozen_string_literal : true -*-'
].each { |magic_comment| yield magic_comment }
end

['before', :before, 'top', :top].each do |position|
it "should put annotation before class if :position == #{position}" do
annotate_one_file position: position
Expand Down Expand Up @@ -2106,7 +2104,7 @@ class Foo::User < ActiveRecord::Base
end

it 'should not touch magic comments' do
magic_comments_list_each do |magic_comment|
MAGIC_COMMENTS.each do |magic_comment|
write_model 'user.rb', <<-EOS
#{magic_comment}
class User < ActiveRecord::Base
Expand All @@ -2126,7 +2124,7 @@ class User < ActiveRecord::Base

it 'adds an empty line between magic comments and annotation (position :before)' do
content = "class User < ActiveRecord::Base\nend\n"
magic_comments_list_each do |magic_comment|
MAGIC_COMMENTS.each do |magic_comment|
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"

annotate_one_file position: :before
Expand All @@ -2138,7 +2136,7 @@ class User < ActiveRecord::Base

it 'only keeps a single empty line around the annotation (position :before)' do
content = "class User < ActiveRecord::Base\nend\n"
magic_comments_list_each do |magic_comment|
MAGIC_COMMENTS.each do |magic_comment|
schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info')
model_file_name, = write_model 'user.rb', "#{magic_comment}\n\n\n\n#{content}"

Expand All @@ -2150,7 +2148,7 @@ class User < ActiveRecord::Base

it 'does not change whitespace between magic comments and model file content (position :after)' do
content = "class User < ActiveRecord::Base\nend\n"
magic_comments_list_each do |magic_comment|
MAGIC_COMMENTS.each do |magic_comment|
model_file_name, = write_model 'user.rb', "#{magic_comment}\n#{content}"

annotate_one_file position: :after
Expand Down

0 comments on commit a0f37e5

Please sign in to comment.