dynport_tools 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ gem 'nokogiri'
9
+
10
+ group :development do
11
+ gem "rspec", "~> 2.3.0"
12
+ gem "bundler", "~> 1.0.0"
13
+ gem "jeweler", "~> 1.6.4"
14
+ gem "rcov", ">= 0"
15
+ gem 'ZenTest', '4.5.0'
16
+ gem "autotest"
17
+ gem "autotest-growl"
18
+ gem "ruby-debug"
19
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,47 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.5.0)
5
+ autotest (4.4.6)
6
+ ZenTest (>= 4.4.1)
7
+ autotest-growl (0.2.9)
8
+ columnize (0.3.4)
9
+ diff-lcs (1.1.2)
10
+ git (1.2.5)
11
+ jeweler (1.6.4)
12
+ bundler (~> 1.0)
13
+ git (>= 1.2.5)
14
+ rake
15
+ linecache (0.46)
16
+ rbx-require-relative (> 0.0.4)
17
+ nokogiri (1.5.0)
18
+ rake (0.9.2)
19
+ rbx-require-relative (0.0.5)
20
+ rcov (0.9.9)
21
+ rspec (2.3.0)
22
+ rspec-core (~> 2.3.0)
23
+ rspec-expectations (~> 2.3.0)
24
+ rspec-mocks (~> 2.3.0)
25
+ rspec-core (2.3.1)
26
+ rspec-expectations (2.3.0)
27
+ diff-lcs (~> 1.1.2)
28
+ rspec-mocks (2.3.0)
29
+ ruby-debug (0.10.4)
30
+ columnize (>= 0.1)
31
+ ruby-debug-base (~> 0.10.4.0)
32
+ ruby-debug-base (0.10.4)
33
+ linecache (>= 0.3)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ ZenTest (= 4.5.0)
40
+ autotest
41
+ autotest-growl
42
+ bundler (~> 1.0.0)
43
+ jeweler (~> 1.6.4)
44
+ nokogiri
45
+ rcov
46
+ rspec (~> 2.3.0)
47
+ ruby-debug
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tobias Schwab
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = dynport_tools
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to dynport_tools
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Tobias Schwab. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "dynport_tools"
18
+ gem.homepage = "http://github.com/tobstarr/dynport_tools"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Collection of various tools}
21
+ gem.description = %Q{Collection of various tools}
22
+ gem.email = "tobias.schwab@dynport.de"
23
+ gem.authors = ["Tobias Schwab"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "dynport_tools #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1 @@
1
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,80 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{dynport_tools}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tobias Schwab"]
12
+ s.date = %q{2011-08-08}
13
+ s.description = %q{Collection of various tools}
14
+ s.email = %q{tobias.schwab@dynport.de}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "autotest/discover.rb",
28
+ "lib/differ.rb",
29
+ "lib/dynport_tools.rb",
30
+ "lib/have_attributes.rb",
31
+ "lib/xml_file.rb",
32
+ "spec/differ_spec.rb",
33
+ "spec/dynport_tools_spec.rb",
34
+ "spec/fixtures/file_a.xml",
35
+ "spec/spec_helper.rb",
36
+ "spec/xml_file_spec.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/tobstarr/dynport_tools}
39
+ s.licenses = ["MIT"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.7.2}
42
+ s.summary = %q{Collection of various tools}
43
+
44
+ if s.respond_to? :specification_version then
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
49
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ s.add_development_dependency(%q<ZenTest>, ["= 4.5.0"])
54
+ s.add_development_dependency(%q<autotest>, [">= 0"])
55
+ s.add_development_dependency(%q<autotest-growl>, [">= 0"])
56
+ s.add_development_dependency(%q<ruby-debug>, [">= 0"])
57
+ else
58
+ s.add_dependency(%q<nokogiri>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
62
+ s.add_dependency(%q<rcov>, [">= 0"])
63
+ s.add_dependency(%q<ZenTest>, ["= 4.5.0"])
64
+ s.add_dependency(%q<autotest>, [">= 0"])
65
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
66
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
67
+ end
68
+ else
69
+ s.add_dependency(%q<nokogiri>, [">= 0"])
70
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
71
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
72
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
73
+ s.add_dependency(%q<rcov>, [">= 0"])
74
+ s.add_dependency(%q<ZenTest>, ["= 4.5.0"])
75
+ s.add_dependency(%q<autotest>, [">= 0"])
76
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
77
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
78
+ end
79
+ end
80
+
data/lib/differ.rb ADDED
@@ -0,0 +1,62 @@
1
+ class Differ
2
+ attr_accessor :diff_all, :use_return
3
+
4
+
5
+ def initialize(options = {})
6
+ self.diff_all = options[:diff_all] != false
7
+ self.use_return = options[:use_return] == true
8
+ end
9
+
10
+ def diff(a, b)
11
+ if both?(a, b, Hash)
12
+ diff_hash_values(a, b, a.keys + (self.diff_all ? b.keys : []))
13
+ elsif both?(a, b, Array)
14
+ diff_hash_values(a, b, all_array_indexes(a, b))
15
+ else
16
+ [a, b] if a != b
17
+ end
18
+ end
19
+
20
+ def diff_to_message_lines(the_diff, prefix = nil)
21
+ if the_diff.is_a?(Array)
22
+ ["expected #{expected_value(the_diff.first)} to be #{expected_value(the_diff.at(1))}"]
23
+ elsif the_diff.is_a?(Hash)
24
+ the_diff.map do |key, diff|
25
+ if diff.is_a?(Array)
26
+ "expected #{merge_prefixes(prefix, key)} to #{use_return ? "return" : "be"} #{expected_value(diff.first)} but #{use_return ? "did" : "was"} #{expected_value(diff.at(1))}"
27
+ else
28
+ diff_to_message_lines(diff, merge_prefixes(prefix, key))
29
+ end
30
+ end.flatten
31
+ else
32
+ []
33
+ end
34
+ end
35
+
36
+ private
37
+ def expected_value(value)
38
+ "<#{value.inspect}>"
39
+ end
40
+
41
+ def all_array_indexes(a, b)
42
+ 0.upto([a.length, b.length].max - 1).to_a
43
+ end
44
+
45
+ def diff_hash_values(a, b, keys)
46
+ ret = keys.uniq.inject({}) do |hash, key|
47
+ if diff = diff(a[key], b[key])
48
+ hash[key] = diff
49
+ end
50
+ hash
51
+ end
52
+ ret.empty? ? nil : ret
53
+ end
54
+
55
+ def both?(a, b, clazz)
56
+ a.is_a?(clazz) && b.is_a?(clazz)
57
+ end
58
+
59
+ def merge_prefixes(prefix, key)
60
+ prefix ? "#{prefix}[#{key}]" : key
61
+ end
62
+ end
File without changes
@@ -0,0 +1,72 @@
1
+ require File.expand_path("../../../lib/differ", __FILE__)
2
+ module HaveAttributesMatcher
3
+ class HaveAttributes
4
+ def initialize(expected)
5
+ @expected = expected
6
+ end
7
+
8
+ def differ
9
+ @differ ||= Differ.new(:diff_all => false)
10
+ end
11
+
12
+ def matches?(target)
13
+ @target = target
14
+ if diff = differ.diff(@expected, target)
15
+ @error = differ.diff_to_message_lines(diff).join("\n")
16
+ false
17
+ else
18
+ true
19
+ end
20
+ end
21
+
22
+ def failure_message
23
+ @error
24
+ end
25
+ end
26
+
27
+ class ReturnValues < HaveAttributes
28
+ def matches?(target)
29
+ differ.use_return = true
30
+ super(@expected.keys.inject({}) { |hash, key| hash.merge!(key => target.send(key)) })
31
+ end
32
+ end
33
+
34
+ class HaveAllAttributes < HaveAttributes
35
+ def matches?(record)
36
+ differ.diff_all = true
37
+ super(record)
38
+ end
39
+ end
40
+
41
+ class HaveOneWithAttributes < HaveAttributes
42
+ def matches?(target)
43
+ target.any? do |record|
44
+ super(record)
45
+ end
46
+ end
47
+
48
+ def failure_message
49
+ "expected to have one record with attributes #{@expected.inspect}"
50
+ end
51
+ end
52
+
53
+ def return_values(expected)
54
+ ReturnValues.new(expected)
55
+ end
56
+
57
+ def have_attributes(expected)
58
+ HaveAttributes.new(expected)
59
+ end
60
+
61
+ def exactly_have_attributes(expected)
62
+ have_all_attributes(expected)
63
+ end
64
+
65
+ def have_all_attributes(expected)
66
+ HaveAllAttributes.new(expected)
67
+ end
68
+
69
+ def have_one_with_attributes(expected)
70
+ HaveOneWithAttributes.new(expected)
71
+ end
72
+ end
data/lib/xml_file.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "nokogiri"
2
+
3
+ class XmlFile
4
+ attr_accessor :path
5
+
6
+ def initialize(path)
7
+ self.path = path.to_s
8
+ end
9
+
10
+ def nodes_hash
11
+ { key_for_node(doc.root) => parse_node(doc.root) }
12
+ end
13
+
14
+ def doc
15
+ @doc ||= Nokogiri::XML(File.open(path))
16
+ end
17
+
18
+ def parse_node(node)
19
+ child_elements = node.children.select { |n| n.is_a?(Nokogiri::XML::Element) }
20
+ value = if child_elements.any?
21
+ flatten_hash(
22
+ child_elements.inject({}) do |hash, el|
23
+ hash[key_for_node(el)] ||= Array.new
24
+ hash[key_for_node(el)] << parse_node(el)
25
+ hash
26
+ end
27
+ )
28
+ else
29
+ txt = node.inner_text.strip
30
+ txt.length == 0 ? nil : txt
31
+ end
32
+ end
33
+
34
+ def key_for_node(node)
35
+ if node.attributes.any?
36
+ node.attributes.inject({ :name => node.name }) do |hash, (key, value)|
37
+ hash.merge!(key => value.value)
38
+ end
39
+ else
40
+ node.name
41
+ end
42
+ end
43
+
44
+ def flatten_hash(in_hash)
45
+ in_hash.inject({}) do |hash, (key, arr_of_value)|
46
+ if arr_of_value.is_a?(Array)
47
+ if arr_of_value.length == 0
48
+ hash[key] = nil
49
+ elsif arr_of_value.length == 1
50
+ hash[key] = arr_of_value.first
51
+ else
52
+ hash[key] = arr_of_value
53
+ end
54
+ else
55
+ hash[key] = arr_of_value
56
+ end
57
+ hash
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ require "differ"
4
+
5
+ describe Differ do
6
+ let(:differ) { Differ.new }
7
+
8
+ describe "#initialize" do
9
+ it "sets diff_all to true by default" do
10
+ Differ.new.diff_all.should == true
11
+ end
12
+
13
+ it "sets diff_all to false when initialized with that option" do
14
+ Differ.new(:diff_all => false).diff_all.should == false
15
+ end
16
+ end
17
+
18
+ describe "diffing two hashes" do
19
+ it "returns true when both hashes are equal" do
20
+ hash = { :a => 1 }
21
+ differ.diff(hash, hash).should be_nil
22
+ end
23
+
24
+ it "returns the diff when there is one" do
25
+ a = { :a => 1, :b => 2 }
26
+ b = { :a => 1, :b => 3 }
27
+
28
+ differ.diff(a, b).should == {
29
+ :b => [2, 3]
30
+ }
31
+ end
32
+
33
+ it "returns nested diffs" do
34
+ a = { :a => 1, :b => { :c => 1 } }
35
+ b = { :a => 1, :b => { :c => 2 } }
36
+
37
+ differ.diff(a, b).should == {
38
+ :b => { :c => [1, 2] }
39
+ }
40
+ end
41
+ end
42
+
43
+ describe "#diff" do
44
+ it "returns an array with both values when not equal" do
45
+ differ.diff("a", "b").should == ["a", "b"]
46
+ end
47
+
48
+ it "returns nil when equal" do
49
+ differ.diff("a", "a").should be_nil
50
+ end
51
+
52
+ describe "with first one being a hash" do
53
+ let(:a) { { :a => 1 } }
54
+ let(:b) { { :a => 2 } }
55
+
56
+ it "returns a hash when first value is a hash" do
57
+ differ.diff(a, b).should be_an_instance_of(Hash)
58
+ end
59
+
60
+ it "returns an array when first is a hash and second is string" do
61
+ differ.diff(a, "a").should == [a, "a"]
62
+ end
63
+
64
+ it "returns an array when first is a string and second is hash" do
65
+ differ.diff("a", a).should == ["a", a]
66
+ end
67
+
68
+ it "returns the correct diff" do
69
+ differ.diff(a, b).should == { :a => [1, 2] }
70
+ end
71
+
72
+ it "sets nil as value when equal" do
73
+ differ.diff(a, a).should be_nil
74
+ end
75
+
76
+ it "sets the correct diff when b has more keys then a" do
77
+ differ.diff({ :a => 1 }, { :b => 2 }).should == { :a => [1, nil], :b => [nil, 2] }
78
+ end
79
+
80
+ it "only uses b's keys when diff_all is true" do
81
+ differ.diff_all = false
82
+ differ.diff({ :a => 1 }, { :a => 2, :b => 3 }).should == { :a => [1, 2] }
83
+ end
84
+ end
85
+
86
+ describe "with two arrays given" do
87
+ let(:a) { [1, 2] }
88
+ let(:b) { [1, 3] }
89
+
90
+ it "returns an empty hash when equal" do
91
+ differ.diff(a, a).should be_nil
92
+ end
93
+
94
+ it "sets the correct diff when there is one" do
95
+ differ.diff(a, b).should == { 1 => [2, 3] }
96
+ end
97
+
98
+ it "sets the correct diff when second array is bigger" do
99
+ differ.diff(a, [1, 2, 3]).should == { 2 => [nil, 3] }
100
+ end
101
+ end
102
+ end
103
+
104
+ describe "diff_to_message_lines" do
105
+ it "returns an empty array when diff is nil" do
106
+ differ.diff_to_message_lines(nil).should == []
107
+ end
108
+
109
+ it "returns a message for array" do
110
+ differ.diff_to_message_lines([1, 2]).should == ["expected <1> to be <2>"]
111
+ end
112
+
113
+ it "returns the correct message for a simple hash" do
114
+ differ.diff_to_message_lines({ :a => [1, 2]}).should == ["expected a to be <1> but was <2>"]
115
+ end
116
+
117
+ it "uses return instead of be" do
118
+ differ.use_return = true
119
+ differ.diff_to_message_lines({ :a => [1, 2]}).should == ["expected a to return <1> but did <2>"]
120
+ end
121
+
122
+ it "adds a prefix when diff is array" do
123
+ differ.diff_to_message_lines({ :a => [1, 2]}, "b").should == ["expected b[a] to be <1> but was <2>"]
124
+ end
125
+
126
+ it "returns the correctly nested diff" do
127
+ differ.diff_to_message_lines({ :a => { :b => [3, 4] } }).should == ["expected a[b] to be <3> but was <4>"]
128
+ end
129
+
130
+ it "returns the correctly neep nested diff" do
131
+ differ.diff_to_message_lines({ :a => { :b => [3, 4] } }, "c").should == ["expected c[a][b] to be <3> but was <4>"]
132
+ end
133
+
134
+ it "returns multiple messages" do
135
+ differ.diff_to_message_lines({ :a => { :b => [3, 4], :c => [nil, 1] } }, "c").should == [
136
+ "expected c[a][b] to be <3> but was <4>",
137
+ "expected c[a][c] to be <nil> but was <1>",
138
+ ]
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "DynportTools" do
4
+ end
@@ -0,0 +1,18 @@
1
+ <file name="some_name">
2
+ <size>101</size>
3
+ <title>Some Title</title>
4
+ <comments>
5
+
6
+ </comments>
7
+ <attributes>
8
+ <rights>rw</rights>
9
+ <type>file</type>
10
+ </attributes>
11
+ <lines>
12
+ <line id="0">
13
+ <content>First Line</content>
14
+ </line>
15
+ <line id="1">
16
+ </line>
17
+ </lines>
18
+ </file>
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'dynport_tools'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
13
+
14
+ ROOT = Pathname.new(File.expand_path("../../", __FILE__))
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require "xml_file"
3
+
4
+ FILE1 = ROOT.join("spec/fixtures/file_a.xml")
5
+ FILE2 = ROOT.join("spec/fixtures/file_b.xml")
6
+
7
+
8
+ describe XmlFile do
9
+ let(:file) { XmlFile.new(FILE1) }
10
+
11
+ describe "#initialize" do
12
+ it "sets the correct path" do
13
+ XmlFile.new(FILE1).path.should == FILE1.to_s
14
+ end
15
+ end
16
+
17
+ describe "#nodes_hash" do
18
+ it "a hash" do
19
+ file.nodes_hash.should be_an_instance_of(Hash)
20
+ end
21
+
22
+ it "parses the root node" do
23
+ root = double("root", :attributes => {}, :name => "product")
24
+ file.stub!(:doc).and_return(double("root", :root => root))
25
+ res = double("response")
26
+ file.should_receive(:parse_node).with(root).and_return res
27
+ file.nodes_hash.should == { "product" => res }
28
+ end
29
+
30
+ let(:key) { { :name => "file", "name" => "some_name" } }
31
+
32
+ it "sets the root node" do
33
+ file.nodes_hash[key]["size"].should == "101"
34
+ file.nodes_hash[key]["title"].should == "Some Title"
35
+ end
36
+ end
37
+
38
+ describe "#parse_node" do
39
+ it "returns a the inner text as value when only text inside" do
40
+ file.parse_node(file.doc.root.at("size")).should == "101"
41
+ end
42
+
43
+ it "sets blank texts to nil" do
44
+ file.parse_node(file.doc.root.at("comments")).should be_nil
45
+ end
46
+
47
+ it "extracts subnodes" do
48
+ file.parse_node(file.doc.root.at("attributes")).should == {"rights" => "rw", "type" => "file" }
49
+ end
50
+
51
+ it "extracts array of tracks" do
52
+ file.parse_node(Nokogiri::XML("<a><title>a</title><title>b</title></a>").at("a"))["title"].should be_an_instance_of(Array)
53
+ end
54
+
55
+ it "uses key_for_node for keys" do
56
+ file.parse_node(file.doc.root.at("lines"))[:name => "line", "id" => "0"].should be_an_instance_of(Hash)
57
+ end
58
+ end
59
+
60
+ describe "key_for_node" do
61
+ it "returns a string when node has no attributes" do
62
+ file.key_for_node(file.doc.root.at("size")).should == "size"
63
+ end
64
+
65
+ it "includes the attributes when node has attributes" do
66
+ file.key_for_node(file.doc.root).should == { :name => "file", "name" => "some_name" }
67
+ end
68
+ end
69
+
70
+ describe "#flatten_hash" do
71
+ it "returns a flat hash when all arrays have length 1" do
72
+ file.flatten_hash({"a" => [1]}).should == { "a" => 1 }
73
+ end
74
+
75
+ it "does not break on empty hashes" do
76
+ file.flatten_hash({"a" => 1}).should == { "a" => 1 }
77
+ end
78
+
79
+ it "does not flatten arrays with more than one element" do
80
+ file.flatten_hash({"a" => [1, 2]}).should == { "a" => [1, 2] }
81
+ end
82
+
83
+ it "sets empty arrays to nil" do
84
+ file.flatten_hash({"a" => []}).should == { "a" => nil }
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,217 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynport_tools
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tobias Schwab
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-08-08 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
22
+ none: false
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ version_requirements: *id001
31
+ name: nokogiri
32
+ prerelease: false
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 2
43
+ - 3
44
+ - 0
45
+ version: 2.3.0
46
+ version_requirements: *id002
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
50
+ - !ruby/object:Gem::Dependency
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ hash: 23
57
+ segments:
58
+ - 1
59
+ - 0
60
+ - 0
61
+ version: 1.0.0
62
+ version_requirements: *id003
63
+ name: bundler
64
+ prerelease: false
65
+ type: :development
66
+ - !ruby/object:Gem::Dependency
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ~>
71
+ - !ruby/object:Gem::Version
72
+ hash: 7
73
+ segments:
74
+ - 1
75
+ - 6
76
+ - 4
77
+ version: 1.6.4
78
+ version_requirements: *id004
79
+ name: jeweler
80
+ prerelease: false
81
+ type: :development
82
+ - !ruby/object:Gem::Dependency
83
+ requirement: &id005 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ version_requirements: *id005
93
+ name: rcov
94
+ prerelease: false
95
+ type: :development
96
+ - !ruby/object:Gem::Dependency
97
+ requirement: &id006 !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - "="
101
+ - !ruby/object:Gem::Version
102
+ hash: 43
103
+ segments:
104
+ - 4
105
+ - 5
106
+ - 0
107
+ version: 4.5.0
108
+ version_requirements: *id006
109
+ name: ZenTest
110
+ prerelease: false
111
+ type: :development
112
+ - !ruby/object:Gem::Dependency
113
+ requirement: &id007 !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ version_requirements: *id007
123
+ name: autotest
124
+ prerelease: false
125
+ type: :development
126
+ - !ruby/object:Gem::Dependency
127
+ requirement: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ version_requirements: *id008
137
+ name: autotest-growl
138
+ prerelease: false
139
+ type: :development
140
+ - !ruby/object:Gem::Dependency
141
+ requirement: &id009 !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ version_requirements: *id009
151
+ name: ruby-debug
152
+ prerelease: false
153
+ type: :development
154
+ description: Collection of various tools
155
+ email: tobias.schwab@dynport.de
156
+ executables: []
157
+
158
+ extensions: []
159
+
160
+ extra_rdoc_files:
161
+ - LICENSE.txt
162
+ - README.rdoc
163
+ files:
164
+ - .document
165
+ - .rspec
166
+ - Gemfile
167
+ - Gemfile.lock
168
+ - LICENSE.txt
169
+ - README.rdoc
170
+ - Rakefile
171
+ - VERSION
172
+ - autotest/discover.rb
173
+ - dynport_tools.gemspec
174
+ - lib/differ.rb
175
+ - lib/dynport_tools.rb
176
+ - lib/have_attributes.rb
177
+ - lib/xml_file.rb
178
+ - spec/differ_spec.rb
179
+ - spec/dynport_tools_spec.rb
180
+ - spec/fixtures/file_a.xml
181
+ - spec/spec_helper.rb
182
+ - spec/xml_file_spec.rb
183
+ homepage: http://github.com/tobstarr/dynport_tools
184
+ licenses:
185
+ - MIT
186
+ post_install_message:
187
+ rdoc_options: []
188
+
189
+ require_paths:
190
+ - lib
191
+ required_ruby_version: !ruby/object:Gem::Requirement
192
+ none: false
193
+ requirements:
194
+ - - ">="
195
+ - !ruby/object:Gem::Version
196
+ hash: 3
197
+ segments:
198
+ - 0
199
+ version: "0"
200
+ required_rubygems_version: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ hash: 3
206
+ segments:
207
+ - 0
208
+ version: "0"
209
+ requirements: []
210
+
211
+ rubyforge_project:
212
+ rubygems_version: 1.7.2
213
+ signing_key:
214
+ specification_version: 3
215
+ summary: Collection of various tools
216
+ test_files: []
217
+