mbrao 1.1.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. data/.travis-gemfile +5 -2
  2. data/.travis.yml +2 -2
  3. data/Gemfile +6 -4
  4. data/README.md +1 -0
  5. data/Rakefile +18 -0
  6. data/doc/ActionView/Template/Handlers/MbraoTemplate.html +5 -5
  7. data/doc/ActionView/Template/Handlers.html +5 -5
  8. data/doc/HTML/Pipeline/KramdownFilter.html +5 -5
  9. data/doc/HTML/Pipeline.html +5 -5
  10. data/doc/HTML.html +5 -5
  11. data/doc/Mbrao/Author.html +12 -12
  12. data/doc/Mbrao/Content.html +104 -140
  13. data/doc/Mbrao/ContentPublicInterface.html +6 -6
  14. data/doc/Mbrao/Exceptions/InvalidDate.html +7 -7
  15. data/doc/Mbrao/Exceptions/InvalidMetadata.html +7 -7
  16. data/doc/Mbrao/Exceptions/Parsing.html +7 -7
  17. data/doc/Mbrao/Exceptions/Rendering.html +7 -7
  18. data/doc/Mbrao/Exceptions/UnavailableLocalization.html +7 -7
  19. data/doc/Mbrao/Exceptions/Unimplemented.html +7 -7
  20. data/doc/Mbrao/Exceptions/UnknownEngine.html +7 -7
  21. data/doc/Mbrao/Exceptions.html +5 -5
  22. data/doc/Mbrao/Parser.html +15 -15
  23. data/doc/Mbrao/ParsingEngines/Base.html +5 -5
  24. data/doc/Mbrao/ParsingEngines/PlainText.html +5 -5
  25. data/doc/Mbrao/ParsingEngines.html +5 -5
  26. data/doc/Mbrao/PublicInterface/ClassMethods.html +20 -32
  27. data/doc/Mbrao/PublicInterface.html +5 -5
  28. data/doc/Mbrao/RenderingEngines/Base.html +5 -5
  29. data/doc/Mbrao/RenderingEngines/HtmlPipeline.html +16 -16
  30. data/doc/Mbrao/RenderingEngines.html +5 -5
  31. data/doc/Mbrao/Validations/ClassMethods.html +13 -351
  32. data/doc/Mbrao/Validations.html +5 -5
  33. data/doc/Mbrao/Version.html +7 -7
  34. data/doc/Mbrao.html +5 -5
  35. data/doc/_index.html +6 -6
  36. data/doc/class_list.html +3 -2
  37. data/doc/file.README.html +7 -6
  38. data/doc/file_list.html +2 -1
  39. data/doc/frames.html +1 -1
  40. data/doc/index.html +7 -6
  41. data/doc/js/full_list.js +7 -2
  42. data/doc/method_list.html +67 -192
  43. data/doc/top-level-namespace.html +5 -5
  44. data/lib/mbrao/author.rb +1 -1
  45. data/lib/mbrao/content.rb +10 -12
  46. data/lib/mbrao/exceptions.rb +7 -7
  47. data/lib/mbrao/parser.rb +4 -65
  48. data/lib/mbrao/rendering_engines/html_pipeline.rb +5 -5
  49. data/lib/mbrao/version.rb +2 -2
  50. data/lib/mbrao.rb +1 -1
  51. data/mbrao.gemspec +4 -4
  52. data/spec/coverage_helper.rb +4 -1
  53. data/spec/mbrao/author_spec.rb +5 -5
  54. data/spec/mbrao/content_spec.rb +3 -3
  55. data/spec/mbrao/integrations/rails_spec.rb +8 -8
  56. data/spec/mbrao/parser_spec.rb +19 -96
  57. data/spec/mbrao/parsing_engines/base_spec.rb +7 -7
  58. data/spec/mbrao/rendering_engines/html_pipeline_spec.rb +13 -14
  59. metadata +11 -11
@@ -8,31 +8,31 @@ module Mbrao
8
8
  # The exceptions raised by mbrao.
9
9
  module Exceptions
10
10
  # Exception raised when metadata are not valid.
11
- class InvalidMetadata < ::Exception
11
+ class InvalidMetadata < RuntimeError
12
12
  end
13
13
 
14
14
  # Exception raised when a date is valid.
15
- class InvalidDate < ::Exception
15
+ class InvalidDate < RuntimeError
16
16
  end
17
17
 
18
18
  # Exception raised when there is a parsing error.
19
- class Parsing < ::Exception
19
+ class Parsing < RuntimeError
20
20
  end
21
21
 
22
22
  # Exception raised when there is a rendering error.
23
- class Rendering < ::Exception
23
+ class Rendering < RuntimeError
24
24
  end
25
25
 
26
26
  # Exception raised when a requested object is not available in any of the desired locales.
27
- class UnavailableLocalization < ::Exception
27
+ class UnavailableLocalization < RuntimeError
28
28
  end
