ducktypechecker 0.1.0
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 +58 -0
- data/lib/duck_type_checker.rb +35 -0
- data/lib/ducktypechecker.rb +2 -0
- data/test/tc_duck_type_checker.rb +83 -0
- data/test/ts_ducktypechecker.rb +2 -0
- metadata +45 -0
data/README
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
Duck Type Checker is copyrighted free software by Brian Takita <brian.takita@gmail.com>.
|
|
2
|
+
You can redistribute it and/or modify it under either the terms of the GPL, or the conditions below:
|
|
3
|
+
|
|
4
|
+
1. You may make and give away verbatim copies of the source form of the
|
|
5
|
+
software without restriction, provided that you duplicate all of the
|
|
6
|
+
original copyright notices and associated disclaimers.
|
|
7
|
+
|
|
8
|
+
2. You may modify your copy of the software in any way, provided that
|
|
9
|
+
you do at least ONE of the following:
|
|
10
|
+
|
|
11
|
+
a) place your modifications in the Public Domain or otherwise
|
|
12
|
+
make them Freely Available, such as by posting said
|
|
13
|
+
modifications to Usenet or an equivalent medium, or by allowing
|
|
14
|
+
the author to include your modifications in the software.
|
|
15
|
+
|
|
16
|
+
b) use the modified software only within your corporation or
|
|
17
|
+
organization.
|
|
18
|
+
|
|
19
|
+
c) rename any non-standard executables so the names do not conflict
|
|
20
|
+
with standard executables, which must also be provided.
|
|
21
|
+
|
|
22
|
+
d) make other distribution arrangements with the author.
|
|
23
|
+
|
|
24
|
+
3. You may distribute the software in object code or executable
|
|
25
|
+
form, provided that you do at least ONE of the following:
|
|
26
|
+
|
|
27
|
+
a) distribute the executables and library files of the software,
|
|
28
|
+
together with instructions (in the manual page or equivalent)
|
|
29
|
+
on where to get the original distribution.
|
|
30
|
+
|
|
31
|
+
b) accompany the distribution with the machine-readable source of
|
|
32
|
+
the software.
|
|
33
|
+
|
|
34
|
+
c) give non-standard executables non-standard names, with
|
|
35
|
+
instructions on where to get the original software distribution.
|
|
36
|
+
|
|
37
|
+
d) make other distribution arrangements with the author.
|
|
38
|
+
|
|
39
|
+
4. You may modify and include the part of the software into any other
|
|
40
|
+
software (possibly commercial). But some files in the distribution
|
|
41
|
+
are not written by the author, so that they are not under this terms.
|
|
42
|
+
|
|
43
|
+
They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
|
|
44
|
+
files under the ./missing directory. See each file for the copying
|
|
45
|
+
condition.
|
|
46
|
+
|
|
47
|
+
5. The scripts and library files supplied as input to or produced as
|
|
48
|
+
output from the software do not automatically fall under the
|
|
49
|
+
copyright of the software, but belong to whomever generated them,
|
|
50
|
+
and may be sold commercially, and may be aggregated with this
|
|
51
|
+
software.
|
|
52
|
+
|
|
53
|
+
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
|
54
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
55
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
56
|
+
PURPOSE.
|
|
57
|
+
|
|
58
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module DuckType
|
|
2
|
+
# Checks the given object if is responds to the given
|
|
3
|
+
# method signatures.
|
|
4
|
+
# ==Arguments
|
|
5
|
+
# :obj = The object being tested
|
|
6
|
+
# :methods = A method name or enumerable of the method names.
|
|
7
|
+
# :var_name = The name of the variable begin tested.
|
|
8
|
+
# This is useful for the default messages.
|
|
9
|
+
# ==Block
|
|
10
|
+
# Optionally a block may be passed to respond to a method that
|
|
11
|
+
# is not implemented by the object.
|
|
12
|
+
def self.check(obj, methods, var_name = nil, &block)
|
|
13
|
+
methods = [methods] unless methods.respond_to?(:each)
|
|
14
|
+
methods.each do |s|
|
|
15
|
+
s = s.intern if s.respond_to?(:intern)
|
|
16
|
+
unless obj.respond_to?(s)
|
|
17
|
+
return yield(s) if block_given?
|
|
18
|
+
|
|
19
|
+
if var_name.to_s == ''
|
|
20
|
+
message = "Object '#{obj.to_s}' needs to implement '#{s.to_s}'."
|
|
21
|
+
else
|
|
22
|
+
message = "Variable '#{var_name}' needs to implement '#{s.to_s}'."
|
|
23
|
+
end
|
|
24
|
+
fail TypeError.new(message)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
true
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Instance implementation of the static method DuckTypeChecker.check
|
|
32
|
+
def duck_type_check(obj, methods, var_name = nil, &block)
|
|
33
|
+
DuckType.check(obj, methods, var_name, &block)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
dir = File.dirname(__FILE__)
|
|
2
|
+
require "#{dir}/../lib/duck_type_checker"
|
|
3
|
+
require "test/unit"
|
|
4
|
+
|
|
5
|
+
class DuckTypeCheckerTest < Test::Unit::TestCase
|
|
6
|
+
public
|
|
7
|
+
|
|
8
|
+
def test_check_pass
|
|
9
|
+
do_test_pass(class_method)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_check_fail_default
|
|
13
|
+
do_test_fail_default(class_method)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_check_fail_block
|
|
17
|
+
do_fail_block(class_method)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_duck_type_check_pass
|
|
21
|
+
do_test_pass(instance_method)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_duck_type_check_fail_default
|
|
25
|
+
do_test_fail_default(instance_method)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_duck_type_check_fail_block
|
|
29
|
+
do_fail_block(instance_method)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
include DuckType
|
|
35
|
+
|
|
36
|
+
def class_method
|
|
37
|
+
DuckType.method(:check)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def instance_method
|
|
41
|
+
method(:duck_type_check)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def do_test_pass(proc)
|
|
45
|
+
n = 1
|
|
46
|
+
assert(proc.call(n, :to_s))
|
|
47
|
+
assert(proc.call(n, :to_s, 'n'))
|
|
48
|
+
assert(proc.call(n, [:to_s]))
|
|
49
|
+
assert(proc.call(n, [:to_s], 'n'))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def do_test_fail_default(proc)
|
|
53
|
+
n = 1
|
|
54
|
+
test_sym = :blah
|
|
55
|
+
|
|
56
|
+
assert_raise(TypeError) do
|
|
57
|
+
begin
|
|
58
|
+
proc.call(n, [test_sym])
|
|
59
|
+
rescue Exception=>e
|
|
60
|
+
e_m = "Object '#{n.to_s}' needs to implement '#{test_sym.to_s}'."
|
|
61
|
+
assert_equal(e_m, e.message)
|
|
62
|
+
raise e
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
var_name = 'test'
|
|
67
|
+
assert_raise(TypeError) do
|
|
68
|
+
begin
|
|
69
|
+
proc.call(n, [test_sym], var_name)
|
|
70
|
+
rescue Exception=>e
|
|
71
|
+
e_m = "Variable '#{var_name.to_s}' needs to implement '#{test_sym.to_s}'."
|
|
72
|
+
assert_equal(e_m, e.message)
|
|
73
|
+
raise e
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def do_fail_block(proc)
|
|
79
|
+
n = 1
|
|
80
|
+
v = proc.call(n, [:blah]) {|s| "#{s} test"}
|
|
81
|
+
assert_equal("blah test", v)
|
|
82
|
+
end
|
|
83
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
rubygems_version: 0.8.11
|
|
3
|
+
specification_version: 1
|
|
4
|
+
name: ducktypechecker
|
|
5
|
+
version: !ruby/object:Gem::Version
|
|
6
|
+
version: 0.1.0
|
|
7
|
+
date: 2005-09-01 00:00:00 -07:00
|
|
8
|
+
summary: Check to see if an object contains the required method signatures.
|
|
9
|
+
require_paths:
|
|
10
|
+
- lib
|
|
11
|
+
email: brian.takita@gmail.com
|
|
12
|
+
homepage: http://DuckTypeChecker.rubyforge.org
|
|
13
|
+
rubyforge_project:
|
|
14
|
+
description:
|
|
15
|
+
autorequire:
|
|
16
|
+
default_executable:
|
|
17
|
+
bindir: bin
|
|
18
|
+
has_rdoc: true
|
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
-
|
|
22
|
+
- ">"
|
|
23
|
+
- !ruby/object:Gem::Version
|
|
24
|
+
version: 0.0.0
|
|
25
|
+
version:
|
|
26
|
+
platform: ruby
|
|
27
|
+
signing_key:
|
|
28
|
+
cert_chain:
|
|
29
|
+
authors:
|
|
30
|
+
- Brian Takita
|
|
31
|
+
files:
|
|
32
|
+
- test/tc_duck_type_checker.rb
|
|
33
|
+
- test/ts_ducktypechecker.rb
|
|
34
|
+
- lib/ducktypechecker.rb
|
|
35
|
+
- lib/duck_type_checker.rb
|
|
36
|
+
- README
|
|
37
|
+
test_files:
|
|
38
|
+
- test/ts_ducktypechecker.rb
|
|
39
|
+
rdoc_options: []
|
|
40
|
+
extra_rdoc_files:
|
|
41
|
+
- README
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
requirements: []
|
|
45
|
+
dependencies: []
|