terse_ruby 0.1.0 → 0.1.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.
@@ -0,0 +1,22 @@
1
+ public class PhoneNumber {
2
+ public String type;
3
+ public String number;
4
+
5
+ public void testMethod1(String var) {
6
+
7
+ }
8
+
9
+ public static void testMethod2 {
10
+
11
+ }
12
+
13
+ private String privateMethod1;
14
+
15
+ protected Integer protMeth1(Float var);
16
+ return Integer.valueOf(var);
17
+
18
+ }
19
+
20
+
21
+
22
+ }
@@ -8,10 +8,10 @@ class AClass
8
8
  require_relative "morethings"
9
9
  puts "this is a real Ruby line"
10
10
  end
11
-
11
+
12
12
  def method2
13
13
  end
14
-
14
+
15
15
  def meth3
16
16
 
17
17
  attr_accessor :field1
@@ -30,13 +30,13 @@ module AModule
30
30
  def m3
31
31
 
32
32
  end
33
-
33
+
34
34
  def method2
35
35
  end
36
36
 
37
37
  def c m r i d e a r w
38
38
  end
39
-
39
+
40
40
  def wraedirmc
41
41
  end
42
42
  end
@@ -0,0 +1,75 @@
1
+ require "test/unit"
2
+ require_relative "../lib/terse_ruby"
3
+
4
+ class TestExpansionJava < Test::Unit::TestCase
5
+
6
+ @@terse_file = "test_file_java.txt"
7
+ @@expanded_file = "test_file_java.java"
8
+ @@premade_file = "expected_test_file_java.java"
9
+ @@argv_altered = false
10
+
11
+ # not used - invoking via cmd means that the gem has to be rebuild & reinstalled after every code change, which does not lend itself to a speedy dev process
12
+ # TODO add test that deliberately invokes via system
13
+ @@cmd = "ruby -e \"require 'terse_ruby'; TerseJava.scan_files ARGV\" #{@@terse_file}"
14
+
15
+ def switch_dir
16
+ # Change this so we know where the expaneded file will be created
17
+ @previous_wd ||= Dir.getwd # Preserve the old wd so we can switch back to it after the test
18
+ Dir.chdir File.expand_path(File.dirname(__FILE__))
19
+ end
20
+
21
+ def run_cmd
22
+ # system @@cmd # old mechanism; don't do this, call TerseRuby.scan_files ARGV instead
23
+
24
+ # ARGV is preserved from one test_ to the next, so we should only alter it once
25
+ unless @@argv_altered
26
+ ARGV << @@terse_file
27
+ ARGV << "-v"
28
+ @@argv_altered = true
29
+ end
30
+ TerseJava.scan_files ARGV
31
+ end
32
+
33
+ # Switch to the same directory as this unit-test file, and check that the expected file is not present
34
+ def setup
35
+ switch_dir
36
+ raise "terse_ruby unit-tests found a pre-existing #{@@expanded_file} in #{Dir.getwd}; unit-tests will clean up after themselves but will not alter pre-existing files" if File.file? @@expanded_file
37
+ raise "terse_ruby unit-tests could not locate the starting test file #{@@terse_file} in #{Dir.getwd}" unless File.file? @@terse_file
38
+ end
39
+
40
+ # Tests that the test file is expanded with the expected name
41
+ def test_expanded_name
42
+ assert_false(File.file?(@@expanded_file), "Expanded test file #{@@expanded_file} already existed before running test")
43
+ run_cmd
44
+ assert(File.exists?(@@expanded_file), "terse_ruby did not expand #{@@terse_file} into a file with name #{@@expanded_file}")
45
+ assert(File.file?(@@expanded_file), "terse_ruby did not expand #{@@terse_file} into a file with name #{@@expanded_file}")
46
+ end
47
+
48
+ # Tests that the test file is expanded into the expected form
49
+ # Expand the file, then compare the two line-by-line
50
+ def test_expansion
51
+ run_cmd
52
+ assert(File.file?(@@expanded_file), "terse_ruby did not expand #{@@terse_file} into a file with name #{@@expanded_file}")
53
+
54
+ expected_lines = File.readlines(@@premade_file)
55
+ actual_lines = File.readlines(@@expanded_file)
56
+
57
+ assert_equal(expected_lines.size, actual_lines.size, "Expanded file did not have the expected number of lines")
58
+
59
+ # As expected_lines and actual_lines are arrays, we can just use Array#==
60
+ assert_equal(expected_lines.sort, actual_lines.sort, "Expanded file did not have the expected lines in the expected order")
61
+ assert_equal(expected_lines, actual_lines, "Expanded file did not have the expected lines")
62
+ end
63
+
64
+ # Delete the expected file from the expected dir
65
+ def teardown
66
+ if File.file?(@@expanded_file)
67
+ count = File.delete @@expanded_file
68
+ raise "Teardown failed to delete expanded file #{@@expanded_file}" unless count == 1
69
+ raise "Teardown failed to delete expanded file #{@@expanded_file}" if File.file?(@@expanded_file)
70
+ puts "Teardown deleted expanded file #{@@expanded_file}"
71
+ end
72
+ Dir.chdir @previous_wd # restore the working directory to what it was previously
73
+ end
74
+
75
+ end
@@ -1,11 +1,15 @@
1
1
  require "test/unit"