29
29
 
30
30
  # Exception raised when a invalid parsing or rendering engine is requested.
31
- class UnknownEngine < ::Exception
31
+ class UnknownEngine < RuntimeError
32
32
  end
33
33
 
34
34
  # Exception raised when a requested method must be overridden by a subclass.
35
- class Unimplemented < ::Exception
35
+ class Unimplemented < RuntimeError
36
36
  end
37
37
  end
38
38
  end
data/lib/mbrao/parser.rb CHANGED
@@ -50,7 +50,7 @@ module Mbrao
50
50
  # @param options [Hash] A list of options for parsing.
51
51
  # @return [Content] The parsed data.
52
52
  def parse(content, options = {})
53
- self.instance.parse(content, options)
53
+ instance.parse(content, options)
54
54
  end
55
55
 
56
56
  # Renders a content.
@@ -60,7 +60,7 @@ module Mbrao
60
60
  # @param context [Hash] A context for rendering.
61
61
  # @return [String] The rendered content.
62
62
  def render(content, options = {}, context = {})
63
- self.instance.render(content, options, context)
63
+ instance.render(content, options, context)
64
64
  end
65
65
 
66
66
  # Instantiates a new engine for rendering or parsing.
@@ -77,8 +77,6 @@ module Mbrao
77
77
  end
78
78
  end
79
79
 
80
-
81
-
82
80
  # Returns a unique (singleton) instance of the parser.
83
81
  #
84
82
  # @param force [Boolean] If to force recreation of the instance.
@@ -135,65 +133,6 @@ module Mbrao
135
133
 
136
134
  text.ensure_string.strip =~ regex
137
135
  end
138
-
139
- # Converts an object making sure that every `Hash` is converted to a `HashWithIndifferentAccess`.
140
- #
141
- # @param object [Object] The object to convert. If the object is not an Hash or doesn't respond to `collect` then the original object is returned.
142
- # @param sanitize_method [Symbol|nil] If not `nil`, the method to use to sanitize entries. Ignored if a block is present.
143
- # @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
144
- # @return [Object] The converted object.
145
- def sanitized_hash(object, sanitize_method = nil, &block)
146
- if object.is_a?(Hash) || object.is_a?(HashWithIndifferentAccess) then
147
- object.inject(HashWithIndifferentAccess.new) do |hash, pair|
148
- hash[pair[0]] = Mbrao::Parser.sanitized_hash(pair[1], sanitize_method, &block)
149
- hash
150
- end
151
- elsif object.respond_to?(:collect) then
152
- object.collect {|item| Mbrao::Parser.sanitized_hash(item, sanitize_method, &block) }
153
- else
154
- sanitized_hash_entry(object, sanitize_method, &block)
155
- end
156
- end
157
-
158
- # Converts an object to a a flatten array with all values sanitized.
159
- #
160
- # @param object [Object] The object to convert.
161
- # @param uniq [Boolean] If to remove duplicates from the array before sanitizing.
162
- # @param compact [Boolean] If to compact the array before sanitizing.
163
- # @param sanitize_method [Symbol|nil] If not `nil`, the method to use to sanitize entries. Ignored if a block is present.
164
- # @param block [Proc] A block to sanitize entries. It must accept the value as unique argument.
165
- # @return [Array] An flattened array whose all values are strings.
166
- def sanitized_array(object, uniq = true, compact = false, sanitize_method = :ensure_string, &block)
167
- rv = object.ensure_array.flatten
168
- rv.uniq! if uniq
169
- rv.compact! if compact
170
-
171
- if block then
172
- rv = rv.collect(&block)
173
- elsif sanitize_method then
174
- rv = rv.collect(&sanitize_method)
175
- end
176
-
177
- rv.uniq! if uniq
178
- rv.compact! if compact
179
- rv
180
- end
181
-
182
- private
183
- # Sanitizes an value for an hash.
184
- #
185
- # @param value [Object] The value to sanitize.
186
- # @param sanitize_method [Symbol|nil] If not `nil`, the method to use to sanitize the value. Ignored if a block is present.
187
- # @param block [Proc] A block to sanitize the value. It must accept the value as unique argument.
188
- def sanitized_hash_entry(value, sanitize_method = :ensure_string, &block)
189
- if block
190
- block.call(value)
191
- elsif sanitize_method
192
- value.send(sanitize_method)
193
- else
194
- value
195
- end
196
- end
197
136
  end
198
137
  end
199
138
 
@@ -230,7 +169,7 @@ module Mbrao
230
169
  # @param options [Hash] The options to sanitize.
231
170
  # @return [HashWithIndifferentAccess] The sanitized options.
232
171
  def sanitize_parsing_options(options)
233
- options = (options.is_a?(Hash) ? options : {}).symbolize_keys
172
+ options = options.ensure_hash(:symbols)
234
173
 
235
174
  options[:engine] ||= Mbrao::Parser.parsing_engine
