snippr 0.3.0 → 0.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/.gitignore +13 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +4 -0
  5. data/README.md +143 -0
  6. data/Rakefile +5 -4
  7. data/lib/snippr.rb +14 -2
  8. data/lib/snippr/i18n.rb +9 -10
  9. data/lib/snippr/links.rb +59 -0
  10. data/lib/snippr/meta_data.rb +33 -0
  11. data/lib/snippr/normalizer.rb +18 -0
  12. data/lib/snippr/normalizer/camelizer.rb +18 -0
  13. data/lib/snippr/normalizer/de_rester.rb +25 -0
  14. data/lib/snippr/path.rb +27 -6
  15. data/lib/snippr/processor.rb +18 -0
  16. data/lib/snippr/processor/dynamics.rb +31 -0
  17. data/lib/snippr/processor/functions.rb +50 -0
  18. data/lib/snippr/processor/links.rb +20 -0
  19. data/lib/snippr/processor/wikilinks.rb +20 -0
  20. data/lib/snippr/snip.rb +57 -0
  21. data/lib/snippr/snippr.rb +41 -45
  22. data/lib/snippr/view_helper.rb +71 -0
  23. data/snippr.gemspec +30 -0
  24. data/spec/fixtures/a/path/aSnippet.snip +1 -0
  25. data/spec/fixtures/a/path/aSnippetWithParam.snip +1 -0
  26. data/spec/fixtures/controller/action/aSnippet.snip +1 -0
  27. data/spec/fixtures/empty.snip +3 -0
  28. data/spec/fixtures/i18n/list_de.snip +0 -0
  29. data/spec/fixtures/meta/broken.snip +5 -0
  30. data/spec/fixtures/meta/withContent.snip +5 -0
  31. data/spec/fixtures/meta/withContentNoNewline.snip +4 -0
  32. data/spec/fixtures/meta/withNoContent.snip +4 -0
  33. data/spec/fixtures/withUnderscore/andUnderscore/aSnippet.snip +1 -0
  34. data/spec/fixtures/withViewHelperMethod.snip +1 -0
  35. data/spec/snippr/i18n_spec.rb +30 -0
  36. data/spec/snippr/links_spec.rb +137 -0
  37. data/spec/snippr/normalizer/camelizer_spec.rb +13 -0
  38. data/spec/snippr/normalizer/de_rester_spec.rb +24 -0
  39. data/spec/snippr/normalizer_spec.rb +40 -0
  40. data/spec/snippr/path_spec.rb +87 -0
  41. data/spec/snippr/processor/dynamics_spec.rb +49 -0
  42. data/spec/snippr/processor/functions_spec.rb +72 -0
  43. data/spec/snippr/processor/links_spec.rb +12 -0
  44. data/spec/snippr/processor/wikilinks_spec.rb +12 -0
  45. data/spec/snippr/processor_spec.rb +35 -0
  46. data/spec/snippr/snip_spec.rb +179 -0
  47. data/spec/snippr/snippr_spec.rb +60 -55
  48. data/spec/snippr/view_helper_spec.rb +221 -0
  49. data/spec/spec_helper.rb +17 -3
  50. metadata +178 -87
  51. data/README.rdoc +0 -77
  52. data/lib/snippr/core_ext.rb +0 -12
  53. data/lib/snippr/helper.rb +0 -23
  54. data/lib/snippr/link.rb +0 -26
  55. data/spec/fixtures/tariff/einheit.snip +0 -1
  56. data/spec/fixtures/wiki.snip +0 -1
  57. data/spec/snippr/core_ext_spec.rb +0 -11
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe Snippr::Processor::Links do
4
+
5
+ it "should call Snippr::Links.adjust_link with the links found and return the results" do
6
+ seq = sequence 'links'
7
+ Snippr::Links.expects(:adjust_link).with('<a href="http://www.blaulabs.de" onclick="return true;">here</a>').in_sequence(seq).returns('--here--')
8
+ Snippr::Links.expects(:adjust_link).with('<A class=\'link\' href="internal.html">or here</A>').in_sequence(seq).returns('--or here--')
9
+ subject.process('click <a href="http://www.blaulabs.de" onclick="return true;">here</a> <A class=\'link\' href="internal.html">or here</A>').should == 'click --here-- --or here--'
10
+ end
11
+
12
+ end
@@ -0,0 +1,12 @@
1
+ require "spec_helper"
2
+
3
+ describe Snippr::Processor::Wikilinks do
4
+
5
+ it "should call Snippr::Links.adjust_link with the links found and return the results" do
6
+ seq = sequence 'wikilinks'
7
+ Snippr::Links.expects(:adjust_link).with('<a href="http://www.blaulabs.de">here</a>').in_sequence(seq).returns('--here--')
8
+ Snippr::Links.expects(:adjust_link).with('<a href="internal.html">or here</a>').in_sequence(seq).returns('--or here--')
9
+ subject.process('click [[http://www.blaulabs.de|here]] [[internal.html|or here]]').should == 'click --here-- --or here--'
10
+ end
11
+
12
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ describe Snippr::Processor do
4
+
5
+ describe ".processors" do
6
+
7
+ it "should be an array" do
8
+ subject.processors.should be_an(Array)
9
+ end
10
+
11
+ it "should have a set of default processors" do
12
+ processors = subject.processors
13
+ processors.size.should == 4
14
+ processors[0].should be_a(Snippr::Processor::Functions)
15
+ processors[1].should be_a(Snippr::Processor::Dynamics)
16
+ processors[2].should be_a(Snippr::Processor::Links)
17
+ processors[3].should be_a(Snippr::Processor::Wikilinks)
18
+ end
19
+
20
+ end
21
+
22
+ describe ".process" do
23
+
24
+ it "should call process on all processors, passing the content between them and returning the last result" do
25
+ seq = sequence 'processors'
26
+ subject.processors.each_with_index do |processor, i|
27
+ processor.should respond_to(:process)
28
+ processor.expects(:process).with(i.to_s, {'1' => '2'}).returns((i + 1).to_s).in_sequence(seq)
29
+ end
30
+ subject.process('0', {'1' => '2'}).should == subject.processors.size.to_s
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,179 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe Snippr::Snip do
5
+
6
+ describe "initializer" do
7
+
8
+ before do
9
+ Snippr::Path.path = 'path'
10
+ end
11
+
12
+ context "without I18n" do
13
+
14
+ before do
15
+ Snippr::I18n.enabled = false
16
+ end
17
+
18
+ it "initializes name, path and opts without opts" do
19
+ snip = Snippr::Snip.new(:path_to_snips, :file)
20
+ snip.name.should == 'pathToSnips/file'
21
+ snip.path.should == 'path/pathToSnips/file.snip'
22
+ snip.opts.should == {}
23
+ end
24
+
25
+ it "initializes name, path and opts with opts" do
26
+ snip = Snippr::Snip.new(:path_to_snips, :file, :key => :value)
27
+ snip.name.should == 'pathToSnips/file'
28
+ snip.path.should == 'path/pathToSnips/file.snip'
29
+ snip.opts.should == {:key => :value}
30
+ end
31
+
32
+ it "initializes name, path and opts with given extension" do
33
+ snip = Snippr::Snip.new(:path_to_snips, :file, :extension => :yml)
34
+ snip.name.should == "pathToSnips/file"
35
+ snip.path.should == "path/pathToSnips/file.yml"
36
+ snip.opts.should == { :extension => :yml }
37
+ end
38
+
39
+ it "initializes name, path and opts with given i18n deactivation" do
40
+ snip = Snippr::Snip.new(:path_to_snips, :file, :i18n => true)
41
+ snip.name.should == "pathToSnips/file_#{::I18n.locale}"
42
+ snip.path.should == "path/pathToSnips/file_#{::I18n.locale}.snip"
43
+ snip.opts.should == { :i18n => true }
44
+ end
45
+
46
+ it "returns no double slahes in the path for nil value" do
47
+ snip = Snippr::Snip.new(:path_to_snips, nil ,:file)
48
+ snip.name.should == 'pathToSnips/file'
49
+ snip.path.should == 'path/pathToSnips/file.snip'
50
+ end
51
+
52
+ it "returns no double slahes in the path for empty string value" do
53
+ snip = Snippr::Snip.new(:path_to_snips, "" ,:file)
54
+ snip.name.should == 'pathToSnips/file'
55
+ snip.path.should == 'path/pathToSnips/file.snip'
56
+ end
57
+
58
+ end
59
+
60
+ context "with I18n" do
61
+
62
+ before do
63
+ Snippr::I18n.enabled = true
64
+ end
65
+
66
+ it "initializes name, path and opts without opts" do
67
+ snip = Snippr::Snip.new(:path_to_snips, :file)
68
+ snip.name.should == "pathToSnips/file_#{::I18n.locale}"
69
+ snip.path.should == "path/pathToSnips/file_#{::I18n.locale}.snip"
70
+ snip.opts.should == {}
71
+ end
72
+
73
+ it "initializes name, path and opts with opts" do
74
+ snip = Snippr::Snip.new(:path_to_snips, :file, :key => :value)
75
+ snip.name.should == "pathToSnips/file_#{::I18n.locale}"
76
+ snip.path.should == "path/pathToSnips/file_#{::I18n.locale}.snip"
77
+ snip.opts.should == {:key => :value}
78
+ end
79
+
80
+ it "initializes name, path and opts with given extension" do
81
+ snip = Snippr::Snip.new(:path_to_snips, :file, :extension => :yml)
82
+ snip.name.should == "pathToSnips/file_#{::I18n.locale}"
83
+ snip.path.should == "path/pathToSnips/file_#{::I18n.locale}.yml"
84
+ snip.opts.should == { :extension => :yml }
85
+ end
86
+
87
+ it "initializes name, path and opts with given i18n deactivation" do
88
+ snip = Snippr::Snip.new(:path_to_snips, :file, :i18n => false)
89
+ snip.name.should == "pathToSnips/file"
90
+ snip.path.should == "path/pathToSnips/file.snip"
91
+ snip.opts.should == { :i18n => false }
92
+ end
93
+ end
94
+
95
+
96
+ end
97
+
98
+ describe "#unprocessed_content" do
99
+
100
+ it "reads an existing snip" do
101
+ Snippr::Snip.new(:home).unprocessed_content.should == '<p>Home</p>'
102
+ end
103
+
104
+ it "stores the read data instead of reading it again" do
105
+ File.expects(:read).once.returns('data')
106
+ snip = Snippr::Snip.new(:home)
107
+ snip.unprocessed_content.should == 'data'
108
+ snip.unprocessed_content.should == 'data'
109
+ end
110
+
111
+ it "returns an empty string on missing snips" do
112
+ File.expects(:read).never
113
+ Snippr::Snip.new(:doesnotexist).unprocessed_content.should == ''
114
+ end
115
+
116
+ it "strips the read content" do
117
+ Snippr::Snip.new(:empty).unprocessed_content.should == ''
118
+ end
119
+
120
+ end
121
+
122
+ [:content, :to_s].each do |method|
123
+
124
+ describe "##{method}" do
125
+
126
+ it "calls Snippr::Processor.process with opts and return decorated result" do
127
+ Snippr::Processor.expects(:process).with('<p>Home</p>', {:a => :b}).returns('processed')
128
+ Snippr::Snip.new(:home, :a => :b).send(method).should == "<!-- starting snippr: home -->\nprocessed\n<!-- closing snippr: home -->"
129
+ end
130
+
131
+ it "stores the processed data instead of processing it again" do
132
+ Snippr::Processor.expects(:process).with('<p>Home</p>', {:a => :b}).once.returns('processed')
133
+ snip = Snippr::Snip.new(:home, :a => :b)
134
+ snip.send(method).should == "<!-- starting snippr: home -->\nprocessed\n<!-- closing snippr: home -->"
135
+ snip.send(method).should == "<!-- starting snippr: home -->\nprocessed\n<!-- closing snippr: home -->"
136
+ end
137
+
138
+ it "doesn't call Snippr::Processor.process and return missing string" do
139
+ Snippr::Processor.expects(:process).never
140
+ Snippr::Snip.new(:doesnotexist, :a => :b).send(method).should == '<!-- missing snippr: doesnotexist -->'
141
+ end
142
+
143
+ end
144
+
145
+ end
146
+
147
+ describe "#missing?" do
148
+
149
+ it "returns true when the snip isn't there" do
150
+ Snippr::Snip.new(:doesnotexist).should be_missing
151
+ end
152
+
153
+ it "returns false when the snip is there but empty" do
154
+ Snippr::Snip.new(:empty).should_not be_missing
155
+ end
156
+
157
+ it "returns false when the snip is there and has content" do
158
+ Snippr::Snip.new(:home).should_not be_missing
159
+ end
160
+
161
+ end
162
+
163
+ describe "#empty?" do
164
+
165
+ it "returns true when the snip isn't there" do
166
+ Snippr::Snip.new(:doesnotexist).should be_empty
167
+ end
168
+
169
+ it "returns false when the snip is there but empty" do
170
+ Snippr::Snip.new(:empty).should be_empty
171
+ end
172
+
173
+ it "returns false when the snip is there and has content" do
174
+ Snippr::Snip.new(:home).should_not be_empty
175
+ end
176
+
177
+ end
178
+
179
+ end
@@ -1,79 +1,84 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Snippr do
4
- before :all do
5
- snippr_path = File.join File.dirname(__FILE__), "..", "fixtures"
6
-
7
- if RUBY_PLATFORM =~ /java/
8
- Snippr::Path::JavaLang::System.set_property Snippr::Path::JVMProperty, snippr_path
9
- else
10
- Snippr.path = snippr_path
11
- end
12
- Snippr.i18n = false
13
- end
14
4
 
