rgot 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b420828d0e0801d5f766faa6ef3c57c67ae81a6b
4
- data.tar.gz: 49271b8268fc5eb3b61da0350a2ac12adb5b91c3
3
+ metadata.gz: 8f276d8ac23961aa00103a1217c57b1ef513b7d1
4
+ data.tar.gz: f742e18e1815dde34451f3566a303dc2239b9433
5
5
  SHA512:
6
- metadata.gz: 384ccc588fe79b31c639d8e4aaa6ada76ba283ab9082ddc113b84362277142616dcb54fba6c4250827bafc3443f90de7a1284b2ebda61e6bab1573b8f5ef39ea
7
- data.tar.gz: 39490b59b9baf6c883b6beafce640ddd5301aa34ee2f0e3f199db0daead6932463c74c3408576638fc63ee5275f083d49b6b73f9bb05fdbb15e690c01e86a27d
6
+ metadata.gz: 7be4569af2fdd700a3df4ed304a7e53d00d23d73d40153b555eb05b132640d2cea8990b89a2db5a73d0d91cea67b6f149de405f14e277f8f39bc02919df44f84
7
+ data.tar.gz: a89b569ed19bac5b3a26d650fa6262f84daf7a0a65d1ce304d3943fbee7bef1abeada4a24023f317d53f8c11bf8a9590957387c473eef7ef37e67c42c032c09e
data/README.md CHANGED
@@ -3,7 +3,9 @@ rgot
3
3
 
