### # Copyright 2013-2014 Simon Lydell # X11 (“MIT”) Licensed. (See LICENSE.) ### render = require("../index") equal = (expected, fragment)-> container = document.createElement("div") container.appendChild(fragment) actual = container.innerHTML return if expected == actual console.dir {expected, actual} throw new Error "Expected `#{actual}` to equal `#{expected}`" suite "render", -> suite "elements", -> test "create", -> equal '

', render -> @p equal '
', render -> @br equal '


', render -> @p @br @p test "underscores to hyphens", -> equal '', render -> @x_foo_bar suite "attributes", -> test "add classes", -> equal '

', render -> @p.class equal '

', render -> @p.a.b.c test "underscores to hyphens", -> equal '

', render -> @p.a_b_c test "add attributes", -> equal '

', render -> @p(attribute: "value") equal '

', render -> @p attribute: "value" equal '

', render -> @p(str: "a", num: 1, arr: ["a", 1], obj: {}) equal '

', render -> @p str: "a", num: 1, arr: ["a", 1], obj: {} test "boolean attributes", -> equal '

', render -> @p(boolean: true) equal '

', render -> @p(boolean: false) test "data attributes", -> equal '

', render -> @p(data: {string: "a"}) equal '

', render -> @p(data: {number: 1}) equal '

', render -> @p(data: {null: null}) equal '

', render -> @p(data: {true: true}) equal '

', render -> @p(data: {false: false}) equal '

', render -> @p(data: {array: ["a", 1]}) equal '

', render -> @p(data: {obj: {key: "value"}}) equal '

', render -> @p(data: {a: 1, b: 2, c: 3}) test "no duplication", -> equal '

', render -> @p(a: 1)(a: 1) suite "class", -> test "whitespace", -> equal '

', render -> @p(class: " a b c ") test "array", -> equal '

', render -> @p(class: ["a", "b", "c"]) test "concatenation", -> equal '

', render -> @p.a.b(class: "c d")(class: ["e", "f"]).g test "no duplication", -> equal '

', render -> @p.a.a(class: "a b")(class: ["b", "b"]).b test "not underscores to hyphens", -> equal '

', render -> @p(class: "a_b_c") equal '

', render -> @p(class: "block__element--modifier") test "dynamic attributes", -> attributes = {a: 1} equal '

', render -> @p(attributes) test "mix classes and attributes", -> equal '

', render -> @p.a.b(c: 1, d: 2, class: "b c").d(d: 3)(e: 4) suite "content", -> test "text", -> equal '

text

', render -> @p "text" equal '

1

', render -> @p 1 equal '

text,1

', render -> @p ["text", 1] test "block", -> equal '

foobar

', render -> @p -> @em "foo" @strong "bar" test "element", -> equal '
foo(bar);
', render -> @pre @code """ foo(bar); """ test "text with inline elements", -> equal '

text link text

', render -> @p "text ", (@a "link"), " te", (@em @strong "xt") test "escaping", -> userInput = "

" equal '

</p> &gt; </p>

', render -> @p "

> #{userInput}" test "both attributes and content", -> equal '

text

', render -> @p.class "text" equal '

text

', render -> @p(attribute: "value") "text" equal '

text

', render -> @p attribute: "value", "text" test "plain text", -> equal 'text', render -> @ "text" test "locals", -> locals = foo: "bar" template = (locals)-> @p locals.foo equal '

bar

', render template, locals test "includes", -> locals = foo: "bar" template = (locals)-> @ require "test/include" equal '

Include with bar

', render template, locals suite "mixins", -> test "inline", -> equal '

foo bar

end

', render -> mixin = (attributes, arg, block)=> @p(attributes) "foo #{arg}" @ block mixin id: "foo", "bar", -> @p "end" test "`require`d (explicit `.call`)", -> equal '

Mixined foo

', render -> mixin = require("test/mixin") @div -> mixin.call @, "foo" test "`require`d (`@`-assigned)", -> equal '

Mixined foo

', render -> @mixin = require("test/mixin") @div -> @mixin "foo" suite "custom document", -> class Document constructor: -> @calls = 0 createDocumentFragment: -> {appendChild: => @calls++} createElement: -> @calls++ assertCalls: (calls)-> unless @calls == calls throw new Error "Expected `render` to take a custom `document`." test "simple", -> doc = new Document render doc, -> @p doc.assertCalls(2) test "bind", -> doc = new Document localRender = render.bind(undefined, doc) localRender -> @p doc.assertCalls(2) test "locals", -> locals = foo: "bar" template = (locals)-> @p locals.foo equal '

bar

', render document, template, locals suite "parent reference", -> test "insert unescaped HTML", -> equal '

outside

foo


', render -> @p -> @ "foo" @$.insertAdjacentHTML("beforebegin", '

outside

') @$.innerHTML += '
'