15
- it "should return the content of a snippr" do
16
- Snippr.load(:home).should include("<p>Home</p>")
5
+ it "should delegate path to Snippr::Path.path" do
6
+ Snippr::Path.should respond_to(:path)
7
+ Snippr::Path.expects(:path).returns('path')
8
+ subject.path.should == 'path'
17
9
  end
18
10
 
19
- it "should return the content a snippr from a subfolder" do
20
- Snippr.load("tariff/einheit").should include("<p>tariff: einheit</p>")
11
+ it "should delegate path= to Snippr::Path.path=" do
12
+ Snippr::Path.should respond_to(:path=)
13
+ Snippr::Path.expects(:path=).with('path')
14
+ subject.path = 'path'
21
15
  end
22
16
 
23
- it "should return the content a snippr from a subfolder specified via multiple arguments" do
24
- Snippr.load(:tariff, :einheit).should include("<p>tariff: einheit</p>")
17
+ it "should delegate i18n? to Snippr::I18n.enabled?" do
18
+ Snippr::I18n.should respond_to(:enabled?)
19
+ Snippr::I18n.expects(:enabled?).returns(true)
20
+ subject.i18n?.should == true
25
21
  end
26
22
 
27
- it "should convert snake_case Symbols to lowerCamelCase Strings" do
28
- Snippr.load(:topup, :some_error).should include("<p>Some error occurred.</p>")
23
+ it "should delegate i18n= to Snippr::I18n.enabled=" do
24
+ Snippr::I18n.should respond_to(:enabled=)
25
+ Snippr::I18n.expects(:enabled=).with(true)
26
+ subject.i18n = true
29
27
  end
