<commit_before>require File.dirname(__FILE__) + '/spec_helper'

class HelperRunner
  def initialize(parent, name)
    @parent = parent
    @name = name
  end

  def run(&block)
    self.instance_eval(&block)
  end

  def it(str, &block)
    @parent.send :it, "#{@name} #{str}", &block
  end
  alias specify it

  def before(symbol=:each, &block)
    @parent.send :before, symbol, &block
  end
end

describe GitHub::Helper do
  include SetupMethods

  def self.helper(name, &block)
    HelperRunner.new(self, name).run(&block)
  end

  before(:each) do
    @helper = GitHub::Helper.new
  end

  helper :print_issues_help do
    it "should exist" do
      @helper.should respond_to(:print_issues_help)
    end
  end

  helper :format_issue do
    before(:each) do
      @issue = {}
      @issue['number'] = 1234
      @issue['title'] = "Isaac Asimov's Science Fiction Magazine"
      @issue['votes'] = 99
    end

    specify "the title, number of votes and ticket number should appear" do
      @helper.format_issue(@issue, {}).should =~ /Issue #1234 \(99 votes\): Isaac Asimov's Science Fiction Magazine/
    end

    specify "the url should appear" do
      setup_url_for("origin",  "hamilton", "foo")
      @helper.format_issue(@issue, {:user => 'hamilton'}).should =~ /http:\/\/github.com\/hamilton\/foo\/issues\/#issue\/#{@issue['number']}/
    end

    specify "created_at should appear" do
      @issue['created_at'] = Time.now - 3600
      @issue['user'] = '<NAME>'
      @helper.format_issue(@issue, {}).should =~ /Opened about 1 hour ago by <NAME>/
    end

    specify "closed_at should appear" do
      @issue['closed_at'] = Time.now - 3600
      @helper.format_issue(@issue, {}).should =~ /Closed about 1 hour ago/
    end

    specify "updated_at should appear" do
      @issue['updated_at'] = Time.now - 3600
      @helper.format_issue(@issue, {}).should =~ /Last updated about 1 hour ago/
    end

    specify "labels should appear" do
      @issue['labels'] = ['Horror','Sci-Fi','Fan Fic']
      @helper.format_issue(@issue, {}).should =~ /Labels: Horror, Sci-Fi, Fan Fic/
    end

    specify "the body should appear" do
      @issue['body'] = <<-EOF
        It was the best of times,
        It was the worst of times.
      EOF
      report = @helper.format_issue(@issue, {})
      report.should =~ /It was the best of times,/
      report.should =~ /It was the worst of times\./
    end
  end

  helper :filter_issue do
    specify "when the after option is present, show only issues updated on or after that date" do
      issue = {'updated_at' => Time.parse('2009-01-02 12:00:00')}
      @helper.filter_issue(issue, :after => '2009-01-02').should be_false
      @helper.filter_issue(issue, :after => '2009-01-03').should be_true
    end

    specify "when a label is specified, show only issues that have that label" do
      @helper.filter_issue({'labels' => nil}, :label => 'foo').should be_true
      @helper.filter_issue({'labels' => []}, :label => 'foo').should be_true
      @helper.filter_issue({'labels' => ['foo']}, :label => 'foo').should be_false
      @helper.filter_issue({'labels' => ['quux','foo','bar']}, :label => 'foo').should be_false
    end
  end

  helper :owner do
    it "should return repo owner" do
      setup_url_for "origin", "hacker"
      @helper.owner.should == "hacker"
    end
  end

  helper :private_url_for do
    it "should return an ssh-style url" do
      setup_url_for "origin", "user", "merb-core"
      @helper.private_url_for("wycats").should == "git@github.com:wycats/merb-core.git"
    end
  end

  helper :private_url_for_user_and_repo do
    it "should return an ssh-style url" do
      @helper.should_not_receive(:project)
      @helper.private_url_for_user_and_repo("defunkt", "github-gem").should == "git@github.com:defunkt/github-gem.git"
    end
  end

  helper :public_url_for do
    it "should return a git:// URL" do
      setup_url_for "origin", "user", "merb-core"
      @helper.public_url_for("wycats").should == "git://github.com/wycats/merb-core.git"
    end
  end

  helper :public_url_for_user_and_repo do
    it "should return a git:// URL" do
      @helper.should_not_receive(:project)
      @helper.public_url_for_user_and_repo("defunkt", "github-gem").should == "git://github.com/defunkt/github-gem.git"
    end
  end

  helper :project do
    it "should return project-awesome" do
      setup_url_for "origin", "user", "project-awesome"
      @helper.project.should == "project-awesome"
    end

    it "should exit due to missing origin" do
      @helper.should_receive(:url_for).twice.with("origin").and_return("")
      STDERR.should_receive(:puts).with("Error: missing remote 'origin'")
      lambda { @helper.project }.should raise_error(SystemExit)
    end

    it "should exit due to non-github origin" do
      @helper.should_receive(:url_for).twice.with("origin").and_return("home:path/to/repo.git")
      STDERR.should_receive(:puts).with("Error: remote 'origin' is not a github URL")
      lambda { @helper.project }.should raise_error(SystemExit)
    end
  end

  helper :repo_for do
    it "should return mephisto.git" do
      setup_url_for "mojombo", "mojombo", "mephisto"
      @helper.repo_for("mojombo").should == "mephisto.git"
    end
  end

  helper :user_and_repo_from do
    it "should parse a git:// url" do
      @helper.user_and_repo_from("git://github.com/defunkt/github.git").should == ["defunkt", "github.git"]
    end

    it "should parse a ssh-based url" do
      @helper.user_and_repo_from("git@github.<EMAIL>:mojombo/god.git").should == ["mojombo", "god.git"]
    end

    it "should parse a non-standard ssh-based url" do
      @helper.user_and_repo_from("ssh://git@github.com:mojombo/god.git").should == ["mojombo", "god.git"]<commit_msg>fixed spec for 'should exit due to missing origin'<commit_after>require File.dirname(__FILE__) + '/spec_helper'

