<commit_before>require 'ostruct'
require 'gitpeer'
require 'gitpeer/representation'

describe GitPeer::Representation do

  User = Struct.new(:name, :birthday)

  class UserRepr < GitPeer::Representation
    prop :name
    prop :birthday
    link :self, href: '/x'
  end

  it 'renders props and links' do
    repr = UserRepr.new(User.new("Andrey", "1987"))
    repr.to_hash.should == {
      name: "Andrey",
      birthday: "1987",
      _links: {self: {href: '/x'}}
    }
  end

  it 'allows getting props from different name' do
    class Repr < GitPeer::Representation
      prop :a, from: :b
    end

    repr = Repr.new(OpenStruct.new(b: 2))
    repr.to_hash.should == {a: 2}
  end

  PowerUser = Struct.new(:name, :birthday, :power)
  class PowerUserRepr < UserRepr
    prop :power
    link :power, href: '/y', templated: true
  end

  it 'allows subclassing representations' do
    repr = PowerUserRepr.new(PowerUser.new("Andrey", "1987", 100))
    repr.to_hash.should == {
      name: "Andrey",
      birthday: "1987",
      power: 100,
      _links: {
        self: {href: '/x'},
        power: {href: '/y', templated: true}
      }
    }
  end

  it 'allows computed props' do
    class MyRepr < GitPeer::Representation
      prop :c, from: proc { @obj.a + @obj.b }
    end

    repr = MyRepr.new(OpenStruct.new(a: 1, b: 2))
    repr.to_hash.should == {c: 3}
  end

  it 'allows representations to depend on context' do
    class ContextualRepr < GitPeer::Representation
      prop :c, from: proc { @context[:b] + @obj.a }
    end

    repr = ContextualRepr.new(OpenStruct.new(a: 1), b: 2)
    repr.to_hash.should == {c: 3}
  end

  it 'allows define props with their own representations' do
    brepr = Class.new(GitPeer::Representation) do
      prop :c
    end
    arepr = Class.new(GitPeer::Representation) do
      prop :a
      prop :b, repr: brepr
    end

    repr = arepr.new(OpenStruct.new(a: 1, b: OpenStruct.new(c: 2)))
    repr.to_hash.should == {a: 1, b: {c: 2}}
  end
  
  it 'allows define props with inline representations' do
    arepr = Class.new(GitPeer::Representation) do
      prop :a
      prop :b do
        prop :c
      end
    end

    repr = arepr.new(OpenStruct.new(a: 1, b: OpenStruct.new(c: 2)))
    repr.to_hash.should == {a: 1, b: {c: 2}}
  end

  it 'allows override props' do
    class XRepr < GitPeer::Representation
      prop :a
    end
    class YRepr < XRepr
      prop :a, from: :b
    end

    repr1 = XRepr.new(OpenStruct.new(a: 1, b: 2))
    repr1.to_hash.should == {a: 1}
    repr2 = YRepr.new(OpenStruct.new(a: 1, b: 2))
    repr2.to_hash.should == {a: 2}
  end

  describe 'representing collections' do

    it 'represents collections as arrays' do
      a = Class.new(GitPeer::Representation) do
        collection :a
        collection :b
      end
      repr = a.new(OpenStruct.new(a: [1, 2], b: [1, 2].to_enum))
      repr.to_hash.should == {a: [1, 2], b: [1, 2]}
    end

    it 'allows to represent elements in collection with custom representer' do
      ea = Class.new(GitPeer::Representation) do
        prop :a, from: :b
      end
      a = Class.new(GitPeer::Representation) do
        collection :a, repr: ea
      end
      repr = a.new(OpenStruct.new(a: [OpenStruct.new(b: 2)]))
      repr.to_hash.should == {a: [{a: 2}]}
    end

    it 'allows to represent elements in collection with inline representer' do
      a = Class.new(GitPeer::Representation) do
        collection :a do
          prop :a, from: :b
        end
      end
      repr = a.new(OpenStruct.new(a: [OpenStruct.new(b: 2)]))
      repr.to_hash.should == {a: [{a: 2}]}
    end
  end

  describe 'representation error' do

    it 'fails if represented obj cannot respond to needed methods' do
      class FailRepr1 < GitPeer::Representation
        prop :x
      end
      expect { 
        FailRepr1.new(OpenStruct.new(z: 1)).to_hash
      }.to raise_error(GitPeer::Representation::RepresentationError)
    end

    it 'fails if represented obj cannot respond to needed methods' do
      class FailRepr2 < GitPeer::Representation
        prop :x, from: :y
      end
      expect { 
        FailRepr2.new(OpenStruct.new(x: 1)).to_hash
      }.to raise_error(GitPeer::Representation::RepresentationError)
    end

  end
end<commit_msg>more specs for reprs<commit_after>require 'ostruct'
require 'gitpeer'
require 'gitpeer/representation'

