1+ #
2+ # Author:: Stephen Delano (<stephen@ospcode.com>)
3+ # Copyright:: Copyright (c) 2010 Opscode, Inc.
4+ # License:: Apache License, Version 2.0
5+ #
6+ # Licensed under the Apache License, Version 2.0 (the "License");
7+ # you may not use this file except in compliance with the License.
8+ # You may obtain a copy of the License at
9+ #
10+ # http://www.apache.org/licenses/LICENSE-2.0
11+ #
12+ # Unless required by applicable law or agreed to in writing, software
13+ # distributed under the License is distributed on an "AS IS" BASIS,
14+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ # See the License for the specific language governing permissions and
16+ # limitations under the License.
17+ #
18+
19+ require File . expand_path ( File . join ( File . dirname ( __FILE__ ) , ".." , "spec_helper" ) )
20+ require 'chef/environment'
21+
22+ describe Chef ::Environment do
23+ before ( :each ) do
24+ @environment = Chef ::Environment . new
25+ end
26+
27+ describe "initialize" do
28+ it "should be a Chef::Environment" do
29+ @environment . should be_a_kind_of ( Chef ::Environment )
30+ end
31+ end
32+
33+ describe "name" do
34+ it "should let you set the name to a string" do
35+ @environment . name ( "production" ) . should == "production"
36+ end
37+
38+ it "should return the current name" do
39+ @environment . name ( "production" )
40+ @environment . name . should == "production"
41+ end
42+
43+ it "should not accept spaces" do
44+ lambda { @environment . name ( "production environment" ) } . should raise_error ( ArgumentError )
45+ end
46+
47+ it "should not accept anything but strings" do
48+ lambda { @environment . name ( Array . new ) } . should raise_error ( ArgumentError )
49+ lambda { @environment . name ( Hash . new ) } . should raise_error ( ArgumentError )
50+ lambda { @environment . name ( 2 ) } . should raise_error ( ArgumentError )
51+ end
52+ end
53+
54+ describe "description" do
55+ it "should let you set the description to a string" do
56+ @environment . description ( "this is my test environment" ) . should == "this is my test environment"
57+ end
58+
59+ it "should return the correct description" do
60+ @environment . description ( "I like running tests" )
61+ @environment . description . should == "I like running tests"
62+ end
63+
64+ it "should not accept anything but strings" do
65+ lambda { @environment . description ( Array . new ) } . should raise_error ( ArgumentError )
66+ lambda { @environment . description ( Hash . new ) } . should raise_error ( ArgumentError )
67+ lambda { @environment . description ( 42 ) } . should raise_error ( ArgumentError )
68+ end
69+ end
70+
71+ describe "to_json" do
72+ before ( :each ) do
73+ @environment . name ( "spec" )
74+ @environment . description ( "Where we run the spec tests" )
75+ @json = @environment . to_json
76+ end
77+
78+ %w{ name description } . each do |t |
79+ it "should include '#{ t } '" do
80+ @json . should =~ /"#{ t } ":"#{ @environment . send ( t . to_sym ) } "/
81+ end
82+ end
83+
84+ it "should include 'json_class'" do
85+ @json . should =~ /"json_class":"Chef::Environment"/
86+ end
87+
88+ it "should include 'chef_type'" do
89+ @json . should =~ /"chef_type":"environment"/
90+ end
91+ end
92+
93+ describe "from_json" do
94+ before ( :each ) do
95+ @data = {
96+ :name => "production" ,
97+ :description => "We are productive" ,
98+ :json_class => "Chef::Environment" ,
99+ :chef_type => "environment"
100+ }
101+ @environment = JSON . parse ( @data . to_json )
102+ end
103+
104+ it "should return a Chef::Environment" do
105+ @environment . should be_a_kind_of ( Chef ::Environment )
106+ end
107+
108+ %w{ name description } . each do |t |
109+ it "should match '#{ t } '" do
110+ @environment . send ( t . to_sym ) . should == @data [ t . to_sym ]
111+ end
112+ end
113+ end
114+ end
0 commit comments