2
2
  require_relative "../lib/terse_ruby"
3
3
 
4
- class TestExpansion < Test::Unit::TestCase
4
+ class TestExpansionRuby < Test::Unit::TestCase
5
5
 
6
- @@terse_file = "test_file.txt"
7
- @@expanded_file = "test_file.rb"
8
- @@premade_file = "expected_test_file.rb"
6
+ @@terse_file = "test_file_ruby.txt"
7
+ @@expanded_file = "test_file_ruby.rb"
8
+ @@premade_file = "expected_test_file_ruby.rb"
9
+ @@argv_altered = false
10
+
11
+ # not used - invoking via cmd means that the gem has to be rebuild & reinstalled after every code change, which does not lend itself to a speedy dev process
12
+ # TODO add test that deliberately invokes via system
9
13
  @@cmd = "ruby -e \"require 'terse_ruby'; TerseRuby.scan_files ARGV\" #{@@terse_file}"
10
14
 
11
15
  def switch_dir
@@ -15,7 +19,14 @@ class TestExpansion < Test::Unit::TestCase
15
19
  end
16
20
 
17
21
  def run_cmd
18
- system @@cmd
22
+ # system @@cmd # old mechanism; don't do this, call TerseRuby.scan_files ARGV instead
23
+
24
+ # ARGV is preserved from one test_ to the next, so we should only alter it once
25
+ unless @@argv_altered
26
+ ARGV << @@terse_file
27
+ @@argv_altered = true
28
+ end
29
+ TerseRuby.scan_files ARGV
19
30
  end
20
31
 
21
32
  # Switch to the same directory as this unit-test file, and check that the expected file is not present
@@ -0,0 +1,14 @@
1
+ p c PhoneNumber
2
+ p s type
3
+ p s number
4
+
5
+ p v testMethod1(s var)
6
+
7
+ p st v testMethod2
8
+
9
+ pv s privateMethod1
10
+
11
+ pt i protMeth1(f var)
12
+ r Integer.valueOf(var)
13
+
14
+
metadata CHANGED
@@ -1,15 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terse_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Morrisby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2017-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: more-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.1.3
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.1.3
33
+ - !ruby/object:Gem::Dependency
34
+ name: argv
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.0.0
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.0.0
13
53
  description: A utility to convert 'shorthand' Ruby into genuine Ruby. See README for
14
54
  more details.
15
55
  email: rmorrisby@gmail.com
@@ -19,15 +59,22 @@ extra_rdoc_files: []
19
59
  files:
20
60
  - LICENCE.txt
21
61
  - README.txt
22
- - bin/terse_ruby.rb
62
+ - build.rb
63
+ - lib/terse/format.rb
64
+ - lib/terse/keyword.rb
65
+ - lib/terse/keyword_java.rb
66
+ - lib/terse/keyword_ruby.rb
67
+ - lib/terse/scan.rb
68
+ - lib/terse/settings.rb
69
+ - lib/terse_java.rb
23
70
  - lib/terse_ruby.rb
24
- - lib/terse_ruby/format.rb
25
- - lib/terse_ruby/keyword.rb
26
- - lib/terse_ruby/scan.rb
27
71
  - terse_ruby.gemspec
