Skip to content
Permalink
Browse files

Adding tags support

  • Loading branch information...
robertodecurnex committed Apr 22, 2016
1 parent a6001ad commit 0c9945659e6eb00c7ff026a5065cdbd9a74f119f
@@ -6,32 +6,36 @@ module Spectro

class Spec

attr_accessor :md5, :description, :rules, :signature
attr_accessor :md5, :description, :rules, :signature, :tags

# @param [String] spec md5
# @param [Spectro::Spec::Signature] signature spec signature
# @param [String] description spec's description
# @param [<Spectro::Spec::Rule>] rules collection of spec rules
def initialize md5, signature, description, rules
# @param [String] tags collection of tags
def initialize md5, signature, description, rules, tags
self.description = description
self.md5 = md5
self.rules = rules
self.signature = signature
self.tags = tags
end

def == spec
return \
self.description == spec.description && \
self.signature == spec.signature && \
self.rules == spec.rules
self.rules == spec.rules && \
self.tags == spec.tags
end

def to_hash
return {
md5: self.md5,
description: self.description,
rules: self.rules.collect(&:to_hash),
signature: self.signature.to_hash
signature: self.signature.to_hash,
tags: self.tags
}
end

@@ -39,25 +39,31 @@ def parse
# @param [String] raw_spec raw spec
# @return [Spectro::Spec] the Spectro::Spec instance
def parse_spec raw_spec
spec_raw_signature, *spec_raw_desc_and_rules = raw_spec.split("\n").reject(&:empty?)
spec_raw_signature, *spec_raw_body = raw_spec.split("\n").reject(&:empty?)

spec_signature = self.parse_spec_signature(spec_raw_signature)

spec_raw_description = spec_raw_desc_and_rules.take_while do |desc_or_rule|
desc_or_rule.match(/^`/)
spec_raw_description = spec_raw_body.take_while do |line|
line.match(/^`/)
end

spec_raw_tags = spec_raw_body.select do |line|
line.match(/^@tags(:| |$)/)
end

spec_description = self.parse_spec_description(spec_raw_description)

spec_raw_rules = spec_raw_desc_and_rules - spec_raw_description
spec_tags = self.parse_spec_tags(spec_raw_tags)

spec_raw_rules = spec_raw_body - spec_raw_description - spec_raw_tags

spec_rules = spec_raw_rules.map do |spec_raw_rule|
self.parse_spec_rule(spec_raw_rule)
end

spec_md5 = Digest::MD5.hexdigest(raw_spec)

return Spectro::Spec.new(spec_md5, spec_signature, spec_description, spec_rules)
return Spectro::Spec.new(spec_md5, spec_signature, spec_description, spec_rules, spec_tags)
end

# Returns the spec description from the raw spec description
@@ -74,6 +80,16 @@ def parse_spec_description(spec_raw_description)
end.join(' ').strip.gsub("\n ", "\n")
end

# Returns the spec tags from the raw spec tags
#
# @param [String] spec_raw_tags spec's raw tags
# @return [<String>] spec tags
def parse_spec_tags(spec_raw_tags)
return spec_raw_tags.collect do |raw_tags|
raw_tags.sub(/^@tags:?/, '').split(' ').compact
end.flatten
end

# Returns an Spectro::Spec::Rule instance from the raw spec rule
#
# @param [String] spec_raw_rule raw rule of the spec

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -5,10 +5,10 @@
sample.rb:
"hello":
lambda_id: bfc794ba0e2f5aca9a2e4550ad8632e7
spec_md5: 1aa2c69a47b054824a21b295f089ca3d
spec_md5: 5fd6bb631c2f04bed050e0080198c6eb
"sum":
lambda_id: 1db993c17e61ad1fa3a064b34892ff11
spec_md5: 74cbc71617213a3a542f1dc71911712d
spec_md5: 79d52405c2c900f7dcb3a00a38f5145d
"rule_of_three":
lambda_id: 8832fa985fc6ed6453428472a05f50c0
spec_md5: 588c12efd07f0c90d3f2c85666babc4b
@@ -20,12 +20,14 @@ def double n1
"Minion" -> "Say Hello to Minion"
"Roberto" -> "Say Hello to Roberto"
"Roland" -> "Say Hello to Roland"
@tags: hello string

