mynyml-simple_example 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.
@@ -0,0 +1,19 @@
1
+ Copyright © 2009 Martin Aumont (mynyml)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ Add easy fancy-pants examples to your projects.
2
+
3
+ running:
4
+
5
+ require 'lib/simple_example'
6
+ include SimpleExample
7
+
8
+ example do
9
+ x = 1 + 1
10
+ y = 2 + 2
11
+ x + y
12
+ end
13
+
14
+ outputs:
15
+
16
+ x = (1 + 1)
17
+ #=> 2
18
+ y = (2 + 2)
19
+ #=> 4
20
+ (x + y)
21
+ #=> 6
22
+
23
+ Easy. Fancy.
24
+
25
+ There are a few neat options to tweak the formatting. See the examples.rb file.
@@ -0,0 +1,64 @@
1
+ # --------------------------------------------------
2
+ # tasks mostly copied from thin's Rakefile
3
+ # http://github.com/macournoyer/thin/tree/master
4
+ # --------------------------------------------------
5
+ require 'rake/gempackagetask'
6
+ require 'pathname'
7
+ require 'yaml'
8
+
9
+ RUBY_1_9 = RUBY_VERSION =~ /^1\.9/
10
+ WIN = (RUBY_PLATFORM =~ /mswin|cygwin/)
11
+ SUDO = (WIN ? "" : "sudo")
12
+ ROOT = Pathname(__FILE__).dirname.expand_path
13
+
14
+ def gem
15
+ RUBY_1_9 ? 'gem19' : 'gem'
16
+ end
17
+
18
+ def all_except(paths)
19
+ Dir['**/*'] - paths.map {|path| path.strip.gsub(/^\//,'').gsub(/\/$/,'') }
20
+ end
21
+
22
+ def gitignored
23
+ ROOT.join('.gitignore').read.strip.split("\n").compact.map {|line| line.strip }
24
+ end
25
+
26
+ spec = Gem::Specification.new do |s|
27
+ s.name = 'simple_example'
28
+ s.version = '0.1'
29
+ s.summary = "Add easy fancy-pants examples to your projects."
30
+ s.description = "Add easy fancy-pants examples to your projects."
31
+ s.author = "Martin Aumont"
32
+ s.email = 'mynyml@gmail.com'
33
+ s.homepage = ''
34
+ s.has_rdoc = true
35
+ s.require_path = "lib"
36
+ s.files = all_except(gitignored << 'generate_examples_results.rb')
37
+
38
+ s.add_dependency 'ruby2ruby'
39
+ s.add_dependency 'ParseTree'
40
+ end
41
+
42
+ Rake::GemPackageTask.new(spec) do |p|
43
+ p.gem_spec = spec
44
+ end
45
+
46
+
47
+ desc "Remove package products"
48
+ task :clean => :clobber_package
49
+
50
+ desc "Update the gemspec for GitHub's gem server"
51
+ task :gemspec do
52
+ Pathname("#{spec.name}.gemspec").open('w') {|f| f << YAML.dump(spec) }
53
+ end
54
+
55
+ desc "Install gem"
56
+ task :install => [:clobber, :package] do
57
+ sh "#{SUDO} #{gem} install pkg/#{spec.full_name}.gem"
58
+ end
59
+
60
+ desc "Uninstall gem"
61
+ task :uninstall => :clean do
62
+ sh "#{SUDO} #{gem} uninstall -v #{spec.version} -x #{spec.name}"
63
+ end
64
+
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ o compact style should keep constant width for source string (so that results align)
2
+ o formatting settings as options to example method?
3
+ o add optional terminal colours?
@@ -0,0 +1,97 @@
1
+ require 'lib/simple_example'
2
+ include SimpleExample
3
+
4
+
5
+ SEP = '-'*10
6
+ SimpleExample::Format.separator = SEP
7
+
8
+ puts SEP
9
+
10
+ # simple
11
+ example do
12
+ 1 + 1
13
+ end
14
+
15
+ # less simple
16
+ example do
17
+ [1.4, 2.4, 3.4].map(&:floor)
18
+ end
19
+
20
+ # returns last statement
21
+ result = example { 1 + 1 }
22
+ puts "previous result: #{result}"
23
+
24
+ puts SEP
25
+
26
+ # multiple statements
27
+ example do
28
+ arr = %w( a b c )
29
+ arr.each {|x| x.upcase! }
30
+ arr.map {|x| x << 'y' }
31
+ end
32
+
33
+ # irb style:
34
+ SimpleExample::Format.source_prefix = '>> '
35
+ SimpleExample::Format.result_prefix = '=> '
36
+
37
+ example { 1 + 1; 2 + 2 }
38
+
39
+ # python shell style:
40
+ SimpleExample::Format.source_prefix = '>>> '
41
+ SimpleExample::Format.result_prefix = ''
42
+
43
+ example { 1 + 1; 2 + 2 }
44
+
45
+ # io shell style
46
+ SimpleExample::Format.source_prefix = 'rb> '
47
+ SimpleExample::Format.result_prefix = '==> '
48
+
49
+ example { 1 + 1; 2 + 2 }
50
+
51
+ # compact:
52
+ SimpleExample::Format.source_prefix = ''
53
+ SimpleExample::Format.result_prefix = '#=> '
54
+ SimpleExample::Format.compact = true
55
+
56
+ example { 1 + 1; 2 + 2 }
57
+
58
+
59
+ __END__
60
+ OUTPUT:
61
+ ----------
62
+ (1 + 1)
63
+ #=> 2
64
+ ----------
65
+ [1.4, 2.4, 3.4].map(&:floor)
66
+ #=> [1, 2, 3]
67
+ ----------
68
+ (1 + 1)
69
+ #=> 2
70
+ ----------
71
+ previous result: 2
72
+ ----------
73
+ arr = ["a", "b", "c"]
74
+ #=> ["a", "b", "c"]
75
+ arr.each { |x| x.upcase! }
76
+ #=> ["A", "B", "C"]
77
+ arr.map { |x| (x << "y") }
78
+ #=> ["Ay", "By", "Cy"]
79
+ ----------
80
+ >> (1 + 1)
81
+ => 2
82
+ >> (2 + 2)
83
+ => 4
84
+ ----------
85
+ >>> (1 + 1)
86
+ 2
87
+ >>> (2 + 2)
88
+ 4
89
+ ----------
90
+ rb> (1 + 1)
91
+ ==> 2
92
+ rb> (2 + 2)
93
+ ==> 4
94
+ ----------
95
+ (1 + 1) #=> 2
96
+ (2 + 2) #=> 4
97
+ ----------
@@ -0,0 +1,54 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+ require 'ruby2ruby'
4
+ require 'parse_tree'
5
+ require 'parse_tree_extensions'
6
+
7
+ module SimpleExample
8
+ extend self
9
+
10
+ class Format
11
+ class << self
12
+ attr_accessor :source_prefix
13
+ attr_accessor :result_prefix
14
+ attr_accessor :separator
15
+ attr_accessor :compact
16
+
17
+ def source_prefix
18
+ @source_prefix || ''
19
+ end
20
+
21
+ def result_prefix
22
+ @result_prefix || '#=> '
23
+ end
24
+
25
+ def separator
26
+ @separator || ''
27
+ end
28
+
29
+ def compact?
30
+ !!@compact
31
+ end
32
+ end
33
+ end
34
+
35
+ def example(&block)
36
+ result = block.call
37
+
38
+ statement = block.to_ruby
39
+ statement.gsub!(/\Aproc \{/,'')
40
+ statement.gsub!(/\}\Z/,'')
41
+
42
+ lines = statement.strip.split(/\n/)
43
+ lines.each do |line|
44
+ src_str = Format.source_prefix + line.strip
45
+ res_str = Format.result_prefix + eval(line.strip, block.binding, Pathname(__FILE__).basename, __LINE__).inspect
46
+ sep_str = Format.compact? ? " " : "\n"
47
+
48
+ Kernel.puts "#{src_str}#{sep_str}#{res_str}"
49
+ end
50
+ Kernel.puts Format.separator unless Format.separator.empty?
51
+
52
+ result
53
+ end
54
+ end
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_example
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-02 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby2ruby
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ParseTree
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Add easy fancy-pants examples to your projects.
36
+ email: mynyml@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib
45
+ - lib/simple_example.rb
46
+ - simple_example.gemspec
47
+ - README
48
+ - Rakefile
49
+ - test
50
+ - test/test_helper.rb
51
+ - test/output_helper.rb
52
+ - test/test_simple_example.rb
53
+ - examples.rb
54
+ - MIT-LICENSE
55
+ - TODO
56
+ has_rdoc: true
57
+ homepage: ""
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.1
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: Add easy fancy-pants examples to your projects.
82
+ test_files: []
83
+
@@ -0,0 +1,24 @@
1
+ module OutputHelper
2
+
3
+ def assert_output(expected, &block)
4
+ keep_stdout do |stdout|
5
+ block.call
6
+ if expected.is_a?(Regexp)
7
+ assert_match expected, stdout.string
8
+ else
9
+ stdout.string.should be(expected.to_s)
10
+ end
11
+ end
12
+ end
13
+
14
+ def keep_stdout(&block)
15
+ begin
16
+ orig_stream, $stdout = $stdout, StringIO.new
17
+ block.call($stdout)
18
+ ensure
19
+ s, $stdout = $stdout.string, orig_stream
20
+ s
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'context'
4
+ require 'matchy'
5
+ require 'unindent'
6
+ require 'mocha'
7
+ begin
8
+ require 'ruby-debug'
9
+ require 'quietbacktrace'
10
+ rescue LoadError, RuntimeError
11
+ end
12
+
13
+ class Test::Unit::TestCase
14
+ end
@@ -0,0 +1,127 @@
1
+ require 'pathname'
2
+ root = Pathname(__FILE__).dirname.parent.expand_path
3
+ require root.join('test/test_helper.rb')
4
+ require root.join('test/output_helper.rb')
5
+ require root.join('lib/simple_example.rb')
6
+
7
+ class String
8
+ # name isn't so intuitive; maybe to_exact_regexp ?
9
+ def to_regexp
10
+ Regexp.new(Regexp.escape(self))
11
+ end
12
+ end
13
+
14
+ class SimpleExampleTest < Test::Unit::TestCase
15
+ include SimpleExample
16
+ include OutputHelper
17
+
18
+ context "SimpleExample" do
19
+ def reset_config!
20
+ SimpleExample::Format.instance_variable_set(:@source_prefix, nil)
21
+ SimpleExample::Format.instance_variable_set(:@result_prefix, nil)
22
+ SimpleExample::Format.instance_variable_set(:@separator, nil)
23
+ SimpleExample::Format.instance_variable_set(:@compact, nil)
24
+ end
25
+
26
+ before do
27
+ reset_config!
28
+
29
+ # shortcuts
30
+ @sp = SimpleExample::Format.source_prefix
31
+ @rp = SimpleExample::Format.result_prefix
32
+ end
33
+
34
+ test "can be called from original scope" do
35
+ Kernel.stubs(:puts)
36
+ lambda { SimpleExample.example {} }.should_not raise_error(NoMethodError)
37
+ end
38
+ context "evaluation" do
39
+ test "returns result" do
40
+ Kernel.stubs(:puts)
41
+ example { 1+1 }.should be(2)
42
+ end
43
+ end
44
+ context "output" do
45
+ test "prints statement's source" do
46
+ assert_output('(1 + 1)'.to_regexp) { example { 1+1 } }
47
+ end
48
+ test "prints statement's result" do
49
+ assert_output(/2/) { example { 1+1 } }
50
+ end
51
+ test "prints every step's source + result" do
52
+ out = <<-STR.unindent
53
+ #{@sp}(1 + 1)
54
+ #{@rp}2
55
+ #{@sp}(3 + 3)
56
+ #{@rp}6
57
+ STR
58
+ assert_output(out) do
59
+ example { 1+1; 3+3 }
60
+ end
61
+ end
62
+ test "doesn't strip closing brakets" do
63
+ Kernel.stubs(:puts)
64
+
65
+ lambda {
66
+ example do
67
+ arr = %w( a b c )
68
+ arr.each {|x| x.upcase! }
69
+ end
70
+ }.should_not raise_error(SyntaxError)
71
+ end
72
+ end
73
+ context "formating" do
74
+ test "prepends source lines with marker" do
75
+ SimpleExample::Format.source_prefix = '> '
76
+ assert_output('> (1 + 1)'.to_regexp) { example { 1+1 } }
77
+ end
78
+ test "prepends result lines with marker" do
79
+ SimpleExample::Format.result_prefix = '#=> '
80
+ assert_output('#=> 2'.to_regexp) { example { 1+1 } }
81
+ end
82
+ test "default source marker" do
83
+ SimpleExample::Format.source_prefix.should be('')
84
+ end
85
+ test "default result marker" do
86
+ SimpleExample::Format.result_prefix.should be('#=> ')
87
+ end
88
+ test "custom source marker" do
89
+ SimpleExample::Format.source_prefix = '| '
90
+ assert_output('| (1 + 1)'.to_regexp) { example { 1+1 } }
91
+ end
92
+ test "custom result marker" do
93
+ SimpleExample::Format.result_prefix = '|=> '
94
+ assert_output('|=> 2'.to_regexp) { example { 1+1 } }
95
+ end
96
+ test "seperator marker" do
97
+ SimpleExample::Format.separator = '-'*10
98
+
99
+ out = <<-STR.unindent
100
+ #{@sp}(1 + 1)
101
+ #{@rp}2
102
+ #{@sp}(2 + 2)
103
+ #{@rp}4
104
+ ----------
105
+ #{@sp}(3 + 3)
106
+ #{@rp}6
107
+ ----------
108
+ STR
109
+ assert_output(out) do
110
+ example { 1+1; 2+2 }
111
+ example { 3+3 }
112
+ end
113
+ end
114
+ test "compact output" do
115
+ SimpleExample::Format.compact = true
116
+
117
+ out = <<-STR.unindent
118
+ #{@sp}(1 + 1) #{@rp}2
119
+ #{@sp}(3 + 3) #{@rp}6
120
+ STR
121
+ assert_output(out) do
122
+ example { 1+1; 3+3 }
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mynyml-simple_example
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Martin Aumont
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-01 21:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby2ruby
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ParseTree
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Add easy fancy-pants examples to your projects.
36
+ email: mynyml@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib
45
+ - lib/simple_example.rb
46
+ - simple_example.gemspec
47
+ - README
48
+ - Rakefile
49
+ - test
50
+ - test/test_helper.rb
51
+ - test/output_helper.rb
52
+ - test/test_simple_example.rb
53
+ - examples.rb
54
+ - MIT-LICENSE
55
+ - TODO
56
+ has_rdoc: true
57
+ homepage: ""
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project:
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: Add easy fancy-pants examples to your projects.
82
+ test_files: []
83
+