mrproper 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.
data/README.md CHANGED
@@ -77,6 +77,8 @@ In addition to plain class names, we can feed `data` with more or less complex e
77
77
  # or [-44, "eB"]
78
78
  data [[Float]] # generates arrays of arrays of floats
79
79
  # such as [[0.12, 3.41], [-2.31]]
80
+ data (1..10) # generates integers between 1 and 10
81
+ data (0.0..10.0) # generates floats between 1 and 10
80
82
  data({Symbol => String}) # generates hashes whose keys are symbols
81
83
  # and whose values are strings such as
82
84
  # {:tR=>"m", :aSKnsndwWK=>"QUrGwAAh"}
@@ -85,8 +87,13 @@ In addition to plain class names, we can feed `data` with more or less complex e
85
87
  # parser thinks we're defining a block `;)`)
86
88
  data({String => [Integer]}) # generates hashes whose keys are strings
87
89
  # and whose values are arrays of integers
90
+ data({:first => String,
91
+ :second => Integer}) # generates a hash with two keys, :first and
92
+ # :second, each one with an String or Integer
93
+ # value (this is specially useful for testing
94
+ # methods with more than one parameter)
88
95
 
89
- In case this is not enough, you can just use a block and do whatever you want to generate the data:
96
+ You can combine this cases ad infinitum, but in case this is not enough, you can just use a block and do whatever you want to generate the data:
90
97
 
91
98
  data do
92
99
  rand > 0.5 ? Wadus.new(rand(9)) : FooBar.new(rand(9))
@@ -1,6 +1,7 @@
1
1
  require 'test/unit'
2
2
  require 'mrproper/base'
3
3
  require 'mrproper/dsl'
4
+ require 'mrproper/data_block'
4
5
  require 'mrproper/errors'
5
6
  require 'mrproper/core_extensions/string_extensions'
6
7
 
@@ -8,8 +8,7 @@ module MrProper
8
8
  dsl.properties.each do |message, test_block|
9
9
  define_method "test_property: #{message.inspect}" do
10
10
  dsl.data_blocks.each do |data_block|
11
- TESTS_PER_PROPERTY.times do
12
- data = data_block.call
11
+ data_block.data.each do |data|
13
12
  begin
14
13
  instance_exec(data, &test_block)
15
14
  rescue Test::Unit::AssertionFailedError, MiniTest::Assertion => e
@@ -0,0 +1,76 @@
1
+ module MrProper
2
+
3
+ class DataBlock
4
+
5
+ EXAMPLES_PER_PROPERTY = 200
6
+
7
+ def initialize(spec = nil, &block)
8
+ @spec = spec
9
+ @block = block
10
+ end
11
+
12
+ def data
13
+ EXAMPLES_PER_PROPERTY.times.map { call }.uniq
14
+ end
15
+
16
+ def call
17
+ to_proc.call
18
+ end
19
+
20
+ def to_proc
21
+ return @block if @block
22
+
23
+ case @spec
24
+ when Array
25
+ if @spec.size == 1
26
+ Proc.new { rand(20).times.map { DataBlock.new(@spec.first).call } }
27
+ else
28
+ Proc.new { @spec.map { |s| DataBlock.new(s).call }}
29
+ end
30
+ when Hash
31
+ if @spec.size == 1
32
+ Proc.new do
33
+ {}.tap do |h|
34
+ rand(20).times.each do
35
+ h[DataBlock.new(@spec.keys.first).call] = DataBlock.new(@spec.values.first).call
36
+ end
37
+ end
38
+ end
39
+ else
40
+ Proc.new do
41
+ {}.tap do |h|
42
+ @spec.each do |k, v|
43
+ h[DataBlock.new(k).call] = DataBlock.new(v).call
44
+ end
45
+ end
46
+ end
47
+ end
48
+ when Range
49
+ case @spec.begin
50
+ when Integer
51
+ Proc.new { @spec.begin + rand(@spec.end - @spec.begin) }
52
+ when Float
53
+ Proc.new { @spec.begin + rand * (@spec.end - @spec.begin) }
54
+ else
55
+ Proc.new { @spec }
56
+ end
57
+ when Class
58
+ if @spec == Integer
59
+ DataBlock.new(-500..500).to_proc
60
+ elsif @spec == Float
61
+ DataBlock.new(-5.0..5.0).to_proc
62
+ elsif @spec == String
63
+ Proc.new { String.random(rand(10)) }
64
+ elsif @spec == Symbol
65
+ Proc.new { DataBlock.new(String).call.to_sym }
66
+ elsif @spec == NilClass
67
+ Proc.new { nil }
68
+ end
69
+ else
70
+ Proc.new { @spec }
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -1,7 +1,5 @@
1
1
  module MrProper
