Skip to content

Commit

Permalink
Update all tests to expect syntax. This changed a few ancestry method…
Browse files Browse the repository at this point in the history
… names.
  • Loading branch information
meltingice committed Aug 25, 2013
1 parent 79b6ea2 commit c9b1096
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 86 deletions.
2 changes: 1 addition & 1 deletion lib/psd/layer_mask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def parse_global_mask
@global_mask = {}
@global_mask[:overlay_color_space] = @file.read_short
@global_mask[:color_components] = 4.times.map { |i| @file.read_short >> 8 }
@global_mask[:opacity] = @file.read_short
@global_mask[:opacity] = @file.read_short / 16.0

# 0 = color selected, 1 = color protected, 128 = use value per layer
@global_mask[:kind] = @file.read(1).bytes.to_a[0]
Expand Down
4 changes: 2 additions & 2 deletions lib/psd/nodes/ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def has_children?
end

# Inverse of has_children?
def is_childless?
def childless?
!has_children?
end

Expand All @@ -44,7 +44,7 @@ def has_siblings?
end

# Is this node the only descendant of its parent?
def is_only_child?
def only_child?
siblings.length == 1
end

Expand Down
Binary file modified spec/files/text.psd
Binary file not shown.
68 changes: 34 additions & 34 deletions spec/hierarchy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
@psd.parse!

tree = @psd.tree.to_hash
tree.should include :children
tree[:children].length.should == 3
expect(tree).to include :children
expect(tree[:children].length).to eq(3)
end

describe "Ancestry" do
Expand All @@ -20,66 +20,66 @@
end

it "should provide tree traversal methods" do
@tree.respond_to?(:root).should be_true
@tree.respond_to?(:siblings).should be_true
@tree.respond_to?(:descendants).should be_true
@tree.respond_to?(:subtree).should be_true
expect(@tree).to respond_to(:root)
expect(@tree).to respond_to(:siblings)
expect(@tree).to respond_to(:descendants)
expect(@tree).to respond_to(:subtree)
end

it "should properly identify the root node" do
@tree.root?.should be_true
@tree.root.should == @tree
@tree.children.last.root.should == @tree
expect(@tree.root?).to be_true
expect(@tree.root).to be @tree
expect(@tree.children.last.root).to be @tree
end

it "should retrieve all descendants of a node" do
@tree.descendants.size.should == 12
@tree.descendant_layers.size.should == 9
@tree.descendant_groups.size.should == 3
@tree.descendants.first.should_not == @tree
expect(@tree.descendants.size).to eq(12)
expect(@tree.descendant_layers.size).to eq(9)
expect(@tree.descendant_groups.size).to eq(3)
expect(@tree.descendants.first).not_to be @tree
end

it "should retreive the entire subtree of a node" do
@tree.subtree.size.should == 13
@tree.subtree_layers.size.should == 9
@tree.subtree_groups.size.should == 3
@tree.subtree.first.should == @tree
expect(@tree.subtree.size).to eq(13)
expect(@tree.subtree_layers.size).to eq(9)
expect(@tree.subtree_groups.size).to eq(3)
expect(@tree.subtree.first).to be @tree
end

it "should properly identify the existence of children" do
@tree.has_children?.should be_true
@tree.is_childless?.should be_false
@tree.descendant_layers.first.has_children?.should be_false
@tree.descendant_layers.first.is_childless?.should be_true
expect(@tree).to have_children
expect(@tree).to_not be_childless
expect(@tree.descendant_layers.first).to_not have_children
expect(@tree.descendant_layers.first).to be_childless
end

it "should retrieve all siblings of a node" do
@tree.children.first.siblings.should == @tree.children
@tree.children.first.siblings.should include @tree.children.first
@tree.children.first.has_siblings?.should be_true
@tree.children.first.is_only_child?.should be_false
expect(@tree.children.first.siblings).to be @tree.children
expect(@tree.children.first.siblings).to include @tree.children.first
expect(@tree.children.first).to have_siblings
expect(@tree.children.first).to_not be_only_child
end

it "should properly calculate node depth" do
@tree.depth.should == 0
@tree.descendant_layers.last.depth.should == 2
@tree.children.first.depth.should == 1
expect(@tree.depth).to eq(0)
expect(@tree.descendant_layers.last.depth).to eq(2)
expect(@tree.children.first.depth).to eq(1)
end

