Add eval to unit tests; break up codegen test into individual elements

Signed-off-by: Clinton Wolfe <clintoncwolfe@gmail.com>
This commit is contained in:
Clinton Wolfe 2019-01-30 14:29:18 -05:00
parent 18011ccfec
commit a1af115e26

View file

@ -229,12 +229,22 @@ describe Inspec::Attribute do
describe 'to_ruby method' do
it 'generates the code for the attribute' do
attribute = Inspec::Attribute.new('application_port', description: 'The port my application uses', value: 80)
attribute.to_ruby.must_equal <<-RUBY.chomp
attr_application_port = attribute('application_port',{
value: 80,
description: 'The port my application uses',
})
RUBY
ruby_code = attribute.to_ruby
ruby_code.must_include "attr_application_port = " # Should assign to a var
ruby_code.must_include "attribute('application_port'" # Should have the DSL call
ruby_code.must_include 'value: 80'
ruby_code.must_include 'default: 80'
ruby_code.must_include "description: 'The port my application uses'"
# Try to eval the code to verify that the generated code was valid ruby.
# Note that the attribute() method is part of the DSL, so we need to
# alter the call into something that can respond - the constructor will do
ruby_code_for_eval = ruby_code.sub(/attribute\(/,'Inspec::Attribute.new(')
# This will throw exceptions if there is a problem
new_attr = eval(ruby_code_for_eval) # Could use ripper!
new_attr.value.must_equal 80
end
end
end