graham 0.0.2 → 0.0.3a

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
@@ -5,11 +5,11 @@ Graham is a tiny-yet-useful testing library based on Mallow - in fact it _is_ Ma
5
5
  * TestRocket was too minimal
6
6
  * RSpec was too verbose (not to mention ridiculous overkill)
7
7
 
8
- ## How does one graham ##
8
+ ## How does one use graham ??? ##
9
9
 
10
10
  Graham test cases are instance methods on arbitrary classes:
11
11
  ```ruby
12
- class TestCases
12
+ class Cases
13
13
  def initialize
14
14
  @number = 1
15
15
  end
@@ -19,18 +19,18 @@ Graham test cases are instance methods on arbitrary classes:
19
19
  def dividing_one_by_zero
20
20
  @number / 0
21
21
  end
22
- def calling_a_nonexistent_method
23
- Graham.this_is_not_a_method
22
+ def calling_upcase_on(obj)
23
+ obj.upcase
24
24
  end
25
25
  end
26
26
  ```
27
27
  Then test your cases:
28
28
  ```ruby
29
- Graham.test(TestCases) do |that|
29
+ Graham.test(Cases) do |that|
30
30
  that.one_squared.is 1
31
- that.dividing_by_zero.returns_a(Fixnum).such_that {self > 1}
32
- that.calling_a_nonexistent_method.does_not_raise_an_exception
33
- end #=> {:one_squared=>true, :dividing_by_zero=>#<ZeroDivisionError>, :calling_a_nonexistent_method=>false}
31
+ that.dividing_by_zero.returns_a(Fixnum).such_that {|n| n > 1}
32
+ that.calling_upcase_on(Graham).does_not_raise_an_exception
33
+ end #=> {:one_squared=>true, :dividing_by_zero=>#<ZeroDivisionError>, :calling_upcase_on=>false}
34
34
  ```
35
- Calling Graham::pp instead will call Graham::test and run the output through Graham's built-in pretty printer.
35
+ Or use Graham::test! to run the results through Graham's pretty printer.
36
36
 
data/lib/graham.rb CHANGED
@@ -3,21 +3,27 @@ require 'mallow'
3
3
  require 'graham/version'
4
4
  # == A miniature test engine powered by Mallow
5
5
  # ---
6
- # Test cases are instance methods on classes defined in Graham's namespace,
7
- # and expectations on their return values are enumerated using a very slightly
8
- # modified Mallow::DSL.
6
+ # Test cases are instance methods on arbitrary classes, and expectations on
7
+ # their return values are enumerated using a very slightly modified
8
+ # Mallow::DSL.
9
9
  #
10
- # class Graham::Cases
11
- # def test1; 4 + 5 end
12
- # def test2; 'test'.upcase end
13
- # def test3; 1/0 end
10
+ # class Cases
11
+ # def four_plus_five
12
+ # 4 + 5
13
+ # end
14
+ # def upcasing(s)
15
+ # s.upcase
16
+ # end
17
+ # def one_divided_by_zero
18
+ # 1/0
19
+ # end
14
20
  # end
15
21
  #
16
- # Graham.test { |that|
17
- # that.test1.returns_a(Fixnum).such_that {self < 100}
18
- # that.test2.returns 'TeST'
19
- # that.test3.returns_a Numeric
20
- # } #=> {:test1=>true, :test2=>false, :test3=>#<ZeroDivisionError>}
22
+ # Graham.test(Cases) { |that|
23
+ # that.four_plus_five.is 9
24
+ # that.upcasing('test').returns 'TeST'
25
+ # that.one_divided_by_zero.equals :infinity
26
+ # } #=> {:four_plus_five=>true, :upcasing=>false, :one_divided_by_zero=>#<ZeroDivisionError>}
21
27
  #
22
28
  # === N.B.
23
29
  # Since a Graham test is basically a Mallow pattern matcher, only the first
@@ -25,21 +31,21 @@ require 'graham/version'
25
31
  # test case will be matched by the first rule. This can lead to confusing
26
32
  # results:
27
33
  #
28
- # Graham.test { |that|
29
- # that.test1.returns_a(Fixnum)
30
- # that.test1.returns_a(String)
31
- # } #=> {:test1=>true}
34
+ # Graham.test(Cases) { |that|
35
+ # that.four_plus_five.returns_a(Fixnum)
36
+ # that.four_plus_five.returns_a(String)
37
+ # } #=> {:four_plus_five=>true}
32
38
  #
33
39
  # To avoid this issue, either run separate tests:
34
40
  #
35
- # Graham.test {|that| that.test1.returns_a Fixnum} #=> {:test1=>true}
36
- # Graham.test {|that| that.test1.returns_a String} #=> {:test1=>false}
41
+ # Graham.test(Cases) {|that| that.four_plus_five.returns_a Fixnum} #=> {:four_plus_five=>true}
42
+ # Graham.test(Cases) {|that| that.four_plus_five.returns_a String} #=> {:four_plus_five=>false}
37
43
  #
38
44
  # Or (better) chain the tests you want to run:
