<commit_before>require 'spec_helper'

describe Pidgin2Adium::BasicParser do
  describe "#parse" do
    it "returns false" do
      Pidgin2Adium::BasicParser.new(create_chat_file, '').parse.should be_false
    end
  end

  describe "#create_adium_time" do
    before do
      @bp = Pidgin2Adium::BasicParser.new(create_chat_file, '')
    end

    it "parses a first line time correctly" do
      first_line_time = "4/18/2007 11:02:00 AM"
      time = @bp.create_adium_time(first_line_time)
      time.should include '2007-04-18T11:02:00'
    end

    it "parses a normal time correctly" do
      normal_time = "2007-08-20 12:33:13"
      time = @bp.create_adium_time(normal_time)
      time.should include '2007-08-20T12:33:13'
    end

    it "parses a minimal time correctly" do
      minimal_time = "04:22:05 AM"
      path = create_chat_file('minimal.html') do |b|
        b.first_line :time => "1/15/2008 7:14:45 AM"
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      time = parser.create_adium_time(minimal_time)
      time.should include '2008-01-15T04:22:05'
    end

    it "parses a minimal time without AM/PM correctly" do
      minimal_time_without_ampm = "04:22:05"
      path = create_chat_file('minimal.html') do |b|
        b.first_line :time => "1/15/2008 7:14:45 AM"
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      time = parser.create_adium_time(minimal_time_without_ampm)
      time.should include '2008-01-15T04:22:05'
    end

    it "returns nil for an invalid time" do
      invalid_time = "Hammer time!"
      time = @bp.create_adium_time(invalid_time)
      time.should be_nil
    end
  end

  describe "#pre_parse!" do
    it "raises an error for an empty file" do
      path = File.join(@current_dir, "logfiles", "invalid-first-line.txt")
      bp =  Pidgin2Adium::BasicParser.new(path, '')
      lambda do
        bp.pre_parse!()
      end.should raise_error(Pidgin2Adium::InvalidFirstLineError)
    end

    it "returns true when everything can be parsed" do
      path = create_chat_file('blah.html') do |b|
        b.first_line
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      parser.pre_parse!.should be_true
    end

    describe "correctly setting variables" do
      before do
        path = create_chat_file('blah.html') do |b|
          b.first_line :from => 'othersn', :to => 'aolsystemmsg',
            :time => '1/15/2008 7:14:45 AM', :service => 'aim'
        end

        @bp =  Pidgin2Adium::BasicParser.new(path, '')
        @bp.pre_parse!
      end

      it "correctly sets @service" do
        @bp.instance_variable_get('@service').should == 'aim'
      end

      it "correctly sets user_SN" do
        @bp.instance_variable_get('@user_SN').should == 'othersn'
      end

      it "correctly sets partner_SN" do
        @bp.instance_variable_get('@partner_SN').should == 'aolsystemmsg'
      end

      it "correctly sets basic_time_info" do
        @bp.instance_variable_get('@basic_time_info').should == {:year=>2008, :month=>1, :day=>15}
      end

      it "correctly sets adium_chat_time_start" do
        @bp.instance_variable_get('@adium_chat_time_start').should == '2008-01-15T07:14:45EST'
      end
    end
  end

  describe "#get_sender_by_alias" do
    before do
      path = create_chat_file('sender.txt') do |b|
        b.first_line :from => "awesome SN", :to => "BUDDY_PERSON"
        b.message :from_alias => "<NAME>"
        b.message :from_alias => "<NAME>"
      end
      @my_alias = "<NAME>"
      @my_SN =  "awesomesn" # normalized from "awesome SN"

      @partner_alias = "<NAME>"
      @partner_SN = "BUDDY_PERSON" # not normalized
      @bp = Pidgin2Adium::BasicParser.new(path, '<NAME>')
    end

    it "returns my SN when passed my alias" do
      @bp.get_sender_by_alias(@my_alias).should == @my_SN
    end

    it "returns my SN when passed my alias with an action" do
      @bp.get_sender_by_alias("***#{@my_alias}").should == @my_SN
    end

    it "returns partner's SN when passed partner's alias" do
      @bp.get_sender_by_alias(@partner_alias).should == @partner_SN<commit_msg>Allow for different time zones in tests.<commit_after>require 'spec_helper'