4
4
  [![Build Status](https://travis-ci.org/ksss/rgot.svg)](https://travis-ci.org/ksss/rgot)
5
5
 
6
- RGOT is a Ruby Golang like Testing package.
6
+ Ruby + Golang Testing = Rgot
7
+
8
+ Rgot is a testing package convert from golang testing.
7
9
 
8
10
  ### usage
9
11
 
@@ -52,3 +54,275 @@ $ rgot -v test/pass_test.rb
52
54
  PASS
53
55
  ok 0.001s
54
56
  ```
57
+
58
+ # Feature
59
+
60
+ ## Testing
61
+
62
+ I provide a very simple testing feature to you.
63
+
64
+ **Rgot** testing is quite different from *RSpec* and *MiniTest* etc.
65
+
66
+ Rgot carve out a new world of testing.
67
+
68
+ So, You check only bad case in testing.
69
+
70
+ ## Benchmark
71
+
72
+ You can write simple benchmark script with testing.
73
+
74
+ This benchmark to adjust the time automatically.
75
+
76
+ ```ruby
77
+ module FooTest
78
+ def benchmark_something(b)
79
+ i = 0
80
+ while i < b.n
81
+ something(1)
82
+ i += 1
83
+ end
84
+ end
85
+ end
86
+ ```
87
+
88
+ `b.n` is automatically adjusted.
89
+
90
+ ## Example
91
+
92
+ Rgot's example feature is the best and if you want to write the sample code of your library.
93
+
94
+ While presenting the sample code, it will be able to test whether the display results match at the same time.
95
+
96
+ ```ruby
97
+ module FooTest
98
+ class User
99
+ def initialize(name)
100
+ @name = name
101
+ end
102
+
103
+ def hello
104
+ "Hello #{@name}"
105
+ end
106
+ end
107
+
108
+ def example_something
109
+ user = User.new('ksss')
110
+ puts user.hello
111
+ # Output:
112
+ # Hello ksss
113
+ end
114
+
115
+ def example_fail
116
+ user = User.new('ksss')
117
+ puts user.hello
118
+ # Output:
119
+ # Hi ksss
120
+ end
121
+ end
122
+ ```
123
+
124
+ `example_fail` fail since output is different.
125
+
126
+ So, you can notice that the sample code is wrong.
127
+
128
+ # Naming convention
129
+
130
+ ## Filename
131
+
132
+ Filename should be set '*_test.rb'
133
+
134
+ ## Module name
135
+
136
+ Module name should be set 'XxxTest'
137
+
138
+ 'Xxx' can replace any string (in range of ruby module)
139
+
140
+ Testing code file can split any number.
141
+
142
+ But all file should be have one module (like golang package name).
143
+
144
+ ```ruby
145
+ module XxxTest
146
+ end
147
+ ```
148
+
149
+ ## Method name
150
+
151
+ Method name should be set 'test_*' for testing.
152
+
153
+ And benchmark method should be set 'benchmark_*'.
154
+
155
+ ```ruby
156
+ module XxxTest
157
+ def test_any_name(t)
158
+ end
159
+
160
+ def benchmark_any_name(b)
161
+ end
162
+ end
163
+ ```
164
+
165
+ # Command
166
+
167
+ ```
168
+ $ rgot -h
169
+ Usage: rgot [options]
170
+ -v, --verbose log all tests
171
+ -b, --bench [regexp] benchmark
172
+ --benchtime [sec] benchmark running time
173
+ --timeout [sec] set timeout sec to testing
174
+ ```
175
+
176
+ ## Basic
177
+
178
+ ```
179
+ $ rgot file_of_test.rb
180
+ PASS
181
+ ok 0.001s
182
+ ```
183
+
184
+ Set filename to argument.
185
+
186
+ Just only start testing file_of_test.rb.
187
+
188
+ ```
189
+ $ rgot sample
190
+ PASS
191
+ ok 0.002s
192
+ ```
193
+
194
+ And set dirname to argument, run all case of testing under this dir.
195
+
196
+ ## Verbose
197
+
198
+ ```
199
+ $ rgot -v target_file_test.rb
200
+ === RUN test_pass
201
+ --- PASS: test_pass (0.00005s)
202
+ PASS
203
+ ok 0.001s
204
+ ```
205
+
206
+ Show all log and more detail infomation of testing.
207
+
208
+ ## Benchmark
209
+
210
+ ```
211
+ $ rgot target_file_test.rb -b .
212
+ ```
213
+
214
+ Run testing with benchmark.
215
+
216
+ `.` means match all string for regexp.
217
+
218
+ Set `someone` if you only run benchmark to match `someone` method.(e.g. benchmark_someone_1)
219
+
220
+ ## Timeout
221
+
222
+ ```
223
+ $ rgot target_file_test.rb --timeout 3
224
+ ```
225
+
226
+ You can set timeout sec for testing (default 0).
227
+
228
+ Fail testing and print raised exception message to STDERR if timeout.
229
+
230
+ # Rgot::M (Main)
231
+
232
+ Main method run first on testing.
233
+
234
+ And this is default virtual main code.
235
+
236
+ ```ruby
237
+ module TestSomeCode
238
+ def test_main(m)
239
+ exit m.run
240
+ end
241
+ end
242
+ ```
243
+
244
+ Main method should be set 'test_main' only.
245
+
246
+ variable `m` is a instance of `Rgot::M` class means Main.
247
+
248
+ `Rgot::M#run` start all testing methods.
249
+
250
+ And return code of process end status.
251
+
252
+ If you want to run before/after all testing method, You can write like this.
253
+
254
+ ```ruby
255
+ module TestSomeCode
256
+ def test_main(m)
257
+ the_before_running_some_code
258
+ code = m.run
259
+ the_after_running_some_code
260
+ exit code
261
+ end
262
+ end
263
+ ```
264
+
265
+ # Rgot::Common
266
+
267
+ `Rgot::Common` is inherited to `Rgot::T` and `Rgot::B`
268
+
269
+ `Rgot::Common` have some logging method.
270
+
271
+ ## Rgot::Common#log
272
+
273
+ ```ruby
274
+ t.log("wooooo")
275
+ ```
276
+
277
+ Write any log message.
278
+
279
+ But this message to show need -v option.
280
+
281
+ ## Rgot::Common#error
282
+
283
+ ```ruby
284
+ t.error("expect #{a} got #{b}")
285
+ ```
286
+
287
+ Test fail and show some error message.
288
+
289
+ ## Rgot::Common#skip
290
+
291
+ ```ruby
292
+ t.skip("this method was skipped")
293
+ ```
294
+
295
+ Skip current testing method.
296
+
297
+ And run to next testing method.
298
+
299
+ # Rgot::T (Testing)
300
+
301
+ Testing is a main usage of this package.
302
+
303
+ ```ruby
304
+ module TestSomeCode
305
+ def test_some_1(t)
306
+ end
307
+ end
308
+ ```
309
+
310
+ The `t` variable is instance of `Rgot::T` class means Testing.
311
+
312
+ # Rgot::B (Benchmark)
313
+
314
+ For Benchmark class.
315
+
316
+ Can use log methods same as `Rgot::T` class
317
+
318
+ ## Rgot::B#reset_timer
319
+
320
+ Reset benchmark timer
321
+
322
+ ## Rgot::B#start_timer
323
+
324
+ Start benchmark timer
325
+
326
+ ## Rgot::B#stop_timer
327
+
328
+ Stop benchmark timer
data/bin/rgot CHANGED
@@ -16,6 +16,9 @@ parser = OptionParser.new do |o|
16
16
  o.on '--benchtime [sec]', "benchmark running time" do |arg|
17
17
  opts[:benchtime] = arg
18
18
  end
19
+ o.on '--timeout [sec]', "set timeout sec to testing" do |arg|
20
+ opts[:timeout] = arg
21
+ end
19
22
  end
20
23
  parser.parse!(ARGV)
21
24
 
@@ -49,12 +52,13 @@ end
49
52
 
50
53
  tests = []
51
54
  benchmarks = []
55
+ examples = []
52
56
  main = nil
53
57
  c = modules.first
54
58
 
55
59
  test_module = Object.const_get(c)
56
- methods = test_module.instance_methods
57
- methods.grep(/\Atest_.*/).sort.each do |m|
60
+ methods = test_module.instance_methods.sort
61
+ methods.grep(/\Atest_/).each do |m|
58
62
  if m == :test_main && main.nil?
59
63
  main = Rgot::InternalTest.new(test_module, m)
60
64
  else
@@ -62,14 +66,20 @@ methods.grep(/\Atest_.*/).sort.each do |m|
62
66
  end
63
67
  end
64
68
 
65
- methods.grep(/\Abenchmark_.*/).sort.each do |m|
69
+ methods.grep(/\Abenchmark_/).each do |m|
66
70
  benchmarks << Rgot::InternalBenchmark.new(test_module, m)
67
71
  end
68
72
 
69
- m = Rgot::M.new(tests: tests, benchmarks: benchmarks, opts: opts)
73
+ methods.grep(/\Aexample_?/).each do |m|
74
+ examples << Rgot::InternalExample.new(test_module, m)
75
+ end
76
+
77
+ m = Rgot::M.new(tests: tests, benchmarks: benchmarks, examples: examples, opts: opts)
70
78
  duration = Rgot.now
71
79
  at_exit {
72
- puts sprintf("ok\t%.3fs", Rgot.now - duration)
80
+ if !$!
81
+ puts sprintf("ok\t%.3fs", Rgot.now - duration)
82
+ end
73
83
  }
74
84
  if main
75
85
  main.module.extend main.module
@@ -1,9 +1,10 @@
1
1
  module Rgot
2
- autoload :VERSION, 'rgot/version'
3
- autoload :Common, 'rgot/common'
4
- autoload :M, 'rgot/m'
5
- autoload :T, 'rgot/t'
6
- autoload :B, 'rgot/b'
2
+ require 'rgot/version'
3
+ require 'rgot/common'
4
+ require 'rgot/m'
5
+ require 'rgot/t'
6
+ require 'rgot/b'
7
+ require 'rgot/example_parser'
7
8
 
8
9
  class OptionError < StandardError
9
10
  end
@@ -14,6 +15,12 @@ module Rgot
14
15
  class InternalBenchmark < Struct.new(:module, :name)
15
16
  end
16
17
 
18
+ class InternalExample < Struct.new(:module, :name)
19
+ end
20
+
21
+ class ExampleOutput < Struct.new(:name, :output)
22
+ end
23
+
17
24
  class << self
18
25
  if "2.0.0" < RUBY_VERSION
19
26
  def now
@@ -9,7 +9,6 @@ module Rgot
9
9
  @opts = opts
10
10
  @benchtime = @opts.fetch(:benchtime, 1).to_f
11
11
  @timer_on = false
12
- @start = Rgot.now
13
12
  @duration = 0
14
13
  @module.extend @module
15
14
  end
@@ -37,7 +36,6 @@ module Rgot
37
36
 
38
37
  def run
39
38
  n = 1
40
- a = Rgot.now
41
39
  run_n(n)
42
40
  while !failed? && @duration < @benchtime && @n < 1e9
43
41
  if @duration < (@benchtime / 100.0)
@@ -28,7 +28,7 @@ module Rgot
28
28
  @skipped = true
29
29
  end
30
30
 
31
- def finished!
31
+ def finish!
32
32
  @finished = true
33
33
  end
34
34
 
@@ -36,6 +36,17 @@ module Rgot
36
36
  internal_log(sprintf(*args))
37
37
  end
38
38
 
39
+ def skip(*args)
40
+ internal_log(sprintf(*args))
41
+ skip_now
42
+ end
43
+
44
+ def skip_now
45
+ skip!
46
+ finish!
47
+ throw :skip
48
+ end
49
+
39
50
  def error(*args)
40
51
  internal_log(sprintf(*args))
41
52
  fail!
@@ -43,13 +54,13 @@ module Rgot
43
54
 
44
55
  def fatal(msg)
45
56
  internal_log(msg)
46
- fail_now!
57
+ fail_now
47
58
  end
48
59
 
49
- def fail_now!
60
+ def fail_now
50
61
  fail!
51
- @finished = true
52
- exit 1
62
+ finish!
63
+ throw :skip
53
64
  end
54
65
 
55
66
  private
@@ -0,0 +1,46 @@
1
+ require 'ripper'
2
+
3
+ module Rgot
4
+ class ExampleParser < Ripper
5
+ attr_accessor :examples
6
+ def initialize(code)
7
+ super
8
+ @examples = []
9
+ @in_def = false
10
+ @has_output = false
11
+ @output = ""
12
+ end
13
+
14
+ def on_def(method, args, body)
15
+ @examples << ExampleOutput.new(method.to_sym, @output.dup)
16
+ @output.clear
17
+ @has_output = false
18
+ @in_def = false
19
+ end
20
+
21
+ def on_comment(a)
22
+ if @in_def
23
+ if @has_output
24
+ @output << a.sub(/\A#\s*/, '')
25
+ else
26
+ if /#\s*Output:\s*(.*?\n)/ =~ a
27
+ text = $1
28
+ if 0 < text.length || text[0] != "\n"
29
+ @output << text
30
+ end
31
+ @has_output = true
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def on_kw(a)
38
+ case a
39
+ when "def"
40
+ @in_def = true
41
+ when "end"
42
+ @in_def = false
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,22 +1,30 @@
1
+ require 'stringio'
2
+
1
3
  module Rgot
2
4
  class M
3
5
  # Ruby-2.0.0 wants default value of keyword_argument
4
- def initialize(tests: [], benchmarks: [], opts: {})
6
+ def initialize(tests: [], benchmarks: [], examples: [], opts: {})
5
7
  @tests = tests
6
8
  @benchmarks = benchmarks
9
+ @examples = examples
7
10
  @opts = opts
8
11
  end
9
12
 
10
13
  def run
11
- test_ok = run_tests
12
- benchmark_ok = run_benchmarks
13
- if !test_ok || !benchmark_ok
14
+ test_ok = false
15
+ example_ok = false
16
+ Timeout.timeout(@opts[:timeout].to_f) {
17
+ test_ok = run_tests
18
+ example_ok = run_examples
19
+ }
20
+ if !test_ok || !example_ok
14
21
  puts "FAIL"
15
22
  1
16
23
  else
17
24
  puts "PASS"
18
25
  0
19
26
  end
27
+ run_benchmarks
20
28
  end
21
29
 
22
30
  private
@@ -26,7 +34,7 @@ module Rgot
26
34
  @tests.each do |test|
27
35
  t = T.new(test.module, test.name.to_sym, @opts)
28
36
  if @opts[:verbose]
29
- puts "=== RUN #{test.name}\n"
37
+ puts "=== RUN #{test.name}"
30
38
  end
31
39
  t.run
32
40
  t.report
@@ -52,5 +60,43 @@ module Rgot
52
60
  end
53
61
  ok
54
62
  end
63
+
64
+ def run_examples
65
+ ok = true
66
+ @examples.each do |example|
67
+ if @opts[:verbose]
68
+ puts "=== RUN #{example.name}"
69
+ end
70
+ example.module.extend(example.module)
71
+ method = example.module.instance_method(example.name).bind(example.module)
72
+ out, err = capture do
73
+ method.call
74
+ end
75
+ file = method.source_location[0]
76
+ r = ExampleParser.new(File.read(file))
77
+ r.parse
78
+ e = r.examples.find{|e| e.name == example.name}
79
+ if e && e.output.strip != out.strip
80
+ ok = false
81
+ puts "got:"
82
+ puts out.strip
83
+ puts "want:"
84
+ puts e.output.strip
85
+ end
86
+ end
87
+ ok
88
+ end
89
+
90
+ private
91
+
92
+ def capture
93
+ orig_out, orig_err = $stdout, $stderr
94
+ out, err = StringIO.new, StringIO.new
95
+ $stdout, $stderr = out, err
96
+ yield
97
+ [out.string, err.string]
98
+ ensure
99
+ $stdout, $stderr = orig_out, orig_err
100
+ end
55
101
  end
56
102
  end
@@ -10,8 +10,8 @@ module Rgot
10
10
 
11
11
  def run
12
12
  begin
13
- call
14
- finished!
13
+ catch(:skip) { call }
14
+ finish!
15
15
  rescue => e
16
16
  fail!
17
17
  report
@@ -1,3 +1,3 @@
1
1
  module Rgot
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["ksss"]
10
10
  spec.email = ["co000ri@gmail.com"]
11
11
 
12
- spec.summary = %q{The Ruby GOlang like Testing module}
13
- spec.description = %q{rgot is golang like testing module in ruby}
12
+ spec.summary = %q{Ruby + Golang Testing = Rgot}
13
+ spec.description = %q{Rgot is a testing package convert from golang testing}
14
14
  spec.homepage = "https://github.com/ksss/rgot"
15
15
  spec.license = "MIT"
16
16
 
metadata CHANGED
@@ -1,44 +1,44 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ksss
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-02 00:00:00.000000000 Z
11
+ date: 2015-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: rgot is golang like testing module in ruby
41
+ description: Rgot is a testing package convert from golang testing
42
42
  email:
43
43
  - co000ri@gmail.com
44
44
  executables:
@@ -46,8 +46,8 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
- - .gitignore
50
- - .travis.yml
49
+ - ".gitignore"
50
+ - ".travis.yml"
51
51
  - Gemfile
52
52
  - LICENSE.txt
53
53
  - README.md
@@ -56,6 +56,7 @@ files:
56
56
  - lib/rgot.rb
57
57
  - lib/rgot/b.rb
58
58
  - lib/rgot/common.rb
59
+ - lib/rgot/example_parser.rb
59
60
  - lib/rgot/m.rb
60
61
  - lib/rgot/t.rb
61
62
  - lib/rgot/version.rb
@@ -70,18 +71,18 @@ require_paths:
70
71
  - lib
71
72
  required_ruby_version: !ruby/object:Gem::Requirement
72
73
  requirements:
73
- - - '>='
74
+ - - ">="
74
75
  - !ruby/object:Gem::Version
75
76
  version: '0'
76
77
  required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  requirements:
78
- - - '>='
79
+ - - ">="
79
80
  - !ruby/object:Gem::Version
80
81
  version: '0'
81
82
  requirements: []
82
83
  rubyforge_project:
83
- rubygems_version: 2.0.14
84
+ rubygems_version: 2.4.5
84
85
  signing_key:
85
86
  specification_version: 4
86
- summary: The Ruby GOlang like Testing module
87
+ summary: Ruby + Golang Testing = Rgot
87
88
  test_files: []