236
175
  options[:metadata] = options.fetch(:metadata, true).to_boolean
@@ -244,7 +183,7 @@ module Mbrao
244
183
  # @param options [Hash] The options to sanitize.
245
184
  # @return [HashWithIndifferentAccess] The sanitized options.
246
185
  def sanitize_rendering_options(options)
247
- options = (options.is_a?(Hash) ? options : {}).symbolize_keys
186
+ options = options.ensure_hash(:symbols)
248
187
 
249
188
  options[:engine] ||= Mbrao::Parser.rendering_engine
250
189
 
@@ -50,7 +50,7 @@ module Mbrao
50
50
  # @param context [Hash] A context for rendering.
51
51
  def render(content, options = {}, context = {})
52
52
  options = sanitize_options(options)
53
- context = context.is_a?(Hash) ? context.symbolize_keys : {}
53
+ context = context.ensure_hash(:symbols)
54
54
 
55
55
  begin
56
56
  create_pipeline(options, context).call(get_body(content, options))[:output].to_s
@@ -72,7 +72,7 @@ module Mbrao
72
72
  #
73
73
  # @return [Array] The default pipeline.
74
74
  def default_pipeline=(value)
75
- @default_pipeline = value.ensure_array.collect {|v| v.ensure_array.flatten.compact.collect { |p| p.ensure_string.to_sym } }
75
+ @default_pipeline = value.ensure_array(nil, false, false) {|v| v.ensure_array(nil, true, true, true) { |p| p.ensure_string.to_sym } }
76
76
  end
77
77
 
78
78
  # Gets the default options.
@@ -86,7 +86,7 @@ module Mbrao
86
86
  #
87
87
  # @param value [Object] The new default options.
88
88
  def default_options=(value)
89
- @default_options = value.is_a?(Hash) ? value : {}
89
+ @default_options = value.ensure_hash
90
90
  end
91
91
 
92
92
  private
@@ -95,9 +95,9 @@ module Mbrao
95
95
  # @param options [Hash] The options to sanitize.
96
96
  # @return [Hash] The sanitized options.
97
97
  def sanitize_options(options)
98
- options = options.is_a?(Hash) ? options.symbolize_keys : {}
98
+ options = options.ensure_hash(:symbols)
99
99
  options = filter_filters(options)
100
- options[:pipeline_options] = self.default_options.merge((options[:pipeline_options].is_a?(Hash) ? options[:pipeline_options] : {}).symbolize_keys)
100
+ options[:pipeline_options] = self.default_options.merge(options[:pipeline_options].ensure_hash(:symbols))
101
101
 
102
102
  options
103
103
  end
data/lib/mbrao/version.rb CHANGED
@@ -14,10 +14,10 @@ module Mbrao
14
14
  MAJOR = 1
15
15
 
16
16
  # The minor version.
17
- MINOR = 1
17
+ MINOR = 2
18
18
 
19
19
  # The patch version.
20
- PATCH = 1
20
+ PATCH = 0
21
21
 
22
22
  # The current version of mbrao.
23
23
  STRING = [MAJOR, MINOR, PATCH].compact.join(".")
data/lib/mbrao.rb CHANGED
@@ -21,4 +21,4 @@ require "mbrao/rendering_engines/base"
21
21
  require "mbrao/rendering_engines/html_pipeline"
22
22
  require "mbrao/integrations/rails" if defined?(ActionView)
23
23
 
24
- Lazier.load!(:object)
24
+ Lazier.load!(:object, :hash)
data/mbrao.gemspec CHANGED
@@ -24,8 +24,8 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.required_ruby_version = ">= 1.9.3"
26
26
 
27
- gem.add_dependency("lazier", "~> 2.8.1")
28
- gem.add_dependency("html-pipeline", "~> 0.0.8")
29
- gem.add_dependency("slim", "~> 1.3.6")
30
- gem.add_dependency("kramdown", "~> 1.0.1")
27
+ gem.add_dependency("lazier", "~> 3.3.3")
28
+ gem.add_dependency("html-pipeline", "~> 0.1.0")
29
+ gem.add_dependency("slim", "~> 2.0.0")
30
+ gem.add_dependency("kramdown", "~> 1.1.0")
31
31
  end
@@ -5,7 +5,10 @@
5
5
  #
6
6
 
7
7
  require "pathname"
8
- require "simplecov"
8
+ require "simplecov"
9
+ require "coveralls"
10
+
11
+ Coveralls.wear!
9
12
 
10
13
  SimpleCov.start do
11
14
  root = Pathname.new(File.dirname(__FILE__)) + ".."
@@ -9,21 +9,21 @@ require "spec_helper"
9
9
  describe Mbrao::Author do
10
10
  describe ".create" do
11
11
  it "creates a author from a non-hash" do
12
- Mbrao::Author.should_receive(:new).with("NAME")
12
+ expect(Mbrao::Author).to receive(:new).with("NAME")
13
13
  Mbrao::Author.create("NAME")