class HelperRunner
  def initialize(parent, name)
    @parent = parent
    @name = name
  end

  def run(&block)
    self.instance_eval(&block)
  end

  def it(str, &block)
    @parent.send :it, "#{@name} #{str}", &block
  end
  alias specify it

  def before(symbol=:each, &block)
    @parent.send :before, symbol, &block
  end
end

describe GitHub::Helper do
  include SetupMethods

  def self.helper(name, &block)
    HelperRunner.new(self, name).run(&block)
  end

  before(:each) do
    @helper = GitHub::Helper.new
  end

  helper :print_issues_help do
    it "should exist" do
      @helper.should respond_to(:print_issues_help)
    end
  end

  helper :format_issue do
    before(:each) do
      @issue = {}
      @issue['number'] = 1234
      @issue['title'] = "<NAME>'s Science Fiction Magazine"
      @issue['votes'] = 99
    end

    specify "the title, number of votes and ticket number should appear" do
      @helper.format_issue(@issue, {}).should =~ /Issue #1234 \(99 votes\): Isaac Asimov's Science Fiction Magazine/
    end

    specify "the url should appear" do
      setup_url_for("origin",  "hamilton", "foo")
      @helper.format_issue(@issue, {:user => 'hamilton'}).should =~ /http:\/\/github.com\/hamilton\/foo\/issues\/#issue\/#{@issue['number']}/
    end

    specify "created_at should appear" do
      @issue['created_at'] = Time.now - 3600
      @issue['user'] = '<NAME>'
      @helper.format_issue(@issue, {}).should =~ /Opened about 1 hour ago by <NAME>/
    end

    specify "closed_at should appear" do
      @issue['closed_at'] = Time.now - 3600
      @helper.format_issue(@issue, {}).should =~ /Closed about 1 hour ago/
    end

    specify "updated_at should appear" do
      @issue['updated_at'] = Time.now - 3600
      @helper.format_issue(@issue, {}).should =~ /Last updated about 1 hour ago/
    end

    specify "labels should appear" do
      @issue['labels'] = ['Horror','Sci-Fi','Fan Fic']
      @helper.format_issue(@issue, {}).should =~ /Labels: Horror, Sci-Fi, Fan Fic/
    end

    specify "the body should appear" do
      @issue['body'] = <<-EOF
        It was the best of times,
        It was the worst of times.
      EOF
      report = @helper.format_issue(@issue, {})
      report.should =~ /It was the best of times,/
      report.should =~ /It was the worst of times\./
    end
  end

  helper :filter_issue do
    specify "when the after option is present, show only issues updated on or after that date" do
      issue = {'updated_at' => Time.parse('2009-01-02 12:00:00')}
      @helper.filter_issue(issue, :after => '2009-01-02').should be_false
      @helper.filter_issue(issue, :after => '2009-01-03').should be_true
    end

    specify "when a label is specified, show only issues that have that label" do
      @helper.filter_issue({'labels' => nil}, :label => 'foo').should be_true
      @helper.filter_issue({'labels' => []}, :label => 'foo').should be_true
      @helper.filter_issue({'labels' => ['foo']}, :label => 'foo').should be_false
      @helper.filter_issue({'labels' => ['quux','foo','bar']}, :label => 'foo').should be_false
    end
  end

  helper :owner do
    it "should return repo owner" do
      setup_url_for "origin", "hacker"
      @helper.owner.should == "hacker"
    end
  end

  helper :private_url_for do
    it "should return an ssh-style url" do
      setup_url_for "origin", "user", "merb-core"
      @helper.private_url_for("wycats").should == "<EMAIL>:wycats/merb-core.git"
    end
  end

  helper :private_url_for_user_and_repo do
    it "should return an ssh-style url" do
      @helper.should_not_receive(:project)
      @helper.private_url_for_user_and_repo("defunkt", "github-gem").should == "<EMAIL>:defunkt/github-gem.git"
    end
  end

  helper :public_url_for do
    it "should return a git:// URL" do
      setup_url_for "origin", "user", "merb-core"
      @helper.public_url_for("wycats").should == "git://github.com/wycats/merb-core.git"
    end
  end

  helper :public_url_for_user_and_repo do
    it "should return a git:// URL" do
      @helper.should_not_receive(:project)
      @helper.public_url_for_user_and_repo("defunkt", "github-gem").should == "git://github.com/defunkt/github-gem.git"
    end
  end

  helper :project do
    it "should return project-awesome" do
      setup_url_for "origin", "user", "project-awesome"
      @helper.project.should == "project-awesome"
    end

    it "should exit due to missing origin" do
      @helper.should_receive(:url_for).twice.with("origin").and_return("")
      @helper.should_receive(:origin).twice.and_return("origin")
      STDERR.should_receive(:puts).with("Error: missing remote 'origin'")
      lambda { @helper.project }.should raise_error(SystemExit)
    end

    it "should exit due to non-github origin" do
      @helper.should_receive(:url_for).twice.with("origin").and_return("home:path/to/repo.git")
      @helper.should_receive(:origin).twice.and_return("origin")
      STDERR.should_receive(:puts).with("Error: remote 'origin' is not a github URL")
      lambda { @helper.project }.should raise_error(SystemExit)
    end
  end

  helper :repo_for do
    it "should return mephisto.git" do
      setup_url_for "mojombo", "mojombo", "mephisto"
      @helper.repo_for("mojombo").should == "mephisto.git"
    end
  end

  helper :user_and_repo_from do
    it "should parse a git:// url" do
      @helper.user_and_repo_from("git://github.com/defunkt/github.git").should == ["defunkt", "github.git"]
    end

    it "should parse a ssh-based url" do
      @helper.user_and_repo_from("git@github.com:mojombo/god.git").should == ["mojombo", "god.git"]
    end

    it "should parse a non-standard ssh-based url" do
      @helper.user_and_repo_from("ssh://git@github.com:mojombo/god.git").should == ["mojombo", "god.git"]
{'fraction_non_alphanumeric': 0.14446062890926228, 'fraction_numerical': 0.009767800531231257, 'mean_word_length': 3.225923244026068, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 5, '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': '1415012', 'n_tokens_mistral': 4029, 'n_tokens_neox': 3856, 'n_words': 984}