ruby-beautify 0.96.0 → 0.97.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module RubyBeautify
2
- VERSION = "0.96.0"
2
+ VERSION = "0.97.0"
3
3
  end
@@ -1,22 +1,41 @@
1
- # one big spec (for now).
2
1
  require 'spec_helper.rb'
3
- require 'digest/md5'
4
-
5
- # this is so simple, it's stupid. I basically used the 'example.rb' file while
6
- # testing this indenting method. So I've visually approved of the output, and
7
- # I've run that through md5. Then we just run our build, gather that md5 and
8
- # compare the two.
9
- # Eventually, I should break out the code into smaller chunks so I can identify
10
- # which component is breaking down.
11
2
 
12
3
  describe "Ruby Beautify" do
13
- before (:all) do
14
- @good_md5_sum = Digest::MD5.hexdigest File.read('spec/example_beautified.rb')
15
- @bad_file = 'spec/example.rb'
16
- @bin = "#{Dir.pwd}/bin/ruby-beautify"
17
- end
18
- it 'will work' do
19
- md5_sum = Digest::MD5.hexdigest `bundle exec #{@bin} #{@bad_file}`
20
- expect(md5_sum).to eq @good_md5_sum
21
- end
4
+ # This acts as a config block for files and anything else that you may set.
5
+ before (:all) do
6
+ # a very quick to parse file, since this isn't to test the function of the parser (but the function of the binary).
7
+ @small_file = 'spec/binary_scenarios/small_example.rb'
8
+ # should be the contents of the small file twice.
9
+ @doubled_file = 'spec/binary_scenarios/doubled_example.rb'
10
+ # Our file to overwrite (should be ugly to start).
11
+ @overwrite_file = "spec/binary_scenarios/overwrite.rb"
12
+ @overwrite_target_file = "tmp/copied.rb"
13
+ @overwrite_pretty_file = "spec/binary_scenarios/overwrite_pretty.rb"
14
+ end
15
+
16
+ it "will work" do
17
+ small_md5_sum = Digest::MD5.hexdigest File.read(@small_file)
18
+ md5_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} #{@small_file}`
19
+ expect(md5_sum).to eq small_md5_sum
20
+ end
21
+
22
+ it "will do multiple files" do
23
+ md5_sum = Digest::MD5.hexdigest `bundle exec #{BEAUTIFY_BIN} #{@small_file} #{@small_file}`
24
+ doubled_md5_sum = Digest::MD5.hexdigest File.read(@doubled_file)
25
+ expect(md5_sum).to eq doubled_md5_sum
26
+ end
27
+
28
+ # I want to make sure the file actually changes, so I do this (I could make yet another file).
29
+ it "will update files (overwrite) in place" do
30
+ FileUtils.mkdir_p File.dirname @overwrite_target_file
31
+ FileUtils.cp @overwrite_file, @overwrite_target_file
32
+ `bundle exec #{BEAUTIFY_BIN} --overwrite #{@overwrite_target_file}`
33
+ md5_sum = Digest::MD5.hexdigest File.read @overwrite_target_file
34
+ overwrite_md5_sum = Digest::MD5.hexdigest File.read(@overwrite_pretty_file)
35
+ expect(md5_sum).to eq overwrite_md5_sum
36
+ FileUtils.rm @overwrite_target_file
37
+ end
38
+
39
+ it "will honor --tabs vs --spaces"
40
+ it "will honor the count prefix"
22
41
  end
@@ -0,0 +1,6 @@
1
+ def small
2
+ puts 'example'
3
+ end
4
+ def small
5
+ puts 'example'
6
+ end
@@ -0,0 +1,6 @@
1
+ def ugly
2
+ puts 'this'
3
+ if true
4
+ puts 'is ugly enough'
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ def ugly
2
+ puts 'this'
3
+ if true
4
+ puts 'is ugly enough'
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ def small
2
+ puts 'example'
3
+ end
@@ -1,2 +1,9 @@
1
+ require 'digest/md5'
2
+
1
3
  require "codeclimate-test-reporter"
2
4
  CodeClimate::TestReporter.start
