Sha256: b83a64beb4391af8037551264f6056a8b0899647daa23c53ce4c086c41254d5c

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

require "spec_helper"

describe Gyoku::XMLValue do

  describe ".create" do
    context "for DateTime objects" do
      it "returns an xs:dateTime compliant String" do
        expect(create(DateTime.new(2012, 03, 22, 16, 22, 33))).to eq("2012-03-22T16:22:33+00:00")
      end
    end

    context "for Date objects" do
      it "returns an xs:date compliant String" do
        expect(create(Date.new(2012, 03, 22))).to eq("2012-03-22")
      end
    end

    context "for Time objects" do
      it "returns an xs:time compliant String" do
        expect(create(Time.local(2012, 03, 22, 16, 22, 33))).to eq("16:22:33")
      end
    end

    it "returns the String value and escapes special characters" do
      expect(create("string")).to eq("string")
      expect(create("<tag>")).to eq("&lt;tag&gt;")
      expect(create("at&t")).to eq("at&amp;t")
      expect(create('"quotes"')).to eq("&quot;quotes&quot;")
    end

    it "returns the String value without escaping special characters" do
      expect(create("<tag>", false)).to eq("<tag>")
    end

    it "returns an xs:dateTime compliant String for Objects responding to #to_datetime" do
      singleton = Object.new
      def singleton.to_datetime
        DateTime.new 2012, 03, 22, 16, 22, 33
      end

      expect(create(singleton)).to eq("2012-03-22T16:22:33+00:00")
    end

    it "calls Proc objects and converts their return value" do
      object = lambda { DateTime.new 2012, 03, 22, 16, 22, 33 }
      expect(create(object)).to eq("2012-03-22T16:22:33+00:00")
    end

    it "calls #to_s unless the Object responds to #to_datetime" do
      expect(create("value")).to eq("value")
    end
  end

  def create(object, escape_xml = true)
    Gyoku::XMLValue.create object, escape_xml
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gyoku-1.3.0 spec/gyoku/xml_value_spec.rb
gyoku-1.2.3 spec/gyoku/xml_value_spec.rb
gyoku-1.2.2 spec/gyoku/xml_value_spec.rb
gyoku-1.2.0 spec/gyoku/xml_value_spec.rb