14
14
 
15
- Mbrao::Author.should_receive(:new).with(nil)
15
+ expect(Mbrao::Author).to receive(:new).with(nil)
16
16
  Mbrao::Author.create(nil)
17
17
 
18
- Mbrao::Author.should_receive(:new).with([])
18
+ expect(Mbrao::Author).to receive(:new).with([])
19
19
  Mbrao::Author.create([])
20
20
 
21
- Mbrao::Author.should_receive(:new).with(["NAME"])
21
+ expect(Mbrao::Author).to receive(:new).with(["NAME"])
22
22
  Mbrao::Author.create(["NAME"])
23
23
  end
24
24
 
25
25
  it "creates a author from a hash" do
26
- Mbrao::Author.should_receive(:new).with("NAME", "EMAIL", "WEBSITE", "IMAGE", {"other" => "OTHER"}, "UID")
26
+ expect(Mbrao::Author).to receive(:new).with("NAME", "EMAIL", "WEBSITE", "IMAGE", {"other" => "OTHER"}, "UID")
27
27
  Mbrao::Author.create({name: "NAME", email: "EMAIL", website: "WEBSITE", image: "IMAGE", other: "OTHER", uid: "UID"})
28
28
  end
29
29
  end
@@ -51,7 +51,7 @@ describe Mbrao::Content do
51
51
  it "should raise an exception if not available for that locale" do
52
52
  reference.locales = [:en, :it, :es]
53
53
  reference.send("#{attribute}=", v1)
54
- expect { reference.send("get_#{attribute}", [:de, :it]) }.not_to raise_error(Mbrao::Exceptions::UnavailableLocalization)
54
+ expect { reference.send("get_#{attribute}", [:de, :it]) }.not_to raise_error
55
55
  expect { reference.send("get_#{attribute}", [:de]) }.to raise_error(Mbrao::Exceptions::UnavailableLocalization)
56
56
  end
57
57
 
@@ -229,8 +229,8 @@ describe Mbrao::Content do
229
229
  it "should create a parsing engine and use it for filtering" do
230
230
  reference.body = "BODY"
231
231
  engine = ::Mbrao::ParsingEngines::Base.new
232
- engine.should_receive(:filter_content).with(reference, ["it", "en"])
233
- ::Mbrao::Parser.should_receive(:create_engine).with("ENGINE").and_return(engine)
232
+ expect(engine).to receive(:filter_content).with(reference, ["it", "en"])
233
+ expect(::Mbrao::Parser).to receive(:create_engine).with("ENGINE").and_return(engine)
234
234
  reference.get_body(["it", "en"], "ENGINE")
235
235
  end
236
236
  end
@@ -22,7 +22,7 @@ describe ActionView::Template::Handlers::MbraoTemplate do
22
22
  {locale: "LOCALE"}
23
23
  end
24
24
 
25
- def self.helper_method(method)
25
+ def self.helper_method(_)
26
26
  end
27
27
  end
28
28
 
@@ -39,7 +39,7 @@ describe ActionView::Template::Handlers::MbraoTemplate do
39
39
 
40
40
  it "should always return the same instance" do
41
41
  other = ActionView::Template::Handlers::MbraoTemplate.instance
42
- ActionView::Template::Handlers::MbraoTemplate.should_not_receive(:new)
42
+ expect(ActionView::Template::Handlers::MbraoTemplate).not_to receive(:new)
43
43
  expect(ActionView::Template::Handlers::MbraoTemplate.instance).to eq(other)
44
44
  end
45
45
 
@@ -60,11 +60,11 @@ describe ActionView::Template::Handlers::MbraoTemplate do
60
60
  it "should initialize a Content and assign it to the controller" do
61
61
  controller = DummyController.new
62
62
  renderer = DummyRenderer.new
63
- renderer.stub(:controller).and_return(controller)
63
+ allow(renderer).to receive(:controller).and_return(controller)
64
64
 
65
65
  expect(controller.respond_to?(:mbrao_content)).to be_false
66
- ::Mbrao::Parser.should_receive(:parse).and_call_original
67
- controller.class.should_receive(:helper_method).with(:mbrao_content)
66
+ expect(::Mbrao::Parser).to receive(:parse).and_call_original
67
+ expect(controller.class).to receive(:helper_method).with(:mbrao_content)
68
68
  ActionView::Template::Handlers::MbraoTemplate.instance.render(renderer, "CONTENT")
69
69
 
70
70
  expect(controller.respond_to?(:mbrao_content)).to be_true
@@ -74,12 +74,12 @@ describe ActionView::Template::Handlers::MbraoTemplate do
74
74
  it "should render contents, using specified engine and locale" do
75
75
  controller = DummyController.new
76
76
  renderer = DummyRenderer.new