describe GitPeer::Representation do

  User = Struct.new(:name, :birthday)

  class UserRepr < GitPeer::Representation
    prop :name
    prop :birthday
    link :self, href: '/x'
  end

  it 'renders props and links' do
    repr = UserRepr.new(User.new("Andrey", "1987"))
    repr.to_hash.should == {
      name: "Andrey",
      birthday: "1987",
      _links: {self: {href: '/x'}}
    }
  end

  it 'allows getting props from different name' do
    class Repr < GitPeer::Representation
      prop :a, from: :b
    end

    repr = Repr.new(OpenStruct.new(b: 2))
    repr.to_hash.should == {a: 2}
  end

  PowerUser = Struct.new(:name, :birthday, :power)
  class PowerUserRepr < UserRepr
    prop :power
    link :power, href: '/y', templated: true
  end

  it 'allows subclassing representations' do
    repr = PowerUserRepr.new(PowerUser.new("Andrey", "1987", 100))
    repr.to_hash.should == {
      name: "Andrey",
      birthday: "1987",
      power: 100,
      _links: {
        self: {href: '/x'},
        power: {href: '/y', templated: true}
      }
    }
  end

  it 'allows computed props' do
    class MyRepr < GitPeer::Representation
      prop :c, from: proc { @obj.a + @obj.b }
    end

    repr = MyRepr.new(OpenStruct.new(a: 1, b: 2))
    repr.to_hash.should == {c: 3}
  end

  it 'allows representations to depend on context' do
    class ContextualRepr < GitPeer::Representation
      prop :c, from: proc { @context[:b] + @obj.a }
    end

    repr = ContextualRepr.new(OpenStruct.new(a: 1), b: 2)
    repr.to_hash.should == {c: 3}
  end

  it 'allows define props with their own representations' do
    brepr = Class.new(GitPeer::Representation) do
      prop :c
    end
    arepr = Class.new(GitPeer::Representation) do
      prop :a
      prop :b, repr: brepr
    end

    repr = arepr.new(OpenStruct.new(a: 1, b: OpenStruct.new(c: 2)))
    repr.to_hash.should == {a: 1, b: {c: 2}}
  end
  
  it 'allows define props with inline representations' do
    arepr = Class.new(GitPeer::Representation) do
      prop :a
      prop :b do
        prop :c
      end
    end

    repr = arepr.new(OpenStruct.new(a: 1, b: OpenStruct.new(c: 2)))
    repr.to_hash.should == {a: 1, b: {c: 2}}
  end

  it 'allows override props' do
    class XRepr < GitPeer::Representation
      prop :a
    end
    class YRepr < XRepr
      prop :a, from: :b
    end

    repr1 = XRepr.new(OpenStruct.new(a: 1, b: 2))
    repr1.to_hash.should == {a: 1}
    repr2 = YRepr.new(OpenStruct.new(a: 1, b: 2))
    repr2.to_hash.should == {a: 2}
  end

  describe 'representing collections' do

    it 'represents collections as arrays' do
      a = Class.new(GitPeer::Representation) do
        collection :a
        collection :b
      end
      repr = a.new(OpenStruct.new(a: [1, 2], b: [1, 2].to_enum))
      repr.to_hash.should == {a: [1, 2], b: [1, 2]}
    end

    it 'allows to represent elements in collection with custom representer' do
      ea = Class.new(GitPeer::Representation) do
        prop :a, from: :b
      end
      a = Class.new(GitPeer::Representation) do
        collection :a, repr: ea
      end
      repr = a.new(OpenStruct.new(a: [OpenStruct.new(b: 2)]))
      repr.to_hash.should == {a: [{a: 2}]}
    end

    it 'allows to represent elements in collection with inline representer' do
      a = Class.new(GitPeer::Representation) do
        collection :a do
          prop :a, from: :b
        end
      end
      repr = a.new(OpenStruct.new(a: [OpenStruct.new(b: 2)]))
      repr.to_hash.should == {a: [{a: 2}]}
    end
  end

  describe 'representation error' do

    it 'fails if represented obj cannot respond to needed methods' do
      class FailRepr1 < GitPeer::Representation
        prop :x
      end
      expect { 
        FailRepr1.new(OpenStruct.new(z: 1)).to_hash
      }.to raise_error(GitPeer::Representation::RepresentationError)
    end

    it 'fails if represented obj cannot respond to needed methods' do
      class FailRepr2 < GitPeer::Representation
        prop :x, from: :y
      end
      expect { 
        FailRepr2.new(OpenStruct.new(x: 1)).to_hash
      }.to raise_error(GitPeer::Representation::RepresentationError)
    end

  end

  it 'automatically generates representations for Structs' do
    user = Struct.new(:name, :age)
    repr = GitPeer::Representation.for_struct(user)
    repr.new(user.new("Andrey", 27)).to_hash.should == {name: "Andrey", age: 27}
  end
end
{'fraction_non_alphanumeric': 0.12588128269274507, 'fraction_numerical': 0.015465089833977713, 'mean_word_length': 2.5854056257643703, 'pattern_counts': {'":': 0, '<': 21, '<?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': '7530397', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3047, 'n_words': 869}