cerubis 0.0.1

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 (75) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +14 -0
  3. data/Gemfile +15 -0
  4. data/README.md +111 -0
  5. data/Rakefile +15 -0
  6. data/TODO.md +31 -0
  7. data/cerubis.gemspec +22 -0
  8. data/examples/blocks.rb +20 -0
  9. data/examples/full/blocks/script_block.rb +8 -0
  10. data/examples/full/config.rb +14 -0
  11. data/examples/full/example.rb +25 -0
  12. data/examples/full/helpers/html_helpers.rb +13 -0
  13. data/examples/full/helpers/include_helper.rb +15 -0
  14. data/examples/full/models/page.rb +17 -0
  15. data/examples/full/models/site.rb +13 -0
  16. data/examples/full/templates/_footer.cerubis +1 -0
  17. data/examples/full/templates/_header.cerubis +1 -0
  18. data/examples/full/templates/main.cerubis +31 -0
  19. data/examples/helpers.rb +22 -0
  20. data/examples/html.rb +18 -0
  21. data/examples/variables.rb +10 -0
  22. data/lib/cerubis.rb +55 -0
  23. data/lib/cerubis/block.rb +27 -0
  24. data/lib/cerubis/block_node.rb +37 -0
  25. data/lib/cerubis/blocks/if.rb +8 -0
  26. data/lib/cerubis/blocks/loop.rb +20 -0
  27. data/lib/cerubis/blocks/unless.rb +9 -0
  28. data/lib/cerubis/condition.rb +59 -0
  29. data/lib/cerubis/context.rb +30 -0
  30. data/lib/cerubis/helper.rb +12 -0
  31. data/lib/cerubis/matcher.rb +19 -0
  32. data/lib/cerubis/method.rb +19 -0
  33. data/lib/cerubis/node.rb +27 -0
  34. data/lib/cerubis/objects/array.rb +4 -0
  35. data/lib/cerubis/objects/fixnum.rb +4 -0
  36. data/lib/cerubis/objects/float.rb +4 -0
  37. data/lib/cerubis/objects/hash.rb +4 -0
  38. data/lib/cerubis/objects/string.rb +4 -0
  39. data/lib/cerubis/parser.rb +125 -0
  40. data/lib/cerubis/syntax_error.rb +4 -0
  41. data/lib/cerubis/template.rb +21 -0
  42. data/lib/cerubis/text_node.rb +10 -0
  43. data/lib/cerubis/variable_replacement.rb +34 -0
  44. data/lib/cerubis/version.rb +3 -0
  45. data/test/all.rb +3 -0
  46. data/test/cerubis/block_node_test.rb +36 -0
  47. data/test/cerubis/blocks/if_test.rb +24 -0
  48. data/test/cerubis/blocks/loop_test.rb +25 -0
  49. data/test/cerubis/blocks/unless_test.rb +25 -0
  50. data/test/cerubis/condition_test.rb +142 -0
  51. data/test/cerubis/context_test.rb +33 -0
  52. data/test/cerubis/helper_test.rb +17 -0
  53. data/test/cerubis/matcher_test.rb +20 -0
  54. data/test/cerubis/method_test.rb +60 -0
  55. data/test/cerubis/parser_test.rb +48 -0
  56. data/test/cerubis/template_test.rb +38 -0
  57. data/test/cerubis/text_node_test.rb +16 -0
  58. data/test/cerubis_test.rb +31 -0
  59. data/test/matchers/test_block_name.rb +25 -0
  60. data/test/matchers/test_close_block.rb +25 -0
  61. data/test/matchers/test_conditions.rb +21 -0
  62. data/test/matchers/test_helpers.rb +21 -0
  63. data/test/matchers/test_object_method.rb +37 -0
  64. data/test/matchers/test_open_block.rb +57 -0
  65. data/test/matchers/test_operators.rb +29 -0
  66. data/test/matchers/test_variable.rb +37 -0
  67. data/test/methods/test_array_methods.rb +21 -0
  68. data/test/methods/test_fixnum_methods.rb +6 -0
  69. data/test/methods/test_float_methods.rb +6 -0
  70. data/test/methods/test_hash_methods.rb +11 -0
  71. data/test/methods/test_string_methods.rb +11 -0
  72. data/test/nodes/test_node_defaults.rb +30 -0
  73. data/test/rendered_test.rb +159 -0
  74. data/test/test_helper.rb +34 -0
  75. metadata +149 -0
