summaryrefslogtreecommitdiff
path: root/spec/ruby/language/heredoc_spec.rb
blob: b3b160fd11102d1805448820320bc868af861006 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- encoding: us-ascii -*-

require_relative '../spec_helper'

describe "Heredoc string" do

  before :each do
    @ip = 'xxx' # used for interpolation
  end

  it "allows HEREDOC with <<identifier, interpolated" do
    s = <<HERE
foo bar#{@ip}
HERE
    s.should == "foo barxxx\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it 'allow HEREDOC with <<"identifier", interpolated' do
    s = <<"HERE"
foo bar#{@ip}
HERE
    s.should == "foo barxxx\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it "allows HEREDOC with <<'identifier', no interpolation" do
    s = <<'HERE'
foo bar#{@ip}
HERE
    s.should == 'foo bar#{@ip}' + "\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it "allows HEREDOC with <<-identifier, allowing to indent identifier, interpolated" do
    s = <<-HERE
    foo bar#{@ip}
    HERE

    s.should == "    foo barxxx\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it 'allows HEREDOC with <<-"identifier", allowing to indent identifier, interpolated' do
    s = <<-"HERE"
    foo bar#{@ip}
    HERE

    s.should == "    foo barxxx\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it "allows HEREDOC with <<-'identifier', allowing to indent identifier, no interpolation" do
    s = <<-'HERE'
    foo bar#{@ip}
    HERE

    s.should == '    foo bar#{@ip}' + "\n"
    s.encoding.should == Encoding::US_ASCII
  end

  it 'raises SyntaxError if quoted HEREDOC identifier is ending not on same line' do
    -> {
      eval %{<<"HERE\n"\nraises syntax error\nHERE}
    }.should raise_error(SyntaxError)
  end

  it "allows HEREDOC with <<~'identifier', allowing to indent identifier and content" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.message.should == "character density, n.:\n  The number of very weird people in the office.\n"
  end

  it "trims trailing newline character for blank HEREDOC with <<~'identifier'" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.blank.should == ""
  end

  it 'allows HEREDOC with <<~identifier, interpolated' do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.unquoted.should == "unquoted interpolated\n"
  end

  it 'allows HEREDOC with <<~"identifier", interpolated' do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.doublequoted.should == "doublequoted interpolated\n"
  end

  it "allows HEREDOC with <<~'identifier', no interpolation" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.singlequoted.should == "singlequoted \#{\"interpolated\"}\n"
  end

  it "allows HEREDOC with <<~'identifier', no interpolation, with backslash" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.backslash.should == "a\nbc\n"
  end

  it "selects the least-indented line and removes its indentation from all the lines" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.least_indented_on_the_first_line.should == "a\n  b\n    c\n"
    SquigglyHeredocSpecs.least_indented_on_the_last_line.should == "    a\n  b\nc\n"
  end

  it "selects the least-indented line and removes its indentation from all the lines for <<~'identifier'" do
    require_relative 'fixtures/squiggly_heredoc'
    SquigglyHeredocSpecs.least_indented_on_the_first_line_single.should == "a\n  b\n    c\n"
    SquigglyHeredocSpecs.least_indented_on_the_last_line_single.should == "    a\n  b\nc\n"
  end
end