# encoding: utf-8 require 'spec_helper' require 'fedux_org_stdlib/file_template' RSpec.describe FileTemplate do context '#proposed_file' do it 'generates a propose file path based on template basename' do with_environment 'HOME' => working_directory do template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new(working_directory: working_directory) expect(template.proposed_file).to eq File.join(working_directory, 'test') end end end context '#preferred_template_file' do it 'has a default template file which is the preferred place to store the template' do with_environment 'HOME' => working_directory do template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.preferred_template_file).to eq File.expand_path('~/.config/my_application/templates/test*.tt') end end it 'works with nested module names' do with_environment 'HOME' => working_directory do template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication::MySub' end end template = template_klass.new expect(template.preferred_template_file).to eq File.expand_path('~/.config/my_application/my_sub/templates/test*.tt') end end end context 'template files' do it 'looks at ~/.config/my_application/templates/test.tt' do with_environment 'HOME' => working_directory do create_file '.config/my_application/templates/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.content).to include 'hello world' end end it 'looks at ~/.my_application/templates/test.tt' do with_environment 'HOME' => working_directory do create_file '.my_application/templates/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.content).to include 'hello world' end end it 'supports star operator in suffix: ~/.my_application/templates/test*.tt' do with_environment 'HOME' => working_directory do create_file '.my_application/templates/test.erb.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.content).to include 'hello world' end end end context '#content' do it 'returns content of template file' do with_environment 'HOME' => working_directory do create_file '.my_application/templates/test.tt', <<-EOS.strip_heredoc <%= hello world %> EOS template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.content).to include 'hello world' end end end context '#proposed_extname' do it 'propose a erb as default extname' do with_environment 'HOME' => working_directory do create_file '.my_application/templates/test.tt' template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.proposed_extname).to eq '.erb' end end it 'extracts the extname from file name' do with_environment 'HOME' => working_directory do create_file '.my_application/templates/test.md.tt' template_klass = Class.new(FileTemplate) do def class_name 'TestTemplate' end def module_name 'MyApplication' end end template = template_klass.new expect(template.proposed_extname).to eq '.md' end end end end