Skip to content
This repository was archived by the owner on Jan 24, 2022. It is now read-only.

Commit

Permalink
tests and fix for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
piranha committed Apr 1, 2013
1 parent 9124a21 commit 9bedb46
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 5 deletions.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
COFFEE = ./node_modules/.bin/coffee

SRC = $(wildcard src/*.coffee)
DST = $(SRC:src/%.coffee=lib/%.js)


all: compile


compile: $(DST)

test: compile
node_modules/.bin/nodeunit test

lib/%.js: src/%.coffee
@mkdir -p $(@D)
$(COFFEE) -bpc $< > $@
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leco",
"description": "Le Embedded CoffeeScript templates",
"author": "Instant Communication Ltd",
"version": "1.0.1",
"version": "1.0.2",
"licenses": {
"type": "ISC",
"url": "http://github.com/piranha/leco/raw/master/LICENSE"
Expand All @@ -12,8 +12,11 @@
"coffee-script": "1.6.2",
"underscore": "1.4.4"
},
"devDependencies" {
"nodeunit": "0.8.0"
},
"scripts": {
"prepublish": "coffee -o lib -c src/*.coffee"
"prepublish": "make"
},
"bin": "./bin/leco",
"main": "./lib/leco.js"
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ exports.safe = (value) ->
return ''
if value.templateSafe
return value
value = value.toString()
value = new String(value.toString())
value.templateSafe = true
return value
11 changes: 9 additions & 2 deletions src/transformer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ processCode = (value, writer) ->
if value == 'end'
return ['', false]
if value.match /(-|=)>/
value = value.replace(/(->|=>)$/, '((out) $1')
# generate a function, so it looks like this:
# (arg) =>
# ((out) =>
# out.push "some template with arg"
# out
# )([]).join("")
# This can be used as a callback with helpers
value = value.replace(/(->|=>)$/, '$1 ((out) $1')
return [value, 'out)([]).join("")']
else if value[value.length - 1] == ':'
return [value.slice(0, value.length - 1), true]
Expand All @@ -93,7 +100,7 @@ getDefaultTokenMap = (helpersName) ->
['out.push ' + JSON.stringify(value), false]
out: (value, writer) ->
[value, indent] = processCode(value, writer)
["out.push #{helpersName}.safe #{value}", indent]
["out.push #{value}", indent]
escape: (value, writer) ->
[value, indent] = processCode(value, writer)
["out.push #{helpersName}.escape #{value}", indent]
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/capture.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% renderItem = (item) -> %>
<div class="item">
<span class="name"><%= item.name %></span>
<span class="price">$<%= item.price %></span>
</div>
<% end %>

<% for item in @items: %>
<%= renderItem item %>
<% end %>
17 changes: 17 additions & 0 deletions test/fixtures/capture.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@




<div class="item">
<span class="name">Caprese</span>
<span class="price">$5.25</span>
</div>



<div class="item">
<span class="name">Artichoke</span>
<span class="price">$6.25</span>
</div>


8 changes: 8 additions & 0 deletions test/fixtures/hello.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ctx) ->
((out) ->
out.push "Hello, "
out.push helpers.escape @name
out.push ".\nI'M SHOUTING AT YOU, "
out.push helpers.escape @name.toUpperCase()
out.push "!\n"
out).call(ctx, []).join("")
2 changes: 2 additions & 0 deletions test/fixtures/hello.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello, <%= @name %>.
I'M SHOUTING AT YOU, <%= @name.toUpperCase() %>!
2 changes: 2 additions & 0 deletions test/fixtures/hello.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello, Sam.
I'M SHOUTING AT YOU, SAM!
1 change: 1 addition & 0 deletions test/fixtures/hello.tok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[["literal","Hello, "],["escape"," @name "],["literal",".\nI'M SHOUTING AT YOU, "],["escape"," @name.toUpperCase() "],["literal","!\n"]]
19 changes: 19 additions & 0 deletions test/fixtures/helpers.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ctx) ->
((out) ->
for item in @items
out.push "\n "
out.push helpers.escape @contentTag "div", class: "item", => ((out) =>
out.push "\n "
out.push helpers.escape @contentTag "span", class: "price", -> ((out) ->
out.push "$"
out.push helpers.escape item.price
out)([]).join("")
out.push "\n "
out.push helpers.escape @contentTag "span", class: "name", -> ((out) ->
out.push helpers.escape item.name
out)([]).join("")
out.push "\n "
out)([]).join("")
out.push "\n"
out.push "\n"
out).call(ctx, []).join("")
6 changes: 6 additions & 0 deletions test/fixtures/helpers.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% for item in @items: %>
<%= @contentTag "div", class: "item", => %>
<%= @contentTag "span", class: "price", -> %>$<%= item.price %><% end %>
<%= @contentTag "span", class: "name", -> %><%= item.name %><% end %>
<% end %>
<% end %>
11 changes: 11 additions & 0 deletions test/fixtures/helpers.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

<div class="item">
<span class="price">$5.25</span>
<span class="name">Caprese</span>
</div>

<div class="item">
<span class="price">$6.25</span>
<span class="name">Artichoke</span>
</div>

1 change: 1 addition & 0 deletions test/fixtures/helpers.tok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[["literal",""],["code"," for item in @items: "],["literal","\n "],["escape"," @contentTag \"div\", class: \"item\", => "],["literal","\n "],["escape"," @contentTag \"span\", class: \"price\", -> "],["literal","$"],["escape"," item.price "],["literal",""],["code"," end "],["literal","\n "],["escape"," @contentTag \"span\", class: \"name\", -> "],["literal",""],["escape"," item.name "],["literal",""],["code"," end "],["literal","\n "],["code"," end "],["literal","\n"],["code"," end "],["literal","\n"]]
17 changes: 17 additions & 0 deletions test/fixtures/projects.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ctx) ->
((out) ->
if @projects.length
out.push "\n "
for project in @projects
out.push "\n <a href=\""
out.push helpers.escape project.url
out.push "\">"
out.push helpers.escape project.name
out.push "</a>\n "
out.push project.description
out.push "\n "
out.push "\n"
else
out.push "\n No projects\n"
out.push "\n"
out).call(ctx, []).join("")
8 changes: 8 additions & 0 deletions test/fixtures/projects.eco
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% if @projects.length: %>
<% for project in @projects: %>
<a href="<%= project.url %>"><%= project.name %></a>
<%- project.description %>
<% end %>
<% else: %>
No projects
<% end %>
12 changes: 12 additions & 0 deletions test/fixtures/projects.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


<a href="/projects/1">PowerTMS Active Shipments Page Redesign</a>


<a href="/projects/2">SCU Intranet</a>
<p><em>On hold</em></p>

<a href="/projects/3">Sales Template</a>



1 change: 1 addition & 0 deletions test/fixtures/projects.tok
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[["literal",""],["code"," if @projects.length: "],["literal","\n "],["code"," for project in @projects: "],["literal","\n <a href=\""],["escape"," project.url "],["literal","\">"],["escape"," project.name "],["literal","</a>\n "],["out"," project.description "],["literal","\n "],["code"," end "],["literal","\n"],["code"," else: "],["literal","\n No projects\n"],["code"," end "],["literal","\n"]]
39 changes: 39 additions & 0 deletions test/test_render.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{tokenize, transform, helpers} = require '../lib/leco'
fs = require 'fs'
CoffeeScript = require 'coffee-script'


read = (x) -> fs.readFileSync(__dirname + '/fixtures/' + x).toString()
f = (x) -> read(x + '.out')
t = (x, ctx) ->
tokens = tokenize(read(x + '.eco'))
tpl = transform(tokens, {}, helpers)
CoffeeScript.eval(tpl)(ctx)


module.exports =
'transforming fixtures/hello.eco': (test) ->
test.deepEqual f('hello'), t('hello', name: 'Sam')
test.done()

'transforming fixtures/projects.eco': (test) ->
result = t('projects', projects: [
{ name: "PowerTMS Active Shipments Page Redesign", url: "/projects/1" },
{ name: "SCU Intranet", url: "/projects/2", description: "<p><em>On hold</em></p>" },
{ name: "Sales Template", url: "/projects/3" }
])
test.deepEqual f('projects'), result
test.done()

'transforming fixtures/helpers.eco': (test) ->
result = t('helpers',
items: [
{ name: "Caprese", price: "5.25"},
{ name: "Artichoke", price: "6.25" }
]
contentTag: (tagName, attributes, callback) ->
attrs = (" #{name}=\"#{value}\"" for name, value of attributes)
helpers.safe "<#{tagName}#{attrs.join("")}>#{callback()}</#{tagName}>"
)
test.deepEqual f('helpers'), result
test.done()
19 changes: 19 additions & 0 deletions test/test_tokenizer.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{tokenize} = require '../lib/leco'
fs = require 'fs'

read = (x) -> fs.readFileSync(__dirname + '/fixtures/' + x).toString()
f = (x) -> JSON.parse(read(x + '.tok'))
t = (x) -> tokenize(read(x + '.eco'))

module.exports =
'tokenizing fixtures/hello.eco': (test) ->
test.deepEqual f('hello'), t('hello')
test.done()

'tokenizing fixtures/projects.eco': (test) ->
test.deepEqual f('projects'), t('projects')
test.done()

'tokenizing fixtures/helpers.eco': (test) ->
test.deepEqual f('helpers'), t('helpers')
test.done()
19 changes: 19 additions & 0 deletions test/test_transformer.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{tokenize, transform} = require '../lib/leco'
fs = require 'fs'

read = (x) -> fs.readFileSync(__dirname + '/fixtures/' + x).toString()
f = (x) -> read(x + '.coffee')
t = (x) -> transform(tokenize(read(x + '.eco')))

module.exports =
'transforming fixtures/hello.eco': (test) ->
test.deepEqual f('hello'), t('hello')
test.done()

'transforming fixtures/projects.eco': (test) ->
test.deepEqual f('projects'), t('projects')
test.done()

'transforming fixtures/helpers.eco': (test) ->
test.deepEqual f('helpers'), t('helpers')
test.done()

0 comments on commit 9bedb46

Please sign in to comment.