cql 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gem 'cucumber'
3
+ gem 'rspec'
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ gql
2
+ ===
3
+
4
+ Gherkin Query Language
data/cql.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "cucumber/platform"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'cql'
7
+ s.version = "0.0.1"
8
+ s.authors = ["Jarrod Folino"]
9
+ s.description = 'Cucumber Query Language'
10
+ s.summary = "cucumber-#{s.version}"
11
+ s.email = 'jdfolino@gmail.com'
12
+
13
+ s.platform = Gem::Platform::RUBY
14
+ s.post_install_message = %{
15
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
16
+
17
+ Thank you for installing gql (Gherkin Query Language)
18
+
19
+ (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::) (::)
20
+
21
+ }
22
+
23
+ s.add_runtime_dependency 'gherkin', '~> 2.8.0'
24
+ s.add_runtime_dependency 'json', '>= 1.4.6'
25
+
26
+ s.add_development_dependency 'rake', '>= 0.9.2'
27
+ s.add_development_dependency 'rspec', '~> 2.7.0'
28
+
29
+
30
+ s.rubygems_version = ">= 1.6.1"
31
+ s.files = `git ls-files`.split("\n").reject {|path| path =~ /\.gitignore$/ }
32
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
33
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_path = "lib"
36
+ end
data/fixtures/.gitkeep ADDED
File without changes
@@ -0,0 +1,15 @@
1
+ Feature: Test Feature
2
+
3
+ Scenario Outline: An Outline
4
+ Given something happend
5
+ Then I expect something else
6
+ Examples:
7
+ | var_a | var_b |
8
+ | 1 | a |
9
+ | 2 | b |
10
+ | 3 | c |
11
+ | 4 | d |
12
+
13
+ Scenario: A Scenario
14
+ Given something happend
15
+ Then I expect something else
@@ -0,0 +1,8 @@
1
+ Feature: Simple
2
+
3
+ Scenario: Has a table
4
+ Given Something
5
+ |a|a|
6
+ |s|a|
7
+ |s|s|
8
+ Then something else
@@ -0,0 +1,9 @@
1
+ Feature: Test Feature
2
+
3
+ Scenario: Testing the slurping
4
+ Given something happend
5
+ Then I expect something else
6
+
7
+ Scenario: Testing the slurping not to be found
8
+ Given something happend
9
+ Then I expect something else
@@ -0,0 +1,5 @@
1
+ Feature: Test2 Feature
2
+
3
+ Scenario: Testing the slurping
4
+ Given something happend
5
+ Then I expect something else
@@ -0,0 +1,22 @@
1
+ @top-tag
2
+ Feature: Test3 Feature
3
+
4
+ @one
5
+ Scenario: Testing the slurping
6
+ Given something happend
7
+ Then I expect something else
8
+
9
+ @two
10
+ Scenario: Testing again
11
+ Given something happend
12
+ Then I expect something else
13
+
14
+ @one
15
+ Scenario: Testing yet again
16
+ Given something happend
17
+ Then I expect something else
18
+
19
+ @one @two
20
+ Scenario: Testing yet again part 2
21
+ Given something happend
22
+ Then I expect something else
@@ -0,0 +1,22 @@
1
+ @top-tag
2
+ Feature: Test3 Feature
3
+
4
+ @one
5
+ Scenario: Testing the slurping
6
+ Given something happend
7
+ Then I expect something else
8
+
9
+ @two
10
+ Scenario: Testing again
11
+ Given something happend
12
+ Then I expect something else
13
+
14
+ @one
15
+ Scenario: Testing yet again
16
+ Given something happend
17
+ Then I expect something else
18
+
19
+ @one @two
20
+ Scenario: Testing yet again part 2
21
+ Given something happend
22
+ Then I expect something else
@@ -0,0 +1,8 @@
1
+ Feature: Simple
2
+
3
+ Scenario: Has a table
4
+ Given Something
5
+ |a|a|
6
+ |s|a|
7
+ |s|s|
8
+ Then something else
@@ -0,0 +1,8 @@
1
+ Feature: Simple
2
+
3
+ Scenario: Has a table
4
+ Given Something
5
+ |a|a|
6
+ |s|a|
7
+ |s|s|
8
+ Then something else
@@ -0,0 +1,10 @@
1
+ @one
2
+ Feature: Test Feature
3
+
4
+ Scenario: Testing the slurping
5
+ Given something happend
6
+ Then I expect something else
7
+
8
+ Scenario: Testing the slurping not to be found
9
+ Given something happend
10
+ Then I expect something else
@@ -0,0 +1,6 @@
1
+ @two
2
+ Feature: Test2 Feature
3
+
4
+ Scenario: Testing the slurping
5
+ Given something happend
6
+ Then I expect something else
@@ -0,0 +1,22 @@
1
+ @one @two
2
+ Feature: Test3 Feature
3
+
4
+ @one
5
+ Scenario: Testing the slurping
6
+ Given something happend
7
+ Then I expect something else
8
+
9
+ @two
10
+ Scenario: Testing again
11
+ Given something happend
12
+ Then I expect something else
13
+
14
+ @one
15
+ Scenario: Testing yet again
16
+ Given something happend
17
+ Then I expect something else
18
+
19
+ @one @two
20
+ Scenario: Testing yet again part 2
21
+ Given something happend
22
+ Then I expect something else
@@ -0,0 +1,24 @@
1
+ Feature: Simple
2
+
3
+ @two
4
+ Scenario: Has a table
5
+ Given Something
6
+ | a | a |
7
+ | s | a |
8
+ | s | s |
9
+ Then something else
10
+
11
+ @one
12
+ Scenario: Next
13
+ Given anything
14
+
15
+ @two
16
+ Scenario: Blah
17
+ Given lalala
18
+
19
+ @one
20
+ Scenario: Another
21
+ Given a car
22
+
23
+ Scenario: Yet Another
24
+ Given a car
@@ -0,0 +1,24 @@
1
+ Feature: Simple
2
+
3
+ @two
4
+ Scenario: Has a table
5
+ Given Something
6
+ | a | a |
7
+ | s | a |
8
+ | s | s |
9
+ Then something else
10
+
11
+ @one
12
+ Scenario: Next
13
+ Given anything
14
+
15
+ Scenario: Another
16
+ Given a car
17
+
18
+ @two
19
+ Scenario: Blah
20
+ Given lalala
21
+
22
+ @one
23
+ Scenario: Another
24
+ Given a car
@@ -0,0 +1,24 @@
1
+ Feature: Simple 2
2
+
3
+ @two @four
4
+ Scenario: Has a table hmmm
5
+ Given Something
6
+ | a | a |
7
+ | s | a |
8
+ | s | s |
9
+ Then something else
10
+
11
+ @one @five
12
+ Scenario: Next
13
+ Given anything
14
+
15
+ Scenario: Another
16
+ Given a car
17
+
18
+ @two
19
+ Scenario: Blah blah
20
+ Given lalala
21
+
22
+ @one
23
+ Scenario: Another
24
+ Given a car
data/lib/dsl.rb ADDED
@@ -0,0 +1,51 @@
1
+ module CQL
2
+ module Dsl
3
+
4
+ %w(names features scenario_outlines uri scenarios).each do |method_name|
5
+ define_method(method_name) { method_name }
6
+ end
7
+
8
+ def select what
9
+ @what = what
10
+ end
11
+
12
+ def from where
13
+ @from = where
14
+ @data
15
+ end
16
+
17
+ def tags *tags
18
+ {'tags'=>tags}
19
+ end
20
+
21
+ def with filter
22
+ if filter.has_key? 'tags'
23
+ @data = CQL::MapReduce.find_feature(@data, 'tags'=>filter['tags'])
24
+ end
25
+ @data
26
+ end
27
+
28
+ class Query
29
+ include Dsl
30
+ attr_reader :data, :what
31
+
32
+ def initialize features, &block
33
+ @data = features
34
+ @data = self.instance_eval(&block)
35
+ key = @what + "-" + @from
36
+ if key=="uri-features"
37
+ @data = CQL::MapReduce.uri(@data)
38
+ elsif key== "names-features"
39
+ @data = CQL::MapReduce.overview(@data)
40
+ elsif key== "names-scenario_outlines"
41
+ @data= CQL::MapReduce.find_child(@data, 'what'=>'scenario_outline')
42
+ elsif key== "names-scenarios"
43
+ @data = CQL::MapReduce.find_child(@data, 'what'=>'scenario')
44
+ end
45
+ @data
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
@@ -0,0 +1,68 @@
1
+ module CQL
2
+ class MapReduce
3
+
4
+ def self.overview input
5
+ input.class == Array ? input.map { |a| a['name'] } : input['name']
6
+ end
7
+
8
+ def self.uri input
9
+ input.map { |a| a['uri'] }
10
+ end
11
+
12
+ def self.find_feature input, args
13
+ input = input.find_all { |feature| feature['name'] == args['feature'] } if args.has_key? 'feature'
14
+ input = input.find_all { |feature| has_tags feature['tags'], args['tags'] } if args.has_key? 'tags'
15
+ if input.size == 1
16
+ input = input.first
17
+ end
18
+ input
19
+ end
20
+
21
+ def self.scenario_by_feature_and_tag input, feature_to_find, condition, *tags_to_find
22
+ scenarios = []
23
+ input.each do |feature|
24
+ if feature['name'] == feature_to_find
25
+ feature['elements'].each do |element|
26
+ if element['type'] == "scenario" and has_tags(element['tags'], tags_to_find) == condition
27
+ scenarios.push element['name']
28
+ end
29
+ end
30
+ end
31
+ end
32
+ scenarios
33
+ end
34
+
35
+ def self.scenario input, args
36
+ input = find_feature(input, 'feature'=>args['feature'])
37
+ input['elements'].find{|element|element['name'] == args['child_name'] }
38
+ end
39
+
40
+ def self.find_child input, args
41
+ results = []
42
+ input = [find_feature(input, 'feature'=>args['feature'])] if args.has_key?('feature')
43
+ input.each do |feature|
44
+ feature['elements'].each do |element|
45
+ results.push element['name'] if element['type'] == args['what']
46
+ end
47
+ end
48
+ results
49
+ end
50
+
51
+ def self.tags input
52
+ tags = Set.new
53
+ input.each do |feature|
54
+ feature['elements'].each do |element|
55
+ break if element['tags'] == nil
56
+ element['tags'].each { |tag| tags.add tag['name'] }
57
+ end
58
+ end
59
+ tags.to_a
60
+ end
61
+
62
+ def self.has_tags given, search
63
+ return false if given == nil
64
+ search.count { |tag_for_search| given.map { |t| t["name"] }.include?(tag_for_search) }==search.size
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,38 @@
1
+ require 'gherkin/parser/parser'
2
+ require 'gherkin/formatter/json_formatter'
3
+ require 'stringio'
4
+ require 'json'
5
+ require 'set'
6
+
7
+ require File.dirname(__FILE__) + "/gherkin_map_reduce"
8
+ require File.dirname(__FILE__) + "/dsl"
9
+
10
+ module CQL
11
+ class GherkinRepository
12
+ include Dsl
13
+ attr_reader :parsed_feature_files
14
+
15
+ def initialize features_home_dir
16
+ @parsed_feature_files = load_features(list_features(features_home_dir))
17
+ end
18
+
19
+ def query &block
20
+ Query.new(parsed_feature_files.clone, &block).data
21
+ end
22
+
23
+ private
24
+ def list_features base_dir
25
+ Dir.glob(base_dir + "/**/*.feature")
26
+ end
27
+
28
+ def load_features sources
29
+ io = StringIO.new
30
+ formatter = Gherkin::Formatter::JSONFormatter.new(io)
31
+ parser = Gherkin::Parser::Parser.new(formatter)
32
+ sources.each { |s| parser.parse(IO.read(s), s, 0) }
33
+ formatter.done
34
+ JSON.parse(io.string)
35
+ end
36
+
37
+ end
38
+ end