mkspec 0.0.4 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/mkspec.rb +9 -44
- data/lib/mkspec/expectation.rb +76 -0
- data/lib/mkspec/matchers/change_matcher.rb +25 -0
- data/lib/mkspec/matchers/equality_matcher.rb +18 -0
- data/lib/mkspec/version.rb +1 -1
- data/spec/mkspec_spec.rb +71 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YWM5Y2EyZGZlYWRiN2E3OThiY2QwNWE0NWY3NTJiMDc4N2Q3NDJjMw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTA3NjkyZmEwNDc3ZTA3ZjVlMDM3NmM4YjgzMTA2NWM0YzI2NjU2Zg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGVjMmU5ZGFhZmIyZTNmODQzMGI3ZjM2ZWEyOTg3ZmM2NzU3OTk5YTI3YjFm
|
10
|
+
ZWExMWM5OTU1Y2RjOWE2NmQ3ZWZmYjg3ZGZhNjNhMDI3ZjY3YWU0NTMxZjJm
|
11
|
+
NjQ5MmQ3NTRiMjQzMzBmZTFmZTM5MjY5NzY2NTIzZTI1MDdhY2E=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MTBhZjI1NGJiMDQ3YmFjYjAyZjE5YjkzNGY1MTI0NDcyMjYzOTM5YzMzNGZh
|
14
|
+
MTE5MWUyYjFkNTk0M2Y5NDllMzNkMmU0MjM4ZmI0YjNhNzRiOTVlZjM1NTM5
|
15
|
+
ZmE4NjU4NDI0ODIwNjQzMGVkMGE0YjYwYzQwNWY2YWFhZDJiNDU=
|
data/lib/mkspec.rb
CHANGED
@@ -1,54 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def initialize(class_name, method_name, args)
|
6
|
-
@class_name = class_name
|
7
|
-
@method_name = method_name
|
8
|
-
@args = args
|
9
|
-
end
|
10
|
-
|
11
|
-
def to(matcher)
|
12
|
-
<<-EOF
|
13
|
-
describe #{@class_name} do
|
14
|
-
subject {described_class.new}
|
1
|
+
require_relative "mkspec/expectation"
|
2
|
+
require_relative "mkspec/matchers/equality_matcher"
|
3
|
+
require_relative "mkspec/matchers/change_matcher"
|
15
4
|
|
16
|
-
|
17
|
-
|
18
|
-
it "does something" do
|
19
|
-
expect(subject.#{@method_name}(#{@args.map{|arg| serialise(arg) }.join(', ')})).to #{matcher}
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
EOF
|
25
|
-
end
|
26
|
-
|
27
|
-
# Attempt to recreate the source-code to represent this argument in the setup
|
28
|
-
# for our generated spec.
|
29
|
-
def serialise(arg)
|
30
|
-
if %w(Array Hash Float Fixnum String).include? arg.class.name
|
31
|
-
arg.pretty_inspect.chop
|
32
|
-
else
|
33
|
-
guess_constructor arg
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
# Don't recognise the type so we don't know how to recreate it
|
38
|
-
# in source code. So we'll take a guess at what might work and
|
39
|
-
# let the user fix it up if necessary.
|
40
|
-
def guess_constructor(arg)
|
41
|
-
"#{arg.class.name}.new(#{serialise(arg.to_s)})"
|
42
|
-
end
|
43
|
-
end
|
5
|
+
module Mkspec
|
44
6
|
|
45
7
|
def expect(sut, method, *args)
|
46
|
-
|
8
|
+
Expectation.new(sut.class.name, method, args)
|
47
9
|
end
|
48
10
|
|
49
11
|
def eq(expected)
|
50
|
-
|
12
|
+
Matchers::EqualityMatcher.new(expected)
|
13
|
+
end
|
51
14
|
|
15
|
+
def change(obj, method)
|
16
|
+
Matchers::ChangeMatcher.new(obj, method)
|
52
17
|
end
|
53
18
|
|
54
19
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
|
2
|
+
class Expectation
|
3
|
+
|
4
|
+
def initialize(class_name, method_name, args)
|
5
|
+
@class_name = class_name
|
6
|
+
@method_name = method_name
|
7
|
+
@args = args
|
8
|
+
@explanation = "does something"
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :inclination
|
12
|
+
|
13
|
+
def to(matcher)
|
14
|
+
@inclination = 'to'
|
15
|
+
@matcher = matcher
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def not_to(matcher)
|
20
|
+
@inclination = 'not_to'
|
21
|
+
@matcher = matcher
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def as(explanation)
|
26
|
+
@explanation = explanation
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_s
|
31
|
+
<<-EOF
|
32
|
+
describe #{@class_name} do
|
33
|
+
subject {described_class.new}
|
34
|
+
|
35
|
+
describe "##{@method_name}" do
|
36
|
+
|
37
|
+
it "#{@explanation}" do
|
38
|
+
#{@matcher.match(self)}
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
EOF
|
44
|
+
end
|
45
|
+
|
46
|
+
def action
|
47
|
+
"subject.#{@method_name}#{parameters}"
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def parameters
|
53
|
+
@args.any? ? "(#{arg_list})" : ""
|
54
|
+
end
|
55
|
+
|
56
|
+
def arg_list
|
57
|
+
@args.map{|arg| serialise(arg) }.join(', ')
|
58
|
+
end
|
59
|
+
|
60
|
+
# Attempt to recreate the source-code to represent this argument in the setup
|
61
|
+
# for our generated spec.
|
62
|
+
def serialise(arg)
|
63
|
+
if %w(Array Hash Float Fixnum String).include? arg.class.name
|
64
|
+
arg.pretty_inspect.chop
|
65
|
+
else
|
66
|
+
guess_constructor arg
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Don't recognise the type so we don't know how to recreate it
|
71
|
+
# in source code. So we'll take a guess at what might work and
|
72
|
+
# let the user fix it up if necessary.
|
73
|
+
def guess_constructor(arg)
|
74
|
+
"#{arg.class.name}.new(#{serialise(arg.to_s)})"
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Mkspec
|
2
|
+
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
class ChangeMatcher
|
6
|
+
|
7
|
+
def initialize(obj, method)
|
8
|
+
@obj = obj
|
9
|
+
@method = method
|
10
|
+
end
|
11
|
+
|
12
|
+
def by(quantity)
|
13
|
+
@quantity = quantity
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def match(expectation)
|
18
|
+
%Q{expect{ #{expectation.action} }.#{expectation.inclination} change(#{@obj}, :count).by(#{@quantity})}
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Mkspec
|
2
|
+
|
3
|
+
module Matchers
|
4
|
+
|
5
|
+
|
6
|
+
class EqualityMatcher
|
7
|
+
def initialize(expected)
|
8
|
+
@expected = expected
|
9
|
+
end
|
10
|
+
|
11
|
+
def match(expectation)
|
12
|
+
"expect(#{expectation.action}).#{expectation.inclination} eq(#{@expected})"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/lib/mkspec/version.rb
CHANGED
data/spec/mkspec_spec.rb
CHANGED
@@ -27,6 +27,53 @@ end
|
|
27
27
|
end
|
28
28
|
|
29
29
|
|
30
|
+
it "is a DSL for building specs" do
|
31
|
+
class SystemUnderTest
|
32
|
+
end
|
33
|
+
sut = SystemUnderTest.new
|
34
|
+
|
35
|
+
sut.extend described_class
|
36
|
+
expect(sut.expect(sut, :calculate, "1+1").to(sut.eq(1+1)).as("solves tricky sums").to_s).to eq(
|
37
|
+
<<-GENERATED_SPEC
|
38
|
+
describe SystemUnderTest do
|
39
|
+
subject {described_class.new}
|
40
|
+
|
41
|
+
describe "#calculate" do
|
42
|
+
|
43
|
+
it "solves tricky sums" do
|
44
|
+
expect(subject.calculate("1+1")).to eq(2)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
GENERATED_SPEC
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "can generate negative tests" do
|
54
|
+
class SystemUnderTest
|
55
|
+
end
|
56
|
+
sut = SystemUnderTest.new
|
57
|
+
|
58
|
+
sut.extend described_class
|
59
|
+
expect(sut.expect(sut, :calculate, "1+1").not_to(sut.eq(1+1)).to_s).to eq(
|
60
|
+
<<-GENERATED_SPEC
|
61
|
+
describe SystemUnderTest do
|
62
|
+
subject {described_class.new}
|
63
|
+
|
64
|
+
describe "#calculate" do
|
65
|
+
|
66
|
+
it "does something" do
|
67
|
+
expect(subject.calculate("1+1")).not_to eq(2)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
GENERATED_SPEC
|
73
|
+
)
|
74
|
+
end
|
75
|
+
|
76
|
+
|
30
77
|
it "can generate specs for methods with varying arity and parameter types" do
|
31
78
|
class CustomType
|
32
79
|
def initialize(str)
|
@@ -59,4 +106,28 @@ end
|
|
59
106
|
)
|
60
107
|
end
|
61
108
|
|
109
|
+
it "can generate specs with change type matchers" do
|
110
|
+
class Fish
|
111
|
+
end
|
112
|
+
sut = Fish.new
|
113
|
+
|
114
|
+
sut.extend described_class
|
115
|
+
expect(
|
116
|
+
sut.expect(sut, :save).to(sut.change(Fish, :count).by(1)).to_s).to eq(
|
117
|
+
<<-GENERATED_SPEC
|
118
|
+
describe Fish do
|
119
|
+
subject {described_class.new}
|
120
|
+
|
121
|
+
describe "#save" do
|
122
|
+
|
123
|
+
it "does something" do
|
124
|
+
expect{ subject.save }.to change(Fish, :count).by(1)
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
end
|
129
|
+
GENERATED_SPEC
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
62
133
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mkspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ritchie Young
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -68,6 +68,9 @@ files:
|
|
68
68
|
- README.md
|
69
69
|
- Rakefile
|
70
70
|
- lib/mkspec.rb
|
71
|
+
- lib/mkspec/expectation.rb
|
72
|
+
- lib/mkspec/matchers/change_matcher.rb
|
73
|
+
- lib/mkspec/matchers/equality_matcher.rb
|
71
74
|
- lib/mkspec/version.rb
|
72
75
|
- mkspec.gemspec
|
73
76
|
- spec/mkspec_spec.rb
|