28
- - test/expected_test_file.rb
29
- - test/test_expansion.rb
30
- - test/test_file.txt
72
+ - test/expected_test_file_java.java
73
+ - test/expected_test_file_ruby.rb
74
+ - test/test_expansion_java.rb
75
+ - test/test_expansion_ruby.rb
76
+ - test/test_file_java.txt
77
+ - test/test_file_ruby.txt
31
78
  homepage: https://rubygems.org/gems/terse_ruby
32
79
  licenses:
33
80
  - MIT
@@ -48,9 +95,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
95
  version: '0'
49
96
  requirements: []
50
97
  rubyforge_project:
51
- rubygems_version: 2.5.1
98
+ rubygems_version: 2.6.7
52
99
  signing_key:
53
100
  specification_version: 4
54
101
  summary: A utility to convert 'shorthand' Ruby into genuine Ruby.
55
102
  test_files:
56
- - test/test_expansion.rb
103
+ - test/test_expansion_java.rb
104
+ - test/test_expansion_ruby.rb
@@ -1,59 +0,0 @@
1
- # Terse Ruby
2
- # A simple utility to allow you to write shorthand for Ruby classes; the utility will then convert this shorthand file into a genuine Ruby .rb file (either writing a new file or overwriting)
3
- #
4
- # E.g. terse_file.txt contains :
5
- #
6
- # req a_gem
7
- # c AClass
8
- # i AnInclude
9
- # d method1
10
- #
11
- # Then Terse Ruby will write a new file terse_file.rb containing :
12
- #
13
- # require "a_gem"
14
- # class AClass
15
- # include AnInclude
16
- # def method1
17
- # end
18
- # end
19
- #
20
- #
21
- # Rules :
22
- # Terse Ruby will look through the supplied files and convert certain keywords at the start of lines :
23
- # c -> class
24
- # m -> module
25
- # r -> require (and will wrap the required item in " " if needed)
26
- # i -> include
27
- # d -> def
28
- # e -> end (method, class & module ends will be added if a subsequent method, class or module is detected, or is end-of-file)
29
- # a -> attr_accessor (and prefixes the variable name with : to make it a symbol)
30
- # r -> attr_reader (and prefixes the variable name with : to make it a symbol)
31
- # w -> attr_writer (and prefixes the variable name with : to make it a symbol)
32
- #
33
- # All remaining text in the line is not altered, even if a matching keyword is present, because the keywords need to be at the beginning of the line (ignoring leading whitespace)
34
- #
35
- #
36
- # Flags :
37
- # v : verbose mode
38
- # o : overwrite - if the new file already exists, the existing file will be overwritten
39
- #
40
- #
41
- # Invokation :
42
- # terse_ruby can be directly invoked from the command-line with :
43
- #
44
- # ruby -e "require 'terse_ruby'; TerseRuby.scan_files ARGV" [one or more files] [flags]
45
- #
46
- # e.g.
47
- #
48
- # ruby -e "require 'terse_ruby'; TerseRuby.scan_files ARGV" terse_file1.txt terse_file2.txt -o -v
49
- #
50
- # Alternatively, the below two lines are all you need to run terse_ruby within your own Ruby file :
51
- #
52
- # require_relative "../lib/terse_ruby"
53
- # TerseRuby.scan_files ARGV
54
- #
55
- # (if ARGV have not been supplied, you'll need to construct this object yourself first)
56
-
57
-
58
- require_relative "../lib/terse_ruby"
59
- TerseRuby.scan_files ARGV
@@ -1,147 +0,0 @@
1
- require "more_ruby"
2
- require_relative "keyword"
3
- require_relative "format"
4
-
5
- module TerseRuby
6
-
7
- # This Scan class does the meat of the work
8
- # TODO generate to_s, ==, initialize
9
- class Scan
10
-
11
- # The main method. ARGV from the invokation are passed in to here
12
- def self.scan_files(argv)
13
- puts "\nTerse Ruby called"
14
-
15
- keywords = TerseRuby::Keyword.generate_keywords
16
- flags = argv.options
17
- @verbose = flags.include_any?("v", "verbose")
18
- @overwrite = flags.include_any?("o", "overwrite", "w", "write")
19
- @no_formatting = flags.include_any?("n", "noformatting")
20
-
21
- files = argv.values
22
-
23
- if files.size < 1
24
- puts "No files supplied"
25
- else
26
- files.each do |f|
27
- is_file? f
28
- end
29
- pv "Will scan #{files.size} file#{plural_or_not files}"
30
- files.each do |f|
31
- is_file? f
32
- filename = File.basename f
33
- new_filename = gen_new_filename f
34
- newlines = []
35
- lines = File.readlines f
36
- pv "File #{filename} has #{lines.size} line#{plural_or_not lines}"
37
- # If we detect the start of a new class/method/etc. before the last one has been closed
38
- # then insert an "end" to close it off
39
- needs_top_level_end = false
40
- needs_inner_end = false
41
- insert_end = false
42
- lines.each do |l|
43
- line = l.chomp
44
- newline = line # keep the orginial line unless we detect a match
45
- keywords.each do |k|
46
- if line =~ k.regex
47
- pv "\nMatch found against line --> #{line}"
48
- pv "Matched -------------------> " + $2
49
- if k.is_end
50
- if needs_inner_end
51
- needs_inner_end = false
52
- elsif needs_top_level_end
53
- needs_top_level_end = false
54
- end
55
- insert_end = false
56
- end
57
-
58
- if k.needs_inner_end
59
- if needs_inner_end
60
- newlines << "end"
61
- # keep needs_inner_end true as we need to watch for an end for this new match
62
- else
63
- needs_inner_end = true # set in readiness for the next match
64
- end
65
- end
66
-
67
- if k.needs_top_level_end
68
- if needs_inner_end
69
- newlines << "end"
70
- needs_inner_end = false
71
- end
72
- if needs_top_level_end
73
- newlines << "end"
74
- # keep needs_top_level_end true as we need to watch for an end for this new match
75
- else
76
- needs_top_level_end = true # set in readiness for the next match
77
- end
78
- end
79
-
80
- if k.try_follow_on_regex && line =~ k.follow_on_regex
81
- # puts ""
82
- # $& is the entire match, not just the bits in ( )
83
- remainder = line[$&.size .. -1]
84
- follow_on = (eval k.follow_on_substitute).inspect
85
- newline = "#{$1}#{k.substitute} #{follow_on}#{remainder}"
86
- else
87
- remainder = line[($1.size + $2.size) .. -1]
88
- newline = "#{$1}#{k.substitute}#{remainder}"
89
- end
90
-
91
- pv "Line will become ----------> " + newline
92
- break
93
- end # end if regex
94
- end # end keywords.each
95
- newlines << newline
96
- end # end lines.each
97
- # Add end lines to end of file if necessary
98
- newlines << "end" if needs_inner_end
99
- newlines << "end" if needs_top_level_end
100
-
101
- # Apply indentation
102
- newlines = TerseRuby::Format.indent(newlines) unless @no_formatting
103
-
104
- # Add newlines between the end of a method and the start of the next, if needed
105
- newlines = TerseRuby::Format.space_lines(newlines) unless @no_formatting
106
-
107
- # Write out the new file
108
- File.open(new_filename, "w") do |new_f|
109
- newlines.each do |l|
110
- new_f.puts l
111
- end
112
- end
113
- puts "\nWrote file #{new_filename}"
114
- end # end else
115
- end # end ARGV.size
116
- puts "\nTerse Ruby finished"
117
- end # end scan_files
118
-
119
- # Returns an "s" string if the size of the item (item.size) is > 1; returns "" otherwise
120
- def self.plural_or_not item
121
- item.size == 1 ? "" : "s"
122
- end
123
-
124
- def self.is_file? f
125
- raise "Could not locate file at " + File.absolute_path(f) unless File.file?(f)
126
- end
127
-
128
- # Rules :
129
- # Keep base-name, change ext. to .rb, write to current working dir
130
- def self.gen_new_filename f
131
- path = Dir.getwd
132
- name = File.basename_no_ext f
133
- ext = ".rb"
134
- new_f = File.join(path, name) + ext
135
-
136
- if File.file?(new_f)
137
- puts "New file wll overwrite existing file " + new_f
138
- raise "Overwrite flag not specified; will not overwrite existing file " + new_f if !@overwrite
139
- end
140
- new_f
141
- end
142
-
143
- def self.pv s
144
- puts s if @verbose
145
- end
146
- end
147
- end