diff --git a/lib/spectro/spec.rb b/lib/spectro/spec.rb index 55a3183..0e7a0bb 100644 --- a/lib/spectro/spec.rb +++ b/lib/spectro/spec.rb @@ -6,24 +6,27 @@ 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 [] 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 @@ -31,7 +34,8 @@ def to_hash 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 diff --git a/lib/spectro/spec/parser.rb b/lib/spectro/spec/parser.rb index a0c3ff1..bf31005 100644 --- a/lib/spectro/spec/parser.rb +++ b/lib/spectro/spec/parser.rb @@ -39,17 +39,23 @@ 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) @@ -57,7 +63,7 @@ def parse_spec 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 # 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 [] 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 diff --git a/test/files/.spectro/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb b/test/files/.spectro/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb deleted file mode 100644 index 73cf623..0000000 --- a/test/files/.spectro/cache/2e0dcba288b50596b4cdd8680fffbf2e.rb +++ /dev/null @@ -1,7 +0,0 @@ -# THIS FILE HAS BEEN AUTOGENERATED BY CS -# DO NOT MODIFY ITS CONTENT. IT WILL BE -# OVERWRITTEN. - -->(f1, f2, f3) { - return f3 * f2 / f1 -} diff --git a/test/files/.spectro/cache/7e46ad07603d4044a8d70c9004c48adf.rb b/test/files/.spectro/cache/7e46ad07603d4044a8d70c9004c48adf.rb deleted file mode 100644 index 035bba9..0000000 --- a/test/files/.spectro/cache/7e46ad07603d4044a8d70c9004c48adf.rb +++ /dev/null @@ -1,7 +0,0 @@ -# THIS FILE HAS BEEN AUTOGENERATED BY CS -# DO NOT MODIFY ITS CONTENT. IT WILL BE -# OVERWRITTEN. - -->(s1) { - return "Say Hello to #{s1}" -} diff --git a/test/files/.spectro/cache/aac019283279bcb5f59f9760865f7d77.rb b/test/files/.spectro/cache/aac019283279bcb5f59f9760865f7d77.rb deleted file mode 100644 index 7e44523..0000000 --- a/test/files/.spectro/cache/aac019283279bcb5f59f9760865f7d77.rb +++ /dev/null @@ -1,7 +0,0 @@ -# THIS FILE HAS BEEN AUTOGENERATED BY CS -# DO NOT MODIFY ITS CONTENT. IT WILL BE -# OVERWRITTEN. - -->(i1, i2) { - return i1 + i2 -} diff --git a/test/files/.spectro/index.yml b/test/files/.spectro/index.yml index cdd6132..0e35ffb 100644 --- a/test/files/.spectro/index.yml +++ b/test/files/.spectro/index.yml @@ -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 diff --git a/test/files/sample.rb b/test/files/sample.rb index eb05ef7..ee89b60 100644 --- a/test/files/sample.rb +++ b/test/files/sample.rb @@ -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 diff --git a/test/minitest/spectro/test_compiler.rb b/test/minitest/spectro/test_compiler.rb index ec1f582..a2641d8 100644 --- a/test/minitest/spectro/test_compiler.rb +++ b/test/minitest/spectro/test_compiler.rb @@ -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') diff --git a/test/minitest/spectro/test_http_client.rb b/test/minitest/spectro/test_http_client.rb index 4ede4df..6788da5 100644 --- a/test/minitest/spectro/test_http_client.rb +++ b/test/minitest/spectro/test_http_client.rb @@ -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 diff --git a/test/minitest/spectro/test_spec.rb b/test/minitest/spectro/test_spec.rb index f6cb5e3..cb7314a 100644 --- a/test/minitest/spectro/test_spec.rb +++ b/test/minitest/spectro/test_spec.rb @@ -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