File: ecr_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (71 lines) | stat: -rw-r--r-- 1,903 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
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 ".render" do
    ECR.render("#{__DIR__}/../data/test_template2.ecr").should eq("123")
  end
end