spec/dslh_spec.rb in dslh-0.3.3 vs spec/dslh_spec.rb in dslh-0.3.4

- old
+ new

@@ -765,10 +765,18 @@ dsl = Dslh.deval(template) evaluated = Dslh.eval(dsl, :key_conv => proc {|i| i.to_s }) expect(evaluated).to eq(template) end + it 'should convert json to dsl (old format)' do + template = JSON.parse(drupal_multi_az_template) + + dsl = Dslh.deval(template) + evaluated = Dslh.eval(dsl, :key_conv => proc {|i| i.to_s }, :dump_old_hash_array_format => true) + expect(evaluated).to eq(template) + end + it 'should convert json to dsl with key_conf' do template = JSON.parse(drupal_multi_az_template) key_conv = proc do |k| k.to_s.gsub('::', '__') @@ -933,58 +941,52 @@ Type "AWS::S3::BucketPolicy" Properties do PolicyDocument do Version "2008-10-17" Id "UploadPolicy" - Statement [ - _{ - Sid "EnableReadWrite" - Action "s3:GetObject", "s3:PutObject", "s3:PutObjectACL" - Effect "Allow" - Resource do - Fn__Join [ - "", - [ - "arn:aws:s3:::", - _{ - Ref "S3Bucket" - }, - "/*" - ] + Statement do |*| + Sid "EnableReadWrite" + Action "s3:GetObject", "s3:PutObject", "s3:PutObjectACL" + Effect "Allow" + Resource do + Fn__Join [ + "", + [ + "arn:aws:s3:::", + _{ + Ref "S3Bucket" + }, + "/*" ] + ] + end + Principal do + AWS do + Fn__GetAtt "S3User", "Arn" end - Principal do - AWS do - Fn__GetAtt "S3User", "Arn" - end - end - } - ] + end + end end Bucket do Ref "S3Bucket" end end end S3User do Type "AWS::IAM::User" Properties do Path "/" - Policies [ - _{ - PolicyName "root" - PolicyDocument do - Statement [ - _{ - Effect "Allow" - Action "s3:*" - Resource "*" - } - ] + Policies do |*| + PolicyName "root" + PolicyDocument do + Statement do |*| + Effect "Allow" + Action "s3:*" + Resource "*" end - } - ] + end + end end end S3Keys do Type "AWS::IAM::AccessKey" Properties do @@ -1000,24 +1002,20 @@ end Properties do AvailabilityZones do Fn__GetAZs "" end - LBCookieStickinessPolicy [ - _{ - PolicyName "CookieBasedPolicy" - CookieExpirationPeriod "30" - } - ] - Listeners [ - _{ - LoadBalancerPort "80" - InstancePort "80" - Protocol "HTTP" - PolicyNames ["CookieBasedPolicy"] - } - ] + LBCookieStickinessPolicy do |*| + PolicyName "CookieBasedPolicy" + CookieExpirationPeriod "30" + end + Listeners do |*| + LoadBalancerPort "80" + InstancePort "80" + Protocol "HTTP" + PolicyNames ["CookieBasedPolicy"] + end HealthCheck do Target "HTTP:80/" HealthyThreshold "2" UnhealthyThreshold "5" Interval "10" @@ -1037,15 +1035,13 @@ MinSize "1" MaxSize "5" DesiredCapacity do Ref "WebServerCapacity" end - LoadBalancerNames [ - _{ - Ref "ElasticLoadBalancer" - } - ] + LoadBalancerNames do |*| + Ref "ElasticLoadBalancer" + end end end LaunchConfig do Type "AWS::AutoScaling::LaunchConfiguration" Metadata do @@ -1164,15 +1160,13 @@ ] end InstanceType do Ref "InstanceType" end - SecurityGroups [ - _{ - Ref "WebServerSecurityGroup" - } - ] + SecurityGroups do |*| + Ref "WebServerSecurityGroup" + end KeyName do Ref "KeyName" end UserData do Fn__Base64 do @@ -1314,15 +1308,13 @@ Ref "DBUsername" end DBInstanceClass do Ref "DBClass" end - DBSecurityGroups [ - _{ - Ref "DBSecurityGroup" - } - ] + DBSecurityGroups do |*| + Ref "DBSecurityGroup" + end AllocatedStorage do Ref "DBAllocatedStorage" end MasterUserPassword do Ref "DBPassword" @@ -1342,31 +1334,29 @@ end WebServerSecurityGroup do Type "AWS::EC2::SecurityGroup" Properties do GroupDescription "Enable HTTP access via port 80, locked down to requests from the load balancer only and SSH access" - SecurityGroupIngress [ - _{ - IpProtocol "tcp" - FromPort "80" - ToPort "80" - SourceSecurityGroupOwnerId do - Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.OwnerAlias" - end - SourceSecurityGroupName do - Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.GroupName" - end - }, - _{ - IpProtocol "tcp" - FromPort "22" - ToPort "22" - CidrIp do - Ref "SSHLocation" - end - } - ] + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "80" + ToPort "80" + SourceSecurityGroupOwnerId do + Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.OwnerAlias" + end + SourceSecurityGroupName do + Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.GroupName" + end + end + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "22" + ToPort "22" + CidrIp do + Ref "SSHLocation" + end + end end end end Outputs do WebsiteURL do @@ -1570,58 +1560,52 @@ Type "AWS::S3::BucketPolicy" Properties do PolicyDocument do Version "2008-10-17" Id "UploadPolicy" - Statement [ - _{ - Sid "EnableReadWrite" - Action "s3:GetObject", "s3:PutObject", "s3:PutObjectACL" - Effect "Allow" - Resource do - Fn__Join [ - "", - [ - "arn:aws:s3:::", - _{ - Ref "S3Bucket" - }, - "/*" - ] + Statement do |*| + Sid "EnableReadWrite" + Action "s3:GetObject", "s3:PutObject", "s3:PutObjectACL" + Effect "Allow" + Resource do + Fn__Join [ + "", + [ + "arn:aws:s3:::", + _{ + Ref "S3Bucket" + }, + "/*" ] + ] + end + Principal do + AWS do + Fn__GetAtt "S3User", "Arn" end - Principal do - AWS do - Fn__GetAtt "S3User", "Arn" - end - end - } - ] + end + end end Bucket do Ref "S3Bucket" end end end S3User do Type "AWS::IAM::User" Properties do Path "/" - Policies [ - _{ - PolicyName "root" - PolicyDocument do - Statement [ - _{ - Effect "Allow" - Action "s3:*" - Resource "*" - } - ] + Policies do |*| + PolicyName "root" + PolicyDocument do + Statement do |*| + Effect "Allow" + Action "s3:*" + Resource "*" end - } - ] + end + end end end S3Keys do Type "AWS::IAM::AccessKey" Properties do @@ -1637,24 +1621,20 @@ end Properties do AvailabilityZones do Fn__GetAZs "" end - LBCookieStickinessPolicy [ - _{ - PolicyName "CookieBasedPolicy" - CookieExpirationPeriod "30" - } - ] - Listeners [ - _{ - LoadBalancerPort "80" - InstancePort "80" - Protocol "HTTP" - PolicyNames ["CookieBasedPolicy"] - } - ] + LBCookieStickinessPolicy do |*| + PolicyName "CookieBasedPolicy" + CookieExpirationPeriod "30" + end + Listeners do |*| + LoadBalancerPort "80" + InstancePort "80" + Protocol "HTTP" + PolicyNames ["CookieBasedPolicy"] + end HealthCheck do Target "HTTP:80/" HealthyThreshold "2" UnhealthyThreshold "5" Interval "10" @@ -1674,15 +1654,13 @@ MinSize "1" MaxSize "5" DesiredCapacity do Ref "WebServerCapacity" end - LoadBalancerNames [ - _{ - Ref "ElasticLoadBalancer" - } - ] + LoadBalancerNames do |*| + Ref "ElasticLoadBalancer" + end end end LaunchConfig do Type "AWS::AutoScaling::LaunchConfiguration" Metadata do @@ -1827,15 +1805,13 @@ ] end InstanceType do Ref "InstanceType" end - SecurityGroups [ - _{ - Ref "WebServerSecurityGroup" - } - ] + SecurityGroups do |*| + Ref "WebServerSecurityGroup" + end KeyName do Ref "KeyName" end UserData do Fn__Base64 do @@ -1977,15 +1953,13 @@ Ref "DBUsername" end DBInstanceClass do Ref "DBClass" end - DBSecurityGroups [ - _{ - Ref "DBSecurityGroup" - } - ] + DBSecurityGroups do |*| + Ref "DBSecurityGroup" + end AllocatedStorage do Ref "DBAllocatedStorage" end MasterUserPassword do Ref "DBPassword" @@ -2005,31 +1979,29 @@ end WebServerSecurityGroup do Type "AWS::EC2::SecurityGroup" Properties do GroupDescription "Enable HTTP access via port 80, locked down to requests from the load balancer only and SSH access" - SecurityGroupIngress [ - _{ - IpProtocol "tcp" - FromPort "80" - ToPort "80" - SourceSecurityGroupOwnerId do - Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.OwnerAlias" - end - SourceSecurityGroupName do - Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.GroupName" - end - }, - _{ - IpProtocol "tcp" - FromPort "22" - ToPort "22" - CidrIp do - Ref "SSHLocation" - end - } - ] + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "80" + ToPort "80" + SourceSecurityGroupOwnerId do + Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.OwnerAlias" + end + SourceSecurityGroupName do + Fn__GetAtt "ElasticLoadBalancer", "SourceSecurityGroup.GroupName" + end + end + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "22" + ToPort "22" + CidrIp do + Ref "SSHLocation" + end + end end end end Outputs do WebsiteURL do @@ -2303,15 +2275,13 @@ ] end InstanceType do Ref "InstanceType" end - SecurityGroups [ - _{ - Ref "WebServerSecurityGroup" - } - ] + SecurityGroups do |*| + Ref "WebServerSecurityGroup" + end KeyName do Ref "KeyName" end UserData do Fn__Base64 do @@ -2418,26 +2388,24 @@ end WebServerSecurityGroup do Type "AWS::EC2::SecurityGroup" Properties do GroupDescription "Enable HTTP access via port 80 and SSH access" - SecurityGroupIngress [ - _{ - IpProtocol "tcp" - FromPort "80" - ToPort "80" - CidrIp "0.0.0.0/0" - }, - _{ - IpProtocol "tcp" - FromPort "22" - ToPort "22" - CidrIp do - Ref "SSHLocation" - end - } - ] + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "80" + ToPort "80" + CidrIp "0.0.0.0/0" + end + SecurityGroupIngress do |*| + IpProtocol "tcp" + FromPort "22" + ToPort "22" + CidrIp do + Ref "SSHLocation" + end + end end end end Outputs do WebsiteURL do @@ -2673,7 +2641,71 @@ end end end end EOS + end + + it 'should convert hash to dsl (include hash array)' do + h = {:glossary=>[ + {:title=>"example glossary", + :date=>Time.parse('2016/05/21 00:00 UTC')}, + {:title=>"example glossary2", + :date=>Time.parse('2016/05/21 00:01 UTC')}], + :glossary2=>[ + {:title=>"example glossary", + :date=>Time.parse('2016/05/21 00:00 UTC')}]} + + dsl = Dslh.deval(h, :time_inspecter => proc {|i| i.to_s.inspect }) + expect(dsl).to eq(<<-EOS) +glossary do |*| + title "example glossary" + date "2016-05-21 00:00:00 UTC" +end +glossary do |*| + title "example glossary2" + date "2016-05-21 00:01:00 UTC" +end +glossary2 do |*| + title "example glossary" + date "2016-05-21 00:00:00 UTC" +end + EOS + end + + it 'should convert dsl to hash (include hash array)' do + h = {:glossary=>[ + {:title=>"example glossary", + :date=>Time.parse('2016/05/21 00:00 UTC')}, + {:title=>"example glossary2", + :date=>Time.parse('2016/05/21 00:01 UTC')}], + :glossary2=>[ + {:title=>"example glossary", + :date=>Time.parse('2016/05/21 00:00 UTC')}]} + + hash = Dslh.eval do + glossary do |*| + title "example glossary" + date "2016-05-21 00:00:00 UTC" + end + glossary do |*| + title "example glossary2" + date "2016-05-21 00:01:00 UTC" + end + glossary2 do |*| + title "example glossary" + date "2016-05-21 00:00:00 UTC" + end + end + + expect(hash).to eq( + {"glossary"=>[ + {"title"=>"example glossary", + "date"=>'2016-05-21 00:00:00 UTC'}, + {"title"=>"example glossary2", + "date"=>'2016-05-21 00:01:00 UTC'}], + "glossary2"=>[ + {"title"=>"example glossary", + "date"=>'2016-05-21 00:00:00 UTC'}]} + ) end end