# encoding: utf-8 require 'spec_helper' require 'fedux_org_stdlib/file_template' RSpec.describe FileTemplate do 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 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 end