mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
add tag object (#1590)
* add tag object Signed-off-by: Christoph Hartmann <chris@lollyrock.com> * add tests for to_hash function in tag Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
This commit is contained in:
parent
8a63e9d12c
commit
7c11ff9280
4 changed files with 93 additions and 2 deletions
|
@ -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'
|
||||
|
|
|
@ -2,17 +2,22 @@
|
|||
|
||||
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) }
|
||||
{ id: id, title: title, desc: desc, impact: impact, tests: tests.map(&:to_hash), tags: tags.map(&:to_hash) }
|
||||
end
|
||||
|
||||
def to_ruby
|
||||
|
@ -20,6 +25,7 @@ module Inspec
|
|||
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")
|
||||
|
|
27
lib/inspec/objects/tag.rb
Normal file
27
lib/inspec/objects/tag.rb
Normal file
|
@ -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: key,
|
||||
value: value,
|
||||
}
|
||||
end
|
||||
|
||||
def to_ruby
|
||||
"tag #{key.inspect}: #{value.inspect}"
|
||||
end
|
||||
|
||||
def to_s
|
||||
"Tag #{key} with #{value}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -330,5 +330,62 @@ end
|
|||
end
|
||||
end
|
||||
|
||||
describe 'Inspec::Tag' do
|
||||
it 'constructs a tag with key and value' do
|
||||
control = Inspec::Control.new
|
||||
|
||||
res1 = { name: "key", value: "value" }
|
||||
tag1 = Inspec::Tag.new(res1[:name], res1[:value])
|
||||
tag1.to_hash.must_equal res1
|
||||
control.add_tag(tag1)
|
||||
|
||||
res2 = { name: "key2'", value: "value'" }
|
||||
tag2 = Inspec::Tag.new(res2[:name], res2[:value])
|
||||
tag2.to_hash.must_equal res2
|
||||
control.add_tag(tag2)
|
||||
|
||||
res3 = { name: "key3\"", value: "value\"" }
|
||||
tag3 = Inspec::Tag.new(res3[:name], res3[:value])
|
||||
tag3.to_hash.must_equal res3
|
||||
control.add_tag(tag3)
|
||||
|
||||
res4 = { name: 'key4', value: ['a', 'b'] }
|
||||
tag4 = Inspec::Tag.new(res4[:name], res4[:value])
|
||||
tag4.to_hash.must_equal res4
|
||||
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
|
||||
|
||||
control_hash = {
|
||||
id:"tag.control.id",
|
||||
title: nil,
|
||||
desc: nil,
|
||||
impact: nil,
|
||||
tests: [],
|
||||
tags:[{
|
||||
name:"key",
|
||||
value:"value"
|
||||
}, {
|
||||
name:"key2'",
|
||||
value:"value'"
|
||||
}, {
|
||||
name:"key3\"",
|
||||
value:"value\""
|
||||
}, {
|
||||
name:"key4",
|
||||
value:["a", "b"]
|
||||
}]
|
||||
}
|
||||
control.to_hash.must_equal control_hash
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue