File: parse_heredoc_spec.rb

package info (click to toggle)
puppet 5.5.10-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,116 kB
  • sloc: ruby: 250,669; sh: 1,620; xml: 218; makefile: 151; sql: 103
file content (180 lines) | stat: -rw-r--r-- 3,901 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'spec_helper'
require 'puppet/pops'

# relative to this spec file (./) does not work as this file is loaded by rspec
require File.join(File.dirname(__FILE__), '/parser_rspec_helper')

describe "egrammar parsing heredoc" do
  include ParserRspecHelper

  it "parses plain heredoc" do
    expect(dump(parse("@(END)\nThis is\nheredoc text\nEND\n"))).to eq([
      "(@()",
      "  (sublocated 'This is\nheredoc text\n')",
      ")"
    ].join("\n"))
  end

  it "parses heredoc with margin" do
    src = [
    "@(END)",
    "   This is",
    "   heredoc text",
    "   | END",
    ""
    ].join("\n")
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated 'This is\nheredoc text\n')",
      ")"
    ].join("\n"))
  end

  it "parses heredoc with margin and right newline trim" do
    src = [
    "@(END)",
    "   This is",
    "   heredoc text",
    "   |- END",
    ""
    ].join("\n")
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated 'This is\nheredoc text')",
      ")"
    ].join("\n"))
  end

  it "parses syntax and escape specification" do
    src = <<-CODE
    @(END:syntax/t)
    Tex\\tt\\n
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@(syntax)",
      "  (sublocated 'Tex\tt\\n')",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression" do
    src = <<-CODE
    @("END")
    Hello $name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated (cat 'Hello ' (str $name)))",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression containing escapes" do
    src = <<-CODE
    @("END")
    Hello \\$name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated (cat 'Hello \\' (str $name)))",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression containing escapes when escaping other things than $" do
    src = <<-CODE
    @("END"/t)
    Hello \\$name
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated (cat 'Hello \\' (str $name)))",
      ")"
    ].join("\n"))
  end

  it "parses with escaped newlines without preceding whitespace" do
    src = <<-CODE
    @(END/L)
    First Line\\
     Second Line
    |- END
    CODE
    parse(src)
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated 'First Line Second Line')",
      ")"
    ].join("\n"))
  end

  it "parses with escaped newlines with proper margin" do
    src = <<-CODE
    @(END/L)
     First Line\\
      Second Line
    |- END
    CODE
    parse(src)
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated ' First Line  Second Line')",
      ")"
    ].join("\n"))
  end

  it "parses interpolated heredoc expression with false start on $" do
    src = <<-CODE
    @("END")
    Hello $name$%a
    |- END
    CODE
    expect(dump(parse(src))).to eq([
      "(@()",
      "  (sublocated (cat 'Hello ' (str $name) '$%a'))",
      ")"
    ].join("\n"))
  end

  it "parses interpolated [] expression by looking at the correct preceding char for space" do
    # NOTE: Important not to use the left margin feature here
    src = <<-CODE
$xxxxxxx = @("END")
${facts['os']['family']}
XXXXXXX XXX
END
CODE
    expect(dump(parse(src))).to eq([
      "(= $xxxxxxx (@()",
      "  (sublocated (cat (str (slice (slice $facts 'os') 'family')) '",
      "XXXXXXX XXX",
      "'))",
      "))"].join("\n"))
  end

  it 'parses multiple heredocs on the same line' do
    src = <<-CODE
    notice({ @(foo) => @(bar) })
    hello
    -foo
    world
    -bar
    notice '!'
    CODE
    expect(dump(parse(src))).to eq([
      '(block',
      '  (invoke notice ({} ((@()',
      '    (sublocated \'    hello\')',
      '  ) (@()',
      '    (sublocated \'    world\')',
      '  ))))',
      '  (invoke notice \'!\')',
      ')'
    ].join("\n"))
  end

end