77
- renderer.stub(:controller).and_return(controller)
77
+ allow(renderer).to receive(:controller).and_return(controller)
78
78
 
79
79
  content = ::Mbrao::Content.create({engine: "ENGINE"}, "CONTENT")
80
- ::Mbrao::Parser.stub(:parse).and_return(content)
80
+ allow(::Mbrao::Parser).to receive(:parse).and_return(content)
81
81
 
82
- ::Mbrao::Parser.should_receive(:render).with(content, {engine: "ENGINE", locale: "LOCALE"})
82
+ expect(::Mbrao::Parser).to receive(:render).with(content, {engine: "ENGINE", locale: "LOCALE"})
83
83
  ActionView::Template::Handlers::MbraoTemplate.instance.render(DummyRenderer.new, "CONTENT")
84
84
  end
85
85
  end
@@ -45,14 +45,14 @@ describe Mbrao::Parser do
45
45
 
46
46
  describe ".parse" do
47
47
  it "should forward to the instance" do
48
- ::Mbrao::Parser.instance.should_receive(:parse).with("TEXT", {options: {}})
48
+ expect(::Mbrao::Parser.instance).to receive(:parse).with("TEXT", {options: {}})
49
49
  ::Mbrao::Parser.parse("TEXT", {options: {}})
50
50
  end
51
51
  end
52
52
 
53
53
  describe ".render" do
54
54
  it "should forward to the instance" do
55
- ::Mbrao::Parser.instance.should_receive(:render).with("TEXT", {options: {}}, {content: {}})
55
+ expect(::Mbrao::Parser.instance).to receive(:render).with("TEXT", {options: {}}, {content: {}})
56
56
  ::Mbrao::Parser.render("TEXT", {options: {}}, {content: {}})
57
57
  end
58
58
  end
@@ -61,11 +61,11 @@ describe Mbrao::Parser do
61
61
  it "should create an engine via Lazier.find_class" do
62
62
  reference = ::Mbrao::ParsingEngines::ScopedParser.new
63
63
  cls = ::Mbrao::ParsingEngines::ScopedParser
64
- cls.should_receive(:new).exactly(2).and_return(reference)
64
+ expect(cls).to receive(:new).exactly(2).and_return(reference)
65
65
 
66
- ::Lazier.should_receive(:find_class).with(:scoped_parser, "::Mbrao::ParsingEngines::%CLASS%").and_return(cls)
66
+ expect(::Lazier).to receive(:find_class).with(:scoped_parser, "::Mbrao::ParsingEngines::%CLASS%").and_return(cls)
67
67
  expect(::Mbrao::Parser.create_engine(:scoped_parser)).to eq(reference)
68
- ::Lazier.should_receive(:find_class).with(:scoped_parser, "::Mbrao::RenderingEngines::%CLASS%").and_return(cls)
68
+ expect(::Lazier).to receive(:find_class).with(:scoped_parser, "::Mbrao::RenderingEngines::%CLASS%").and_return(cls)
69
69
  expect(::Mbrao::Parser.create_engine(:scoped_parser, :rendering)).to eq(reference)
70
70
  end
71
71
 
@@ -77,19 +77,19 @@ describe Mbrao::Parser do
77
77
  describe ".instance" do
78
78
  it("should call .new") do
79
79
  ::Mbrao::Parser.instance_variable_set(:@instance, nil)
80
- ::Mbrao::Parser.should_receive(:new)
80
+ expect(::Mbrao::Parser).to receive(:new)
81
81
  ::Mbrao::Parser.instance
82
82
  end
83
83
 
84
84
  it("should return the same instance") do
85
- ::Mbrao::Parser.stub(:new) do Time.now end
85
+ allow(::Mbrao::Parser).to receive(:new) do Time.now end
86
86
  instance = ::Mbrao::Parser.instance
87
87
  expect(::Mbrao::Parser.instance).to be(instance)
88
88
  ::Mbrao::Parser.instance_variable_set(:@instance, nil)
89
89
  end
90
90
 
91
91
  it("should return a new instance if requested to") do
92
- ::Mbrao::Parser.stub(:new) do Time.now end
92
+ allow(::Mbrao::Parser).to receive(:new) do Time.now end
93
93
  instance = ::Mbrao::Parser.instance
94
94
  expect(::Mbrao::Parser.instance(true)).not_to be(instance)
95
95
  ::Mbrao::Parser.instance_variable_set(:@instance, nil)
@@ -120,91 +120,14 @@ describe Mbrao::Parser do
120
120
  end
121
121
  end
122
122
 
