<commit_before>require 'test_helper'

class RepresenterTest < MiniTest::Spec
  describe "Representer" do
    before do
      @c = Class.new do
        include Roar::Representer::Base
      end
    end
    
    it "aliases #representable_property to #property" do
      @c.property :title
      assert_equal "title", @c.representable_attrs.first.name
    end
    
    it "aliases #representable_collection to #collection" do
      @c.collection :songs
      assert_equal "songs", @c.representable_attrs.first.name
    end
    
    
    describe "#from_attributes" do
      it "accepts a block yielding the created representer instance" do
        @c.class_eval { attr_accessor :name }
        
        assert_equal("Conan", @c.from_attributes({}) { |rep| rep.name = "Conan" }.name)
      end
      
      it "copies known properties, only, but doesn't complain" do
        @c.class_eval { property :id }
        
        assert_equal 1, @c.from_attributes("id" => 1, "unknown" => "don't use me").id
      end
      
      it "accepts symbols and strings as property name" do
        @c.class_eval { property :id }
        
        assert_equal @c.from_attributes(:id => 1).id, @c.from_attributes("id" => 1).id
      end
    end
  end
end
<commit_msg>added a test for inheritance.<commit_after>require 'test_helper'

class RepresenterTest < MiniTest::Spec
  describe "Representer::Base" do
    before do
      @c = Class.new do
        include Roar::Representer::Base
      end
    end
    
    it "aliases #representable_property to #property" do
      @c.property :title
      assert_equal "title", @c.representable_attrs.first.name
    end
    
    it "aliases #representable_collection to #collection" do
      @c.collection :songs
      assert_equal "songs", @c.representable_attrs.first.name
    end
    
    
    describe "#from_attributes" do
      it "accepts a block yielding the created representer instance" do
        @c.class_eval { attr_accessor :name }
        
        assert_equal("Conan", @c.from_attributes({}) { |rep| rep.name = "Conan" }.name)
      end
      
      it "copies known properties, only, but doesn't complain" do
        @c.class_eval { property :id }
        
        assert_equal 1, @c.from_attributes("id" => 1, "unknown" => "don't use me").id
      end
      
      it "accepts symbols and strings as property name" do
        @c.class_eval { property :id }
        
        assert_equal @c.from_attributes(:id => 1).id, @c.from_attributes("id" => 1).id
      end
    end
  end
  
  describe "Inheritance" do
    it "properly inherits properties" do
      base = Class.new do
        include Roar::Representer::JSON
        self.representation_name= "collection"
        property :name
      end
      
      assert_equal "{\"collection\":{\"name\":\"Paulo\"}}", Class.new(base).from_attributes(:name => "Paulo").to_json
    end
    
  end
end

{'fraction_non_alphanumeric': 0.1065232477446218, 'fraction_numerical': 0.002775850104094379, 'mean_word_length': 2.4078014184397163, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 12, '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': '6506273', 'n_tokens_mistral': 914, 'n_tokens_neox': 882, 'n_words': 240}