Skip to content

Commit

Permalink
Adding tags support
Browse files Browse the repository at this point in the history
  • Loading branch information
robertodecurnex committed Apr 22, 2016
1 parent a6001ad commit 0c99456
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 39 deletions.
12 changes: 8 additions & 4 deletions lib/spectro/spec.rb
Expand Up @@ -6,32 +6,36 @@ module Spectro


class Spec class Spec


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


# @param [String] spec md5 # @param [String] spec md5
# @param [Spectro::Spec::Signature] signature spec signature # @param [Spectro::Spec::Signature] signature spec signature
# @param [String] description spec's description # @param [String] description spec's description
# @param [<Spectro::Spec::Rule>] rules collection of spec rules # @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.description = description
self.md5 = md5 self.md5 = md5
self.rules = rules self.rules = rules
self.signature = signature self.signature = signature
self.tags = tags
end end


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


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


Expand Down
26 changes: 21 additions & 5 deletions lib/spectro/spec/parser.rb
Expand Up @@ -39,25 +39,31 @@ def parse
# @param [String] raw_spec raw spec # @param [String] raw_spec raw spec
# @return [Spectro::Spec] the Spectro::Spec instance # @return [Spectro::Spec] the Spectro::Spec instance
def parse_spec raw_spec 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_signature = self.parse_spec_signature(spec_raw_signature)


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

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


spec_description = self.parse_spec_description(spec_raw_description) 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| spec_rules = spec_raw_rules.map do |spec_raw_rule|
self.parse_spec_rule(spec_raw_rule) self.parse_spec_rule(spec_raw_rule)
end end


spec_md5 = Digest::MD5.hexdigest(raw_spec) 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 end


# Returns the spec description from the raw spec description # Returns the spec description from the raw spec description
Expand All @@ -74,6 +80,16 @@ def parse_spec_description(spec_raw_description)
end.join(' ').strip.gsub("\n ", "\n") end.join(' ').strip.gsub("\n ", "\n")
end 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 # Returns an Spectro::Spec::Rule instance from the raw spec rule
# #
# @param [String] spec_raw_rule raw rule of the spec # @param [String] spec_raw_rule raw rule of the spec
Expand Down
7 changes: 0 additions & 7 deletions test/files/.spectro/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/files/.spectro/cache/7e46ad07603d4044a8d70c9004c48adf.rb

This file was deleted.

7 changes: 0 additions & 7 deletions test/files/.spectro/cache/aac019283279bcb5f59f9760865f7d77.rb

This file was deleted.

4 changes: 2 additions & 2 deletions test/files/.spectro/index.yml
Expand Up @@ -5,10 +5,10 @@
sample.rb: sample.rb:
"hello": "hello":
lambda_id: bfc794ba0e2f5aca9a2e4550ad8632e7 lambda_id: bfc794ba0e2f5aca9a2e4550ad8632e7
spec_md5: 1aa2c69a47b054824a21b295f089ca3d spec_md5: 5fd6bb631c2f04bed050e0080198c6eb
"sum": "sum":
lambda_id: 1db993c17e61ad1fa3a064b34892ff11 lambda_id: 1db993c17e61ad1fa3a064b34892ff11
spec_md5: 74cbc71617213a3a542f1dc71911712d spec_md5: 79d52405c2c900f7dcb3a00a38f5145d
"rule_of_three": "rule_of_three":
lambda_id: 8832fa985fc6ed6453428472a05f50c0 lambda_id: 8832fa985fc6ed6453428472a05f50c0
spec_md5: 588c12efd07f0c90d3f2c85666babc4b spec_md5: 588c12efd07f0c90d3f2c85666babc4b
Expand Down
2 changes: 2 additions & 0 deletions test/files/sample.rb
Expand Up @@ -20,12 +20,14 @@ def double n1
"Minion" -> "Say Hello to Minion" "Minion" -> "Say Hello to Minion"
"Roberto" -> "Say Hello to Roberto" "Roberto" -> "Say Hello to Roberto"
"Roland" -> "Say Hello to Roland" "Roland" -> "Say Hello to Roland"
@tags: hello string


spec_for sum Fixnum, Fixnum -> Fixnum spec_for sum Fixnum, Fixnum -> Fixnum
`Single line description `Single line description
1, 1 -> 2 1, 1 -> 2
1, 2 -> 3 1, 2 -> 3
2, 2 -> 4 2, 2 -> 4
@tags: sum math number