123
- describe ".sanitized_hash" do
124
- it "should inject a new hash from existing one" do
125
- reference = {a: "b"}
126
- reference.should_receive(:inject).with(an_instance_of(HashWithIndifferentAccess)).and_call_original
127
- sanitized = Mbrao::Parser.sanitized_hash(reference)
128
- expect(sanitized).to be_a(HashWithIndifferentAccess)
129
- expect(sanitized["a"]).to eq("b")
130
-
131
- end
132
-
133
- it "should collect sanitized elements" do
134
- reference = [{a: "b"}]
135
- reference.should_receive(:collect).and_call_original
136
- sanitized = Mbrao::Parser.sanitized_hash(reference)
137
- expect(sanitized).to be_a(Array)
138
- expect(sanitized[0]).to eq({"a" => "b"})
139
-
140
- end
141
-
142
- it "should iterate over nested elements recursively" do
143
- reference = [{a: "b"}]
144
- Mbrao::Parser.should_receive(:sanitized_hash).with(anything, :ensure_array).exactly(4).and_call_original
145
- sanitized = Mbrao::Parser.sanitized_hash({c: reference}, :ensure_array)
146
- expect(sanitized).to be_a(HashWithIndifferentAccess)
147
- expect(sanitized["c"]).to eq([{"a" => ["b"]}])
148
- end
149
-
150
- it "should convert a non-enumerable object using a method" do
151
- reference = {a: "b"}
152
- expect(Mbrao::Parser.sanitized_hash(reference, :ensure_array).fetch("a")).to eq(["b"])
153
- end
154
-
155
- it "should convert a non-enumerable object using a block" do
156
- reference = {a: ["b", "c"]}
157
- expect((Mbrao::Parser.sanitized_hash(reference) {|v| v.upcase }.fetch("a"))).to eq(["B", "C"])
158
- end
159
-
160
- it "should not modify a non-enumerable object if requested to" do
161
- reference = {a: nil}
162
- expect((Mbrao::Parser.sanitized_hash(reference, false).fetch("a"))).to be_nil
163
- end
164
- end
165
-
166
- describe ".sanitized_array" do
167
- it "should return a sanitized array" do
168
- expect(Mbrao::Parser.sanitized_array([:en, ["A", 1]])).to eq(["en", "A", "1"])
169
- expect(Mbrao::Parser.sanitized_array(:en)).to eq(["en"])
170
- expect(Mbrao::Parser.sanitized_array(:en)).to eq(["en"])
171
- expect(Mbrao::Parser.sanitized_array([:en, nil])).to eq(["en", ""])
172
- end
173
-
174
- it "should not uniq if requested to" do
175
- ref = [{a: "b"}]
176
- ref.should_not_receive(:uniq)
177
- expect(Mbrao::Parser.sanitized_array(ref, false)).to eq(["{:a=>\"b\"}"])
178
- end
179
-
180
- it "should compact if requested to" do
181
- ref = [{a: "b"}, nil]
182
- ref.should_not_receive(:compact)
183
- expect(Mbrao::Parser.sanitized_array(ref, false, true)).to eq(["{:a=>\"b\"}"])
184
- end
185
-
186
- it "should use the requested method for sanitizing" do
187
- ref = [{a: "b"}]
188
- ref.should_not_receive(:ensure_string)
189
- Hash.any_instance.should_receive(:to_json).and_call_original
190
- expect(Mbrao::Parser.sanitized_array(ref, true, false, :to_json)).to eq(["{\"a\":\"b\"}"])
191
- end
192
-
193
- it "should use the block for sanitizing" do
194
- ref = [{a: "b"}]
195
- ref.should_not_receive(:ensure_string)
196
- expect(Mbrao::Parser.sanitized_array(ref) {|e| e.keys.first }).to eq([:a])
197
- end
198
- end
199
-
200
123
  describe "#parse" do
201
124
  it "should sanitize options" do
202
125
  reference = BlankParser.new
203
- ::Mbrao::Parser.should_receive(:create_engine).exactly(3).and_return(reference)
126
+ expect(::Mbrao::Parser).to receive(:create_engine).exactly(3).and_return(reference)
204
127
 
205
- reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser})
206
- reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => false, "engine" => :blank_parser})
207
- reference.should_receive(:parse).with("CONTENT", {"metadata" => false, "content" => false, "engine" => :blank_parser})
128
+ expect(reference).to receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser})
129
+ expect(reference).to receive(:parse).with("CONTENT", {"metadata" => true, "content" => false, "engine" => :blank_parser})
130
+ expect(reference).to receive(:parse).with("CONTENT", {"metadata" => false, "content" => false, "engine" => :blank_parser})
208
131
 
209
132
  ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser})
210
133
  ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser, content: 2})
@@ -213,9 +136,9 @@ describe Mbrao::Parser do
213
136
 
214
137
  it "should call .create_engine call its #parse method" do
215
138
  reference = BlankParser.new
216
- ::Mbrao::Parser.should_receive(:create_engine).and_return(reference)
139
+ expect(::Mbrao::Parser).to receive(:create_engine).and_return(reference)
217
140
 
218
- reference.should_receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser, "other" => "OK"})
141
+ expect(reference).to receive(:parse).with("CONTENT", {"metadata" => true, "content" => true, "engine" => :blank_parser, "other" => "OK"})
219
142
  ::Mbrao::Parser.new.parse("CONTENT", {engine: :blank_parser, other: "OK"})
