Skip to content

Commit

Permalink
Improve specs and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
krasnoukhov committed Aug 1, 2014
1 parent b473a4e commit 4e21ad8
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 75 deletions.
5 changes: 1 addition & 4 deletions lib/sax-machine/config/sax_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def value_from_attrs(attrs)
def attrs_match?(attrs)
attrs.key?(@name) || attrs.value?(@name)
end

def has_value_and_attrs_match?(attrs)
attrs_match?(attrs)
end
alias_method :has_value_and_attrs_match?, :attrs_match?

def collection?
false
Expand Down
6 changes: 3 additions & 3 deletions lib/sax-machine/sax_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ def parse(*args)
def element(name, options = {})
real_name = (options[:as] ||= name).to_s
sax_config.add_top_level_element(name, options)
create_attr real_name
create_attr(real_name)
end

def attribute(name, options = {})
real_name = (options[:as] ||= name).to_s
sax_config.add_top_level_attribute(self.class.to_s, options.merge(name: name))
create_attr real_name
create_attr(real_name)
end

def value(name, options = {})
real_name = (options[:as] ||= name).to_s
sax_config.add_top_level_element_value(self.class.to_s, options.merge(name: name))
create_attr real_name
create_attr(real_name)
end

def ancestor(name, options = {})
Expand Down
26 changes: 12 additions & 14 deletions spec/sax-machine/sax_activerecord_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require 'active_record'

describe "ActiveRecord" do
describe "integration" do
before :each do
class MySaxModel < ActiveRecord::Base
SAXMachine.configure(MySaxModel) do |c|
c.element :title
end
describe "SAXMachine ActiveRecord integration" do
before do
class MySaxModel < ActiveRecord::Base
SAXMachine.configure(MySaxModel) do |c|
c.element :title
end
end
end

after :each do
Object.send(:remove_const, :MySaxModel)
end
after do
Object.send(:remove_const, :MySaxModel)
end

it "parses document" do
document = MySaxModel.parse("<xml><title>My Title</title></xml>")
expect(document.title).to eq("My Title")
end
it "parses document" do
document = MySaxModel.parse("<xml><title>My Title</title></xml>")
expect(document.title).to eq("My Title")
end
end
49 changes: 26 additions & 23 deletions spec/sax-machine/sax_configure_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

class A
SAXMachine.configure(A) do |c|
c.element :title
end
end
describe "SAXMachine configure" do
before do
class A
SAXMachine.configure(A) do |c|
c.element :title
end
end

class B < A
SAXMachine.configure(B) do |c|
c.element :b
end
end
class B < A
SAXMachine.configure(B) do |c|
c.element :b
end
end

class C < B
SAXMachine.configure(C) do |c|
c.element :c
end
end
class C < B
SAXMachine.configure(C) do |c|
c.element :c
end
end

describe "SAXMachine configure" do
before do
xml = "<top><title>Test</title><b>Matched!</b><c>And Again</c></top>"
@a = A.new
@a.parse xml
@b = B.new
@b.parse xml
@c = C.new
@c.parse xml
@a = A.parse xml
@b = B.parse xml
@c = C.parse xml
end

after do
Object.send(:remove_const, :A)
Object.send(:remove_const, :B)
Object.send(:remove_const, :C)
end

it { expect(@a).to be_a(A) }
Expand Down
78 changes: 59 additions & 19 deletions spec/sax-machine/sax_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
describe "SAXMachine" do
describe "element" do
describe "when parsing a single element" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :title
ancestor :body
value :something, required: false
attribute :anything, required: true
end
end

Expand All @@ -21,10 +24,6 @@
expect(document.title).to eq("Title")
end

it "allows introspection of the elements" do
expect(@klass.column_names).to match_array([:title])
end

it "does not overwrites the getter is there is already one present" do
@klass = Class.new do
def title
Expand Down Expand Up @@ -97,6 +96,38 @@ def title=(val)
expect(document.title).to eq("My Title")
end

describe "the introspection" do
it "allows to get column names" do
expect(@klass.column_names).to match_array([:title])
end

it "allows to get elements" do
expect(@klass.sax_config.top_level_elements.values.flatten.map(&:to_s)).to \
match_array(["name: title dataclass: setter: title= required: value: as:title collection: with: {}"])
end