spec_for rule_of_three Float, Float, Float -> Float spec_for rule_of_three Float, Float, Float -> Float
`Multi-line `Multi-line
Expand Down
6 changes: 4 additions & 2 deletions test/minitest/spectro/test_compiler.rb
Expand Up @@ -27,8 +27,8 @@ def test_compile
--- ---
undefined_sample.rb: undefined_sample.rb:
- !ruby/object:Spectro::Spec - !ruby/object:Spectro::Spec
md5: 23d8f3f75459cc94364520d99717a284
description: '' description: ''
md5: 23d8f3f75459cc94364520d99717a284
rules: rules:
- !ruby/object:Spectro::Spec::Rule - !ruby/object:Spectro::Spec::Rule
output: !ruby/class 'TrueClass' output: !ruby/class 'TrueClass'
Expand All @@ -37,10 +37,11 @@ def test_compile
name: i_am_undefined name: i_am_undefined
output_type: TrueClass output_type: TrueClass
params_types: [] params_types: []
tags: []
sample.rb: sample.rb:
- !ruby/object:Spectro::Spec - !ruby/object:Spectro::Spec
md5: d10062f3fefde7c4b1388be2cb7c7bb6
description: "Multi-line description \\nWith blank line in the middle" description: "Multi-line description \\nWith blank line in the middle"
md5: d10062f3fefde7c4b1388be2cb7c7bb6
rules: rules:
- !ruby/object:Spectro::Spec::Rule - !ruby/object:Spectro::Spec::Rule
output: true output: true
Expand All @@ -51,6 +52,7 @@ def test_compile
output_type: TrueClass output_type: TrueClass
params_types: params_types:
- FalseClass - FalseClass
tags: []
YAML 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 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') assert_equal YAML.load(expected_yaml), YAML.load_file('test/files/.spectro/undefined.yml')
Expand Down
6 changes: 4 additions & 2 deletions test/minitest/spectro/test_http_client.rb
Expand Up @@ -18,8 +18,8 @@ def test_upload_undefined_specs
--- ---
undefined_sample.rb: undefined_sample.rb:
- !ruby/object:Spectro::Spec - !ruby/object:Spectro::Spec
md5: 23d8f3f75459cc94364520d99717a284
description: '' description: ''
md5: 23d8f3f75459cc94364520d99717a284
rules: rules:
- !ruby/object:Spectro::Spec::Rule - !ruby/object:Spectro::Spec::Rule
output: !ruby/class 'TrueClass' output: !ruby/class 'TrueClass'
Expand All @@ -28,10 +28,11 @@ def test_upload_undefined_specs
name: i_am_undefined name: i_am_undefined
output_type: TrueClass output_type: TrueClass
params_types: [] params_types: []
tags: []
sample.rb: sample.rb:
- !ruby/object:Spectro::Spec - !ruby/object:Spectro::Spec
md5: d10062f3fefde7c4b1388be2cb7c7bb6
description: "Multi-line description \\nWith blank line in the middle" description: "Multi-line description \\nWith blank line in the middle"
md5: d10062f3fefde7c4b1388be2cb7c7bb6
rules: rules:
- !ruby/object:Spectro::Spec::Rule - !ruby/object:Spectro::Spec::Rule
output: true output: true
Expand All @@ -42,6 +43,7 @@ def test_upload_undefined_specs
output_type: TrueClass output_type: TrueClass
params_types: params_types:
- FalseClass - FalseClass
tags: []
BODY BODY


Dir.chdir('test/files') do Dir.chdir('test/files') do
Expand Down
11 changes: 8 additions & 3 deletions test/minitest/spectro/test_spec.rb
Expand Up @@ -10,11 +10,12 @@ def setup
@rule = Spectro::Spec::Rule.new([1, 2], 3) @rule = Spectro::Spec::Rule.new([1, 2], 3)
@signature = Spectro::Spec::Signature.new('local_name', ['Fixnum'], 'Fixnum') @signature = Spectro::Spec::Signature.new('local_name', ['Fixnum'], 'Fixnum')
@description = "Random Description In Here" @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 end


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


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


Expand Down

0 comments on commit 0c99456

Please sign in to comment.