spec_for sum Fixnum, Fixnum -> Fixnum
`Single line description
1, 1 -> 2
1, 2 -> 3
2, 2 -> 4
@tags: sum math number
spec_for rule_of_three Float, Float, Float -> Float
`Multi-line
@@ -27,8 +27,8 @@ def test_compile
---
undefined_sample.rb:
- !ruby/object:Spectro::Spec
md5: 23d8f3f75459cc94364520d99717a284
description: ''
md5: 23d8f3f75459cc94364520d99717a284
rules:
- !ruby/object:Spectro::Spec::Rule
output: !ruby/class 'TrueClass'
@@ -37,10 +37,11 @@ def test_compile
name: i_am_undefined
output_type: TrueClass
params_types: []
tags: []
sample.rb:
- !ruby/object:Spectro::Spec
md5: d10062f3fefde7c4b1388be2cb7c7bb6
description: "Multi-line description \\nWith blank line in the middle"
md5: d10062f3fefde7c4b1388be2cb7c7bb6
rules:
- !ruby/object:Spectro::Spec::Rule
output: true
@@ -51,6 +52,7 @@ def test_compile
output_type: TrueClass
params_types:
- FalseClass
tags: []
YAML
assert File.exists?('test/files/.spectro/undefined.yml'), 'Spectro::Compiler#compile was expected to create an undefined.yml file but it did not.'
assert_equal YAML.load(expected_yaml), YAML.load_file('test/files/.spectro/undefined.yml')
@@ -18,8 +18,8 @@ def test_upload_undefined_specs
---
undefined_sample.rb:
- !ruby/object:Spectro::Spec
md5: 23d8f3f75459cc94364520d99717a284
description: ''
md5: 23d8f3f75459cc94364520d99717a284
rules:
- !ruby/object:Spectro::Spec::Rule
output: !ruby/class 'TrueClass'
@@ -28,10 +28,11 @@ def test_upload_undefined_specs
name: i_am_undefined
output_type: TrueClass
params_types: []
tags: []
sample.rb:
- !ruby/object:Spectro::Spec
md5: d10062f3fefde7c4b1388be2cb7c7bb6
description: "Multi-line description \\nWith blank line in the middle"
md5: d10062f3fefde7c4b1388be2cb7c7bb6
rules:
- !ruby/object:Spectro::Spec::Rule
output: true
@@ -42,6 +43,7 @@ def test_upload_undefined_specs
output_type: TrueClass
params_types:
- FalseClass
tags: []
BODY

Dir.chdir('test/files') do
@@ -10,11 +10,12 @@ def setup
@rule = Spectro::Spec::Rule.new([1, 2], 3)
@signature = Spectro::Spec::Signature.new('local_name', ['Fixnum'], 'Fixnum')
@description = "Random Description In Here"
@spec = Spectro::Spec.new('md5', @signature, @description, [@rule])
@tags = ["tag1", "tag2"]
@spec = Spectro::Spec.new('md5', @signature, @description, [@rule], @tags)
end

def test_equal
spec2 = Spectro::Spec.new('md5', @signature, @description, [@rule])
spec2 = Spectro::Spec.new('md5', @signature, @description, [@rule], @tags)
assert @spec === spec2
end

@@ -32,7 +33,11 @@ def test_to_hash
name: "local_name",
output_type: "Fixnum",
params_type: ["Fixnum"]
}
},
tags: [
"tag1",
"tag2"
]
}
end

0 comments on commit 0c99456

Please sign in to comment.
You can’t perform that action at this time.