it "allows to get ancestors" do
expect(@klass.sax_config.ancestors.map(&:column)).to \
match_array([:body])
end

it "allows to get values" do
expect(@klass.sax_config.top_level_element_value.map(&:column)).to \
match_array([:something])
expect(@klass.sax_config.top_level_element_value.map(&:required?)).to \
match_array([false])
end

it "allows to get attributes" do
expect(@klass.sax_config.top_level_attributes.map(&:column)).to \
match_array([:anything])
expect(@klass.sax_config.top_level_attributes.map(&:required?)).to \
match_array([true])
expect(@klass.sax_config.top_level_attributes.map(&:collection?)).to \
match_array([false])
end
end

describe "the class attribute" do
before(:each) do
@klass = Class.new do
Expand Down Expand Up @@ -180,7 +211,7 @@ def title=(val)
end

describe "when parsing multiple elements" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :title
Expand Down Expand Up @@ -227,7 +258,7 @@ def items=(val)

describe "when using options for parsing elements" do
describe "using the 'as' option" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :description, as: :summary
Expand All @@ -248,7 +279,7 @@ def items=(val)

describe "using the :with option" do
describe "and the :value option" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, value: :href, with: { foo: "bar" }
Expand All @@ -266,7 +297,7 @@ def items=(val)
end

describe "and the :as option" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, value: :href, as: :url, with: { foo: "bar" }
Expand All @@ -283,7 +314,7 @@ def items=(val)
end

describe "with only one element" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, with: { foo: "bar" }
Expand Down Expand Up @@ -312,7 +343,7 @@ def items=(val)
end

describe "with multiple elements of same tag" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, as: :first, with: { foo: "bar" }
Expand All @@ -332,7 +363,7 @@ def items=(val)
end

describe "with only one element as a regular expression" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, with: { foo: /ar$/ }
Expand Down Expand Up @@ -362,7 +393,7 @@ def items=(val)
end

describe "using the 'value' option" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link, value: :foo
Expand Down Expand Up @@ -410,7 +441,7 @@ def items=(val)
end

describe "when desiring both the content and attributes of an element" do
before :each do
before do
@klass = Class.new do
include SAXMachine
element :link
Expand All @@ -431,7 +462,7 @@ def items=(val)

describe "elements" do
describe "when parsing multiple elements" do
before :each do
before do
@klass = Class.new do
include SAXMachine
elements :entry, as: :entries
Expand Down Expand Up @@ -466,7 +497,7 @@ def items=(val)
end

describe "when using the with and class options" do
before :each do
before do
class Bar
include SAXMachine
element :title
Expand Down Expand Up @@ -499,7 +530,7 @@ class Item
end

describe "when using the class option" do
before :each do
before do
class Foo
include SAXMachine
element :title
Expand Down Expand Up @@ -574,7 +605,7 @@ class Dashes
end

describe "full example" do
before :each do
before do
@xml = File.read("spec/fixtures/atom.xml")

class AtomEntry
Expand Down Expand Up @@ -896,11 +927,20 @@ class ItemElement5
end

@errors = []
@item = ItemElement5.parse(@xml, ->(x) { @errors << x })
@warnings = []
@item = ItemElement5.parse(
@xml,
->(x) { @errors << x },
->(x) { @warnings << x },
)
end

it "has error" do
expect(@errors.uniq.size).to eq(1)
end

it "has no warning" do
expect(@warnings.uniq.size).to eq(0)
end
end
end
30 changes: 18 additions & 12 deletions spec/sax-machine/sax_include_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

class A
include SAXMachine
element :title
end
describe "SAXMachine inheritance" do
before do
class A
include SAXMachine
element :title
end

class B < A
element :b
end
class B < A
element :b
end

class C < B
element :c
end
class C < B
element :c
end

describe "SAXMachine inheritance" do
before do
xml = "<top><title>Test</title><b>Matched!</b><c>And Again</c></top>"
@a = A.new
@a.parse xml
Expand All @@ -24,6 +24,12 @@ class C < B
@c.parse xml
end

after do
Object.send(:remove_const, :A)
Object.send(:remove_const, :B)
Object.send(:remove_const, :C)
end

it { expect(@a).to be_a(A) }
it { expect(@a).not_to be_a(B) }
it { expect(@a).to be_a(SAXMachine) }
Expand Down

0 comments on commit 4e21ad8

Please sign in to comment.