Skip to content

Commit

Permalink
add tag object
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
  • Loading branch information
chris-rock committed Mar 22, 2017
1 parent 2d9d7aa commit 71bfc5b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/inspec/objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Inspec
autoload :Attribute, 'inspec/objects/attribute'
autoload :Tag, 'inspec/objects/tag'
autoload :Control, 'inspec/objects/control'
autoload :EachLoop, 'inspec/objects/each_loop'
autoload :List, 'inspec/objects/list'
Expand Down
8 changes: 7 additions & 1 deletion lib/inspec/objects/control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

module Inspec
class Control
attr_accessor :id, :title, :desc, :impact, :tests
attr_accessor :id, :title, :desc, :impact, :tests, :tags
def initialize
@tests = []
@tags = []
end

def add_test(t)
@tests.push(t)
end

def add_tag(t)
@tags.push(t)
end

def to_hash
{ id: id, title: title, desc: desc, impact: impact, tests: tests.map(&:to_hash) }
end
Expand All @@ -20,6 +25,7 @@ def to_ruby
res.push " title #{title.inspect}" unless title.to_s.empty?
res.push " desc #{desc.inspect}" unless desc.to_s.empty?
res.push " impact #{impact}" unless impact.nil?
tags.each { |t| res.push(indent(t.to_ruby, 2)) }
tests.each { |t| res.push(indent(t.to_ruby, 2)) }
res.push 'end'
res.join("\n")
Expand Down
27 changes: 27 additions & 0 deletions lib/inspec/objects/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# encoding:utf-8

module Inspec
class Tag
attr_accessor :key, :value

def initialize(key, value)
@key = key
@value = value
end

def to_hash
{
name: @name,
options: @opts,
}
end

def to_ruby
"tag #{key.inspect}: #{value.inspect}"
end

def to_s
"Tag #{@key} with #{@value}"
end
end
end
23 changes: 22 additions & 1 deletion test/unit/dsl/objects_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,26 @@
end
end


describe 'Inspec::Tag' do
it 'constructs a tag with key and value' do
control = Inspec::Control.new
tag1 = Inspec::Tag.new('key', 'value')
control.add_tag(tag1)
tag2 = Inspec::Tag.new("key2'", "value'")
control.add_tag(tag2)
tag3 = Inspec::Tag.new("key3\"", "value\"")
control.add_tag(tag3)
tag4 = Inspec::Tag.new('key4', ['a', 'b'])
control.add_tag(tag4)
control.id = 'tag.control.id'
control.to_ruby.must_equal '
control "tag.control.id" do
tag "key": "value"
tag "key2\'": "value\'"
tag "key3\"": "value\""
tag "key4": ["a", "b"]
end
'.strip
end
end
end

0 comments on commit 71bfc5b

Please sign in to comment.