<commit_before>require 'test_helper'
require 'representable/coercion'

class VirtusCoercionTest < MiniTest::Spec
  class Song  # note that we don't define accessors for the properties here.
  end
    
  describe "Coercion with Virtus" do
    #before do
    #  module SongRepresenter
    #    include Representable::JSON
    #    include Representable::Coercion
    #    property :composed_at, :type => DateTime 
    #  end
    #end
   # 
   # it "coerces properties in #from_json" do
   #   song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\"}")
   #   assert_kind_of DateTime, song.composed_at
   #   assert_equal "expected", song.composed_at
   # end
    class ImmigrantSong
      include Representable::JSON
      include Virtus
      extend Representable::Coercion::ClassMethods
      
      property :composed_at, :type => DateTime 
    end
   
  end
end<commit_msg>coercion with virtus on class level works, even with default values.<commit_after>require 'test_helper'
require 'representable/coercion'

class VirtusCoercionTest < MiniTest::Spec
  class Song  # note that we don't define accessors for the properties here.
  end
    
  describe "Coercion with Virtus" do
    #before do
    #  module SongRepresenter
    #    include Representable::JSON
    #    include Representable::Coercion
    #    property :composed_at, :type => DateTime 
    #  end
    #end
   # 
   # it "coerces properties in #from_json" do
   #   song = Song.new.extend(SongRepresenter).from_json("{\"composed_at\":\"November 18th, 1983\"}")
   #   assert_kind_of DateTime, song.composed_at
   #   assert_equal "expected", song.composed_at
   # end
    class ImmigrantSong
      include Representable::JSON
      include Virtus
      extend Representable::Coercion::ClassMethods
      
      property :composed_at, :type => DateTime, :default => "May 12th, 2012"
    end
    
    it "coerces into the provided type" do
      song = ImmigrantSong.new.from_json("{\"composed_at\":\"November 18th, 1983\"}")
      assert_equal DateTime.parse("Fri, 18 Nov 1983 00:00:00 +0000"), song.composed_at
    end
    
    it "respects the :default options" do
      song = ImmigrantSong.new.from_json("{}")
      assert_kind_of DateTime, song.composed_at
      assert_equal DateTime.parse("Mon, 12 May 2012 00:00:00 +0000"), song.composed_at
    end
    
  end
end
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.02359882005899705, 'mean_word_length': 3.1649122807017545, 'pattern_counts': {'":': 3, '<': 5, '<?xml version=': 0, '>': 8, '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': '5072178', 'n_tokens_mistral': 814, 'n_tokens_neox': 733, 'n_words': 195}