<commit_before>require 'helper'

class TestJumpstartBase < Test::Unit::TestCase
        
  context "Testing JumpStart::Base with a @default_template_name and @jumpstart_templates_path specified.\n" do
    
    setup do
      input = StringIO.new
      output = StringIO.new
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
      @test_project = JumpStart::Base.new(["test_jumpstart_project"])
      @test_project.instance_eval do
        @input = input
        @output = output
        @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
        @default_template_name = "test_template_1"
        @template_name = "test_template_1"
        @template_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_1"
        set_config_file_options
        @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
      end
    end
    
    setup do
      input = StringIO.new
      output = StringIO.new
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
      @test_project_2 = JumpStart::Base.new(["test_jumpstart_project", "a_name_that_does_not_exist"])
      @test_project_2.instance_eval do
        @input = input
        @output = output
        @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
        @default_template_name = "test_template_2"
        @template_name = "test_template_2"
        @template_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_2"
        set_config_file_options
        @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
      end
    end
    
    teardown do
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
    end
    
    context "Tests for the JumpStart::Base#intialize instance method. \n" do
            
      should "set @jumpstart_template_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates", @test_project.instance_eval {@jumpstart_templates_path}
      end
      
      should "set @default_template_name" do
        assert_equal "test_template_1", @test_project.instance_eval {@default_template_name}
      end
            
      should "set @project_name" do
        assert_equal "test_jumpstart_project", @test_project.instance_eval {@project_name}
      end
      
      should "set @template_name" do
        assert_equal "test_template_1", @test_project.instance_eval {@template_name}
      end
            
      should "set @template_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_1", @test_project.instance_eval {@template_path}
      end
      
    end
    
    context "Tests for the JumpStart::Base#set_config_file_options instance method. \n" do
            
      should "set @config_file" do
        assert_equal YAML.load_file("#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/#{@test_project_2.instance_eval {@template_name}}/jumpstart_config/#{@test_project_2.instance_eval {@template_name}}.yml"), @test_project_2.instance_eval {@config_file}
      end
      
      should "set @install_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/destination_dir", @test_project_2.instance_eval {@install_path}
      end
      
      should "set @install_command" do
        assert_equal "rails", @test_project_2.instance_eval {@install_command}
      end
      
      should "set @install_command_args" do
        assert_equal "-J -T", @test_project_2.instance_eval {@install_command_args}
      end
      
      should "set @replace_strings" do
        assert_equal [{:target_path=>"/config/deploy.rb", :symbols=>{:project_name=>"name_of_my_app", :remote_server=>"thoughtplant"}}], @test_project_2.instance_eval {@replace_strings}
      end

      # TODO Looks like testing methods that call gets is going to be tough this way. Look at using a mocking tool like 'RR'
      should "load the jumpstart menu if the specified yaml config file does not exist" do
        FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
        @test_project_2b = JumpStart::Base.new(["test_jumpstart_project"])
        @test_project_2b.instance_variable_set(:@jumpstart_templates_path, "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates")
        @test_project_2b.instance_variable_set(:@default_template_name, "test_template_2")
        @test_project_2b.instance_variable_set(:@template_name, "a_name_that_does_not_exist")
        @test_project_2b.instance_variable_set(:@template_path, "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_2")
        stub(@test_project_2b).jumpstart_menu
        @test_project_2b.set_config_file_options
        assert_received(@test_project_2b) {|x| x.jumpstart_menu}
      end
      
    end
    
    context "Tests for the JumpStart::Base#lookup_existing_projects instance method. \n" do
      
      should "run lookup_existing_projects method and return an array of existing templates" do
        @test_project.lookup_existing_templates
        assert_equal %w[test_template_1 test_template_2 test_template_3], @test_project.instance_eval {@existing_templates}
      end
      
    end
    
    context "Tests for the JumpStart::Base#check_project_name instance method. \n" do
      
      context "when the project name is over three characters" do
        
        should "return the project name unchanged and without errors" do
          assert_equal @test_project.instance_eval {@project_name}, @test_project.instance_eval {check_project_name}
        end
        
      end
      
      context "when the project name is not empty but is not more than 3 characters" do
        
        setup do
          input = StringIO.new("testo\n")
          output = StringIO.new
          @test_project = JumpStart::Base.new(["tr"])
          @test_project.instance_eval do
            @input = input
            @output = output
            @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
            @default_template_name = "test_template_1"
            @template_name = "test_template_1"
            set_config_file_options
            @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
          end
        end
        
        should "read input from STDIN" do
          assert_equal "testo\n", @test_project.input.string
        end
        
        should "ask the user to provide a longer project name" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[31m\n  The name tr is too short. Please enter a name at least 3 characters long.\e[0m\n" , @test_project.output.string
        end
        
        should "ask the user to provide a longer project name and then return the name of the project when a name longer than three characters is provided" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[31m\n  The name tr is too short. Please enter a name at least 3 characters long.\e[0m\n" , @test_project.output.string
          assert_equal "testo", @test_project.instance_eval {check_project_name}
        end
                                
      end
      
      context "when the project name is empty or nil" do
        
        setup do
          input = StringIO.new("testorama\n")
          output = StringIO.new
          @test_project = JumpStart::Base.new([nil])
          @test_project.instance_eval do
            @input = input
            @output = output
            @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
            @default_template_name = "test_template_1"
            @template_name = "test_template_1"
            set_config_file_options
            @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
          end
        end
        
        should "ask the user to specify a name for the project if @project_name is empty or nil" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[1m\e[33m\n  Enter a name for your project.\e[0m\n", @test_project.output.string
        end
        
        should "ask the user to specify a name for the project if @project_name is empty or nil and then set it when a name of at least 3 characters is provided" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[1m\e[33m\n  Enter a name for your project.\e[0m\n", @test_project.output.string<commit_msg>changed a block of tests to use instance_variable_get instead of instance_eval. Purely so I will remember the alternative way of doing this.<commit_after>require 'helper'

class TestJumpstartBase < Test::Unit::TestCase
        
  context "Testing JumpStart::Base with a @default_template_name and @jumpstart_templates_path specified.\n" do
    
    setup do
      input = StringIO.new
      output = StringIO.new
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
      @test_project = JumpStart::Base.new(["test_jumpstart_project"])
      @test_project.instance_eval do
        @input = input
        @output = output
        @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
        @default_template_name = "test_template_1"
        @template_name = "test_template_1"
        @template_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_1"
        set_config_file_options
        @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
      end
    end
    
    setup do
      input = StringIO.new
      output = StringIO.new
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
      @test_project_2 = JumpStart::Base.new(["test_jumpstart_project", "a_name_that_does_not_exist"])
      @test_project_2.instance_eval do
        @input = input
        @output = output
        @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
        @default_template_name = "test_template_2"
        @template_name = "test_template_2"
        @template_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_2"
        set_config_file_options
        @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
      end
    end
    
    teardown do
      FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
    end
    
    context "Tests for the JumpStart::Base#intialize instance method. \n" do
            
      should "set @jumpstart_template_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates", @test_project.instance_eval {@jumpstart_templates_path}
      end
      
      should "set @default_template_name" do
        assert_equal "test_template_1", @test_project.instance_eval {@default_template_name}
      end
            
      should "set @project_name" do
        assert_equal "test_jumpstart_project", @test_project.instance_eval {@project_name}
      end
      
      should "set @template_name" do
        assert_equal "test_template_1", @test_project.instance_eval {@template_name}
      end
            
      should "set @template_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_1", @test_project.instance_eval {@template_path}
      end
      
    end
    
    context "Tests for the JumpStart::Base#set_config_file_options instance method. \n" do
            
      should "set @config_file" do
        assert_equal YAML.load_file("#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/#{@test_project_2.instance_variable_get(:@template_name)}/jumpstart_config/#{@test_project_2.instance_variable_get(:@template_name)}.yml"), @test_project_2.instance_variable_get(:@config_file)
      end
      
      should "set @install_path" do
        assert_equal "#{JumpStart::ROOT_PATH}/test/destination_dir", @test_project_2.instance_variable_get(:@install_path) 
      end
      
      should "set @install_command" do
        assert_equal "rails", @test_project_2.instance_variable_get(:@install_command)
      end
      
      should "set @install_command_args" do
        assert_equal "-J -T", @test_project_2.instance_variable_get(:@install_command_args)
      end
      
      should "set @replace_strings" do
        assert_equal [{:target_path=>"/config/deploy.rb", :symbols=>{:project_name=>"name_of_my_app", :remote_server=>"thoughtplant"}}], @test_project_2.instance_variable_get(:@replace_strings)
      end

      # TODO Looks like testing methods that call gets is going to be tough this way. Look at using a mocking tool like 'RR'
      should "load the jumpstart menu if the specified yaml config file does not exist" do
        FileUtils.delete_dir_contents("#{JumpStart::ROOT_PATH}/test/destination_dir")
        @test_project_2b = JumpStart::Base.new(["test_jumpstart_project"])
        @test_project_2b.instance_variable_set(:@jumpstart_templates_path, "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates")
        @test_project_2b.instance_variable_set(:@default_template_name, "test_template_2")
        @test_project_2b.instance_variable_set(:@template_name, "a_name_that_does_not_exist")
        @test_project_2b.instance_variable_set(:@template_path, "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates/test_template_2")
        stub(@test_project_2b).jumpstart_menu
        @test_project_2b.set_config_file_options
        assert_received(@test_project_2b) {|x| x.jumpstart_menu}
      end
      
    end
    
    context "Tests for the JumpStart::Base#lookup_existing_projects instance method. \n" do
      
      should "run lookup_existing_projects method and return an array of existing templates" do
        @test_project.lookup_existing_templates
        assert_equal %w[test_template_1 test_template_2 test_template_3], @test_project.instance_eval {@existing_templates}
      end
      
    end
    
    context "Tests for the JumpStart::Base#check_project_name instance method. \n" do
      
      context "when the project name is over three characters" do
        
        should "return the project name unchanged and without errors" do
          assert_equal @test_project.instance_eval {@project_name}, @test_project.instance_eval {check_project_name}
        end
        
      end
      
      context "when the project name is not empty but is not more than 3 characters" do
        
        setup do
          input = StringIO.new("testo\n")
          output = StringIO.new
          @test_project = JumpStart::Base.new(["tr"])
          @test_project.instance_eval do
            @input = input
            @output = output
            @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
            @default_template_name = "test_template_1"
            @template_name = "test_template_1"
            set_config_file_options
            @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
          end
        end
        
        should "read input from STDIN" do
          assert_equal "testo\n", @test_project.input.string
        end
        
        should "ask the user to provide a longer project name" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[31m\n  The name tr is too short. Please enter a name at least 3 characters long.\e[0m\n" , @test_project.output.string
        end
        
        should "ask the user to provide a longer project name and then return the name of the project when a name longer than three characters is provided" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[31m\n  The name tr is too short. Please enter a name at least 3 characters long.\e[0m\n" , @test_project.output.string
          assert_equal "testo", @test_project.instance_eval { check_project_name }
        end
                                
      end
      
      context "when the project name is empty or nil" do
        
        setup do
          input = StringIO.new("testorama\n")
          output = StringIO.new
          @test_project = JumpStart::Base.new([nil])
          @test_project.instance_eval do
            @input = input
            @output = output
            @jumpstart_templates_path = "#{JumpStart::ROOT_PATH}/test/test_jumpstart_templates"
            @default_template_name = "test_template_1"
            @template_name = "test_template_1"
            set_config_file_options
            @install_path = "#{JumpStart::ROOT_PATH}/test/destination_dir"
          end
        end
        
        should "ask the user to specify a name for the project if @project_name is empty or nil" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[1m\e[33m\n  Enter a name for your project.\e[0m\n", @test_project.output.string
        end
        
        should "ask the user to specify a name for the project if @project_name is empty or nil and then set it when a name of at least 3 characters is provided" do
          @test_project.instance_eval {check_project_name}
          assert_equal "\e[1m\e[33m\n  Enter a name for your project.\e[0m\n", @test_project.output.string
{'fraction_non_alphanumeric': 0.08596104799443542, 'fraction_numerical': 0.006144215163459309, 'mean_word_length': 2.969857340082835, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6617325', 'n_tokens_mistral': 5159, 'n_tokens_neox': 5046, 'n_words': 1127}