<commit_before>require 'spec/spec_helper'

describe ROXML::Definition do
  describe "#name_explicit?" do
    it "should indicate whether from option is present" do
      ROXML::Definition.new(:element, :from => 'somewhere').name_explicit?.should be_true
      ROXML::Definition.new(:element).name_explicit?.should be_false
    end

    it "should not consider name proxies as explicit" do
      ROXML::Definition.new(:element, :from => :attr).name_explicit?.should be_false
      ROXML::Definition.new(:element, :from => :content).name_explicit?.should be_false
    end
  end

  describe "DateTime reference", :shared => true do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("12:05pm, September 3rd, 1970").to_s == "1970-09-03T12:05:00+00:00"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["12:05pm, September 3rd, 1970", "3:00pm, May 22, 1700"]).map(&:to_s).should == ["1970-09-03T12:05:00+00:00", "1700-05-22T15:00:00+00:00"]
      end
    end
  end

  describe "Date reference", :shared => true do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("September 3rd, 1970").to_s == "1970-09-03"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["September 3rd, 1970", "1776-07-04"]).map(&:to_s).should == ["1970-09-03", "1776-07-04"]
      end
    end
  end

  it "should unescape xml entities" do
    ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>&quot;Wickard &amp; Filburn&quot; &gt;</question>
        <question> &lt; McCulloch &amp; Maryland?</question>
      </xml>
    }).should == ["\"Wickard & Filburn\" >", " < McCulloch & Maryland?"]
  end

  describe "attr name" do
    context "when ending with '_at'" do
      context "and without an :as argument" do
        before(:all) do
          @subject = ROXML::Definition.new(:time_at)
        end
        it_should_behave_like "DateTime reference"
      end<commit_msg>Ensure that utf characters are properly handled<commit_after>require 'spec/spec_helper'

describe ROXML::Definition do
  describe "#name_explicit?" do
    it "should indicate whether from option is present" do
      ROXML::Definition.new(:element, :from => 'somewhere').name_explicit?.should be_true
      ROXML::Definition.new(:element).name_explicit?.should be_false
    end

    it "should not consider name proxies as explicit" do
      ROXML::Definition.new(:element, :from => :attr).name_explicit?.should be_false
      ROXML::Definition.new(:element, :from => :content).name_explicit?.should be_false
    end
  end

  describe "DateTime reference", :shared => true do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("12:05pm, September 3rd, 1970").to_s == "1970-09-03T12:05:00+00:00"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["12:05pm, September 3rd, 1970", "3:00pm, May 22, 1700"]).map(&:to_s).should == ["1970-09-03T12:05:00+00:00", "1700-05-22T15:00:00+00:00"]
      end
    end
  end

  describe "Date reference", :shared => true do
    it "should return nil on empty string" do
      @subject.blocks.first.call("  ").should be_nil
    end

    it "should return a time version of the string" do
      @subject.blocks.first.call("September 3rd, 1970").to_s == "1970-09-03"
    end

    context "when passed an array of values" do
      it "should timify all of them" do
        @subject.blocks.first.call(["September 3rd, 1970", "1776-07-04"]).map(&:to_s).should == ["1970-09-03", "1776-07-04"]
      end
    end
  end

  it "should unescape xml entities" do
    ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>&quot;Wickard &amp; Filburn&quot; &gt;</question>
        <question> &lt; McCulloch &amp; Maryland?</question>
      </xml>
    }).should == ["\"Wickard & Filburn\" >", " < McCulloch & Maryland?"]
  end

  it "should unescape utf characters in xml" do
    ROXML::Definition.new(:questions, :as => []).to_ref(RoxmlObject.new).value_in(%{
      <xml>
        <question>ROXML\342\204\242</question>
      </xml>
    }).should == ["ROXML™"]
  end

  describe "attr name" do
    context "when ending with '_at'" do
      context "and without an :as argument" do
        before(:all) do
          @subject = ROXML::Definition.new(:time_at)
        end
        it_should_behave_like "DateTime reference"
      end
{'fraction_non_alphanumeric': 0.13979141596470115, 'fraction_numerical': 0.05114320096269555, 'mean_word_length': 3.1802179379715003, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '777343', 'n_tokens_mistral': 1798, 'n_tokens_neox': 1608, 'n_words': 450}