require 'test/spec' require 'ferret' require 'pyrosoma/frame' require 'pyrosoma/database' context "Pyrosoma::Frame" do setup do @db = Pyrosoma::Database.new(:dir => Ferret::Store::RAMDirectory.new) @ubermaster = Pyrosoma::Frame.new({ :id => "ubermaster", :foo => "foo", }) @master = Pyrosoma::Frame.new({ :id => "master", :a => "quux", :b => "quux", :c => "quux", :indirect => "@ubermaster", }) @frame = Pyrosoma::Frame.new({ :id => "frame", :a => "foo", :b => "bar", :multiple => ["a", "b", "c"], :master => "@master", :indirect => "@master", :multiindirect => ["@master", "@ubermaster"], }) @frame2 = Pyrosoma::Frame.new({ :id => "frame2", :master => "@master", :content => "foo\nbar\nbaz\n" }) @frame3 = Pyrosoma::Frame.new({ :id => "frame3", :master => "@master", :content => ["foo", "bar", "baz"] }) @db << @ubermaster @db << @master @db << @frame << @frame2 << @frame3 end specify "should represent a frame" do @frame[:a].should.equal "foo" @frame[:b].should.equal "bar" end specify "should support inheritance by master" do @frame[:c].should.equal "quux" end specify "should support indirect links" do @frame.fetch(:indirect).should.equal "@master" @frame[:indirect][:a].should.equal "quux" @frame[:indirect][:indirect][:foo].should.equal "foo" end specify "should support reference making" do @frame.to_ref.should.be.kind_of String @frame.to_ref.should.equal "@frame" end specify "should support multi-values" do @frame[:multiple].should.equal ["a", "b", "c"] @frame[:multiindirect].should.equal [@master, @ubermaster] end specify "should support inverse relations" do inv = @master.inverse inv.should.be.kind_of Hash inv[:indirect].should.equal "@frame" inv[:multiindirect].should.equal "@frame" inv[:master].sort.should.equal ["@frame", "@frame2", "@frame3"] @master.inverse(:master).should.equal [@frame, @frame2, @frame3] withinv = @master.dup.add_inverse withinv[:inverse_indirect].should.equal @frame withinv[:inverse_master].should.equal [@frame, @frame2, @frame3] end specify "should support RFC2822-like stringification" do @frame2.to_s.should.equal < "a", :n => "9") @db << Pyrosoma::Frame.new(:id => "b", :n => "8") @db << Pyrosoma::Frame.new(:id => "c", :n => "2") @db << Pyrosoma::Frame.new(:id => "d", :n => "33") # Automagic integer sort @db["b"].next_by(:n).should.equal @db["a"] @db["a"].next_by(:n).should.be.nil @db["a"].prev_by(:n).should.equal @db["d"] @db["c"].prev_by(:n).should.be.nil @db["c"].next_by(:n, 2).should.equal [@db["b"], @db["a"]] # Override for ASCII sort @db["b"].next_by(:n, nil, :string).should.equal @db["a"] @db["a"].next_by(:n, nil, :string).should.be.nil @db["a"].prev_by(:n, nil, :string).should.equal @db["b"] @db["c"].prev_by(:n, nil, :string).should.be.nil @db["c"].next_by(:n, 2, :string).should.equal [@db["d"], @db["b"]] end end