describe Pidgin2Adium::BasicParser do
  describe "#parse" do
    it "returns false" do
      Pidgin2Adium::BasicParser.new(create_chat_file, '').parse.should be_false
    end
  end

  describe "#create_adium_time" do
    before do
      @bp = Pidgin2Adium::BasicParser.new(create_chat_file, '')
    end

    it "parses a first line time correctly" do
      first_line_time = "4/18/2007 11:02:00 AM"
      time = @bp.create_adium_time(first_line_time)
      time.should include '2007-04-18T11:02:00'
    end

    it "parses a normal time correctly" do
      normal_time = "2007-08-20 12:33:13"
      time = @bp.create_adium_time(normal_time)
      time.should include '2007-08-20T12:33:13'
    end

    it "parses a minimal time correctly" do
      minimal_time = "04:22:05 AM"
      path = create_chat_file('minimal.html') do |b|
        b.first_line :time => "1/15/2008 7:14:45 AM"
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      time = parser.create_adium_time(minimal_time)
      time.should include '2008-01-15T04:22:05'
    end

    it "parses a minimal time without AM/PM correctly" do
      minimal_time_without_ampm = "04:22:05"
      path = create_chat_file('minimal.html') do |b|
        b.first_line :time => "1/15/2008 7:14:45 AM"
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      time = parser.create_adium_time(minimal_time_without_ampm)
      time.should include '2008-01-15T04:22:05'
    end

    it "returns nil for an invalid time" do
      invalid_time = "Hammer time!"
      time = @bp.create_adium_time(invalid_time)
      time.should be_nil
    end
  end

  describe "#pre_parse!" do
    it "raises an error for an empty file" do
      path = File.join(@current_dir, "logfiles", "invalid-first-line.txt")
      bp =  Pidgin2Adium::BasicParser.new(path, '')
      lambda do
        bp.pre_parse!()
      end.should raise_error(Pidgin2Adium::InvalidFirstLineError)
    end

    it "returns true when everything can be parsed" do
      path = create_chat_file('blah.html') do |b|
        b.first_line
      end
      parser = Pidgin2Adium::BasicParser.new(path, '')
      parser.pre_parse!.should be_true
    end

    describe "correctly setting variables" do
      before do
        path = create_chat_file('blah.html') do |b|
          b.first_line :from => 'othersn', :to => 'aolsystemmsg',
            :time => '1/15/2008 7:14:45 AM', :service => 'aim'
        end

        @bp =  Pidgin2Adium::BasicParser.new(path, '')
        @bp.pre_parse!
      end

      it "correctly sets @service" do
        @bp.instance_variable_get('@service').should == 'aim'
      end

      it "correctly sets user_SN" do
        @bp.instance_variable_get('@user_SN').should == 'othersn'
      end

      it "correctly sets partner_SN" do
        @bp.instance_variable_get('@partner_SN').should == 'aolsystemmsg'
      end

      it "correctly sets basic_time_info" do
        @bp.instance_variable_get('@basic_time_info').should == {:year=>2008, :month=>1, :day=>15}
      end

      it "correctly sets adium_chat_time_start" do
        @bp.instance_variable_get('@adium_chat_time_start').should include '2008-01-15T07:14:45'
      end
    end
  end

  describe "#get_sender_by_alias" do
    before do
      path = create_chat_file('sender.txt') do |b|
        b.first_line :from => "awesome SN", :to => "BUDDY_PERSON"
        b.message :from_alias => "<NAME>"
        b.message :from_alias => "<NAME>"
      end
      @my_alias = "<NAME>"
      @my_SN =  "awesomesn" # normalized from "awesome SN"

      @partner_alias = "<NAME>"
      @partner_SN = "BUDDY_PERSON" # not normalized
      @bp = Pidgin2Adium::BasicParser.new(path, 'Gabe B-W')
    end

    it "returns my SN when passed my alias" do
      @bp.get_sender_by_alias(@my_alias).should == @my_SN
    end

    it "returns my SN when passed my alias with an action" do
      @bp.get_sender_by_alias("***#{@my_alias}").should == @my_SN
    end

    it "returns partner's SN when passed partner's alias" do
      @bp.get_sender_by_alias(@partner_alias).should == @partner_SN
{'fraction_non_alphanumeric': 0.11949305974652988, 'fraction_numerical': 0.03910681955340978, 'mean_word_length': 2.853953488372093, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 38, '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': '807767', 'n_tokens_mistral': 3077, 'n_tokens_neox': 2824, 'n_words': 659}