summaryrefslogtreecommitdiff
path: root/spec/unit/util/monkey_patches/lines_spec.rb
blob: 421604bbba4d9165d30f695d47f8935d2d9a8c04 (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
#! /usr/bin/env ruby
require 'spec_helper'
require 'puppet/util/monkey_patches/lines'

class Puppet::Util::MonkeyPatches::Lines::TestHelper < String
  include Puppet::Util::MonkeyPatches::Lines
end

describe Puppet::Util::MonkeyPatches::Lines::TestHelper do
  ["\n", "\n\n", "$", "$$", "@", "@@"].each do |sep|
    context "with #{sep.inspect}" do
      it "should delegate to each_line if given a block" do
        subject.expects(:each_line).with(sep)
        subject.lines(sep) do |x| nil end
      end

      it "should delegate to each_line without a block" do
        subject.expects(:enum_for).with(:each_line, sep)
        subject.lines(sep)
      end

      context "with one line" do
        context "with trailing separator" do
          subject { described_class.new("foo" + sep) }

          it "should yield the string" do
            got = []
            subject.lines(sep) do |x| got << x end
            got.should == ["foo#{sep}"]
          end

          it "should return the string" do
            subject.lines(sep).to_a.should == ["foo#{sep}"]
          end
        end

        context "without trailing separator" do
          subject { described_class.new("foo") }

          it "should yield the string" do
            got = []
            subject.lines(sep) do |x| got << x end
            got.should == ["foo"]
          end

          it "should return the string" do
            subject.lines(sep).to_a.should == ["foo"]
          end
        end
      end

      context "with multiple lines" do
        context "with trailing separator" do
          subject { described_class.new("foo#{sep}bar#{sep}baz#{sep}") }

          it "should yield the strings" do
            got = []
            subject.lines(sep) do |x| got << x end
            got.should == ["foo#{sep}", "bar#{sep}", "baz#{sep}"]
          end

          it "should return the strings" do
            subject.lines(sep).to_a.should == ["foo#{sep}", "bar#{sep}", "baz#{sep}"]
          end
        end

        context "without trailing separator" do
          subject { described_class.new("foo#{sep}bar#{sep}baz") }

          it "should yield the strings" do
            got = []
            subject.lines(sep) do |x| got << x end
            got.should == ["foo#{sep}", "bar#{sep}", "baz"]
          end

          it "should return the strings" do
            subject.lines(sep).to_a.should == ["foo#{sep}", "bar#{sep}", "baz"]
          end
        end
      end
    end
  end
end