Permalink
Browse files

Add Jasmine specs for the hamlcoffee helpers.

Interestingly whether `preserve` nor `findAndPreserve` have ever
worked as intended.
  • Loading branch information...
1 parent f583a27 commit c9e63507d671136f01e48be3521ecd8b097c95af @netzpirat netzpirat committed May 18, 2012
View
2 .travis.yml
@@ -7,6 +7,8 @@ rvm:
branches:
only:
- master
+script: 'rake spec'
+after_script: 'bundle exec guard-jasmine'
notifications:
recipients:
- michi@netzpiraten.ch
View
31 Gemfile
@@ -14,3 +14,34 @@ end
platform :jruby do
gem 'therubyrhino'
end
+
+group :assets do
+ gem 'coffee-script'
+end
+
+group :development, :test do
+ gem 'actionpack', '~> 3.2'
+ gem 'railties', '~> 3.2'
+ gem 'tzinfo'
+
+ gem 'thin'
+ gem 'jasminerice'
+ gem 'jquery-rails'
+
+ gem 'guard-jasmine'
+ gem 'guard-rspec'
+
+ gem 'rake'
+ gem 'pry'
+
+ gem 'jasminerice'
+ gem 'rspec'
+
+ gem 'guard-rspec'
+ gem 'guard-jasmine'
+ gem 'rb-fsevent'
+ gem 'ruby_gntp'
+
+ gem 'yard'
+ gem 'redcarpet'
+end
View
6 Guardfile
@@ -1,5 +1,11 @@
# coding: UTF-8
+guard :jasmine, :server => :thin do
+ watch(%r{spec/javascripts/spec\.(js\.coffee|js|coffee)$}) { "spec/javascripts" }
+ watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
+ watch(%r{vendor/assets/javascripts/(.+?)\.(js\.coffee\.erb|js\.coffee|js|coffee)$}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
+end
+
guard 'rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{ m[1] }_spec.rb" }
View
4 README.md
@@ -355,9 +355,9 @@ You can see the [default implementation](https://github.com/netzpirat/haml_coffe
and the [Haml Coffee documentation](https://github.com/9elements/haml-coffee#custom-helper-function-compiler-options)
for more information about each helper function.
-## Shameless self promotion
+## Author
-Developed by Michael Kessler, sponsored by [mksoft.ch](https://mksoft.ch).
+Developed by Michael Kessler, [mksoft.ch](https://mksoft.ch).
If you like Haml Coffee Assets, you can watch the repository at [GitHub](https://github.com/netzpirat/haml_coffee_assets) and
follow [@netzpirat](https://twitter.com/#!/netzpirat) on Twitter for project updates.
View
21 config.ru
@@ -0,0 +1,21 @@
+require 'rails'
+require 'rails/all'
+require 'jasminerice'
+require 'sprockets/railtie'
+require 'jquery-rails'
+require 'haml_coffee_assets'
+
+class HamlCoffeeAssetsJasmineTest < Rails::Application
+ routes.append do
+ mount Jasminerice::Engine => '/jasmine'
+ end
+
+ config.cache_classes = true
+ config.active_support.deprecation = :log
+ config.assets.enabled = true
+ config.assets.version = '1.0'
+ config.secret_token = '9696be98e32a5f213730cb7ed6161c79'
+end
+
+HamlCoffeeAssetsJasmineTest.initialize!
+run HamlCoffeeAssetsJasmineTest
View
10 haml_coffee_assets.gemspec
@@ -19,14 +19,6 @@ Gem::Specification.new do |s|
s.add_runtime_dependency "execjs", ">= 1.2.9"
s.add_runtime_dependency "tilt", ">= 1.3.3"
s.add_runtime_dependency "sprockets", ">= 2.0.3"
-
+
s.add_development_dependency 'bundler'
-
- s.add_development_dependency 'railties', '>= 3.1'
- s.add_development_dependency 'rspec'
- s.add_development_dependency 'guard-rspec'
- s.add_development_dependency 'yard'
- s.add_development_dependency 'redcarpet'
- s.add_development_dependency 'pry'
- s.add_development_dependency 'rake'
end
View
2 lib/haml_coffee_assets/engine.rb
@@ -18,7 +18,7 @@ class Engine < Rails::Engine
:cleanValue => true,
:customHtmlEscape => 'HAML.escape',
:customCleanValue => 'HAML.cleanValue',
- :customPreserve => 'HAML.escape',
+ :customPreserve => 'HAML.preserve',
:customFindAndPreserve => 'HAML.findAndPreserve',
:customSurround => 'HAML.surround',
:customSucceed => 'HAML.succeed',
View
101 spec/javascripts/hamlcoffee_spec.js.coffee
@@ -0,0 +1,101 @@
+describe 'HAML', ->
+ describe '.escape', ->
+ it 'escapes an ampersand', ->
+ expect(HAML.escape('house & garden')).toEqual 'house &amp; garden'
+
+ it 'escapes the less than sign', ->
+ expect(HAML.escape('money < fun')).toEqual 'money &lt; fun'
+
+ it 'escapes the greater than sign', ->
+ expect(HAML.escape('love > hate')).toEqual 'love &gt; hate'
+
+ it 'escapes a double quote', ->
+ expect(HAML.escape('Michael "netzpirat" Kessler')).toEqual 'Michael &quot;netzpirat&quot; Kessler'
+
+ describe '.cleanValue', ->
+ it 'returns an empty string for null', ->
+ expect(HAML.cleanValue(null)).toEqual ''
+
+ it 'returns an empty string for undefined', ->
+ expect(HAML.cleanValue(undefined)).toEqual ''
+
+ it 'passes everything else unchanged', ->
+ expect(HAML.cleanValue('Still the same')).toEqual 'Still the same'
+
+ describe '.extend', ->
+ it 'extends the given object from a source object', ->
+ object = HAML.extend {}, { a: 1, b: 2 }
+
+ expect(object.a).toEqual 1
+ expect(object.b).toEqual 2
+
+ it 'extends the given object from multipe source objects', ->
+ object = HAML.extend {}, { a: 1 }, { b: 2 }, { c: 3, d: 4 }
+
+ expect(object.a).toEqual 1
+ expect(object.b).toEqual 2
+ expect(object.c).toEqual 3
+ expect(object.d).toEqual 4
+
+ it 'overwrites existing properties', ->
+ object = HAML.extend {}, { a: 1 }, { b: 2 }, { a: 2, b: 4 }
+
+ expect(object.a).toEqual 2
+ expect(object.b).toEqual 4
+
+ describe '.globals', ->
+ it 'retuns the global object', ->
+ expect(HAML.globals).toEqual Object(HAML.globals)
+
+ it 'returns an empty object', ->
+ expect(Object.keys(HAML.globals).length).toEqual 0
+
+ describe '.context', ->
+ it 'merges the locals into the globals', ->
+ spyOn(HAML, 'globals').andCallFake -> { b: 2, d: 4 }
+ context = HAML.context({ a: 1, c: 3 })
+
+ expect(context.a).toEqual 1
+ expect(context.b).toEqual 2
+ expect(context.c).toEqual 3
+ expect(context.d).toEqual 4
+
+ describe '.preserve', ->
+ it 'preserves all newlines', ->
+ expect(HAML.preserve("Newlines\nall\nthe\nway\n")).toEqual "Newlines&#x000A;all&#x000A;the&#x000A;way&#x000A;"
+
+ describe '.findAndPreserve', ->
+ it 'replaces newlines within the preserved tags', ->
+ expect(HAML.findAndPreserve("""
+ <pre>
+ This will
+ be preserved
+ </pre>
+ <p>
+ This will not
+ be preserved
+ </p>
+ <textarea>
+ This will
+ be preserved
+ </textarea>
+ """)).toEqual """
+ <pre>&#x000A; This will&#x000A; be preserved&#x000A;</pre>
+ <p>
+ This will not
+ be preserved
+ </p>
+ <textarea>&#x000A; This will&#x000A; be preserved&#x000A;</textarea>
+ """
+
+ describe '.surround', ->
+ it 'surrounds the text to the function result', ->
+ expect(HAML.surround('Prefix', 'Suffix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>Suffix'
+
+ describe '.succeed', ->
+ it 'appends the text to the function result', ->
+ expect(HAML.succeed('Suffix', -> '<p>text</p>')).toEqual '<p>text</p>Suffix'
+
+ describe '.precede', ->
+ it 'prepends the text to the function result', ->
+ expect(HAML.precede('Prefix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>'
View
3 spec/javascripts/spec.js.coffee
@@ -0,0 +1,3 @@
+#= require hamlcoffee
+#= require jquery
+#= require_tree .
View
215 vendor/assets/javascripts/hamlcoffee.js.coffee.erb
@@ -2,120 +2,101 @@
# HAML Coffee namespace
#
-window.HAML ||= {}
-
-# HAML Coffee html escape function
-#
-# @param text [String] the text to escape
-# @return [String] the escaped text
-#
-window.HAML.escape ||= (text) ->
- ("" + text)
- .replace(/&/g, "&amp;")
- .replace(/</g, "&lt;")
- .replace(/>/g, "&gt;")
- .replace(/"/g, "&quot;")
-
-# HAML Coffee clean value function
-#
-# @param text [String] the text to be cleaned
-# @return [String] the cleaned text
-#
-window.HAML.cleanValue ||= (text) ->
- if text is null or text is undefined then '' else text
-
-# HAML Coffee extend function.
-#
-# This will reuse the extend function from either:
-#
-# - jQuery
-# - Underscore.js
-# - Prototype
-# - MooTools
-# - Zepto.js
-#
-# You can assign a custom extend function if your framework
-# is not supported out of the box.
-#
-window.HAML.extend ||= (globals, locals) ->
- if jQuery?.extend
- jQuery.extend {}, globals, locals
-
- else if _?.extend
- _.extend {}, globals, locals
-
- else if Zepto?.extend
- Zepto.extend(Zepto.extend({}, globals), locals)
-
- else if Object.append
- Object.append(Object.append({}, globals), locals)
-
- else if Object.extend
- Object.extend(Object.extend({}, globals), locals)
-
- else
- locals
-
-# HAML Coffee global template context.
-#
-# @return [Object] the global template context
-#
-window.HAML.globals ||= -> {}
-
-# Get the HAML template context. This merges the local
-# and the global template context into a new context.
-#
-# @param locals [Object] the local template context
-# @return [Object] the merged context
-#
-window.HAML.context ||= (locals) ->
- HAML.extend(HAML.globals(), locals)
-
-# Preserve newlines in the text
-#
-# @param text [String] the text to preserve
-# @return [String] the preserved text
-#
-window.HAML.preserve ||= (text) ->
- text.replace /\\n/g, '&#x000A;'
-
-# Find and preserve newlines in the preserving tags
-#
-# @param text [String] the text to preserve
-# @return [String] the preserved text
-#
-window.HAML.findAndPreserve ||= (text) ->
- tags = '<%= configuration.preserveTags %>'.split(',').join('|')
- text.replace ///<(#{ tags })>([^]*?)<\/\1>///g, (str, tag, content) ->
- "<#{ tag }>#{ <%= configuration.customPreserve %>(content) }</#{ tag }>"
-
-# The surround helper surrounds the function output
-# with the start and end string without whitespace in between.
-#
-# @param [String] start the text to prepend to the text
-# @param [String] end the text to append to the text
-# @param [Function] fn the text generating function
-# @return [String] the surrounded text
-#
-window.HAML.surround ||= (start, end, fn) ->
- start + fn() + end
-
-# The succeed helper appends text to the function output
-# without whitespace in between.
-#
-# @param [String] end the text to append to the text
-# @param [Function] fn the text generating function
-# @return [String] the succeeded text
-#
-window.HAML.succeed ||= (end, fn) ->
- fn() + end
-
-# The precede helper prepends text to the function output
-# without whitespace in between.
-#
-# @param [String] start the text to prepend to the text
-# @param [Function] fn the text generating function
-# @return [String] the preeceded text
-#
-window.HAML.precede ||= (start, fn) ->
- start + fn()
+class window.HAML
+
+ # HAML Coffee html escape function
+ #
+ # @param text [String] the text to escape
+ # @return [String] the escaped text
+ #
+ @escape: (text) ->
+ ("" + text)
+ .replace(/&/g, "&amp;")
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;")
+ .replace(/"/g, "&quot;")
+
+ # HAML Coffee clean value function
+ #
+ # @param text [String] the text to be cleaned
+ # @return [String] the cleaned text
+ #
+ @cleanValue: (text) ->
+ if text is null or text is undefined then '' else text
+
+ # Extends an object with the properties from
+ # the source objects.
+ #
+ # @param [Object] obj the object to extended
+ # @param [Object] sources the objects to copy from
+ # @return [Object] the extended object
+ #
+ @extend: (obj, sources...) ->
+ for source in sources
+ obj[key] = val for key, val of source
+
+ obj
+
+ # HAML Coffee global template context.
+ #
+ # @return [Object] the global template context
+ #
+ @globals: -> {}
+
+ # Get the HAML template context. This merges the local
+ # and the global template context into a new context.
+ #
+ # @param locals [Object] the local template context
+ # @return [Object] the merged context
+ #
+ @context: (locals) ->
+ @extend {}, HAML.globals(), locals
+
+ # Preserve newlines in the text
+ #
+ # @param text [String] the text to preserve
+ # @return [String] the preserved text
+ #
+ @preserve: (text) ->
+ text.replace /\n/g, '&#x000A;'
+
+ # Find and preserve newlines in the preserving tags
+ #
+ # @param text [String] the text to preserve
+ # @return [String] the preserved text
+ #
+ @findAndPreserve: (text) ->
+ tags = '<%= configuration.preserveTags %>'.split(',').join('|')
+ text = text.replace ///<(#{ tags })>([^]*?)<\/\1>///g, (str, tag, content) ->
+ "<#{ tag }>#{ <%= configuration.customPreserve %>(content) }</#{ tag }>"
+
+ # The surround helper surrounds the function output
+ # with the start and end string without whitespace in between.
+ #
+ # @param [String] start the text to prepend to the text
+ # @param [String] end the text to append to the text
+ # @param [Function] fn the text generating function
+ # @return [String] the surrounded text
+ #
+ @surround: (start, end, fn) ->
+ start + fn() + end
+
+ # The succeed helper appends text to the function output
+ # without whitespace in between.
+ #
+ # @param [String] end the text to append to the text
+ # @param [Function] fn the text generating function
+ # @return [String] the succeeded text
+ #
+ @succeed: (end, fn) ->
+ fn() + end
+
+ # The precede helper prepends text to the function output
+ # without whitespace in between.
+ #
+ # @param [String] start the text to prepend to the text
+ # @param [Function] fn the text generating function
+ # @return [String] the preeceded text
+ #
+ @precede: (start, fn) ->
+ start + fn()

0 comments on commit c9e6350

Please sign in to comment.