describe "Searching" do
it "should find a node given a path" do
@tree.children_at_path('Version A/Matte').is_a?(Array).should be_true
@tree.children_at_path('Version A/Matte').size.should == 1
@tree.children_at_path('Version A/Matte').first.is_a?(PSD::Node::Layer).should be_true
expect(@tree.children_at_path('Version A/Matte')).to be_an_instance_of(Array)
expect(@tree.children_at_path('Version A/Matte').size).to eq(1)
expect(@tree.children_at_path('Version A/Matte').first).to be_an_instance_of(PSD::Node::Layer)
end

it "should ignore leading slashes" do
@tree.children_at_path('/Version A/Matte').size.should == 1
expect(@tree.children_at_path('/Version A/Matte').size).to eq(1)
end

it "should return an empty array when a node is not found" do
@tree.children_at_path('LOLWUTOMGBBQSAUCE').is_a?(Array).should be_true
@tree.children_at_path('LOLWUTOMGBBQSAUCE').size.should == 0
expect(@tree.children_at_path('NOPE')).to be_an_instance_of(Array)
expect(@tree.children_at_path('NOPE').size).to eq(0)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/identity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
tmpfile = Tempfile.new("simplest_export.psd")
psd.export tmpfile.path

Digest::MD5.hexdigest(tmpfile.read).should == Digest::MD5.hexdigest(File.read(filepath))
expect(Digest::MD5.hexdigest(tmpfile.read)).to eq(Digest::MD5.hexdigest(File.read(filepath)))
end

it "should export a file with a layer" do
Expand All @@ -19,7 +19,7 @@
tmpfile = Tempfile.new("one_layer_export.psd")
psd.export tmpfile.path

Digest::MD5.hexdigest(tmpfile.read).should == Digest::MD5.hexdigest(File.read(filepath))
expect(Digest::MD5.hexdigest(tmpfile.read)).to eq(Digest::MD5.hexdigest(File.read(filepath)))
end

it "should export a PSD with vector paths" do
Expand All @@ -29,6 +29,6 @@
tmpfile = Tempfile.new("path_export.psd")
psd.export tmpfile.path

Digest::MD5.hexdigest(tmpfile.read).should == Digest::MD5.hexdigest(File.read(filepath))
expect(Digest::MD5.hexdigest(tmpfile.read)).to eq(Digest::MD5.hexdigest(File.read(filepath)))
end
end
83 changes: 42 additions & 41 deletions spec/parsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

it "should parse without error" do
@psd.parse!
@psd.parsed?.should be_true
expect(@psd).to be_parsed
end

describe 'Header' do
Expand All @@ -16,25 +16,25 @@
end

it "should contain data" do
@psd.header.should_not be_nil
expect(@psd.header).not_to be_nil
end

it "should be the proper version" do
@psd.header.version.should == 1
expect(@psd.header.version).to eq(1)
end

it "should have the proper number of channels" do
@psd.header.channels.should == 3
expect(@psd.header.channels).to eq(3)
end

it "should parse the proper document dimensions" do
@psd.header.width.should == 900
@psd.header.height.should == 600
expect(@psd.header.width).to eq(900)
expect(@psd.header.height).to eq(600)
end

it "should correctly parse the color mode" do
@psd.header.mode.should == 3
@psd.header.mode_name.should == 'RGBColor'
expect(@psd.header.mode).to eq(3)
expect(@psd.header.mode_name).to eq('RGBColor')
end
end

Expand All @@ -44,18 +44,18 @@
end

it "should contain data" do
@psd.resources.should_not be_nil
@psd.resources.data.is_a?(Hash).should be_true
@psd.resources.data.size.should >= 1
expect(@psd.resources).not_to be_nil
expect(@psd.resources.data).to be_an_instance_of(Hash)
expect(@psd.resources.data.size).to be >= 1
end

it "should be of type 8BIM" do
@psd.resources.data.each { |id, r| r.type.should == '8BIM' }
@psd.resources.data.each { |id, r| expect(r.type).to eq('8BIM') }
end

it "should have an ID" do
@psd.resources.data.each do |id, r|
r.id.should_not be_nil
expect(r.id).to_not be_nil
end
end
end
Expand All @@ -66,18 +66,19 @@
end

it "should contain data" do
@psd.layer_mask.should_not be_nil
@psd.layer_mask.is_a?(PSD::LayerMask).should be_true
expect(@psd.layer_mask).to_not be_nil
expect(@psd.layer_mask).to be_an_instance_of(PSD::LayerMask)
end

it "should contain layers" do
@psd.layer_mask.layers.size.should > 0
expect(@psd.layer_mask.layers.size).to be > 0
end

