seeing_is_believing 0.0.11 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -88,15 +88,23 @@ TextMate Integration
88
88
  Go to the bundle editor, create this new command in the Ruby bundle:
89
89
 
90
90
  ```shell
91
- "${TM_RUBY}" -r seeing_is_believing/binary -e '
92
- SeeingIsBelieving::Binary.new(ARGV, $stdin, $stdout, $stderr).call
93
- ' $TM_FILEPATH 2>/dev/null
91
+ if [ -z "$TM_FILEPATH" ]; then
92
+ "${TM_RUBY}" -S seeing_is_believing -Ku --result-length 200
93
+ else
94
+ "${TM_RUBY}" -S seeing_is_believing -Ku --result-length 200 --as "$TM_FILEPATH"
95
+ fi
94
96
  ```
95
97
 
96
98
  It should look like this:
97
99
 
98
100
  ![textmate-integration][textmate-integration]
99
101
 
102
+ I also recommend a second command for cleaning the output:
103
+
104
+ ```shell
105
+ "${TM_RUBY}" -S seeing_is_believing -Ku --clean
106
+ ```
107
+
100
108
  Emacs Integration
101
109
  =================
102
110
 
@@ -177,6 +177,60 @@ Feature: Using flags
177
177
  'ç' # => "ç"