220
143
  end
221
144
  end
@@ -223,10 +146,10 @@ describe Mbrao::Parser do
223
146
  describe "#render" do
224
147
  it "should sanitize options" do
225
148
  reference = Object.new
226
- ::Mbrao::Parser.should_receive(:create_engine).exactly(2).and_return(reference)
149
+ expect(::Mbrao::Parser).to receive(:create_engine).exactly(2).and_return(reference)
227
150
 
228
- reference.should_receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
229
- reference.should_receive(:render).with("CONTENT", {"engine" => :html_pipeline}, {content: "OK"})
151
+ expect(reference).to receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
152
+ expect(reference).to receive(:render).with("CONTENT", {"engine" => :html_pipeline}, {content: "OK"})
230
153
 
231
154
  ::Mbrao::Parser.new.render("CONTENT", {engine: :blank_rendered}, {content: "OK"})
232
155
  ::Mbrao::Parser.new.render("CONTENT", {engine: :html_pipeline}, {content: "OK"})
@@ -234,9 +157,9 @@ describe Mbrao::Parser do
234
157
 
235
158
  it "should call .create_engine call its #parse method" do
236
159
  reference = Object.new
237
- ::Mbrao::Parser.should_receive(:create_engine).with(:blank_rendered, :rendering).and_return(reference)
160
+ expect(::Mbrao::Parser).to receive(:create_engine).with(:blank_rendered, :rendering).and_return(reference)
238
161
 
239
- reference.should_receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
162
+ expect(reference).to receive(:render).with("CONTENT", {"engine" => :blank_rendered}, {content: "OK"})
240
163
  ::Mbrao::Parser.new.render("CONTENT", {engine: :blank_rendered}, {content: "OK"})
241
164
  end
242
165
  end
@@ -31,21 +31,21 @@ describe Mbrao::ParsingEngines::Base do
31
31
  let(:reference) { ::Mbrao::ParsingEngines::Base.new }
32
32
 
33
33
  it "should forward to ::Mbrao::Content.create" do
34
- reference.should_receive(:separate_components).with("CONTENT", {a: "b"}).and_return([{a: "b"}, "BODY"])
35
- reference.stub(:parse_metadata).and_return({a: "b"})
36
- ::Mbrao::Content.should_receive(:create).with({a: "b"}, "BODY")
34
+ expect(reference).to receive(:separate_components).with("CONTENT", {a: "b"}).and_return([{a: "b"}, "BODY"])
35
+ allow(reference).to receive(:parse_metadata).and_return({a: "b"})
36
+ expect(::Mbrao::Content).to receive(:create).with({a: "b"}, "BODY")
37
37
  reference.parse("CONTENT", {a: "b"})
38
38
  end
39
39
 
40
40
  it "should return a Content object" do
41
- reference.stub(:separate_components).with("CONTENT", {a: "b"}).and_return([])
42
- reference.stub(:parse_metadata).and_return({})
41
+ allow(reference).to receive(:separate_components).with("CONTENT", {a: "b"}).and_return([])
42
+ allow(reference).to receive(:parse_metadata).and_return({})
43
43
  expect(reference.parse("CONTENT", {a: "b"})).to be_a(::Mbrao::Content)
44
44
  end
45
45
 
46
46
  it "should separate_components" do
47
- reference.should_receive(:separate_components).with("CONTENT", {a: "b"})
48
- reference.stub(:parse_metadata).and_return({})
47
+ expect(reference).to receive(:separate_components).with("CONTENT", {a: "b"})
48
+ allow(reference).to receive(:parse_metadata).and_return({})
49
49
  reference.parse("CONTENT", {a: "b"})
50
50
  end
51
51
  end
@@ -12,51 +12,50 @@ describe Mbrao::RenderingEngines::HtmlPipeline do
12
12
  describe "#render" do
13
13
  it "should forward everything to the html-pipeline" do
14
14
  pipeline = Object.new
15
- pipeline.should_receive(:call).with("CONTENT").and_return({output: ""})
16
- ::HTML::Pipeline.should_receive(:new).with(an_instance_of(Array), an_instance_of(Hash)).and_return(pipeline)
15
+ expect(pipeline).to receive(:call).with("CONTENT").and_return({output: ""})
16
+ expect(::HTML::Pipeline).to receive(:new).with(an_instance_of(Array), an_instance_of(Hash)).and_return(pipeline)
17
17
  reference.render("CONTENT")
18
18
  end
19
19
 
20
20
  it "should raise a specific exception if a locale is not available" do
21
-
22
21
  expect { reference.render(::Mbrao::Content.create({locales: ["en"]}, "BODY"), {locales: ["it"]}) }.to raise_error(::Mbrao::Exceptions::UnavailableLocalization)
