<commit_before>require "spec_helper"

sample_spec = [
          { name: :identifyer,    type: :string,     fix: "BLA"                },
          { name: :b,             type: :currency },
          { name: :c,             type: :float },
          { name: :d,             type: :integer },
          { name: :e,             type: :date },
]

empty_hash = { identifyer: "BLA", b: nil, c: nil, d: nil, e: nil }

empty_array = ["BLA", nil, nil, nil, nil]

filled_array = ["BLA", 20, 5.1, 10, Date.parse("12.10.1985")]

filled_csv   = "BLA;0,20;5,10;10;19851012\n"


describe Collmex::Api do

  describe ".is_a_collmex_api_line_obj?" do
    it "should fail for an array" do
      a = Array.new
      described_class.is_a_collmex_api_line_obj?(a).should be_false
    end

    it "should succeed for a Collmex::Api Object" do
      b = Collmex::Api::AccdocGet.new()
      described_class.is_a_collmex_api_line_obj?(b).should be_true
    end
  end

  describe ".line_class_exists?" do
    it "should be true for a existing class" do
      Collmex::Api.line_class_exists?("Line").should be true
    end

    it "should be false for a non existant class" do
      Collmex::Api.line_class_exists?("asdasdasdasdaaBla").should be false
    end
  end

  describe ".stringify_field" do
    tests = [
              { type: :string,      input: "asd",             outcome: "asd" },
              { type: :string,      input: "",                outcome: "" },
              { type: :string,      input: nil,               outcome: "" },

              { type: :integer,     input: nil,               outcome: "" },
              { type: :integer,     input: 2,                 outcome: "2" },
              { type: :integer,     input: 2.2,               outcome: "2" },
              { type: :integer,     input: -2.2,              outcome: "-2" },
              { type: :integer,     input: "-2.2",            outcome: "-2" },

              { type: :float,       input: 2.2,               outcome: "2,20" },
              { type: :float,       input: 2,                 outcome: "2,00" },
              { type: :float,       input: "2",               outcome: "2,00" },
              { type: :float,       input: "-2.00",           outcome: "-2,00" },
              { type: :float,       input: -2.00,             outcome: "-2,00" },

              { type: :currency,    input: 2,                 outcome: "0,02" },
              { type: :currency,    input: "2",               outcome: "0,02" },
              { type: :currency,    input: "-2.23",           outcome: "-2,23" },   # <= WARNING
              { type: :currency,    input: "-2,23",           outcome: "-2,23" },   # <= WARNING
              { type: :currency,    input: -2.00,             outcome: "-2,00" },
              { type: :currency,    input: -2.90,             outcome: "-2,90" },
              { type: :currency,    input: -2.999,             outcome: "-3,00" },
              { type: :currency,    input: -102.90,           outcome: "-102,90" },    # <= WARNING


    ]
    tests.each do |test|
      it "should represent #{test[:type]} \"#{test[:input].inspect}\" as \"#{test[:outcome]}\"" do
        described_class.stringify(test[:input],test[:type]).should === test[:outcome]
      end
    end
  end


  describe ".parse_line" do
    context "when given a valid line" do
      context "as an array" do
        it "should instanciate an api line object" do
          line = Collmex::Api::Login.new([12,34]).to_a
          described_class.parse_line(line).should be_a Collmex::Api::Line
        end
      end
      context "as n csv string" do
        it "should instanciate an api line object" do
          line = Collmex::Api::Login.new([12,34]).to_csv
          described_class.parse_line(line).should be_a Collmex::Api::Line
        end
      end
    end

    context "when given an invalid line" do
      it "should throw an error" do
        line = ["OMG", 2,3,4,5,6]
        lambda { described_class.parse_line(line) }.should raise_error 'Could not find a Collmex::Api::Line class for "Omg"'
      end
    end
  end

  describe ".parse_field" do
    tests = [
              { type: :string,      input: "asd",             outcome: "asd" },
              { type: :string,      input: "2",               outcome: "2" },
              { type: :string,      input: "2",               outcome: "2" },
              { type: :string,      input: 2,                 outcome: "2" },
              { type: :string,      input: "-2.3",            outcome: "-2.3" },
              { type: :string,      input:  nil,              outcome: "" },

              { type: :date,        input: nil,               outcome: nil },
              { type: :date,        input: "19851012",        outcome: Date.parse("12.10.1985") },
              { type: :date,        input: "1985/10/12",      outcome: Date.parse("12.10.1985") },
              { type: :date,        input: "1985-10-12",      outcome: Date.parse("12.10.1985") },


              { type: :integer,     input: "2,3",             outcome: 2 },          # <= WARNING
              { type: :integer,     input: "2",               outcome: 2 },
              { type: :integer,     input: "2.2",             outcome: 2 },
              { type: :integer,     input: 2,                 outcome: 2 },
              { type: :integer,     input: 2.2,               outcome: 2 },
              { type: :integer,     input: nil,               outcome: nil },          # <= WARNING

              { type: :float,       input: "2",               outcome: 2.0 },
              { type: :float,       input: 2,                 outcome: 2.0 },
              { type: :float,       input: 2.2,               outcome: 2.2 },
              { type: :float,       input: "2,0",             outcome: 2.0 },
              { type: :float,       input: "2.0",             outcome: 2.0 },<commit_msg>Fix spec to respect changed error message.<commit_after>require "spec_helper"

sample_spec = [
          { name: :identifyer,    type: :string,     fix: "BLA"                },
          { name: :b,             type: :currency },
          { name: :c,             type: :float },
          { name: :d,             type: :integer },
          { name: :e,             type: :date },
]

empty_hash = { identifyer: "BLA", b: nil, c: nil, d: nil, e: nil }

empty_array = ["BLA", nil, nil, nil, nil]

filled_array = ["BLA", 20, 5.1, 10, Date.parse("12.10.1985")]

filled_csv   = "BLA;0,20;5,10;10;19851012\n"


describe Collmex::Api do

  describe ".is_a_collmex_api_line_obj?" do
    it "should fail for an array" do
      a = Array.new
      described_class.is_a_collmex_api_line_obj?(a).should be_false
    end

    it "should succeed for a Collmex::Api Object" do
      b = Collmex::Api::AccdocGet.new()
      described_class.is_a_collmex_api_line_obj?(b).should be_true
    end
  end

  describe ".line_class_exists?" do
    it "should be true for a existing class" do
      Collmex::Api.line_class_exists?("Line").should be true
    end

    it "should be false for a non existant class" do
      Collmex::Api.line_class_exists?("asdasdasdasdaaBla").should be false
    end
  end

  describe ".stringify_field" do
    tests = [
              { type: :string,      input: "asd",             outcome: "asd" },
              { type: :string,      input: "",                outcome: "" },
              { type: :string,      input: nil,               outcome: "" },

              { type: :integer,     input: nil,               outcome: "" },
              { type: :integer,     input: 2,                 outcome: "2" },
              { type: :integer,     input: 2.2,               outcome: "2" },
              { type: :integer,     input: -2.2,              outcome: "-2" },
              { type: :integer,     input: "-2.2",            outcome: "-2" },

              { type: :float,       input: 2.2,               outcome: "2,20" },
              { type: :float,       input: 2,                 outcome: "2,00" },
              { type: :float,       input: "2",               outcome: "2,00" },
              { type: :float,       input: "-2.00",           outcome: "-2,00" },
              { type: :float,       input: -2.00,             outcome: "-2,00" },

              { type: :currency,    input: 2,                 outcome: "0,02" },
              { type: :currency,    input: "2",               outcome: "0,02" },
              { type: :currency,    input: "-2.23",           outcome: "-2,23" },   # <= WARNING
              { type: :currency,    input: "-2,23",           outcome: "-2,23" },   # <= WARNING
              { type: :currency,    input: -2.00,             outcome: "-2,00" },
              { type: :currency,    input: -2.90,             outcome: "-2,90" },
              { type: :currency,    input: -2.999,             outcome: "-3,00" },
              { type: :currency,    input: -102.90,           outcome: "-102,90" },    # <= WARNING


    ]
    tests.each do |test|
      it "should represent #{test[:type]} \"#{test[:input].inspect}\" as \"#{test[:outcome]}\"" do
        described_class.stringify(test[:input],test[:type]).should === test[:outcome]
      end
    end
  end


  describe ".parse_line" do
    context "when given a valid line" do
      context "as an array" do
        it "should instanciate an api line object" do
          line = Collmex::Api::Login.new([12,34]).to_a
          described_class.parse_line(line).should be_a Collmex::Api::Line
        end
      end
      context "as n csv string" do
        it "should instanciate an api line object" do
          line = Collmex::Api::Login.new([12,34]).to_csv
          described_class.parse_line(line).should be_a Collmex::Api::Line
        end
      end
    end

    context "when given an invalid line" do
      it "should throw an error" do
        line = ["OMG", 2,3,4,5,6]
        lambda { described_class.parse_line(line) }.should raise_error 'Could not find a Collmex::Api::Line class for "Omg" ("OMG")'
      end
    end
  end

  describe ".parse_field" do
    tests = [
              { type: :string,      input: "asd",             outcome: "asd" },
              { type: :string,      input: "2",               outcome: "2" },
              { type: :string,      input: "2",               outcome: "2" },
              { type: :string,      input: 2,                 outcome: "2" },
              { type: :string,      input: "-2.3",            outcome: "-2.3" },
              { type: :string,      input:  nil,              outcome: "" },

              { type: :date,        input: nil,               outcome: nil },
              { type: :date,        input: "19851012",        outcome: Date.parse("12.10.1985") },
              { type: :date,        input: "1985/10/12",      outcome: Date.parse("12.10.1985") },
              { type: :date,        input: "1985-10-12",      outcome: Date.parse("12.10.1985") },


              { type: :integer,     input: "2,3",             outcome: 2 },          # <= WARNING
              { type: :integer,     input: "2",               outcome: 2 },
              { type: :integer,     input: "2.2",             outcome: 2 },
              { type: :integer,     input: 2,                 outcome: 2 },
              { type: :integer,     input: 2.2,               outcome: 2 },
              { type: :integer,     input: nil,               outcome: nil },          # <= WARNING

              { type: :float,       input: "2",               outcome: 2.0 },
              { type: :float,       input: 2,                 outcome: 2.0 },
              { type: :float,       input: 2.2,               outcome: 2.2 },
              { type: :float,       input: "2,0",             outcome: 2.0 },
              { type: :float,       input: "2.0",             outcome: 2.0 },
{'fraction_non_alphanumeric': 0.14861546278262341, 'fraction_numerical': 0.036751630112625965, 'mean_word_length': 1.3999187157081894, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '2947489', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3643, 'n_words': 951}