Skip to content

Commit

Permalink
splitting refinement development to branch and returning to simle sol…
Browse files Browse the repository at this point in the history
…ution in master
  • Loading branch information
gorn committed Mar 7, 2016
1 parent a42c571 commit 3ffc5c7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
9 changes: 5 additions & 4 deletions lib/helpers/class_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ def sum(identity = 0, &block)
end
end
end

refine LibXML::XML::Node do
def equals?(node2); raise 'nic' end
def ==(node2)
self.simplification_of?(node2) and node2.simplification_of?(self)
end
Expand All @@ -29,7 +30,7 @@ def first_diff(node2)
self.attributes.each do |attr|
return "#{where}> Attribute #{attr} have diffent values: #{attr.value} != #{node2.attributes[attr.name]}" unless node2.attributes[attr.name] == attr.value
end

elems1 = self.elements
elems2 = node2.elements
# return "#{where}> elements have different number of subelements #{elems1.length} != #{elems2.length}" if (elems1.length != elems2.length)
Expand All @@ -48,12 +49,12 @@ def elements
each_element { |e| result << e }
return result
end

end

end # module ClassExtensions

else # Monkeypatching

class Array
def sum(identity = 0, &block)
if block_given?
Expand Down Expand Up @@ -100,6 +101,6 @@ def elements
each_element { |e| result << e }
return result
end

end

end
4 changes: 3 additions & 1 deletion lib/rspreadsheet.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require 'rspreadsheet/version'
require 'rspreadsheet/workbook'
require 'rspreadsheet/worksheet'
require 'helpers/class_extensions'
require 'helpers/configuration'
# refinements
require 'helpers/class_extensions'
using ClassExtensions

module Rspreadsheet
extend Configuration
Expand Down
33 changes: 18 additions & 15 deletions spec/class_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
require 'spec_helper'

if RUBY_VERSION > '2.1'
using ClassExtensions

describe Array do
it 'can sum simple array' do
a = [1,2,3,4]
a.sum.should == 10
end
it 'ignores text and nils while summing' do
a = [1,nil, nil,2,3,'foo',5.0]
a.sum.should == 11
[nil, 'nic'].sum.should == 0
[].sum.should == 0
end
end

# testing ClassExtensionsForSpec
describe LibXML::XML::Node do
before do
@n = LibXML::XML::Node.new('a')
Expand Down Expand Up @@ -43,4 +29,21 @@
end
end

# testing ClassExtensions
begin
using ClassExtensions

describe Array do
it 'can sum simple array' do
a = [1,2,3,4]
a.sum.should == 10
end
it 'ignores text and nils while summing' do
a = [1,nil, nil,2,3,'foo',5.0]
a.sum.should == 11
[nil, 'nic'].sum.should == 0
[].sum.should == 0
end
end
end
end
23 changes: 12 additions & 11 deletions spec/io_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
using ClassExtensions

describe Rspreadsheet do
it 'can open spreadsheet and save it to file, resulting file has same content as original' do
Expand All @@ -24,15 +25,15 @@
end

def contents_of_files_are_identical(filename1,filename2)
@content_xml1 = Zip::File.open(filename1) do |zip|
LibXML::XML::Document.io zip.get_input_stream('content.xml')
end
@content_xml2 = Zip::File.open(filename2) do |zip|
LibXML::XML::Document.io zip.get_input_stream('content.xml')
end
@content_xml2.root.first_diff(@content_xml1.root).should be_nil
@content_xml1.root.first_diff(@content_xml2.root).should be_nil
@content_xml1.root.equals?(@content_xml2.root).should == true
@content_xml1 = Zip::File.open(filename1) do |zip|
LibXML::XML::Document.io zip.get_input_stream('content.xml')
end
@content_xml2 = Zip::File.open(filename2) do |zip|
LibXML::XML::Document.io zip.get_input_stream('content.xml')
end

@content_xml2.root.first_diff(@content_xml1.root).should be_nil
@content_xml1.root.first_diff(@content_xml2.root).should be_nil

(@content_xml1.root == @content_xml2.root).should == true
end
6 changes: 3 additions & 3 deletions spec/rspreadsheet_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'
using ClassExtensions if RUBY_VERSION > '2.1'
using ClassExtensions

describe Rspreadsheet do
it 'can open ods testfile and reads its content correctly' do
Expand Down Expand Up @@ -91,7 +91,7 @@
sheet.cells(5,2).format.background_color = '#FF0000'
}.not_to raise_error

sheet.rows(4).cellvalues.sum{|val| val.to_f}.should eq 4+7*4
sheet.rows(4).cellvalues.sum.should eq 4+7*4
sheet.rows(4).cells.sum{ |cell| cell.value.to_f }.should eq 4+7*4

total = 0
Expand All @@ -106,7 +106,7 @@ def p(*par); end # supress p in the example
expect do
book = Rspreadsheet::Workbook.new
sheet = book.create_worksheet 'Top icecreams'

sheet[1,1] = 'My top 5'
p sheet[1,1].class # => String
p sheet[1,1] # => "My top 5"
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# This tells Bundler only load gems in side gemspec (not the locally installed ones). See http://stackoverflow.com/questions/4398262/setup-rspec-to-test-a-gem-not-rails
require 'bundler/setup'
Bundler.setup
Expand Down

0 comments on commit 3ffc5c7

Please sign in to comment.