178
178
  """
179
179
 
180
+ Scenario: --as and stdin
181
+ Given the file "example.rb" "1+1"
182
+ Given I have the stdin content:
183
+ """
184
+ 1+1
185
+ __FILE__
186
+ """
187
+ When I run "seeing_is_believing --as example.rb"
188
+ Then stderr is empty
189
+ Then the exit status is 0
190
+ And stdout is:
191
+ """
192
+ 1+1 # => 2
193
+ __FILE__ # => "example.rb"
194
+ """
195
+
196
+ Scenario: --as and -e
197
+ Given the file "example.rb" "1+1"
198
+ When I run 'seeing_is_believing --as example.rb -e "__FILE__"'
199
+ Then stderr is empty
200
+ And the exit status is 0
201
+ And stdout is '__FILE__ # => "example.rb"'
202
+
203
+ Scenario: --clean
204
+ Given the file "example.rb":
205
+ """
206
+ 1 + 1 # => not 2
207
+ 2 + 2 # ~> Exception, something
208
+
209
+
210
+ # >> some stdout output
211
+
212
+ # !> some stderr output
213
+ __END__
214
+ 1
215
+ """
216
+ When I run "seeing_is_believing --clean example.rb"
217
+ Then stderr is empty
218
+ And the exit status is 0
219
+ And stdout is:
220
+ """
221
+ 1 + 1
222
+ 2 + 2
223
+
224
+ __END__
225
+ 1
226
+ """
227
+
228
+ Scenario: --clean on an invalid file will clean
229
+ When I run 'seeing_is_believing --clean -e "1+ # => lkj"'
230
+ Then stderr is empty
231
+ And the exit status is 0
232
+ And stdout is '1+'
233
+
180
234
  Scenario: --help
181
235
  When I run "seeing_is_believing --help"
182
236
  Then stderr is empty
@@ -1,35 +1,11 @@
1
- Given 'the file "$filename":' do |filename, body|
2
- CommandLineHelpers.write_file filename, body
3
- end
4
-
5
- Given 'the file "$filename" "$body"' do |filename, body|
6
- CommandLineHelpers.write_file filename, body
7
- end
8
-
9
- Given 'I have the stdin content "$content"' do |content|
10
- @stdin_data = content
11
- end
12
-
13
- When 'I run "$command"' do |command|
14
- @last_executed = CommandLineHelpers.execute command, @stdin_data
15
- end
16
-
17
- Then /^(stderr|stdout) is:$/ do |stream_name, output|
18
- @last_executed.send(stream_name).chomp.should == eval_curlies(output)
19
- end
20
-
21
- Then /^(stderr|stdout) is "(.*?)"$/ do |stream_name, output|
22
- @last_executed.send(stream_name).chomp.should == eval_curlies(output)
23
- end
24
-
25
- Then 'the exit status is $status' do |status|
26
- @last_executed.exitstatus.to_s.should == status
27
- end
28
-
29
- Then /^(stderr|stdout) is empty$/ do |stream_name|
30
- @last_executed.send(stream_name).should == ''
31
- end
32
-
33
- Then /^(stderr|stdout) includes "([^"]*)"$/ do |stream_name, content|
34
- @last_executed.send(stream_name).should include content
35
- end
1
+ Given('the file "$filename" "$body"') { |filename, body| CommandLineHelpers.write_file filename, body }
2
+ Given('the file "$filename":') { |filename, body| CommandLineHelpers.write_file filename, body }
3
+ Given('I have the stdin content "$content"') { |content| @stdin_data = content }
4
+ Given('I have the stdin content:') { |content| @stdin_data = content }
5
+ When('I run "$command"') { |command| @last_executed = CommandLineHelpers.execute command, @stdin_data }
6
+ When("I run '$command'") { |command| @last_executed = CommandLineHelpers.execute command, @stdin_data }
7
+ Then(/^(stderr|stdout) is:$/) { |stream_name, output| @last_executed.send(stream_name).chomp.should == output }
8
+ Then(/^(stderr|stdout) is ["'](.*?)["']$/) { |stream_name, output| @last_executed.send(stream_name).chomp.should == output }
9
+ Then(/^(stderr|stdout) is empty$/) { |stream_name| @last_executed.send(stream_name).should == '' }
10
+ Then(/^(stderr|stdout) includes "([^"]*)"$/) { |stream_name, content| @last_executed.send(stream_name).should include content }
11
+ Then('the exit status is $status') { |status| @last_executed.exitstatus.to_s.should == status }
@@ -15,11 +15,12 @@ class SeeingIsBelieving
15
15
  end
16
16
 
17
17
  def call
18
- @exitstatus ||= if flags_have_errors? then print_errors ; 1
19
- elsif should_print_help? then print_help ; 0
20
- elsif has_filename? && file_dne? then print_file_dne ; 1
21
- elsif invalid_syntax? then print_syntax_error ; 1
22
- else print_program ; (results.has_exception? ? 1 : 0)
18
+ @exitstatus ||= if flags_have_errors? then print_errors ; 1
19
+ elsif should_print_help? then print_help ; 0
20
+ elsif has_filename? && file_dne? then print_file_dne ; 1
21
+ elsif should_clean? then print_cleaned_program ; 0
22
+ elsif invalid_syntax? then print_syntax_error ; 1
23
+ else print_program ; (results.has_exception? ? 1 : 0)
23
24
  end
24
25
  end
25
26
 
@@ -31,14 +32,14 @@ class SeeingIsBelieving
31
32
  flags[:filename]
32
33
  end
33
34
 
34
- def body
35
+ def cleaned_body
35
36
  @body ||= PrintResultsNextToLines.remove_previous_output_from \
36
37
  flags[:program] || (file_is_on_stdin? && stdin.read) || File.read(flags[:filename])
37
38
  end
38
39
 
39
40
  def results
40
- @results ||= SeeingIsBelieving.call body,
41
- filename: flags[:filename],
41
+ @results ||= SeeingIsBelieving.call cleaned_body,
42
+ filename: (flags[:as] || flags[:filename]),
42
43
  require: flags[:require],
43
44
  load_path: flags[:load_path],
44
45
  encoding: flags[:encoding],
@@ -46,7 +47,7 @@ class SeeingIsBelieving
46
47
  end
47
48
 
48
49
  def printer
49
- @printer ||= PrintResultsNextToLines.new body, results, flags
50
+ @printer ||= PrintResultsNextToLines.new cleaned_body, results, flags
50
51
  end
51
52
 
52
53
  def flags
@@ -86,7 +87,7 @@ class SeeingIsBelieving
86
87
  end
87
88
 
88
89
  def syntax_error_notice
89
- out, err, syntax_status = Open3.capture3 'ruby', '-c', stdin_data: body
90
+ out, err, syntax_status = Open3.capture3 'ruby', '-c', stdin_data: cleaned_body
90
91
  return err unless syntax_status.success?
91
92
  end
92
93
 
@@ -98,5 +99,12 @@ class SeeingIsBelieving
98
99
  stderr.puts syntax_error_notice
99
100
  end
100
101
 
102
+ def should_clean?
103
+ flags[:clean]
104
+ end
105
+
106
+ def print_cleaned_program
107
+ stdout.print cleaned_body
108
+ end
101
109
  end
102
110
  end
@@ -17,13 +17,16 @@ class SeeingIsBelieving
17
17
  until args.empty?
18
18
  case (arg = args.shift)
19
19
  when '-h', '--help' then options[:help] = self.class.help_screen
20
+ when '-c', '--clean' then options[:clean] = true
20
21
  when '-l', '--start-line' then extract_positive_int_for :start_line, arg
21
22
  when '-L', '--end-line' then extract_positive_int_for :end_line, arg
22
23
  when '-d', '--line-length' then extract_positive_int_for :line_length, arg
23
24
  when '-D', '--result-length' then extract_positive_int_for :result_length, arg
24
- when '-r', '--require' then next_arg("#{arg} expected a filename but did not see one") { |filename| options[:require] << filename }
25
- when '-I', '--load-path' then next_arg("#{arg} expected a directory but did not see one") { |dir| options[:load_path] << dir }
26
- when '-e', '--program' then next_arg("#{arg} expects a program as the following argument") { |program| options[:program] = program }
25
+ when '-r', '--require' then next_arg("#{arg} expected a filename as the following argument but did not see one") { |filename| options[:require] << filename }
26
+ when '-I', '--load-path' then next_arg("#{arg} expected a directory as the following argument but did not see one") { |dir| options[:load_path] << dir }
27
+ when '-e', '--program' then next_arg("#{arg} expected a program as the following argument but did not see one") { |program| options[:program] = program }
28
+ when '-a', '--as' then next_arg("#{arg} expected a filename as the following argument but did not see one") { |filename| options[:as] = filename }
29
+ when /\A-K(.+)/ then options[:encoding] = $1
27
30
  when '-K', '--encoding' then next_arg("#{arg} expects an encoding, see `man ruby` for possibile values") { |encoding| options[:encoding] = encoding }
28
31
  when /^-/ then options[:errors] << "Unknown option: #{arg.inspect}" # unknown flags
29
32
  else
@@ -54,13 +57,14 @@ class SeeingIsBelieving
54
57
 
55
58
  def options
56
59
  @options ||= {
60
+ clean: false,
57
61
  program: nil,
58
62
  filename: nil,
59
- errors: [],
60
63
  start_line: 1,
61
64
  line_length: Float::INFINITY,
62
65
  end_line: Float::INFINITY,
63
66
  result_length: Float::INFINITY,
67
+ errors: [],
64
68
  require: [],
65
69
  load_path: [],
66
70
  }
@@ -90,13 +94,17 @@ Usage: #{$0} [options] [filename]
90
94
 
91
95
  If no filename is provided, the binary will read the program from standard input.
92
96
 
93
- -l, --start-line # line number to begin showing results on
94
- -L, --end-line # line number to stop showing results on
95
- -d, --line-length # max length of the entire line (only truncates results, not source lines)
96
- -D, --result-length # max length of the portion after the "# => "
97
- -r, --require # additional files to be required before running the program
98
- -e, --program # Pass the program to execute as an argument
99
- -h, --help # this help screen
97
+ -l, --start-line n # line number to begin showing results on
98
+ -L, --end-line n # line number to stop showing results on
99
+ -d, --line-length n # max length of the entire line (only truncates results, not source lines)
100
+ -D, --result-length n # max length of the portion after the "# => "
101
+ -I, --load-path dir # a dir that should be added to the $LOAD_PATH
102
+ -r, --require file # additional files to be required before running the program
103
+ -e, --program program # Pass the program to execute as an argument
104
+ -K, --encoding encoding # sets file encoding, equivalent to Ruby's -Kx (see `man ruby` for valid values)
105
+ -a, --as filename # run the program as if it was the specified filename
106
+ -c, --clean # remove annotations from previous runs of seeing_is_believing
107
+ -h, --help # this help screen
100
108
  HELP_SCREEN
101
109
  end
102
110
  end
@@ -49,7 +49,7 @@ class SeeingIsBelieving
49
49
  end
50
50
  },
51
51
  ensure: -> {
52
- restore_backup
52
+ set_back_to_initial_conditions
53
53
  }
54
54
  end
55
55
 
@@ -75,9 +75,12 @@ class SeeingIsBelieving
75
75
  @was_backed_up = true
76
76
  end
77
77
 
78
- def restore_backup
79
- return unless @was_backed_up
80
- FileUtils.mv temp_filename, filename
78
+ def set_back_to_initial_conditions
79
+ if @was_backed_up
80
+ FileUtils.mv temp_filename, filename
81
+ else
82
+ FileUtils.rm filename
83
+ end
81
84
  end
82
85
 
83
86
  def write_program_to_file
@@ -1,3 +1,3 @@
1
1
  class SeeingIsBelieving
2
- VERSION = '0.0.11'
2
+ VERSION = '0.0.13'
3
3
  end
@@ -41,8 +41,8 @@ describe SeeingIsBelieving::Binary::ArgParser do
41
41
  end
42
42
 
43
43
  specify 'unknown options set an error' do
44
- parse(['--abc']).should have_error 'Unknown option: "--abc"'
45
- parse(['-a']).should have_error 'Unknown option: "-a"'
44
+ parse(['--xyz']).should have_error 'Unknown option: "--xyz"'
45
+ parse(['-x']).should have_error 'Unknown option: "-x"'
46
46
  end
47
47
 
48
48
  example 'example: multiple args' do
@@ -206,10 +206,42 @@ describe SeeingIsBelieving::Binary::ArgParser do
206
206
  parse(%w[--encoding u])[:encoding].should == 'u'
207
207
  end
208
208
 
209
+ specify 'with -K, the argument can be placed immediately after it (e.g. -Ku) because Ruby allows this' do
210
+ parse(['-Ku'])[:encoding].should == 'u'
211
+ parse(['-Ku']).should_not have_error /-K/
212
+ end
213
+
209
214
  it 'sets an error if not provided with an encoding' do
210
215
  parse(['-K']).should have_error /-K/
211
216
  parse(['--encoding']).should have_error /--encoding/
212
217
  end
213
218
  end
219
+
220
+ describe ':as' do
221
+ it 'defaults to nil' do
222
+ parse([])[:as].should be_nil
223
+ end
224
+
225
+ it 'can be set with -a and --as' do
226
+ parse(%w[-a abc])[:as].should == 'abc'
227
+ parse(%w[--as abc])[:as].should == 'abc'
228
+ end
229
+
230
+ it 'sets an error if not provided with a filename' do
231
+ parse(%w[-a]).should have_error /-a/
232
+ parse(%w[--as]).should have_error /--as/
233
+ end
234
+ end
235
+
236
+ describe ':clean' do
237
+ it 'defaults to false' do
238
+ parse([])[:clean].should == false
239
+ end
240
+
241
+ it 'can be set with -c and --clean' do
242
+ parse(%w[-c])[:clean].should == true
243
+ parse(%w[--clean])[:clean].should == true
244
+ end
245
+ end
214
246
  end
215
247
 
@@ -63,6 +63,24 @@ describe SeeingIsBelieving::EvaluateByMovingFiles do
63
63
  evaluator.call
64
64
  end
65
65
 
66
+ it 'uses HardCoreEnsure to delete the file if it wrote it where one did not previously exist' do
67
+ evaluator = described_class.new 'PROGRAM', filename, error_stream: StringIO.new
68
+ FileUtils.rm_rf filename
69
+ SeeingIsBelieving::HardCoreEnsure.should_receive(:call) do |options|
70
+ # initial state
71
+ File.exist?(filename).should == false
72
+
73
+ # after code
74
+ options[:code].call rescue nil
75
+ File.read(filename).should == 'PROGRAM'
76
+
77
+ # after ensure
78
+ options[:ensure].call
79
+ File.exist?(filename).should == false
80
+ end
81
+ evaluator.call
82
+ end
83
+
66
84
  it 'can require files' do
67
85
  other_filename1 = File.join filedir, 'other1.rb'
68
86
  other_filename2 = File.join filedir, 'other2.rb'
@@ -53,7 +53,7 @@ describe SeeingIsBelieving::Queue do
53
53
  end
54
54
 
55
55
  describe 'conditional iteration' do
56
- it 'will iterate while a condition is met' do
56
+ it 'can iterate while a condition is met' do
57
57
  queue = queue_for *1..5
58
58
  seen = []
59
59
  queue.while { |arg| arg < 4 }.each { |arg| seen << arg }
@@ -61,7 +61,7 @@ describe SeeingIsBelieving::Queue do
61
61
  queue.peek.should == 4
62
62
  end
63
63
 
64
- it 'will iterate until a condition is met' do
64
+ it 'can iterate until a condition is met' do
65
65
  queue = queue_for *1..5
66
66
  seen = []
67
67
  queue.until { |arg| arg == 4 }.each { |arg| seen << arg }
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seeing_is_believing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.13
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-02-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70217249132080 !ruby/object:Gem::Requirement
16
+ requirement: &70333554807460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 10.0.3
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70217249132080
24
+ version_requirements: *70333554807460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70217249131460 !ruby/object:Gem::Requirement
27
+ requirement: &70333554806960 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.12.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70217249131460
35
+ version_requirements: *70333554806960
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &70217249130620 !ruby/object:Gem::Requirement
38
+ requirement: &70333554806500 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.2.1
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70217249130620
46
+ version_requirements: *70333554806500
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ichannel
49
- requirement: &70217249129100 !ruby/object:Gem::Requirement
49
+ requirement: &70333554806040 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 5.1.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70217249129100
57
+ version_requirements: *70333554806040
58
58
  description: Records the results of every line of code in your file (intended to be
59
59
  like xmpfilter), inspired by Bret Victor's JavaScript example in his talk "Inventing
60
60
  on Principle"