assert 2.7.1 → 2.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/assert/factory.rb +146 -0
- data/lib/assert/version.rb +1 -1
- data/test/unit/factory_tests.rb +132 -0
- metadata +8 -5
@@ -0,0 +1,146 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
module Assert
|
5
|
+
|
6
|
+
module Factory
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def integer(max = nil)
|
10
|
+
self.type_cast(Random.integer(max), :integer)
|
11
|
+
end
|
12
|
+
|
13
|
+
def float(max = nil)
|
14
|
+
self.type_cast(Random.float(max), :float)
|
15
|
+
end
|
16
|
+
|
17
|
+
DAYS_IN_A_YEAR = 365
|
18
|
+
SECONDS_IN_DAY = 24 * 60 * 60
|
19
|
+
|
20
|
+
def date
|
21
|
+
@date ||= self.type_cast(Random.date_string, :date)
|
22
|
+
@date + Random.integer(DAYS_IN_A_YEAR)
|
23
|
+
end
|
24
|
+
|
25
|
+
def time
|
26
|
+
@time ||= self.type_cast(Random.time_string, :time)
|
27
|
+
@time + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
def datetime
|
31
|
+
@datetime ||= self.type_cast(Random.datetime_string, :datetime)
|
32
|
+
@datetime + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def string(length = nil)
|
36
|
+
self.type_cast(Random.string(length), :string)
|
37
|
+
end
|
38
|
+
|
39
|
+
def text(length = nil)
|
40
|
+
self.type_cast(Random.string(length || 20), :string)
|
41
|
+
end
|
42
|
+
|
43
|
+
def slug(length = nil)
|
44
|
+
self.type_cast(Random.slug_string(length), :string)
|
45
|
+
end
|
46
|
+
|
47
|
+
def hex(length = nil)
|
48
|
+
self.type_cast(Random.hex_string(length), :string)
|
49
|
+
end
|
50
|
+
|
51
|
+
def file_name(length = nil)
|
52
|
+
self.type_cast(Random.file_name_string(length), :string)
|
53
|
+
end
|
54
|
+
|
55
|
+
def dir_path(length = nil)
|
56
|
+
self.type_cast(Random.dir_path_string(length), :string)
|
57
|
+
end
|
58
|
+
|
59
|
+
def file_path
|
60
|
+
self.type_cast(Random.file_path_string, :string)
|
61
|
+
end
|
62
|
+
|
63
|
+
def binary
|
64
|
+
self.type_cast(Random.binary, :binary)
|
65
|
+
end
|
66
|
+
|
67
|
+
def boolean
|
68
|
+
self.type_cast(Random.integer.even?, :boolean)
|
69
|
+
end
|
70
|
+
|
71
|
+
def type_cast(value, type)
|
72
|
+
self.type_converter.send(type, value)
|
73
|
+
end
|
74
|
+
|
75
|
+
def type_converter; TypeConverter; end
|
76
|
+
|
77
|
+
module TypeConverter
|
78
|
+
def self.string(input); input.to_s; end
|
79
|
+
def self.integer(input); input.to_i; end
|
80
|
+
def self.float(input); input.to_f; end
|
81
|
+
def self.datetime(input); DateTime.parse(input.to_s); end
|
82
|
+
def self.time(input); Time.parse(input.to_s); end
|
83
|
+
def self.date(input); Date.parse(input.to_s); end
|
84
|
+
def self.boolean(input); !!input; end
|
85
|
+
def self.binary(input); input; end
|
86
|
+
end
|
87
|
+
|
88
|
+
module Random
|
89
|
+
def self.integer(max = nil)
|
90
|
+
rand(max || 100) + 1
|
91
|
+
end
|
92
|
+
|
93
|
+
# `rand` with no args gives a float between 0 and 1
|
94
|
+
def self.float(max = nil)
|
95
|
+
(self.integer((max || 100) - 1) + rand).to_f
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.date_string
|
99
|
+
Time.now.strftime("%Y-%m-%d")
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.datetime_string
|
103
|
+
Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.time_string
|
107
|
+
Time.now.strftime("%H:%M:%S")
|
108
|
+
end
|
109
|
+
|
110
|
+
DICTIONARY = [*'a'..'z'].freeze
|
111
|
+
def self.string(length = nil)
|
112
|
+
[*0..((length || 10) - 1)].map{ |n| DICTIONARY[rand(DICTIONARY.size)] }.join
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.slug_string(length = nil)
|
116
|
+
length ||= 8
|
117
|
+
self.string(length).scan(/.{1,4}/).join('-')
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.hex_string(length = nil)
|
121
|
+
length ||= 10
|
122
|
+
self.integer(("f" * length).hex - 1).to_s(16).rjust(length, '0')
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.file_name_string(length = nil)
|
126
|
+
length ||= 6
|
127
|
+
"#{self.string(length)}.#{self.string(3)}"
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.dir_path_string(length = nil)
|
131
|
+
length ||= 12
|
132
|
+
File.join(*self.string(length).scan(/.{1,4}/))
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.file_path_string
|
136
|
+
File.join(self.dir_path_string, self.file_name_string)
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.binary
|
140
|
+
[ self.integer(10000) ].pack('N*')
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
data/lib/assert/version.rb
CHANGED
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'assert'
|
2
|
+
require 'assert/factory'
|
3
|
+
|
4
|
+
module Assert::Factory
|
5
|
+
|
6
|
+
class UnitTests < Assert::Context
|
7
|
+
desc "Assert::Factory"
|
8
|
+
subject{ Assert::Factory }
|
9
|
+
|
10
|
+
should have_imeths :integer, :float
|
11
|
+
should have_imeths :date, :time, :datetime
|
12
|
+
should have_imeths :string, :text, :slug, :hex
|
13
|
+
should have_imeths :file_name, :dir_path, :file_path
|
14
|
+
should have_imeths :binary, :boolean
|
15
|
+
should have_imeths :type_cast, :type_converter
|
16
|
+
|
17
|
+
should "return a random integer using `integer`" do
|
18
|
+
assert_kind_of Integer, subject.integer
|
19
|
+
end
|
20
|
+
|
21
|
+
should "allow passing a maximum value using `integer`" do
|
22
|
+
assert_includes subject.integer(2), [ 1, 2 ]
|
23
|
+
end
|
24
|
+
|
25
|
+
should "return a random float using `float`" do
|
26
|
+
assert_kind_of Float, subject.float
|
27
|
+
end
|
28
|
+
|
29
|
+
should "allow passing a maximum value using `float`" do
|
30
|
+
float = subject.float(2)
|
31
|
+
assert float <= 2
|
32
|
+
assert float >= 1
|
33
|
+
end
|
34
|
+
|
35
|
+
should "return a random date using `date`" do
|
36
|
+
assert_kind_of Date, subject.date
|
37
|
+
end
|
38
|
+
|
39
|
+
should "return a random time object using `time`" do
|
40
|
+
assert_kind_of Time, subject.time
|
41
|
+
end
|
42
|
+
|
43
|
+
should "return a random time object using `datetime`" do
|
44
|
+
assert_kind_of DateTime, subject.datetime
|
45
|
+
end
|
46
|
+
|
47
|
+
should "return a random string using `string`" do
|
48
|
+
assert_kind_of String, subject.string
|
49
|
+
assert_equal 10, subject.string.length
|
50
|
+
end
|
51
|
+
|
52
|
+
should "allow passing a maximum length using `string`" do
|
53
|
+
assert_equal 1, subject.string(1).length
|
54
|
+
end
|
55
|
+
|
56
|
+
should "return a random string using `text`" do
|
57
|
+
assert_kind_of String, subject.text
|
58
|
+
assert_equal 20, subject.text.length
|
59
|
+
end
|
60
|
+
|
61
|
+
should "allow passing a maximum length using `text`" do
|
62
|
+
assert_equal 1, subject.text(1).length
|
63
|
+
end
|
64
|
+
|
65
|
+
should "return a random hex string using `hex`" do
|
66
|
+
assert_kind_of String, subject.hex
|
67
|
+
assert_match /\A[0-9a-f]{10}\Z/, subject.hex
|
68
|
+
end
|
69
|
+
|
70
|
+
should "allow passing a maximum length using `hex`" do
|
71
|
+
assert_equal 1, subject.hex(1).length
|
72
|
+
end
|
73
|
+
|
74
|
+
should "return a random slug string using `slug`" do
|
75
|
+
assert_kind_of String, subject.slug
|
76
|
+
segments = subject.slug.split('-')
|
77
|
+
assert_equal 2, segments.size
|
78
|
+
segments.each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
79
|
+
end
|
80
|
+
|
81
|
+
should "allow passing a maximum length using `slug`" do
|
82
|
+
assert_equal 1, subject.slug(1).length
|
83
|
+
end
|
84
|
+
|
85
|
+
should "return a random file name string using `file_name`" do
|
86
|
+
assert_kind_of String, subject.file_name
|
87
|
+
assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, subject.file_name
|
88
|
+
end
|
89
|
+
|
90
|
+
should "allow passing a name length using `file_name`" do
|
91
|
+
assert_match /\A[a-z]{1}.[a-z]{3}\Z/, subject.file_name(1)
|
92
|
+
end
|
93
|
+
|
94
|
+
should "return a random folder path string using `dir_path`" do
|
95
|
+
assert_kind_of String, subject.dir_path
|
96
|
+
path_segments = subject.dir_path.split('/')
|
97
|
+
assert_equal 3, path_segments.size
|
98
|
+
path_segments.each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
99
|
+
end
|
100
|
+
|
101
|
+
should "allow passing a maximum length using `dir_path`" do
|
102
|
+
assert_equal 1, subject.dir_path(1).length
|
103
|
+
end
|
104
|
+
|
105
|
+
should "return a random folder path and file name using `file_path`" do
|
106
|
+
assert_kind_of String, subject.file_path
|
107
|
+
segments = subject.file_path.split('/')
|
108
|
+
assert_equal 4, segments.size
|
109
|
+
segments[0..-2].each{ |s| assert_match /\A[a-z]{4}\Z/, s }
|
110
|
+
assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, segments.last
|
111
|
+
end
|
112
|
+
|
113
|
+
should "return a random binary string using `binary`" do
|
114
|
+
assert_kind_of String, subject.binary
|
115
|
+
end
|
116
|
+
|
117
|
+
should "return a random boolean using `boolean`" do
|
118
|
+
assert_includes subject.boolean.class, [ TrueClass, FalseClass ]
|
119
|
+
end
|
120
|
+
|
121
|
+
should "type cast values to a specified type using `type_cast`" do
|
122
|
+
expected = Date.parse('2013-01-01')
|
123
|
+
assert_equal expected, subject.type_cast('2013-01-01', :date)
|
124
|
+
end
|
125
|
+
|
126
|
+
should "use `TypedConverter` for the default type converter" do
|
127
|
+
assert_equal TypeConverter, subject.type_converter
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 47
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 2.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 2.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kelly Redding
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2014-02-
|
19
|
+
date: 2014-02-11 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
prerelease: false
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- lib/assert/context/subject_dsl.rb
|
63
63
|
- lib/assert/context/suite_dsl.rb
|
64
64
|
- lib/assert/context/test_dsl.rb
|
65
|
+
- lib/assert/factory.rb
|
65
66
|
- lib/assert/macro.rb
|
66
67
|
- lib/assert/macros/methods.rb
|
67
68
|
- lib/assert/result.rb
|
@@ -103,6 +104,7 @@ files:
|
|
103
104
|
- test/unit/context/suite_dsl_tests.rb
|
104
105
|
- test/unit/context/test_dsl_tests.rb
|
105
106
|
- test/unit/context_tests.rb
|
107
|
+
- test/unit/factory_tests.rb
|
106
108
|
- test/unit/macro_tests.rb
|
107
109
|
- test/unit/result_tests.rb
|
108
110
|
- test/unit/runner_tests.rb
|
@@ -172,6 +174,7 @@ test_files:
|
|
172
174
|
- test/unit/context/suite_dsl_tests.rb
|
173
175
|
- test/unit/context/test_dsl_tests.rb
|
174
176
|
- test/unit/context_tests.rb
|
177
|
+
- test/unit/factory_tests.rb
|
175
178
|
- test/unit/macro_tests.rb
|
176
179
|
- test/unit/result_tests.rb
|
177
180
|
- test/unit/runner_tests.rb
|