23
22
  end
24
23
 
25
24
  it "should raise an exception if anything goes wrong" do
26
- ::HTML::Pipeline.stub(:new).and_raise(ArgumentError.new("ERROR"))
25
+ allow(::HTML::Pipeline).to receive(:new).and_raise(ArgumentError.new("ERROR"))
27
26
  expect { reference.render("CONTENT") }.to raise_error(::Mbrao::Exceptions::Rendering)
28
27
  end
29
28
 
30
29
  it "should have default options" do
31
30
  filters = [:kramdown, :table_of_contents, :autolink, :emoji, :image_max_width].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
32
- ::HTML::Pipeline.should_receive(:new).with(filters, {gfm: true, asset_root: "/"}).and_call_original
31
+ expect(::HTML::Pipeline).to receive(:new).with(filters, {gfm: true, asset_root: "/"}).and_call_original
33
32
  reference.render("CONTENT")
34
33
  end
35
34
 
36
35
  it "should merge context to options" do
37
- ::HTML::Pipeline.should_receive(:new).with(an_instance_of(Array), {gfm: true, asset_root: "/", additional: true}).and_call_original
36
+ expect(::HTML::Pipeline).to receive(:new).with(an_instance_of(Array), {gfm: true, asset_root: "/", additional: true}).and_call_original
38
37
  reference.render("CONTENT", {}, {additional: true})
39
38
  end
40
39
 
41
40
  it "should restrict filter used" do
42
41
  filters = [:table_of_contents, :autolink, :emoji, :image_max_width].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
43
- ::HTML::Pipeline.should_receive(:new).with(filters, an_instance_of(Hash)).and_call_original
42
+ expect(::HTML::Pipeline).to receive(:new).with(filters, an_instance_of(Hash)).and_call_original
44
43
  reference.render("CONTENT", {kramdown: false})
45
44
 
46
45
  filters = [:kramdown, :autolink, :emoji, :image_max_width].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
47
- ::HTML::Pipeline.should_receive(:new).with(filters, an_instance_of(Hash)).and_call_original
46
+ expect(::HTML::Pipeline).to receive(:new).with(filters, an_instance_of(Hash)).and_call_original
48
47
  reference.render("CONTENT", {toc: false})
49
48
 
50
49
  filters = [:kramdown, :table_of_contents, :emoji, :image_max_width].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
51
- ::HTML::Pipeline.should_receive(:new).with(filters, an_instance_of(Hash)).and_call_original
50
+ expect(::HTML::Pipeline).to receive(:new).with(filters, an_instance_of(Hash)).and_call_original
52
51
  reference.render("CONTENT", {links: false})
53
52
 
54
53
  filters = [:kramdown, :table_of_contents, :autolink, :image_max_width].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
55
- ::HTML::Pipeline.should_receive(:new).with(filters, an_instance_of(Hash)).and_call_original
54
+ expect(::HTML::Pipeline).to receive(:new).with(filters, an_instance_of(Hash)).and_call_original
56
55
  reference.render("CONTENT", {emoji: false})
57
56
 
58
57
  filters = [:kramdown, :table_of_contents, :autolink, :emoji].collect {|f| ::Lazier.find_class(f, "::HTML::Pipeline::%CLASS%Filter", true) }
59
- ::HTML::Pipeline.should_receive(:new).with(filters, an_instance_of(Hash)).and_call_original
58
+ expect(::HTML::Pipeline).to receive(:new).with(filters, an_instance_of(Hash)).and_call_original
60
59
  reference.render("CONTENT", {image_max_width: false})
61
60
  end
62
61
  end
@@ -109,7 +108,7 @@ end
109
108
  describe HTML::Pipeline::KramdownFilter do
110
109
  describe "#initialize" do
111
110
  it "should call the parent constructor" do
112
- HTML::Pipeline::TextFilter.should_receive(:new).with("\rCONTENT\r", {a: "b"}, {c: "d"})
111
+ expect(HTML::Pipeline::TextFilter).to receive(:new).with("\rCONTENT\r", {a: "b"}, {c: "d"})
113
112
  HTML::Pipeline::KramdownFilter.new("\rCONTENT\r", {a: "b"}, {c: "d"})
114
113
  end
115
114
 
@@ -120,8 +119,8 @@ describe HTML::Pipeline::KramdownFilter do
120
119
 
121
120
  it "should use Kramdown with given options for building the result" do
122
121
  object = Object.new
123
- Kramdown::Document.should_receive(:new).with("CONTENT", {a: "b"}).and_return(object)
124
- object.should_receive(:to_html)
122
+ expect(Kramdown::Document).to receive(:new).with("CONTENT", {a: "b"}).and_return(object)
123
+ expect(object).to receive(:to_html)
125
124
  HTML::Pipeline::KramdownFilter.new("\rCONTENT\r", {a: "b"}, {c: "d"}).call
126
125
  end
127
126
  end