@@ -0,0 +1,48 @@
1
+ require 'test_helper'
2
+
3
+ class Cerubis::ParserTest < MiniTest::Unit::TestCase
4
+ def test_parse_one_block
5
+ content = <<-STR
6
+ <body>
7
+ {{#if true}}
8
+ Foo Content
9
+ {{/if}}
10
+ </body>
11
+ STR
12
+
13
+ nodes = Cerubis::Parser.new(content, options).nodes
14
+ assert_equal '<body>', nodes[0].render.strip
15
+ assert_instance_of Cerubis::TextNode, nodes[0]
16
+
17
+ assert_equal 'Foo Content', nodes[1].render.strip
18
+ assert_instance_of Cerubis::BlockNode, nodes[1]
19
+
20
+ assert_equal '</body>', nodes[2].render.strip
21
+ assert_instance_of Cerubis::TextNode, nodes[2]
22
+ end
23
+
24
+ def test_open_close_block_on_single_line
25
+ content = '<p>{{#if true}} Foo Content {{/if}}</p>'
26
+ nodes = Cerubis::Parser.new(content, options).nodes
27
+ assert_equal '<p> Foo Content </p>', nodes.map(&:render).join.strip
28
+ end
29
+
30
+ def test_exception_raised_if_closing_block_is_found_without_an_opening
31
+ assert_raises(Cerubis::SyntaxError) do
32
+ content = '{{/if}}'
33
+ Cerubis::Parser.new(content, options)
34
+ end
35
+ end
36
+
37
+ def test_exception_raised_if_open_block_is_found_without_a_closing
38
+ assert_raises(Cerubis::SyntaxError) do
39
+ content = '{{#if true}}'
40
+ Cerubis::Parser.new(content, options)
41
+ end
42
+ end
43
+
44
+ private
45
+ def options
46
+ { :parent => StubObject.new }
47
+ end
48
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class Cerubis::TemplateTest < MiniTest::Unit::TestCase
4
+ def test_returns_template_content
5
+ content = 'template content'
6
+ template = Cerubis::Template.new(content)
7
+
8
+ assert_equal content, template.content
9
+ end
10
+
11
+ def test_returns_template_context
12
+ context = { :foo => true, :bar => false}
13
+ template = Cerubis::Template.new('', context)
14
+
15
+ assert_equal context[:foo], template.context[:foo]
16
+ assert_equal context[:bar], template.context[:bar]
17
+ assert_instance_of Cerubis::Context, template.context
18
+ end
19
+
20
+ def test_node_stucture
21
+ template = Cerubis::Template.new(content)
22
+
23
+ assert_equal '<body>', template.nodes[0].render.strip
24
+ assert_equal '<section>Foo Content</section>', template.nodes[1].render.strip
25
+ assert_equal '</body>', template.nodes[2].render.strip
26
+ end
27
+
28
+ private
29
+ def content
30
+ <<-STR
31
+ <body>
32
+ {{#if true}}
33
+ <section>Foo Content</section>
34
+ {{/if}}
35
+ </body>
36
+ STR
37
+ end
38
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+ require 'nodes/test_node_defaults'
3
+
4
+ class Cerubis::TextNodeTest < MiniTest::Unit::TestCase
5
+ include TestNodeDefaults
6
+
7
+ def test_render_returns_content_with_variables_replaced
8
+ node = Cerubis::TextNode.new(content, options)
9
+ assert_equal '<h1>Foo Title</h1>', node.render
10
+ end
11
+
12
+ private
13
+ def class_name
14
+ Cerubis::TextNode
15
+ end
16
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ class CerubisTest < MiniTest::Unit::TestCase
4
+ def test_render_returns_a_template
5
+ assert_instance_of Cerubis::Template, Cerubis.render('')
6
+ end
7
+
8
+ def test_blocks_returns_block_collection
9
+ assert_instance_of Hash, Cerubis.blocks
10
+ end
11
+
12
+ def test_helpers_returns_array
13
+ assert_instance_of Hash, Cerubis.helpers
14
+ end
15
+
16
+ def test_helpers_contains_single_registered_helper
17
+ Cerubis.register_helper :foo_helper, FooMod
18
+ assert_equal FooMod, Cerubis.helpers[:foo_helper]
19
+ end
20
+
21
+ def test_helpers_contains_multiple_registered_helpers
22
+ Cerubis.register_helper :foo_helper, :bar_helper, FooMod
23
+ assert_equal FooMod, Cerubis.helpers[:foo_helper]
24
+ assert_equal FooMod, Cerubis.helpers[:bar_helper]
25
+ end
26
+
27
+ module FooMod
28
+ def foo_helper; end
29
+ def bar_helper; end
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ module TestBlockName
2
+ def test_block_names_have_letters
3
+ assert_match 'if', Regexp.new(Cerubis::Matcher::BlockName)
4
+ end
5
+
6
+ def test_block_names_have_underscores
7
+ assert_match 'if_block', Regexp.new(Cerubis::Matcher::BlockName)
8
+ end
9
+
10
+ def test_block_names_have_numbers
11
+ assert_match 'if_block_19', Regexp.new(Cerubis::Matcher::BlockName)
12
+ end
13
+
14
+ def test_block_names_can_start_with_underscores
15
+ assert_match '_if', Regexp.new(Cerubis::Matcher::BlockName)
16
+ end
17
+
18
+ def test_block_names_dont_have_periods
19
+ refute_match 'if.block', Regexp.new("^#{Cerubis::Matcher::BlockName}$")
20
+ end
21
+
22
+ def test_block_names_dont_have_spaces
23
+ refute_match 'if block', Regexp.new("^#{Cerubis::Matcher::BlockName}$")
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module TestCloseBlock
2
+ def test_closing_block_name_only
3
+ assert_match '{{/if}}', Cerubis::Matcher::CloseBlock
4
+ end
5
+
6
+ def test_closing_block_name_with_underscores
7
+ assert_match '{{/if_block}}', Cerubis::Matcher::CloseBlock
8
+ end
9
+
10
+ def test_dont_match_closing_with_beginning_space
11
+ refute_match '{{ /if_block}}', Cerubis::Matcher::CloseBlock
12
+ end
13
+
14
+ def test_dont_match_closing_with_ending_space
15
+ refute_match '{{/if_block }}', Cerubis::Matcher::CloseBlock
16
+ end
17
+
18
+ def test_dont_match_closing_with_middle_space
19
+ refute_match '{{/if block}}', Cerubis::Matcher::CloseBlock
20
+ end
21
+
22
+ def test_dont_match_closing_with_period
23
+ refute_match '{{/if.block}}', Cerubis::Matcher::CloseBlock
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module TestConditions
2
+ def test_true
3
+ assert_match Cerubis::Matcher::Conditions, 'true'
4
+ end
5
+
6
+ def test_2_greater_than_1_with_spaces
7
+ assert_match Cerubis::Matcher::Conditions, '2 > 1'
8
+ end
9
+
10
+ def test_2_greater_than_1_without_spaces
11
+ assert_match Cerubis::Matcher::Conditions, '2>1'
12
+ end
13
+
14
+ def test_object_attribute
15
+ assert_match Cerubis::Matcher::Conditions, 'item.attribute'
16
+ end
17
+
18
+ def test_object_attribute_question_mark
19
+ assert_match Cerubis::Matcher::Conditions, 'item.attribute?'
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module TestHelpers
2
+ def test_helpers_with_one_object
3
+ assert_match Cerubis::Matcher::Helpers, 'helper obj'
4
+ end
5
+
6
+ def test_helpers_with_one_object_and_method
7
+ assert_match Cerubis::Matcher::Helpers, 'helper obj.method'
8
+ end
9
+
10
+ def test_helpers_with_two_objects
11
+ assert_match Cerubis::Matcher::Helpers, 'helper obj, 1'
12
+ end
13
+
14
+ def test_helpers_with_multiple_objects
15
+ assert_match Cerubis::Matcher::Helpers, 'helper obj, 1, foo'
16
+ end
17
+
18
+ def test_helpers_with_two_objects_one_with_quotes
19
+ assert_match Cerubis::Matcher::Helpers, "helper obj, 'foo'"
20
+ end
21
+ end
@@ -0,0 +1,37 @@
1
+ module TestObjectMethod
2
+ def test_object_name_has_letters
3
+ assert_match 'object', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
4
+ end
5
+
6
+ def test_object_name_can_begin_with_underscores
7
+ assert_match '_object', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
8
+ end
9
+
10
+ def test_object_name_cant_begin_with_periods
11
+ refute_match '.object', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
12
+ end
13
+
14
+ def test_object_name_has_underscores
15
+ assert_match 'object_name', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
16
+ end
17
+
18
+ def test_object_name_has_numbers
19
+ assert_match 'object_name_19', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
20
+ end
21
+
22
+ def test_object_with_attribute
23
+ assert_match 'object.name', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
24
+ end
25
+
26
+ def test_object_with_attribute_and_question_mark
27
+ assert_match 'object.name?', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
28
+ end
29
+
30
+ def test_chained_object_call
31
+ assert_match 'object.foo.bar.baz', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
32
+ end
33
+
34
+ def test_object_and_method_cant_have_spaces
35
+ refute_match 'object .name', Regexp.new("^#{Cerubis::Matcher::ObjectMethod}$")
36
+ end
37
+ end
@@ -0,0 +1,57 @@
1
+ module TestOpenBlock
2
+ def test_block_name_only
3
+ assert_match '{{#if_block}}', Cerubis::Matcher::OpenBlock
4
+ end
5
+
6
+ def test_match_operators_without_spaces
7
+ assert_match '{{#if a==b}}', Cerubis::Matcher::OpenBlock
8
+ end
9
+
10
+ def test_match_double_equals_if_with_spaces
11
+ assert_match '{{#if a == b}}', Cerubis::Matcher::OpenBlock
12
+ end
13
+
14
+ def test_match_not_equals_if
15
+ assert_match '{{#if a !== b}}', Cerubis::Matcher::OpenBlock
16
+ end
17
+
18
+ def test_match_greater_than_if
19
+ assert_match '{{#if 2 > 1}}', Cerubis::Matcher::OpenBlock
20
+ end
21
+
22
+ def test_match_true_if
23
+ assert_match '{{#if true}}', Cerubis::Matcher::OpenBlock
24
+ end
25
+
26
+ def test_match_object_if
27
+ assert_match '{{#if item}}', Cerubis::Matcher::OpenBlock
28
+ end
29
+
30
+ def test_match_object_attribute_if
31
+ assert_match '{{#if item.method}}', Cerubis::Matcher::OpenBlock
32
+ end
33
+
34
+ def test_dont_match_space_after_hash
35
+ refute_match '{{ #if item}}', Cerubis::Matcher::OpenBlock
36
+ end
37
+
38
+ def test_dont_match_space_before_ending_currlies
39
+ refute_match '{{#if item }}', Cerubis::Matcher::OpenBlock
40
+ end
41
+
42
+ def test_dont_match_single_equals
43
+ refute_match '{{#if item = 1}}', Cerubis::Matcher::OpenBlock
44
+ end
45
+
46
+ def test_dont_match_object_with_beginning_period
47
+ refute_match '{{#if .item}}', Cerubis::Matcher::OpenBlock
48
+ end
49
+
50
+ def test_match_if_with_string
51
+ assert_match "{{#if 'item' == 'item'}}", Cerubis::Matcher::OpenBlock
52
+ end
53
+
54
+ def test_loop_syntax
55
+ assert_match '{{#loop child in root.children}}', Cerubis::Matcher::OpenBlock
56
+ end
57
+ end
@@ -0,0 +1,29 @@
1
+ module TestOperators
2
+ def test_double_equals
3
+ assert_includes Cerubis::Matcher::Operators, :==
4
+ end
5
+
6
+ def test_triple_equals
7
+ assert_includes Cerubis::Matcher::Operators, :===
8
+ end
9
+
10
+ def test_not_equals
11
+ assert_includes Cerubis::Matcher::Operators, :'!='
12
+ end
13
+
14
+ def test_less_than
15
+ assert_includes Cerubis::Matcher::Operators, :<
16
+ end
17
+
18
+ def test_greater_than
19
+ assert_includes Cerubis::Matcher::Operators, :<
20
+ end
21
+
22
+ def test_less_than_equal_to
23
+ assert_includes Cerubis::Matcher::Operators, :<=
24
+ end
25
+
26
+ def test_greater_than_equal_to
27
+ assert_includes Cerubis::Matcher::Operators, :>=
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ module TestVariable
2
+ def test_variable_object
3
+ assert_match '{{object}}', Cerubis::Matcher::Variable
4
+ end
5
+
6
+ def test_variable_object_with_spaces
7
+ assert_match '{{ object }}', Cerubis::Matcher::Variable
8
+ end
9
+
10
+ def test_variable_object_attribute
11
+ assert_match '{{object.attribute}}', Cerubis::Matcher::Variable
12
+ end
13
+
14
+ def test_variable_object_attribute_with_spaces
15
+ assert_match '{{ object.attribute }}', Cerubis::Matcher::Variable
16
+ end
17
+
18
+ def test_variable_object_attribute_chain
19
+ assert_match '{{object.attribute.attr2}}', Cerubis::Matcher::Variable
20
+ end
21
+
22
+ def test_variable_object_attribute_chain_with_spaces
23
+ assert_match '{{ object.attribute.attr2 }}', Cerubis::Matcher::Variable
24
+ end
25
+
26
+ def test_variable_helper_with_object
27
+ assert_match '{{ helper object }}', Cerubis::Matcher::Variable
28
+ end
29
+
30
+ def test_variable_helper_with_multiple_objecst
31
+ assert_match '{{ helper object, 1 }}', Cerubis::Matcher::Variable
32
+ end
33
+
34
+ def test_variable_helper_with_string
35
+ assert_match "{{ foo_helper 'foo title', obj }}", Cerubis::Matcher::Variable
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ module TestArrayMethods
2
+ def test_array_responds_to_empty
3
+ obj = []
4
+ assert obj.cerubis_respond_to?(:empty?)
5
+ end
6
+
7
+ def test_array_responds_to_size
8
+ obj = []
9
+ assert obj.cerubis_respond_to?(:size)
10
+ end
11
+
12
+ def test_array_responds_to_first
13
+ obj = []
14
+ assert obj.cerubis_respond_to?(:first)
15
+ end
16
+
17
+ def test_array_responds_to_last
18
+ obj = []
19
+ assert obj.cerubis_respond_to?(:last)
20
+ end
21
+ end
@@ -0,0 +1,6 @@
1
+ module TestFixnumMethods
2
+ def test_fixnum_respond_to_zero
3
+ obj = 0
4
+ assert obj.cerubis_respond_to?(:zero?)
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TestFloatMethods
2
+ def test_float_respond_to_zero
3
+ obj = 0.0
4
+ assert obj.cerubis_respond_to?(:zero?)
5
+ end
6
+ end