it "should contain the global layer mask data" do
pending "Not implemented yet"

@psd.layer_mask.global_mask.should_not be_nil
expect(@psd.layer_mask.global_mask).to_not be_nil
expect(@psd.layer_mask.global_mask).to include :overlay_color_space
expect(@psd.layer_mask.global_mask).to include :color_components
expect(@psd.layer_mask.global_mask).to include opacity: 1.0
end
end

Expand All @@ -87,48 +88,48 @@
end

it "should contain each layer" do
@psd.layer_mask.layers.size.should == 15
@psd.layers.should == @psd.layer_mask.layers
@psd.layers.each { |l| l.is_a?(PSD::Layer).should be_true }
expect(@psd.layer_mask.layers.size).to eq(15)
expect(@psd.layers).to be @psd.layer_mask.layers
@psd.layers.each { |l| expect(l).to be_an_instance_of(PSD::Layer) }
end

it "should have a name" do
@psd.layers.first.name.should == 'Version C'
expect(@psd.layers.first.name).to eq('Version C')
end

it "should properly identify folders" do
@psd.layers.first.folder?.should be_true
@psd.layers.select { |l| l.name == 'Matte' }.first.folder?.should be_false
expect(@psd.layers.first).to be_folder
expect(@psd.layers.select { |l| l.name == 'Matte' }.first).not_to be_folder
end

it "should properly detect visibility" do
@psd.layers.first.visible?.should be_false
@psd
.layers
.select { |l| l.name == 'Version A' }.first
.visible?
.should be_true
expect(@psd.layers.first).not_to be_visible
expect(
@psd
.layers
.select { |l| l.name == 'Version A' }.first
).to be_visible
end

it "should properly calculate dimensions" do
layer = @psd.layers.select { |l| l.name == 'Logo_Glyph' }.last
layer.width.should == 142
layer.height.should == 179
expect(layer.width).to eq(142)
expect(layer.height).to eq(179)
end

it "should properly calculate coordinates" do
layer = @psd.layers.select { |l| l.name == 'Logo_Glyph' }.last
layer.left.should == 379
layer.top.should == 210
expect(layer.left).to eq(379)
expect(layer.top).to eq(210)
end

it "should have a blend mode" do
layer = @psd.layers.select { |l| l.name == 'Version A' }.last
layer.blend_mode.should_not be_nil
layer.blend_mode.mode.should == 'normal'
layer.blend_mode.opacity.should == 255
layer.blend_mode.opacity_percentage.should == 100
layer.blend_mode.visible.should be_true
blend_mode = @psd.layers.select { |l| l.name == 'Version A' }.last.blend_mode
expect(blend_mode).to_not be_nil
expect(blend_mode.mode).to eq('normal')
expect(blend_mode.opacity).to eq(255)
expect(blend_mode.opacity_percentage).to eq(100)
expect(blend_mode.visible).to be_true
end
end
end
10 changes: 6 additions & 4 deletions spec/psd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

it 'should open a file without a block' do
psd = PSD.open(filename)
psd.parsed?.should == true
psd.should be_instance_of PSD
expect(psd).to be_parsed
expect(psd).to be_an_instance_of(PSD)
end

it 'should refuse to open a bad filename' do
Expand All @@ -15,11 +15,13 @@

it 'should open a file and feed it to a block' do
PSD.open(filename) do |psd|
psd.parsed?.should == true
psd.should be_instance_of PSD
expect(psd).to be_parsed
expect(psd).to be_an_instance_of(PSD)
end
end

# We have to use #should syntax here because the DSL binds
# the block to the PSD instance.
it 'should open a file and feed it to a block DSL style' do
PSD.open(filename) do
parsed?.should == true
Expand Down
17 changes: 16 additions & 1 deletion spec/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

text = psd.tree.children.first.text
text.should be_an_instance_of(Hash)
text[:value].should == 'Test'
expect(text[:value]).to eq('Test')
end

it "can be exported as CSS" do
psd = PSD.new('spec/files/text.psd')
psd.parse!

type = psd.tree.children.first.type
css = type.to_css
expect(css).to be_an_instance_of(String)
expect(css).to include 'MyriadPro-Regular'
expect(css).to include '37.0pt'
expect(css).to include 'rgba(24, 24, 24, 255)'
css.split(/\n/).each do |c|
expect(c[-1]).to eq(";")
end
end
end

0 comments on commit c9b1096

Please sign in to comment.