bahuvrihi-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,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bahuvrihi-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-07-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ruby-units
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.1.3
23
+ version:
24
+ description:
25
+ email: simon.a.chiang@gmail.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ - MIT-LICENSE
33
+ files:
34
+ - MIT-LICENSE
35
+ - Rakefile
36
+ - README
37
+ - lib/constants/constant.rb
38
+ - lib/constants/constant_library.rb
39
+ - lib/constants/libraries/element.rb
40
+ - lib/constants/libraries/particle.rb
41
+ - lib/constants/libraries/physical.rb
42
+ - lib/constants/library.rb
43
+ - lib/constants/stash.rb
44
+ - lib/constants/uncertainty.rb
45
+ - lib/constants.rb
46
+ - test/constants/constant_library_test.rb
47
+ - test/constants/constant_test.rb
48
+ - test/constants/libraries/element_test.rb
49
+ - test/constants/libraries/particle_test.rb
50
+ - test/constants/libraries/physical_test.rb
51
+ - test/constants/library_test.rb
52
+ - test/constants/stash_test.rb
53
+ - test/constants_test_helper.rb
54
+ - test/constants_test_suite.rb
55
+ - test/readme_doc_test.rb
56
+ has_rdoc: true
57
+ homepage: http://bioactive.rubyforge.org/constants/
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ requirements: []
76
+
77
+ rubyforge_project: bioactive
78
+ rubygems_version: 1.2.0
79
+ signing_key:
80
+ specification_version: 2
81
+ summary: Libraries of constants. Includes libraries for elements, particles, and physical constants.
82
+ test_files:
83
+ - test/constants_test_suite.rb