30
28
 
31
- it "should wrap the snippr in descriptive comments" do
32
- Snippr.load(:home).should ==
33
- "<!-- starting snippr: home -->\n" <<
34
- "<p>Home</p>\n" <<
35
- "<!-- closing snippr: home -->"
29
+ it "should delegate adjust_urls_except? to Snippr::Links.adjust_urls_except" do
30
+ Snippr::Links.should respond_to(:adjust_urls_except)
31
+ Snippr::Links.expects(:adjust_urls_except).returns([1])
32
+ subject.adjust_urls_except.should == [1]
36
33
  end
37
34
 
38
- it "should replace placeholders with dynamic values" do
39
- snippr = Snippr.load :topup, :success, :topup_amount => "15,00 &euro;", :date_today => Date.today
40
- snippr.should include("<p>You're topup of 15,00 &euro; at #{Date.today} was successful.</p>")
35
+ it "should delegate adjust_urls_except= to Snippr::Links.adjust_urls_except=" do
36
+ Snippr::Links.should respond_to(:adjust_urls_except=)
37
+ Snippr::Links.expects(:adjust_urls_except=).with([2])
38
+ subject.adjust_urls_except = [2]
41
39
  end
42
40
 
43
- it "should return a fallback wrapped in descriptive comments for missing snipprs" do
44
- Snippr.load(:doesnotexist).should ==
45
- "<!-- starting snippr: doesnotexist -->\n" <<
46
- "<samp class=\"missing snippr\" />\n" <<
47
- "<!-- closing snippr: doesnotexist -->"
48
- end
49
-
50
- it "should convert wiki links to html links" do
51
- Snippr.load(:wiki).should ==
52
- "<!-- starting snippr: wiki -->\n" <<
53
- "<p>Click <a href=\"http://www.blaulabs.de\">here with blanks</a>.</p>\n" <<
54
- "<!-- closing snippr: wiki -->"
55
- end
41
+ context "the logger" do
56
42
 
