summaryrefslogtreecommitdiff
path: root/spec/unit/settings/value_translator_spec.rb
blob: a318f6d4850b3cd243bf81ef0f1d3e3dd5dd1ba5 (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
#! /usr/bin/env ruby -S rspec
require 'spec_helper'
require 'puppet/settings/value_translator'

describe Puppet::Settings::ValueTranslator do
  let(:translator) { Puppet::Settings::ValueTranslator.new }

  context "booleans" do
    it "translates strings representing booleans to booleans" do
      translator['true'].should == true
      translator['false'].should == false
    end

    it "translates boolean values into themselves" do
      translator[true].should == true
      translator[false].should == false
    end

    it "leaves a boolean string with whitespace as a string" do
      translator[' true'].should == " true"
      translator['true '].should == "true"

      translator[' false'].should == " false"
      translator['false '].should == "false"
    end
  end

  context "numbers" do
    it "translates integer strings to integers" do
      translator["1"].should == 1
      translator["2"].should == 2
    end

    it "translates numbers starting with a 0 as octal" do
      translator["011"].should == 9
    end

    it "leaves hex numbers as strings" do
      translator["0x11"].should == "0x11"
    end
  end

  context "arbitrary strings" do
    it "translates an empty string as the empty string" do
      translator[""].should == ""
    end


    it "strips double quotes" do
      translator['"a string"'].should == 'a string'
    end

    it "strips single quotes" do
      translator["'a string'"].should == "a string"
    end

    it "does not strip preceeding whitespace" do
      translator[" \ta string"].should == " \ta string"
    end

    it "strips trailing whitespace" do
      translator["a string\t "].should == "a string"
    end

    it "leaves leading quote that is preceeded by whitespace" do
      translator[" 'a string'"].should == " 'a string"
    end

    it "leaves trailing quote that is succeeded by whitespace" do
      translator["'a string' "].should == "a string'"
    end

    it "leaves quotes that are not at the beginning or end of the string" do
      translator["a st'\"ring"].should == "a st'\"ring"
    end
  end
end