File: ecr_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (83 lines) | stat: -rw-r--r-- 2,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "spec"
require "ecr"
require "ecr/processor"

private class ECRSpecHelloView
  @msg : String

  def initialize(@msg)
  end

  ECR.def_to_s "#{__DIR__}/../data/test_template.ecr"
end

describe "ECR" do
  it "builds a crystal program from a source" do
    program = ECR.process_string "hello <%= 1 %> wor\nld <% while true %> 2 <% end %>\n<%# skip %> <%% \"string\" %>", "foo.cr"

    pieces = [
      %(__str__ << "hello "),
      %(#<loc:push>(#<loc:"foo.cr",1,10> 1 )#<loc:pop>.to_s __str__),
      %(__str__ << " wor\\nld "),
      %(#<loc:push>#<loc:"foo.cr",2,6> while true #<loc:pop>),
      %(__str__ << " 2 "),
      %(#<loc:push>#<loc:"foo.cr",2,25> end #<loc:pop>),
      %(__str__ << "\\n"),
      %(#<loc:push>#<loc:"foo.cr",3,3> # skip #<loc:pop>),
      %(__str__ << " "),
      %(__str__ << "<% \\"string\\" %>"),
    ]
    program.should eq(pieces.join('\n') + '\n')
  end

  it "does ECR.def_to_s" do
    view = ECRSpecHelloView.new("world!")
    view.to_s.strip.should eq("Hello world! 012")
  end

  it "does with <%= -%>" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template2.ecr", io
    io.to_s.should eq("123")
  end

  it "does with <%- %> (1)" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template3.ecr", io
    io.to_s.should eq("01")
  end

  it "does with <%- %> (2)" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template4.ecr", io
    io.to_s.should eq("hi\n01")
  end

  it "does with <% -%>" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template5.ecr", io
    io.to_s.should eq("hi\n      0\n      1\n  ")
  end

  it "does with -% inside string" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template6.ecr", io
    io.to_s.should eq("string with -%")
  end

  it "does with <%% %>" do
    io = IO::Memory.new
    ECR.embed "#{__DIR__}/../data/test_template7.ecr", io
    io.to_s.should eq(<<-ECR)
      <% if @name %>
      Greetings, <%= @name %>!
        <%- else -%>
      Greetings!
      <%- end -%>
      ECR
  end

  it ".render" do
    ECR.render("#{__DIR__}/../data/test_template2.ecr").should eq("123")
  end
end