57
- describe "I18n" do
58
- before { Snippr.i18n = true }
43
+ after do
44
+ Snippr.logger = nil
45
+ end
46
+
47
+ it "can be configured" do
48
+ Snippr.logger = :logger
49
+ Snippr.logger.should == :logger
50
+ end
59
51
 
60
- it "should prepend the current locale prefixed with a '_' to a snippr file" do
61
- I18n.locale = :de
62
-
63
- Snippr.load(:i18n, :shop).should ==
64
- "<!-- starting snippr: i18n/shop_de -->\n" <<
65
- "<p>Willkommen in unserem Shop.</p>\n" <<
66
- "<!-- closing snippr: i18n/shop_de -->"
52
+ it "defaults to a custom logger" do
53
+ Snippr.logger.should be_a(Logger)
67
54
  end
68
55
 
69
- it "should also work for other locales" do
70
- I18n.locale = :en
71
-
72
- Snippr.load(:i18n, :shop).should ==
73
- "<!-- starting snippr: i18n/shop_en -->\n" <<
74
- "<p>Welcome to our shop.</p>\n" <<
75
- "<!-- closing snippr: i18n/shop_en -->"
56
+ context "in a Rails app" do
57
+
58
+ before do
59
+ Rails = Class.new { def self.logger; :rails_logger end }
60
+ end
61
+
62
+ after do
63
+ Object.send(:remove_const, :Rails)
64
+ end
65
+
66
+ it "uses the Rails logger" do
67
+ Snippr.logger.should == :rails_logger
68
+ end
69
+
76
70
  end