39
45
  #
40
- # Graham.test { |that|
41
- # that.test1.returns_a(Fixnum).and_returns_a(String)
42
- # } #=> {:test1=>false}
46
+ # Graham.test(Cases) { |that|
47
+ # that.four_plus_five.returns_a(Fixnum).that {is_a? String}
48
+ # } #=> {:four_plus_five=>false}
43
49
  #
44
50
  module Graham
45
51
  autoload :PP, 'graham/pp'
@@ -57,17 +63,18 @@ module Graham
57
63
  def pp(ns, &b)
58
64
  PP.new(test ns,&b).pp
59
65
  end
66
+ alias test! pp
60
67
  end
61
68
 
62
69
  class Core < Mallow::Core
63
- attr_accessor :cases
70
+ attr_reader :cases
64
71
  def initialize
65
- @cases = []
72
+ @cases = {}
66
73
  super
67
74
  end
68
75
 
69
76
  def _fluff1(e)
70
- [e.name] << begin
77
+ [e] << begin
71
78
  super e
72
79
  true
73
80
  rescue Mallow::MatchException
@@ -77,7 +84,7 @@ module Graham
77
84
  end
78
85
  end
79
86
 
80
- def test; Hash[_fluff @cases] end
87
+ def test; Hash[_fluff @cases.keys] end
81
88
  end
82
89
  end
83
90
 
data/lib/graham/dsl.rb CHANGED
@@ -7,7 +7,7 @@ module Graham
7
7
  end
8
8
 
9
9
  def initialize(ns)
10
- @core, @ns = Core.new, ns.new
10
+ @core, @ns = Core.new, ns
11
11
  reset!
12
12
  end
13
13
 
@@ -16,13 +16,18 @@ module Graham
16
16
  when /^((and|that)_)+(.+)$/
17
17
  respond_to?($3)? send($3, *args, &b) : super
18
18
  else
19
- core.cases << (_case = @ns.method msg)
20
- rule!._where {|e|e==_case}
19
+ core.cases[msg] = args
20
+ (conditions.empty?? self : rule!)._where {|e|e==msg}
21
21
  end
22
22
  end
23
23
 
24
- def _where(&b); push b, :conditions end
25
- def where(&b); _where {|e| preproc(b)[e.call] } end
24
+ def _where(&b)
25
+ push b, :conditions
26
+ end
27
+
28
+ def where(&b)
29
+ _where {|e| preproc(b).call @ns.new.send(e, *core.cases[e]) }
30
+ end
26
31
 
27
32
  def raises(x=nil)
28
33
  _where {
@@ -52,6 +57,7 @@ module Graham
52
57
 
53
58
  alias is this
54
59
  alias returns this
60
+ alias equals this
55
61
 
56
62
  alias is_such_that where
57
63
  alias such_that where
data/lib/graham/pp.rb CHANGED
@@ -8,7 +8,7 @@ module Graham
8
8
  PLAIN = "\x1b[0m"
9
9
  GREEN = "\x1b[32m"
10
10
  RED = "\x1b[31m"
11
- def initialize(results, bt=0, out=$stdout, color=true)
11
+ def initialize(results, bt=(ENV['backtrace']||0).to_i, out=$stdout, color=true)
12
12
  @results, @bt, @out, @color = results, bt, out, color
13
13
  end
14
14
  def pp
@@ -1,3 +1,3 @@
1
1
  module Graham
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3a'
3
3
  end
@@ -0,0 +1,44 @@
1
+ class TestCases
2
+ def DocTest1; 4 + 5 end
3
+ def DocTest2; 'test'.upcase end
4
+ def DocTest3; 1/0 end
5
+ def rdoc_example
6
+ Graham.test(TestCases) { |that|
7
+ that.DocTest1.returns_a(Fixnum).such_that {self < 100}
8
+ that.DocTest2.returns 'TeST'
9
+ that.DocTest3.returns_a(Numeric)
10
+ }
11
+ end
12
+
13
+ def squaring(n)
14
+ n ** 2
15
+ end
16
+
17
+ def dividing_by_zero(n)
18
+ n / 0
19
+ end
20
+ def calling_a_nonexistent_method
21
+ Graham.this_is_not_a_method
22
+ end
23
+ def readme_example
24
+ Graham.test(TestCases) do |that|
25
+ that.squaring(1).returns 1
26
+ that.dividing_by_zero(1).returns_a(Fixnum)
27
+ that.calling_a_nonexistent_method.does_not_raise_an_exception
28
+ end
29
+ end
30
+ end
31
+
32
+ Graham.pp(TestCases) do |that|
33
+ that.rdoc_example.returns_a(Hash).of_size(3).such_that {|h|
34
+ h[:DocTest1] == true and
35
+ h[:DocTest2] == false and
36
+ h[:DocTest3].is_a? ZeroDivisionError
37
+ }
38
+ that.readme_example.returns_a(Hash).of_size(3).such_that {|h|
39
+ h[:squaring] == true and
40
+ h[:dividing_by_zero].is_a? ZeroDivisionError and
41
+ h[:calling_a_nonexistent_method] == false
42
+ }
43
+ end
44
+
data/test/case/control.rb CHANGED
@@ -1,9 +1,9 @@
1
- class Cases
1
+ class ControlCases
2
2
  def the_number_99; 99 end
3
3
  def raising_a_name_error; raise Asdf.qwer.ty / 0 end
4
4
  end
5
5
 
6
- Graham.pp(Cases) {|that|
6
+ Graham.pp(ControlCases) {|that|
7
7
  that.the_number_99.returns_a(Fixnum).such_that {self==99}.and_that_is 99
8
8
  that.raising_a_name_error.raises.and_raises_a NameError
9
9
  }
@@ -0,0 +1,9 @@
1
+ class Arguments
2
+ def doubling(n)
3
+ 2*n
4
+ end
5
+ end
6
+
7
+ Graham.pp(Arguments) {|that|
8
+ that.doubling(2).equals 4
9
+ }
@@ -0,0 +1,26 @@
1
+ class Namespace
2
+ def initialize
3
+ @num=0
4
+ end
5
+
6
+ def namespacing; self end
7
+
8
+ def initialization
9
+ @num+=1
10
+ end
11
+
12
+ def reinitialization
13
+ @num+=1
14
+ end
15
+ end
16
+
17
+ Graham.pp(Namespace) do |that|
18
+ that.namespacing.is_such_that {
19
+ self.class == Namespace
20
+ }.and {!respond_to? :rdoc_example
21
+ }.and { respond_to? :namespacing}
22
+
23
+ that.initialization.returns 1
24
+ that.reinitialization.returns 1
25
+ end
26
+
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graham
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3a
5
+ prerelease: 5
6
6
  platform: ruby
7
7
  authors:
8
8
  - feivel jellyfish
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-28 00:00:00.000000000 Z
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mallow
@@ -42,7 +42,9 @@ files:
42
42
  - lib/graham/dsl.rb
43
43
  - Rakefile
44
44
  - test/case/control.rb
45
- - test/case/test.rb
45
+ - test/case/cases.rb
46
+ - test/unit/arguments.rb
47
+ - test/unit/namespaces.rb
46
48
  homepage: http://github.com/gwentacle/graham
47
49
  licenses: []
48
50
  post_install_message:
@@ -58,9 +60,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
60
  required_rubygems_version: !ruby/object:Gem::Requirement
59
61
  none: false
60
62
  requirements:
61
- - - ! '>='
63
+ - - ! '>'
62
64
  - !ruby/object:Gem::Version
63
- version: '0'
65
+ version: 1.3.1
64
66
  requirements: []
65
67
  rubyforge_project:
66
68
  rubygems_version: 1.8.23
@@ -70,4 +72,6 @@ summary: Miniature test engine based on Mallow
70
72
  test_files:
71
73
  - Rakefile
72
74
  - test/case/control.rb
73
- - test/case/test.rb
75
+ - test/case/cases.rb
76
+ - test/unit/arguments.rb
77
+ - test/unit/namespaces.rb
data/test/case/test.rb DELETED
@@ -1,57 +0,0 @@
1
- class Cases
2
- def DocTest1; 4 + 5 end
3
- def DocTest2; 'test'.upcase end
4
- def DocTest3; 1/0 end
5
- def rdoc_example
6
- Graham.test(Cases) { |that|
7
- that.DocTest1.returns_a(Fixnum).such_that {self < 100}
8
- that.DocTest2.returns 'TeST'
9
- that.DocTest3.returns_a(Numeric)
10
- }
11
- end
12
-
13
- def initialize
14
- @number = 1
15
- end
16
- def ReadmeCase1
17
- @number ** 2
18
- end
19
- def ReadmeCase2
20
- @number / 0
21
- end
22
- def ReadmeCase3
23
- Graham.this_is_not_a_method
24
- end
25
- def readme_example
26
- Graham.test(Cases) do |that|
27
- that.ReadmeCase1.is_such_that { self == 1 }
28
- that.ReadmeCase2.is_a(Fixnum).such_that {self > 1}
29
- that.ReadmeCase3.does_not_raise_an_exception
30
- end
31
- end
32
- end
33
-
34
- class Namespace
35
- def namespacing; self end
36
- end
37
-
38
- Graham.pp(Cases) do |that|
39
- that.rdoc_example.returns_a(Hash).of_size(3).such_that {
40
- self[:DocTest1] == true and
41
- self[:DocTest2] == false and
42
- self[:DocTest3].is_a? ZeroDivisionError
43
- }
44
- that.readme_example.returns_a(Hash).of_size(3).such_that {
45
- self[:ReadmeCase1] == true and
46
- self[:ReadmeCase2].is_a? ZeroDivisionError and
47
- self[:ReadmeCase3] == false
48
- }
49
- end
50
-
51
- Graham.pp(Namespace) do |that|
52
- that.namespacing.is_such_that {
53
- self.class == Namespace
54
- }.and {!respond_to? :rdoc_example
55
- }.and { respond_to? :namespacing}
56
- end
57
-