gherkin3_reformat 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +7 -0
- data/bin/console +14 -0
- data/bin/gherkin3_reformat +26 -0
- data/bin/setup +7 -0
- data/gherkin3_reformat.gemspec +27 -0
- data/lib/gherkin3_reformat.rb +139 -0
- data/lib/gherkin3_reformat/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 729547e698b28b14aac4ed057af2e803eab98353
|
4
|
+
data.tar.gz: 80a9d16ae0bb38fa876ed141377934a25f5819c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff6e7a64c2cec9e80072d8a48ca987c7a19c361b5f9fb5d27389c8e7de47299216c1b65c958555a8fbfa332f06bda2751d633ed9d4cc8a7eb21a0287f2da8640
|
7
|
+
data.tar.gz: 9a1f732380078608dbf0900f24900d6a9cc8fde8557779b164d7ebf2ae3a94fb8932189d7d5495f02b207247730aa75268163c70d7d9d320028ff6607ffb9775
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 James Pickering
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Gherkin3Reformat
|
2
|
+
|
3
|
+
A script to reformat Gherkin3 code, to a consistent style.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gherkin3_reformat'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gherkin3_reformat
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```bash
|
24
|
+
gherkin3_reformat features/myfeature.feature > features/myfeature_reformatted.feature
|
25
|
+
gherkin3_reformat --replace features/*.feature
|
26
|
+
```
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
31
|
+
|
32
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gherkin3_reformat.
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
42
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gherkin3_reformat"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'gherkin3_reformat'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
options = {}
|
6
|
+
OptionParser.new do |opts|
|
7
|
+
opts.banner = "Usage: gherkin3_reformat [options] filenames"
|
8
|
+
|
9
|
+
opts.on("-r", "--[no-]replace", "Replace original files") do |r|
|
10
|
+
options[:replace] = r
|
11
|
+
end
|
12
|
+
end.parse!
|
13
|
+
|
14
|
+
ARGV.each do |filename|
|
15
|
+
begin
|
16
|
+
output = Gherkin3Reformat.format_file(filename)
|
17
|
+
if options[:replace] then
|
18
|
+
File.write(filename, output)
|
19
|
+
else
|
20
|
+
puts output
|
21
|
+
end
|
22
|
+
rescue => e
|
23
|
+
STDERR.puts "Error formatting #{filename}: #{e}"
|
24
|
+
STDERR.puts e.backtrace.join("\n")
|
25
|
+
end
|
26
|
+
end
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gherkin3_reformat/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gherkin3_reformat"
|
8
|
+
spec.version = Gherkin3Reformat::VERSION
|
9
|
+
spec.authors = ["James Pickering"]
|
10
|
+
spec.email = ["james_pic@hotmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Tool to reformat Gherkin3 files}
|
13
|
+
spec.description = %q{Tool to reformat Gherkin3 files to a consistent style}
|
14
|
+
spec.homepage = "http://github.com/jamespic/gherkin3_reformat"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
|
19
|
+
spec.bindir = "bin"
|
20
|
+
spec.executables = ['gherkin3_reformat']
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.3.0"
|
26
|
+
spec.add_dependency "gherkin3", "~> 3.1.2"
|
27
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require "gherkin3_reformat/version"
|
2
|
+
|
3
|
+
require 'gherkin3/parser'
|
4
|
+
|
5
|
+
module Gherkin3Reformat
|
6
|
+
def self.format(feature)
|
7
|
+
items = []
|
8
|
+
items.push(feature)
|
9
|
+
feature[:comments].each {|comment| items.push(comment)}
|
10
|
+
items.push(feature[:background]) if feature.has_key?(:background)
|
11
|
+
feature[:scenarioDefinitions].each do |scenario|
|
12
|
+
items.push(scenario)
|
13
|
+
if scenario.has_key?(:examples) then
|
14
|
+
scenario[:examples].each do |example|
|
15
|
+
items.push(example)
|
16
|
+
table = [example[:tableHeader]] + example[:tableBody]
|
17
|
+
items.concat(self.handle_table(table, 4))
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
scenario[:tags].each{|tag| items.push(tag)}
|
22
|
+
scenario[:steps].each do |step|
|
23
|
+
if step.has_key?(:argument) then
|
24
|
+
argument = step[:argument]
|
25
|
+
case argument[:type]
|
26
|
+
when :DataTable
|
27
|
+
items.concat(self.handle_table(argument[:rows], 6))
|
28
|
+
when :DocString
|
29
|
+
items.push(argument)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
items.push(step)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
items.sort! do |a, b|
|
37
|
+
linecomp = a[:location][:line] <=> b[:location][:line]
|
38
|
+
linecomp != 0 ? linecomp : a[:location][:column] <=> b[:location][:column]
|
39
|
+
end
|
40
|
+
|
41
|
+
self.calculate_comment_indents_and_add_newlines_before_scenarios(items)
|
42
|
+
|
43
|
+
items.map {|item| self.pretty_print(item)}.join('')
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.format_string(data)
|
47
|
+
parser = Gherkin3::Parser.new
|
48
|
+
self.format(parser.parse(data))
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.format_file(filename)
|
52
|
+
parser = Gherkin3::Parser.new
|
53
|
+
File.open(filename) do |file|
|
54
|
+
self.format(parser.parse(file))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def self.pretty_print(node)
|
61
|
+
formatted = case node[:type]
|
62
|
+
when :Feature
|
63
|
+
self.pretty_print_node_with_blurb_and_indent(node, 0)
|
64
|
+
when :Background, :Scenario, :ScenarioOutline, :Examples
|
65
|
+
self.pretty_print_node_with_blurb_and_indent(node, 2)
|
66
|
+
when :Step
|
67
|
+
"#{' ' * 4}#{node[:keyword]}#{node[:text]}\n"
|
68
|
+
when 'TableRow'
|
69
|
+
"#{' ' * node[:indent]}| #{node[:cells].map{|cell| cell[:value].ljust(cell[:maxWidth], ' ')}.join(' | ')} |\n"
|
70
|
+
when :DocString
|
71
|
+
self.pretty_print_docstring(node)
|
72
|
+
when 'Comment'
|
73
|
+
"#{' ' * node[:indent]}#{node[:text].strip}\n"
|
74
|
+
end
|
75
|
+
if node[:precedingNewline] then "\n#{formatted}" else formatted end
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.pretty_print_node_with_blurb_and_indent(node, indent)
|
79
|
+
tags = node[:tags].any? ? "#{' ' * indent}#{node[:tags].map {|tag| tag[:name]}.join(' ')}\n" : ''
|
80
|
+
first_row = "#{' ' * indent}#{node[:keyword]}: #{node[:name].strip}".rstrip
|
81
|
+
description = node.has_key?(:description) ? node[:description].split("\n").map {|row| (' ' * (indent + 2)) + row.strip}.join("\n") + "\n" : ''
|
82
|
+
"#{tags}#{first_row}\n#{description}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.handle_table(table, indent)
|
86
|
+
widths = table.map{
|
87
|
+
|row| row[:cells].map{|cell| cell[:value].size}
|
88
|
+
}.reduce{
|
89
|
+
|row1, row2| row1.zip(row2).map(&:max)
|
90
|
+
}
|
91
|
+
table.map do |row|
|
92
|
+
row[:cells].zip(widths).each do |cell, width|
|
93
|
+
cell[:maxWidth] = width
|
94
|
+
end
|
95
|
+
row[:indent] = indent
|
96
|
+
row
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.pretty_print_docstring(docstring)
|
101
|
+
%Q{#{' ' * 6}"""#{docstring[:contentType]}\n#{' ' * 6}#{docstring[:content].gsub("\n", "\n#{' ' * 6}")}\n#{' ' * 6}"""\n}
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.calculate_comment_indents_and_add_newlines_before_scenarios(items)
|
105
|
+
target_for_preceding_newline = nil
|
106
|
+
indent = 0
|
107
|
+
attach_any_newlines = lambda do
|
108
|
+
target_for_preceding_newline[:precedingNewline] = true if target_for_preceding_newline
|
109
|
+
target_for_preceding_newline = nil
|
110
|
+
end
|
111
|
+
items.reverse.each do |item|
|
112
|
+
case item[:type]
|
113
|
+
when :Feature
|
114
|
+
attach_any_newlines.call
|
115
|
+
indent = 0
|
116
|
+
when :Background, :Scenario, :ScenarioOutline
|
117
|
+
attach_any_newlines.call
|
118
|
+
indent = 2
|
119
|
+
target_for_preceding_newline = item
|
120
|
+
when :Step
|
121
|
+
attach_any_newlines.call
|
122
|
+
indent = 4
|
123
|
+
when :Examples
|
124
|
+
attach_any_newlines.call
|
125
|
+
indent = 2
|
126
|
+
when 'TableRow'
|
127
|
+
attach_any_newlines.call
|
128
|
+
indent = item[:indent]
|
129
|
+
when :DocString
|
130
|
+
attach_any_newlines.call
|
131
|
+
indent = 6
|
132
|
+
when 'Comment'
|
133
|
+
target_for_preceding_newline = item if target_for_preceding_newline
|
134
|
+
item[:indent] = indent
|
135
|
+
end
|
136
|
+
end
|
137
|
+
attach_any_newlines.call
|
138
|
+
end
|
139
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gherkin3_reformat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Pickering
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.3.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.3.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: gherkin3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.1.2
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.1.2
|
69
|
+
description: Tool to reformat Gherkin3 files to a consistent style
|
70
|
+
email:
|
71
|
+
- james_pic@hotmail.com
|
72
|
+
executables:
|
73
|
+
- gherkin3_reformat
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/gherkin3_reformat
|
85
|
+
- bin/setup
|
86
|
+
- gherkin3_reformat.gemspec
|
87
|
+
- lib/gherkin3_reformat.rb
|
88
|
+
- lib/gherkin3_reformat/version.rb
|
89
|
+
homepage: http://github.com/jamespic/gherkin3_reformat
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Tool to reformat Gherkin3 files
|
113
|
+
test_files: []
|