5
+
6
+ require 'ruby-beautify'
7
+
8
+
9
+ BEAUTIFY_BIN = "#{Dir.pwd}/bin/ruby-beautify"
@@ -0,0 +1,44 @@
1
+ ## This Directory
2
+
3
+ The examples in this directory are not direct examples but file pairs we want to consume/test to ensure that what we want to see is what we get.
4
+
5
+ For example, the most basic test would be something like:
6
+
7
+ ```ruby
8
+ def this_is_ugly(nowhere)
9
+ if true
10
+ puts true
11
+ else
12
+ puts false
13
+ end
14
+ end
15
+ ```
16
+
17
+ We would expect this to look like:
18
+
19
+ ```ruby
20
+ def this_is_ugly(nowhere)
21
+ if true
22
+ puts true
23
+ else
24
+ puts false
25
+ end
26
+ end
27
+ ```
28
+
29
+ Notice we are testing that we get the *actual* output we want. Not the configuratbility of the binary or our output but rather the representation of ugly vs beauty.
30
+
31
+ We can expect a high level of redundancy in these specific examples, and that is ok. We want to ensure that as we make changes to the binary, we don't break the possible ways people use Ruby. I don't care if one file tests something another file does, as long as our ideas of what we want prettied up are throughly tested.
32
+
33
+ We should avoid testing the binary. Therefor, the tests will always be run with the binary in *default* mode. Testing the binary's options can be handled elsewhere.
34
+
35
+ ## File names
36
+
37
+ Each usage pair of files will need to be in a specific format of:
38
+
39
+ ```
40
+ test_or_whatever.rb
41
+ test_or_whatever_pretty.rb
42
+ ```
43
+
44
+ With the pretty file being what we want it to look like by default. The spec framework will loop through, consume the pairs, execute the parsing method and print out the results. This will allow for dynamic testing and migrating from one framework to another if need be.
@@ -0,0 +1,11 @@
1
+ # Test to validate case statements
2
+ def case_test opts=nil
3
+ case test_case
4
+ when 'Passing'
5
+ call(:pass)
6
+ when 'Failing'
7
+ call(:fail)
8
+ when 'Error'
9
+ call(:error)
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ # Test to validate case statements
2
+ def case_test opts=nil
3
+ case test_case
4
+ when 'Passing'
5
+ call(:pass)
6
+ when 'Failing'
7
+ call(:fail)
8
+ when 'Error'
9
+ call(:error)
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ # Test for variable assignment from an if statement
2
+ @products = if params[:category]
3
+ Category.find(params[:category]).products
4
+ else
5
+ Product.all
6
+ end
7
+
8
+ # Test for variable assignment from a block
9
+ response = begin
10
+ if true?
11
+ api_call(test)
12
+ else
13
+ rejected
14
+ end
15
+ rescue
16
+ 'FALSE'
17
+ end
@@ -0,0 +1,17 @@
1
+ # Test for variable assignment from an if statement
2
+ @products = if params[:category]
3
+ Category.find(params[:category]).products
4
+ else
5
+ Product.all
6
+ end
7
+
8
+ # Test for variable assignment from a block
9
+ response = begin
10
+ if true?
11
+ api_call(test)
12
+ else
13
+ rejected
14
+ end
15
+ rescue
16
+ 'FALSE'
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'ripper'
2
+
3
+ h = Here::There.new
4
+
5
+ h.why?({
6
+ here: 'there',
7
+ there: 'here'
8
+ })
9
+
10
+ h.with_block {
11
+ puts 'yahooie'
12
+ }
13
+
14
+
15
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
16
+ 'alfkjsdlkfjsflgkjfglk',
17
+ 'alfkjsdlkfjsflgkjfglk',
18
+ 'alfkjsdlkfjsflgkjfglk'
19
+ )
20
+
21
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
22
+ 'alfkjsdlkfjsflgkjfglk',
23
+ 'alfkjsdlkfjsflgkjfglk',
24
+ 'alfkjsdlkfjsflgkjfglk'
25
+ ).map do |i|
26
+ i
27
+ end.map! { |i| i }
28
+
29
+ if 1 > 0
30
+ puts 'something'
31
+ elsif 1 < 0
32
+ puts 'never!'
33
+ else
34
+ puts 'not likely.'
35
+ end
36
+
37
+ # Flattened formatting
38
+ if 1 > 0 then puts 'something'
39
+ else puts 'nothing' end
40
+ puts "This line should stay the same"
@@ -0,0 +1,40 @@
1
+ require 'ripper'
2
+
3
+ h = Here::There.new
4
+
5
+ h.why?({
6
+ here: 'there',
7
+ there: 'here'
8
+ })
9
+
10
+ h.with_block {
11
+ puts 'yahooie'
12
+ }
13
+
14
+
15
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
16
+ 'alfkjsdlkfjsflgkjfglk',
17
+ 'alfkjsdlkfjsflgkjfglk',
18
+ 'alfkjsdlkfjsflgkjfglk'
19
+ )
20
+
21
+ h.complex_method('asdkjasflkjdglksjglksfgjlfkgjdf',
22
+ 'alfkjsdlkfjsflgkjfglk',
23
+ 'alfkjsdlkfjsflgkjfglk',
24
+ 'alfkjsdlkfjsflgkjfglk'
25
+ ).map do |i|
26
+ i
27
+ end.map! { |i| i }
28
+
29
+ if 1 > 0
30
+ puts 'something'
31
+ elsif 1 < 0
32
+ puts 'never!'
33
+ else
34
+ puts 'not likely.'
35
+ end
36
+
37
+ # Flattened formatting
38
+ if 1 > 0 then puts 'something'
39
+ else puts 'nothing' end
40
+ puts "This line should stay the same"
@@ -0,0 +1,10 @@
1
+ # Test for multiline string
2
+ def m (x)
3
+ puts "This is multi-line string. It's line1 \
4
+ It's line 2\
5
+ And this is\n line3
6
+ This line should not be mutated"
7
+ puts "This is multi line interpolated string #{
8
+ x
9
+ }"
10
+ end
@@ -0,0 +1,10 @@
1
+ # Test for multiline string
2
+ def m (x)
3
+ puts "This is multi-line string. It's line1 \
4
+ It's line 2\
5
+ And this is\n line3
6
+ This line should not be mutated"
7
+ puts "This is multi line interpolated string #{
8
+ x
9
+ }"
10
+ end
@@ -0,0 +1,34 @@
1
+ module Here
2
+ class There
3
+ def why?(argument = nil)
4
+ @array = [
5
+ 1,
6
+ 2,
7
+ 3
8
+ ]
9
+ hash = {
10
+ one:1,
11
+ two:2,
12
+ three:3
13
+ }
14
+ end
15
+
16
+ # a comment
17
+ def why_not?(argument: {})
18
+ @array = [4,5,6]
19
+ hash = {four:4, five:5, six:6}
20
+ s = "a #{"complex"} string."
21
+ end
22
+
23
+ def complex_method(one, two, three, four)
24
+ regex = /regex/
25
+ end
26
+
27
+ def with_block
28
+ run = Proc.new { |argument| puts arugment }
29
+ run do |arugment|
30
+ puts argument
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,34 @@
1
+ module Here
2
+ class There
3
+ def why?(argument = nil)
4
+ @array = [
5
+ 1,
6
+ 2,
7
+ 3
8
+ ]
9
+ hash = {
10
+ one:1,
11
+ two:2,
12
+ three:3
13
+ }
14
+ end
15
+
16
+ # a comment
17
+ def why_not?(argument: {})
18
+ @array = [4,5,6]
19
+ hash = {four:4, five:5, six:6}
20
+ s = "a #{"complex"} string."
21
+ end
22
+
23
+ def complex_method(one, two, three, four)
24
+ regex = /regex/
25
+ end
26
+
27
+ def with_block
28
+ run = Proc.new { |argument| puts arugment }
29
+ run do |arugment|
30
+ puts argument
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,21 @@
1
+ # Test or-equals
2
+ def expensive_value
3
+ @expensive_value ||= begin
4
+ if conn.active?
5
+ expensive_call
6
+ else
7
+ :none
8
+ end
9
+ end
10
+ end
11
+
12
+ # Test multiple or-equals
13
+ def expensive_value
14
+ @expensive_value ||= @cached_value ||= begin
15
+ if conn.active?
16
+ expensive_call
17
+ else
18
+ :none
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ # Test or-equals
2
+ def expensive_value
3
+ @expensive_value ||= begin
4
+ if conn.active?
5
+ expensive_call
6
+ else
7
+ :none
8
+ end
9
+ end
10
+ end
11
+
12
+ # Test multiple or-equals
13
+ def expensive_value
14
+ @expensive_value ||= @cached_value ||= begin
15
+ if conn.active?
16
+ expensive_call
17
+ else
18
+ :none
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ # Test for already indented blocks
2
+ class There2 < There
3
+ def m1()
4
+ puts "m1"
5
+ end
6
+ def m2()
7
+ puts "m2"
8
+ end
9
+ def m3()
10
+ puts "m3"
11
+ end
12
+ end