constants 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,99 @@
1
+ require File.join(File.dirname(__FILE__), '../constants_test_helper.rb')
2
+ require 'constants/stash'
3
+
4
+ class StashTest < Test::Unit::TestCase
5
+ include Constants
6
+
7
+ class StashingHash < Hash
8
+ include Constants::Stash
9
+
10
+ def initialize(*args)
11
+ super(*args)
12
+ @nil_value = nil
13
+ end
14
+ end
15
+
16
+ attr_reader :s
17
+
18
+ def setup
19
+ @s = StashingHash.new
20
+ end
21
+
22
+ #
23
+ # documentation test
24
+ #
25
+
26
+ def test_documentation
27
+ s = StashingHash.new
28
+
29
+ s.stash('key', 'one')
30
+ assert_equal 'one', s['key']
31
+
32
+ s.stash('key', 'two')
33
+ assert_equal ['one' , 'two'], s['key']
34
+ assert_equal Constants::Stash::StashArray, s['key'].class
35
+
36
+ ###
37
+ s = StashingHash.new
38
+ assert_nil s['key']
39
+ assert_nil s.nil_value
40
+
41
+ s.stash('key', 1)
42
+ assert_equal 1, s['key']
43
+
44
+ s.stash('key', 2)
45
+ assert_equal [1, 2], s['key']
46
+
47
+ ###
48
+ assert_raise(ArgumentError) { s.stash('key', nil) }
49
+ end
50
+
51
+ #
52
+ # initialization test
53
+ #
54
+
55
+ def test_initialization_sets_nil_value
56
+ s = StashingHash.new
57
+ assert_equal({}, s)
58
+ assert_equal(nil, s.nil_value)
59
+ end
60
+
61
+ #
62
+ # stash test
63
+ #
64
+
65
+ def test_stash_stores_new_values_at_key
66
+ s.stash('key', 'one')
67
+ assert_equal({'key' => 'one'}, s)
68
+ end
69
+
70
+ def test_stash_stores_addition_values_in_a_StashArray
71
+ s.stash('key', 'one')
72
+ s.stash('key', 'two')
73
+ s.stash('key', 'three')
74
+
75
+ assert_equal({'key' => ['one', 'two', 'three']}, s)
76
+ assert_equal Constants::Stash::StashArray, s['key'].class
77
+ end
78
+
79
+ def test_stash_handles_array_values_properly
80
+ s.stash('key', ['one'])
81
+ assert_equal({'key' => ['one']}, s)
82
+
83
+ s.stash('key', ['two'])
84
+ assert_equal({'key' => [['one'], ['two']]}, s)
85
+
86
+ s.stash('key', ['three'])
87
+ assert_equal({'key' => [['one'], ['two'], ['three']]}, s)
88
+ end
89
+
90
+ def test_stash_raises_error_for_nil_value_and_StashArray_values
91
+ assert_raise(ArgumentError) { s.stash('key', s.nil_value) }
92
+ assert_raise(ArgumentError) { s.stash('key', Stash::StashArray.new) }
93
+ end
94
+
95
+ def test_stash_returns_self
96
+ assert_equal s, s.stash('key', 'value')
97
+ end
98
+
99
+ end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'benchmark'
4
+ require 'pp'
5
+
6
+ class Test::Unit::TestCase
7
+ include Benchmark
8
+
9
+ #
10
+ # mass tests
11
+ #
12
+
13
+ def delta_mass
14
+ 10**-5
15
+ end
16
+
17
+ def delta_abundance
18
+ 10**-1
19
+ end
20
+
21
+ def benchmark_test(length=10, &block)
22
+ if ENV["benchmark"] =~ /true/i
23
+ puts
24
+ puts method_name
25
+ bm(length, &block)
26
+ else
27
+ print 'b'
28
+ end
29
+ end
30
+
31
+ def const_ordered_assert_equal(a,b, msg=nil)
32
+ if RUBY_VERSION =~ /^1.8/
33
+ print "*"
34
+
35
+ case a
36
+ when Array
37
+ assert_equal a.sort, b.sort, msg
38
+ when Hash
39
+ [a,b].each do |hash|
40
+ hash.each_pair do |key, value|
41
+ value.sort! if value.kind_of?(Array)
42
+ end
43
+ end
44
+ assert_equal a, b, msg
45
+ end
46
+ else
47
+ assert_equal a, b, msg
48
+ end
49
+ end
50
+
51
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '../lib')
2
+
3
+ Dir.glob("./**/*_test.rb").each {|test| require test}
@@ -0,0 +1,58 @@
1
+ require File.join(File.dirname(__FILE__), 'constants_test_helper.rb')
2
+ require 'constants'
3
+
4
+ class ReadMeDocTest < Test::Unit::TestCase
5
+
6
+ include Constants::Libraries
7
+
8
+ # A library of amino acid residues.
9
+ class Residue
10
+ attr_reader :letter, :abbr, :name
11
+
12
+ def initialize(letter, abbr, name)
13
+ @letter = letter
14
+ @abbr = abbr
15
+ @name = name
16
+ end
17
+
18
+ A = Residue.new('A', "Ala", "Alanine")
19
+ C = Residue.new('C', "Cys", "Cysteine")
20
+ D = Residue.new('D', "Asp", "Aspartic Acid")
21
+ # ... normally you'd add the rest here ...
22
+
23
+ include Constants::Library
24
+
25
+ # add an index by an attribute or method
26
+ library.index_by_attribute :letter
27
+
28
+ # add an index where keys are calculated by a block
29
+ library.index_by 'upcase abbr' do |residue|
30
+ residue.abbr.upcase
31
+ end
32
+
33
+ # add a collection (same basic idea, but using an array)
34
+ library.collect_attribute 'name'
35
+ end
36
+
37
+ def test_documentation
38
+ # Element predefines all chemical elements
39
+ c = Element::C
40
+ assert_equal "Carbon", c.name
41
+ assert_equal "C", c.symbol
42
+ assert_equal 6, c.atomic_number
43
+ assert_equal 12.0, c.mass
44
+ assert_equal 13.0033548378, c.mass(13)
45
+
46
+ assert c == Element['Carbon']
47
+ assert c == Element['C']
48
+ assert c == Element[6]
49
+
50
+ ###
51
+ assert_equal Residue::D, Residue['D']
52
+ assert_equal Residue::A, Residue['ALA']
53
+
54
+ assert_equal({'ALA' => Residue::A, 'CYS' => Residue::C, 'ASP' => Residue::D}, Residue.index('upcase abbr'))
55
+ const_ordered_assert_equal(["Alanine", "Cysteine", "Aspartic Acid"], Residue.collection('name'))
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constants
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Chiang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-11 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-units
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.3
24
+ version:
25
+ description:
26
+ email: simon.a.chiang@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - MIT-LICENSE
34
+ files:
35
+ - MIT-LICENSE
36
+ - Rakefile
37
+ - README
38
+ - lib/constants/constant.rb
39
+ - lib/constants/constant_library.rb
40
+ - lib/constants/libraries/element.rb
41
+ - lib/constants/libraries/particle.rb
42
+ - lib/constants/libraries/physical.rb
43
+ - lib/constants/library.rb
44
+ - lib/constants/stash.rb
45
+ - lib/constants/uncertainty.rb
46
+ - lib/constants.rb
47
+ - test/constants/constant_library_test.rb
48
+ - test/constants/constant_test.rb
49
+ - test/constants/libraries/element_test.rb
50
+ - test/constants/libraries/particle_test.rb
51
+ - test/constants/libraries/physical_test.rb
52
+ - test/constants/library_test.rb
53
+ - test/constants/stash_test.rb
54
+ - test/constants_test_helper.rb
55
+ - test/constants_test_suite.rb
56
+ - test/readme_doc_test.rb
57
+ has_rdoc: true
58
+ homepage: http://bioactive.rubyforge.org/constants/
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: bioactive
79
+ rubygems_version: 1.2.0
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: Libraries of constants. Includes libraries for elements, particles, and physical constants.
83
+ test_files:
84
+ - test/constants_test_suite.rb