assert_type 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+ gem 'rspec', '>2.5'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "assert_type/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "assert_type"
7
+ s.version = AssertType::VERSION
8
+ s.authors = ["Joel Plane"]
9
+ s.email = ["joel.plane@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{simple method to do type assertions}
12
+ s.description = %q{A method for very basic nested type assertions}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ # s.add_development_dependency "rspec"
21
+ # s.add_runtime_dependency "rest-client"
22
+ end
23
+
@@ -0,0 +1,14 @@
1
+ module AssertType
2
+ class AssertionError < RuntimeError
3
+
4
+ def initialize expected, actual, expected_is_nested_type
5
+ @expected = expected_is_nested_type ? expected.to_s : expected.inspect
6
+ @actual = expected_is_nested_type ? actual.to_s : actual.inspect
7
+ end
8
+
9
+ def message
10
+ "assertion failed - expected #{@expected} but was #{@actual}"
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module AssertType
2
+ class CallError < RuntimeError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module AssertType
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,56 @@
1
+ require "assert_type/version"
2
+ require "assert_type/assertion_error"
3
+ require "assert_type/call_error"
4
+
5
+ module AssertType
6
+
7
+ module AssertMethods
8
+ def at_assert truth
9
+ unless truth
10
+ raise AssertionError, "Expected truthy but was #{truth.inspect}"
11
+ end
12
+ end
13
+
14
+ def at_assert_equal expected, actual
15
+ unless expected == actual
16
+ raise AssertionError.new expected, actual, false
17
+ end
18
+ end
19
+
20
+ def at_assert_type expected_type, value
21
+ if expected_type.is_a? Class
22
+ unless expected_type === value
23
+ raise AssertionError.new expected_type, value, false
24
+ end
25
+ elsif expected_type.is_a? Array
26
+ unless expected_type.any? {|t| t === value}
27
+ types = expected_type.collect do |t|
28
+ "<#{t}>"
29
+ end.join(' or ')
30
+ raise AssertionError.new types, value, true
31
+ end
32
+ else
33
+ if (matches = expected_type.match /([a-zA-Z0-9]+)\<([a-zA-Z0-9]+)\>/)
34
+ enumerable_type = matches[1]
35
+ child_type = matches[2]
36
+ e_type = Object.const_get(enumerable_type)
37
+ c_type = Object.const_get(child_type)
38
+ unless e_type === value
39
+ raise AssertionError.new e_type, value, false
40
+ end
41
+ unless value.first.nil? || c_type === value.first
42
+ raise AssertionError.new "#{e_type}<#{c_type}>", "#{e_type}<#{value.first.class}>", true
43
+ end
44
+ else
45
+ raise CallError.new
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ # # Left this for the client application to decide - to add to Object or use otherwise
54
+ # class Object
55
+ # include AssertType::AssertMethods
56
+ # end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ include AssertType::AssertMethods
4
+
5
+ describe AssertType do
6
+
7
+ it "quick and nasty tests" do
8
+
9
+ at_assert_type Array, []
10
+ expect { at_assert_type "Array", [] }.to raise_error AssertType::CallError
11
+ at_assert_type "Array<Fixnum>", [1,2,3]
12
+ at_assert_type "Array<Fixnum>", []
13
+ expect { at_assert_type "Array<Fixnum>", ["one", "two", "three"] }.to raise_error(AssertType::AssertionError) { |error|
14
+ error.message.should include %{expected Array<Fixnum> but was Array<String>}
15
+ }
16
+ at_assert_type [Fixnum, String], 1
17
+ at_assert_type [Fixnum, String], "1"
18
+ expect { at_assert_type [Fixnum, String], 1.1 }.to raise_error(AssertType::AssertionError) { |error|
19
+ error.message.should include %{expected <Fixnum> or <String> but was 1.1}
20
+ }
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require File.expand_path("../lib/assert_type.rb", File.dirname(__FILE__))
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assert_type
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Joel Plane
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-01-14 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: A method for very basic nested type assertions
22
+ email:
23
+ - joel.plane@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Rakefile
34
+ - assert_type.gemspec
35
+ - lib/assert_type.rb
36
+ - lib/assert_type/assertion_error.rb
37
+ - lib/assert_type/call_error.rb
38
+ - lib/assert_type/version.rb
39
+ - spec/assert_type_spec.rb
40
+ - spec/spec_helper.rb
41
+ homepage: ""
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.7.2
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: simple method to do type assertions
74
+ test_files: []
75
+