2
2
 
3
- TESTS_PER_PROPERTY = 100
4
-
5
3
  class DSL
6
4
 
7
5
  attr_reader :properties, :data_blocks
@@ -12,50 +10,13 @@ module MrProper
12
10
  end
13
11
 
14
12
  def data(spec = nil, &block)
15
- @data_blocks << data_block(spec, &block)
13
+ @data_blocks << DataBlock.new(spec, &block)
16
14
  end
17
15
 
18
16
  def property(message, &block)
19
17
  @properties << [message, block]
20
18
  end
21
19
 
22
- private
23
-
24
- def data_block(spec = nil, &block)
25
- return block if block_given?
26
-
27
- case spec
28
- when Array
29
- if spec.size == 1
30
- Proc.new { rand(20).times.map { data_block(spec.first).call } }
31
- else
32
- Proc.new { spec.map { |s| data_block(s).call }}
33
- end
34
- when Hash
35
- Proc.new do
36
- {}.tap do |h|
37
- rand(20).times.each do
38
- h[data_block(spec.keys.first).call] = data_block(spec.values.first).call
39
- end
40
- end
41
- end
42
- when Class
43
- if spec == Integer
44
- Proc.new { rand(1000) - 500 }
45
- elsif spec == Float
46
- Proc.new { rand * 10 - 10 }
47
- elsif spec == String
48
- Proc.new { String.random(rand(10)) }
49
- elsif spec == Symbol
50
- Proc.new { data_block(String).call.to_sym }
51
- elsif spec == NilClass
52
- Proc.new { nil }
53
- end
54
- else
55
- Proc.new { spec }
56
- end
57
- end
58
-
59
20
  end
60
21
 
61
22
  end
metadata CHANGED
@@ -1,54 +1,74 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mrproper
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Sergio Gil
9
- - Mari Carmen Gutiérrez
14
+ - "Mari Carmen Guti\xC3\xA9rrez"
10
15
  autorequire:
11
16
  bindir: bin
12
17
  cert_chain: []
13
- date: 2011-11-09 00:00:00.000000000Z
18
+
19
+ date: 2011-11-25 00:00:00 Z
14
20
  dependencies: []
21
+
15
22
  description:
16
23
  email: sgilperez@gmail.com
17
24
  executables: []
25
+
18
26
  extensions: []
19
- extra_rdoc_files:
27
+
28
+ extra_rdoc_files:
20
29
  - README.md
21
- files:
30
+ files:
22
31
  - README.md
23
32
  - lib/mrproper/base.rb
24
33
  - lib/mrproper/core_extensions/string_extensions.rb
34
+ - lib/mrproper/data_block.rb
25
35
  - lib/mrproper/dsl.rb
26
36
  - lib/mrproper/errors.rb
27
37
  - lib/mrproper.rb
28
38
  homepage: http://github.com/porras/mrproper
29
39
  licenses: []
40
+
30
41
  post_install_message:
31
- rdoc_options:
42
+ rdoc_options:
32
43
  - --main
33
44
  - README.md
34
- require_paths:
45
+ require_paths:
35
46
  - lib
36
- required_ruby_version: !ruby/object:Gem::Requirement
47
+ required_ruby_version: !ruby/object:Gem::Requirement
37
48
  none: false
38
- requirements:
39
- - - ! '>='
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
57
  none: false
44
- requirements:
45
- - - ! '>='
46
- - !ruby/object:Gem::Version
47
- version: '0'
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
48
65
  requirements: []
66
+
49
67
  rubyforge_project:
50
68
  rubygems_version: 1.8.10
51
69
  signing_key:
52
70
  specification_version: 3
53
71
  summary: Property Based Testing library
54
72
  test_files: []
73
+
74
+ has_rdoc: