must 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README CHANGED
@@ -19,6 +19,10 @@ Must
19
19
  pages = [{:name=>"...", :url=>"...",} ...]
20
20
  pages.must.struct([Hash])
21
21
 
22
+ You want Boolean class? try this!
23
+
24
+ flag = hash["flag"].must(true, false)
25
+
22
26
 
23
27
  Asking Methods
24
28
  ==============
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
1
3
  require 'rake'
2
4
  require 'rake/testtask'
3
5
  require 'rdoc/task'
data/lib/must/rule.rb CHANGED
@@ -42,7 +42,8 @@ module Must
42
42
  def kind_of(*targets, &block)
43
43
  bool = targets.any?{|klass| is_a?(klass)}
44
44
  block ||= proc {
45
- target = targets.map{|i| instance?(i) ? i.class.name : i.name}.join('/')
45
+ target = targets.map{|i| instance?(i) ? i.class.name : i.name}.join('|')
46
+ target = "(#{target})" if targets.size > 1
46
47
  raise Invalid, "expected #{target} but got #{object.class}"
47
48
  }
48
49
  valid?(bool, &block)
@@ -104,6 +105,7 @@ module Must
104
105
  end
105
106
 
106
107
  def struct(target, &block)
108
+ block ||= proc{ raise Invalid, Must::StructInfo::Differ.new(@object, target, "").execute.to_s }
107
109
  valid?(struct?(target), &block)
108
110
  end
109
111
 
@@ -75,6 +75,56 @@ module Must
75
75
  extend self
76
76
  end
77
77
 
78
+ class Differ
79
+ attr_reader :a, :b, :path
80
+
81
+ def initialize(a, b, path)
82
+ @a, @b, @path = a, b, path
83
+ end
84
+
85
+ def execute!
86
+ sa = StructInfo::new(a).inspect
87
+ sb = StructInfo::new(b).inspect
88
+
89
+ unless sa == sb
90
+ failed("%s expected %s, but got %s" % [path, sb, sa])
91
+ end
92
+
93
+ if a.class == Array
94
+ max = [a.size, b.size].max
95
+ (0...max).each do |i|
96
+ Differ.new(a[i], b[i], "#{path}[#{i}]").execute!
97
+ end
98
+ return true
99
+ end
100
+
101
+ if a.class == Hash
102
+ (a.keys | b.keys).each do |key|
103
+ Differ.new(a[key], b[key], "#{path}[#{key}]").execute!
104
+ end
105
+ return true
106
+ end
107
+
108
+ unless a == b
109
+ failed("%s expected %s, but got %s" % [path, a.inspect, b.inspect])
110
+ end
111
+
112
+ return nil
113
+ end
114
+
115
+ def execute
116
+ execute!
117
+ return nil
118
+ rescue => err
119
+ return err
120
+ end
121
+
122
+ private
123
+ def failed(msg)
124
+ raise msg
125
+ end
126
+ end
127
+
78
128
  ######################################################################
79
129
  ### StructInfo
80
130
 
data/lib/must/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Must
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
data/must.gemspec CHANGED
@@ -16,7 +16,5 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- # specify any dependencies here; for example:
20
- # s.add_development_dependency "rspec"
21
- # s.add_runtime_dependency "rest-client"
19
+ s.add_development_dependency "rspec"
22
20
  end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+
4
+ require File.dirname(__FILE__) + '/../init'
5
+
6
+ RSpec.configure do |config|
7
+ # == Mock Framework
8
+ #
9
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
10
+ #
11
+ # config.mock_with :mocha
12
+ # config.mock_with :flexmock
13
+ # config.mock_with :rr
14
+ config.mock_with :rspec
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Must do
4
+ let(:obj) { {"xyz" => ["a","b"] } }
5
+
6
+ specify "show more info about struct differs" do
7
+ lambda {
8
+ obj.must.struct({String => [Hash]})
9
+ }.should raise_error(/expected \{String=>\[Hash\]/)
10
+ end
11
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: must
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
- prerelease: false
4
+ hash: 7
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 7
10
- version: 0.2.7
9
+ - 8
10
+ version: 0.2.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - maiha
@@ -15,10 +15,22 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-27 00:00:00 +09:00
19
- default_executable:
20
- dependencies: []
21
-
18
+ date: 2013-02-22 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
22
34
  description: add Object#must method to constrain its origin and conversions
23
35
  email:
24
36
  - maiha@wota.jp
@@ -29,6 +41,7 @@ extensions: []
29
41
  extra_rdoc_files: []
30
42
 
31
43
  files:
44
+ - Gemfile
32
45
  - README
33
46
  - Rakefile
34
47
  - init.rb
@@ -38,13 +51,14 @@ files:
38
51
  - lib/must/struct_info.rb
39
52
  - lib/must/version.rb
40
53
  - must.gemspec
54
+ - spec/spec_helper.rb
55
+ - spec/struct_spec.rb
41
56
  - test/coerced_test.rb
42
57
  - test/duck_test.rb
43
58
  - test/match_test.rb
44
59
  - test/must_test.rb
45
60
  - test/struct_info_test.rb
46
61
  - test/struct_test.rb
47
- has_rdoc: true
48
62
  homepage: http://github.com/maiha/must
49
63
  licenses: []
50
64
 
@@ -74,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
88
  requirements: []
75
89
 
76
90
  rubyforge_project:
77
- rubygems_version: 1.3.7
91
+ rubygems_version: 1.8.15
78
92
  signing_key:
79
93
  specification_version: 3
80
94
  summary: a runtime specification tool