71
+
72
+ end
73
+
74
+ it "should delegate load to Snippr::Snip.new" do
75
+ Snippr::Snip.expects(:new).with(:a, :b).returns('snip')
76
+ subject.load(:a, :b).should == 'snip'
77
+ end
78
+
79
+ it "should delegate list to Snippr::Path.list" do
80
+ Snippr::Path.expects(:list).with(:c, :d).returns([:snip])
81
+ subject.list(:c, :d).should == [:snip]
77
82
  end
78
83
 
79
84
  end
@@ -0,0 +1,221 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "spec_helper"
3
+
4
+ describe Snippr::ViewHelper do
5
+ include Snippr::ViewHelper
6
+
7
+ describe "snippr" do
8
+
9
+ def helper_method(param)
10
+ "wrapppppp #{param.upcase} ppppppparw"
11
+ end
12
+
13
+ it "should allow calling of methods on a Rails view" do
14
+ snippr(:with_view_helper_method).should == "<!-- starting snippr: withViewHelperMethod -->\nwith helper *wrapppppp TEST ppppppparw*\n<!-- closing snippr: withViewHelperMethod -->"
15
+ end
16
+
17
+ context "existance check on string returned by snippr" do
18
+
19
+ context "existing snippet" do
20
+
21
+ it "returns true when calling .exists?" do
22
+ snippr(:home).exists?.should be_true
23
+ end
24
+
25
+ it "returns false when calling .missing?" do
26
+ snippr(:home).missing?.should be_false
27
+ end
28
+
29
+ end
30
+
31
+ context "missing snippet" do
32
+ it "returns false when calling .exists?" do
33
+ snippr(:missing).exists?.should be_false
34
+ end
35
+
36
+ it "returns true when calling .missing?" do
37
+ snippr(:missing).missing?.should be_true
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ context "snippr with meta data" do
44
+
45
+ context "and content" do
46
+
47
+ it "returns a Hash of meta information" do
48
+ snippr(:meta, :with_content).meta.should == {
49
+ "description" => "Die mit dem Fluegli",
50
+ "keywords" => "blau Mobilfunk GmbH, blau.de, blauworld, handy, sim"
51
+ }
52
+ end
53
+
54
+ it "returns a hash ofmeta information even if the YAML Fron Matter BLock ends without newline" do
55
+ snippr(:meta, :with_content_no_newline).meta.should == {
56
+ "description" => "Die mit dem Fluegli",
57
+ "keywords" => "blau Mobilfunk GmbH, blau.de, blauworld, handy, sim"
58
+ }
59
+
60
+ end
61
+
62
+ it "accepts a key to search for" do
63
+ snippr(:meta, :with_content).meta("description").should == "Die mit dem Fluegli"
64
+ end
65
+
66
+ it "returns the content without meta information" do
67
+ snippr(:meta, :with_content).to_s.should == "<!-- starting snippr: meta/withContent -->\n<p>So meta!</p>\n<!-- closing snippr: meta/withContent -->"
68
+ end
69
+
70
+ end
71
+
72
+ context "and no content" do
73
+
74
+ it "still returns a Hash of meta information" do
75
+ snippr(:meta, :with_no_content).meta.should == {
76
+ "description" => "Die mit dem Fluegli",
77
+ "keywords" => "blau Mobilfunk GmbH, blau.de, blauworld, handy, sim"
78
+ }
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ context "snippr with broken meta data" do
86
+
87
+ it "logs a warning and acts as if no meta information exist" do
88
+ Snippr.logger.expects(:warn).with do |msg|
89
+ msg =~ /Unable to extract meta data from Snip \[:meta, :broken\]/
90
+ end
91
+
92
+ snip = snippr(:meta, :broken)
93
+ snip.meta.should == {}
94
+ snip.to_s.should == "<!-- starting snippr: meta/broken -->\n<p>Broken!</p>\n<!-- closing snippr: meta/broken -->"
95
+ end
96
+
97
+ end
98
+
99
+ context "existing snippr" do
100
+
101
+ it "should call html_safe and return html safe value" do
102
+ content = snippr(:home)
103
+ content.should == "<!-- starting snippr: home -->\n<p>Home</p>\n<!-- closing snippr: home -->"
104
+ content.should be_html_safe
105
+ end
106
+
107
+ it "should pass html_safe snippr to block" do
108
+ lambda {
109
+ snippr(:home) do |snippr|
110
+ snippr.should be_html_safe
111
+ raise StandardError.new('block should be called')
112
+ end
113
+ }.should raise_error StandardError, 'block should be called'
114
+ end
115
+
116
+ it "should return 0 with block" do
117
+ snippr(:home) do; end.should == 0
118
+ end
119
+
120
+ end
121
+
122
+ context "empty snippr" do
123
+
124
+ it "should call html_safe return html save value" do
125
+ content = snippr(:empty)
126
+ content.should == "<!-- starting snippr: empty -->\n\n<!-- closing snippr: empty -->"
127
+ content.should be_html_safe
128
+ end
129
+
130
+ it "should not pass snippr to block but concat snippr and return 0" do
131
+ expects(:concat).with("<!-- starting snippr: empty -->\n\n<!-- closing snippr: empty -->")
132
+ lambda {
133
+ snippr(:empty) do
134
+ raise StandardError.new('block should not be called')
135
+ end.should == 0
136
+ }.should_not raise_error
137
+ end
138
+
139
+ end
140
+
141
+ context "missing snippr" do
142
+
143
+ it "should call html_safe return html save value" do
144
+ content = snippr(:doesnotexist)
145
+ content.should == '<!-- missing snippr: doesnotexist -->'
146
+ content.should be_html_safe
147
+ end
148
+
149
+ it "should not pass snippr to block but concat snippr and return 0" do
150
+ expects(:concat).with('<!-- missing snippr: doesnotexist -->')
151
+ lambda {
152
+ snippr(:doesnotexist) do
153
+ raise StandardError.new('block should not be called')
154
+ end.should == 0
155
+ }.should_not raise_error
156
+ end
157
+
158
+ end
159
+
160
+ end
161
+
162
+ describe "#snippr_with_path" do
163
+
164
+ context "on pages controller (special case)" do
165
+
166
+ before do
167
+ stubs(:controller_name).returns("pages")
168
+ stubs(:params).returns(:id => "a/path", :action => :view)
169
+ end
170
+
171
+ it "uses the path given in 'id' param" do
172
+ snippr_with_path(:a_snippet).should == "<!-- starting snippr: a/path/aSnippet -->\na snippet\n<!-- closing snippr: a/path/aSnippet -->"
173
+ end
174
+
175
+ it "works with a given block" do
176
+ lambda {
177
+ snippr_with_path(:a_snippet) do |snippr|
178
+ snippr.should be_html_safe
179
+ raise StandardError.new('block should be called')
180
+ end
181
+ }.should raise_error StandardError, 'block should be called'
182
+ end
183
+
184
+ it "works with parameters hash" do
185
+ snippr_with_path(:a_snippet_with_param, :param => "value").should == "<!-- starting snippr: a/path/aSnippetWithParam -->\na snippet with param value\n<!-- closing snippr: a/path/aSnippetWithParam -->"
186
+ end
187
+
188
+ end
189
+
190
+ context "on standard controllers" do
191
+
192
+ before do
193
+ stubs(:controller_name).returns("with_underscore")
194
+ stubs(:params).returns(:action => :and_underscore)
195
+ end
196
+
197
+ it "should camelize controller and action names" do
198
+ snippr_with_path(:a_snippet).should == "<!-- starting snippr: withUnderscore/andUnderscore/aSnippet -->\nan underscored snippet with param {param}\n<!-- closing snippr: withUnderscore/andUnderscore/aSnippet -->"
199
+ end
200
+
201
+ it "works with a given block" do
202
+ lambda {
203
+ snippr_with_path(:a_snippet) do |snippr|
204
+ snippr.should be_html_safe
205
+ raise StandardError.new('block should be called')
206
+ end
207
+ }.should raise_error StandardError, 'block should be called'
208
+ end
209
+
210
+ it "works with parameters hash" do
211
+ snippr_with_path(:a_snippet, :param => "value").should == "<!-- starting snippr: withUnderscore/andUnderscore/aSnippet -->\nan underscored snippet with param value\n<!-- closing snippr: withUnderscore/andUnderscore/aSnippet -->"
212
+ end
213
+
214
+ it "should allow multiple arguments" do
215
+ snippr_with_path(:deeper, :nested, :snippet).should == "<!-- missing snippr: withUnderscore/andUnderscore/deeper/nested/snippet -->"
216
+ end
217
+ end
218
+
219
+ end
220
+
221
+ end