caruby-core 1.4.4 → 1.4.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/History.txt +2 -0
  2. data/README.md +0 -2
  3. data/lib/caruby/migration/migrator.rb +1 -1
  4. data/lib/caruby/version.rb +1 -1
  5. data/test/lib/caruby/csv/csv_mapper_test.rb +40 -0
  6. data/test/lib/caruby/csv/csvio_test.rb +69 -0
  7. data/test/lib/caruby/database/persistable_test.rb +92 -0
  8. data/test/lib/caruby/domain/domain_test.rb +126 -0
  9. data/test/lib/caruby/domain/inversible_test.rb +99 -0
  10. data/test/lib/caruby/domain/reference_visitor_test.rb +130 -0
  11. data/test/lib/caruby/import/java_test.rb +58 -0
  12. data/test/lib/caruby/util/cache_test.rb +23 -0
  13. data/test/lib/caruby/util/class_test.rb +61 -0
  14. data/test/lib/caruby/util/collection_test.rb +379 -0
  15. data/test/lib/caruby/util/command_test.rb +55 -0
  16. data/test/lib/caruby/util/controlled_value_test.rb +26 -0
  17. data/test/lib/caruby/util/domain_extent_test.rb +60 -0
  18. data/test/lib/caruby/util/file_separator_test.rb +30 -0
  19. data/test/lib/caruby/util/inflector_test.rb +12 -0
  20. data/test/lib/caruby/util/lazy_hash_test.rb +34 -0
  21. data/test/lib/caruby/util/merge_test.rb +83 -0
  22. data/test/lib/caruby/util/module_test.rb +29 -0
  23. data/test/lib/caruby/util/options_test.rb +59 -0
  24. data/test/lib/caruby/util/partial_order_test.rb +41 -0
  25. data/test/lib/caruby/util/person_test.rb +115 -0
  26. data/test/lib/caruby/util/pretty_print_test.rb +85 -0
  27. data/test/lib/caruby/util/properties_test.rb +50 -0
  28. data/test/lib/caruby/util/stopwatch_test.rb +18 -0
  29. data/test/lib/caruby/util/topological_sync_enumerator_test.rb +69 -0
  30. data/test/lib/caruby/util/transitive_closure_test.rb +59 -0
  31. data/test/lib/caruby/util/tree_test.rb +23 -0
  32. data/test/lib/caruby/util/trie_test.rb +14 -0
  33. data/test/lib/caruby/util/validation_test.rb +14 -0
  34. data/test/lib/caruby/util/version_test.rb +31 -0
  35. data/test/lib/caruby/util/visitor_test.rb +277 -0
  36. data/test/lib/caruby/util/weak_hash_test.rb +45 -0
  37. metadata +179 -161
@@ -0,0 +1,55 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+
5
+ require 'caruby/cli/command'
6
+ require 'set'
7
+
8
+ module CaRuby
9
+ class CommandTest < Test::Unit::TestCase
10
+ def test_empty
11
+ verify_execution(Command.new, "test", {})
12
+ end
13
+
14
+ def test_args_only
15
+ verify_execution(Command.new(:arg), "test arg", :arg => 'arg')
16
+ end
17
+
18
+ def test_nonmandatory_argument_option
19
+ verify_execution(Command.new(:opt => :none), "test --opt", :opt => true)
20
+ end
21
+
22
+ def test_mandatory_argument_option
23
+ verify_execution(Command.new(:opt => :mandatory), "test --opt opt", {:opt => 'opt'})
24
+ end
25
+
26
+ def test_integer_argument_option
27
+ verify_execution(Command.new(:opt => :integer), "test --opt 1", {:opt => 1})
28
+ end
29
+
30
+ def test_array_argument_option
31
+ verify_execution(Command.new(:opt => :array), "test --opt a,b", {:opt => ['a', 'b']})
32
+ end
33
+
34
+ def test_complex
35
+ verify_execution(Command.new(:arg1, :opt1 => :none, :opt2 => :optional, :opt3 => :mandatory),
36
+ "test --opt1 --opt3 opt3 arg1 arg2",
37
+ {:opt1 => true, :opt3 => 'opt3', :arg1 => 'arg1'},
38
+ 'arg2')
39
+ end
40
+
41
+ private
42
+
43
+ def verify_execution(cmd, s, opts, *args)
44
+ ARGV.clear.concat(s.split[1..-1])
45
+ cmd.start do |copts, *cargs|
46
+ opts.each do |opt, expected|
47
+ actual = copts[opt]
48
+ assert_not_nil(actual, "Command option #{opt} not found.")
49
+ assert_equal(expected, actual, "Command option #{opt} parsed incorrectly.")
50
+ end
51
+ assert_equal(cargs, args, "Command arguments differ.")
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+
5
+ require 'caruby/util/controlled_value'
6
+ require 'set'
7
+
8
+ class ControlledValueTest < Test::Unit::TestCase
9
+ def setup
10
+ @parent = CaRuby::ControlledValue.new('parent')
11
+ @c1 = CaRuby::ControlledValue.new('c1', @parent)
12
+ @gc11 = CaRuby::ControlledValue.new('gc11', @c1)
13
+ @gc12 = CaRuby::ControlledValue.new('gc12', @c2)
14
+ @c2 = CaRuby::ControlledValue.new('c2', @parent)
15
+ @gc21 = CaRuby::ControlledValue.new('gc21', @c2)
16
+ end
17
+
18
+ def test_parent
19
+ assert_same(@c1, @gc11.parent, "Parent incorrect")
20
+ assert(@c1.children.include?(@gc11), "Children incorrect")
21
+ end
22
+
23
+ def test_descendants
24
+ assert(@parent.descendants.include?(@gc21), "Descendants incorrect")
25
+ end
26
+ end
@@ -0,0 +1,60 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+
5
+ require 'caruby/util/domain_extent'
6
+
7
+ class DomainExtentTest < Test::Unit::TestCase
8
+ # Test composite key class
9
+ class Composite
10
+ attr_reader :a, :b, :c
11
+
12
+ def initialize(a, b, c=3)
13
+ super()
14
+ @a = a; @b = b; @c = c
15
+ end
16
+
17
+ def ==(other)
18
+ a == other.a && b == other.b && c == other.c
19
+ end
20
+ end
21
+
22
+ attr_reader :extent
23
+
24
+ def setup
25
+ @extent = DomainExtent.new
26
+ end
27
+
28
+ def test_create_on_demand
29
+ assert(!extent.has_key?(String), 'New domain extent not empty')
30
+ assert_not_nil(extent[String], 'Entry not created on demand')
31
+ end
32
+
33
+ def test_existing_fetch
34
+ expected = extent[String][1] = 'a'
35
+ target = extent.get(String, 1)
36
+ assert_equal(expected, target, 'Existing entry not found')
37
+ end
38
+
39
+ def test_missing_fetch
40
+ assert_nil(DomainExtent.new.get(Symbol, :a), 'Default factory instance not nil')
41
+ end
42
+
43
+ def test_simple_key_factory
44
+ extent = DomainExtent.new
45
+ # the String extent key is a number
46
+ # the String extent value is the number as a string
47
+ extent.set_factory(String) { |key| key.to_s }
48
+ assert_equal('1', extent.get(String, 1), 'Simple key instance not found')
49
+ end
50
+
51
+ def test_composite_key_factory
52
+ extent = DomainExtent.new
53
+ # the String extent key is a number
54
+ # the String extent value is the number as a string
55
+ extent.set_factory(Composite) { |key| Composite.new(key[:a], key[:b], key[:c]) }
56
+ key = {:a => 1, :b => 2, :c => 3}
57
+ expected = Composite.new(1, 2, 3)
58
+ assert_equal(expected, extent.get(Composite, key), 'Composite key instance not found')
59
+ end
60
+ end
@@ -0,0 +1,30 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/file_separator'
5
+
6
+ class FileSeparatorTest < Test::Unit::TestCase
7
+ FIXTURES_DIR = 'test/fixtures/caruby/util'
8
+ LF_FILE = File.join(FIXTURES_DIR, 'lf_line_sep.txt')
9
+ CR_FILE = File.join(FIXTURES_DIR, 'cr_line_sep.txt')
10
+ CRLF_FILE = File.join(FIXTURES_DIR, 'crlf_line_sep.txt')
11
+
12
+ def test_lf_line_separator
13
+ verify_read(LF_FILE, "LF")
14
+ end
15
+
16
+ def test_cr_line_separator
17
+ verify_read(CR_FILE, "CR")
18
+ end
19
+
20
+ def test_crlf_line_separator
21
+ verify_read(CRLF_FILE, "CRLF")
22
+ end
23
+
24
+ def verify_read(file, type)
25
+ lines = File.open(file) { |io| io.readlines }
26
+ assert_equal(3, lines.size, "#{type} line separator not recognized in readlines")
27
+ lines = File.open(file) { |io| io.to_a }
28
+ assert_equal(3, lines.size, "#{type} line separator not recognized in to_a")
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/inflector'
5
+
6
+ class InflectorTest < Test::Unit::TestCase
7
+ def test_quantified_s
8
+ assert_equal("1 person", "person".quantify(1))
9
+ assert_equal("2 people", "person".quantify(2))
10
+ assert_equal("0 people", "person".quantify(0))
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+
5
+ require 'caruby/util/domain_extent'
6
+
7
+ class LazyHastTest < Test::Unit::TestCase
8
+
9
+ def test_value_factory
10
+ hash = LazyHash.new { |key| key.to_s }
11
+ assert_equal('1', hash[1], "Factory return value is incorrect")
12
+ end
13
+
14
+ def test_default_value_factory
15
+ hash = LazyHash.new
16
+ assert_nil(hash[1], "Default factory does not return")
17
+ assert(hash.has_key?(1), "Default entry not created")
18
+ end
19
+
20
+ def test_nil_key
21
+ hash = LazyHash.new
22
+ assert_nil(hash[nil], "nil key does not return")
23
+ assert(!hash.has_key?(nil), "Entry created for nil key")
24
+ end
25
+
26
+ def test_reject_missing_value
27
+ hash = LazyHash.new(:compact => true)
28
+ assert(!hash.has_key?(1), "Default entry created for nil value")
29
+ end
30
+
31
+ def test_fetch
32
+ assert_raises(IndexError, "Fetch non-existent doesn't raise IndexError") { LazyHash.new.fetch(:a) }
33
+ end
34
+ end
@@ -0,0 +1,83 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/merge'
5
+ require 'caruby/util/log'
6
+
7
+ class MergeTest < Test::Unit::TestCase
8
+ LogCaRuby::Log.instance.open(File.join('test', 'results', 'log', 'caruby.log'), :debug => true)
9
+
10
+ class Target
11
+ include Mergeable
12
+
13
+ attr_accessor :a, :b, :c, :array
14
+
15
+ def self.mergeable_attributes
16
+ [:a, :b]
17
+ end
18
+ end
19
+
20
+ class Partial
21
+ attr_accessor :a, :c, :d
22
+ end
23
+
24
+ attr_reader :target
25
+
26
+ def setup
27
+ @target = Target.new
28
+ end
29
+
30
+ def test_merge_attributes_hash
31
+ target.merge_attributes(:a => 1, :b => 2, :c => 3)
32
+ assert_equal(1, target.a, 'Attribute not merged')
33
+ assert_equal(2, target.b, 'Attribute not merged')
34
+ assert_equal(3, target.c, 'Attribute not merged')
35
+ end
36
+
37
+ def test_merge_attributes_other
38
+ other = Target.new
39
+ other.a = 1
40
+ other.b = 2
41
+ other.c = 3
42
+ target.merge_attributes(other)
43
+ assert_equal(1, target.a, 'Attribute not merged')
44
+ assert_equal(2, target.b, 'Attribute not merged')
45
+ assert_nil(target.c, 'Non-mergeable attribute merged')
46
+ end
47
+
48
+ def test_merge_attributes_other_partial
49
+ other = Partial.new
50
+ other.a = 1
51
+ other.c = 3
52
+ other.d = 4
53
+ target.merge_attributes(other)
54
+ assert_equal(1, target.a, 'Attribute not merged')
55
+ assert_nil(target.c, 'Non-mergeable attribute merged')
56
+ end
57
+
58
+ def test_array_merge
59
+ array = [1]
60
+ array.merge([1, 2])
61
+ assert_equal([1, 2], array, 'Array deep merge incorrect')
62
+ end
63
+
64
+ def test_array_attribute_merge
65
+ target.a = [1]
66
+ target.merge_attributes(:array => [1, 2])
67
+ assert_equal([1, 2], target.array, 'Array attribute not merged correctly')
68
+ end
69
+
70
+ def test_hash_deep_merge
71
+ assert_equal({:a => [1], :b => [2, 3]}, {:a => [1], :b => [2]}.merge({:b => [3]}, :deep), 'Hash deep merge incorrect')
72
+ end
73
+
74
+ def test_hash_in_place_deep_merge
75
+ hash = {:a => [1], :b => [2]}
76
+ hash.merge!({:b => [3], :c => 4}, :deep)
77
+ assert_equal({:a => [1], :b => [2, 3], :c => 4}, hash, 'Hash deep merge incorrect')
78
+ end
79
+
80
+ def test_nested_hash_deep_merge
81
+ assert_equal({:a => {:b => [1, 2]}, :c => 3}, {:a => {:b => [1]}}.merge({:a => {:b => [2]}, :c => 3}, :deep), 'Hash deep merge incorrect')
82
+ end
83
+ end
@@ -0,0 +1,29 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/module'
5
+
6
+ module Outer
7
+ module Middle
8
+ module InnerModule; end
9
+ class InnerClass; end
10
+ end
11
+ end
12
+
13
+ class ModuleTest < Test::Unit::TestCase
14
+ def test_top_level_module_with_name
15
+ assert_equal(Array, Module.module_with_name(nil, 'Array'), "Top level module incorrect")
16
+ end
17
+
18
+ def test_module_with_unqualified_name
19
+ assert_equal(Outer::Middle, Outer.module_with_name('Middle'), "Unqualified module incorrect")
20
+ end
21
+
22
+ def test_module_with_qualified_name
23
+ assert_equal(Outer::Middle::InnerModule, Outer.module_with_name('Middle::InnerModule'), "Qualified module incorrect")
24
+ end
25
+
26
+ def test_class_with_name
27
+ assert_equal(Outer::Middle::InnerClass, Outer.module_with_name('Middle::InnerClass'), "Inner class incorrect")
28
+ end
29
+ end
@@ -0,0 +1,59 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/options'
5
+
6
+ class OptionsTest < Test::Unit::TestCase
7
+ def test_present
8
+ assert(Options.get(:key, {:key => true}), "Option true value not returned")
9
+ assert(!Options.get(:key, {:key => false}), "Option true value not returned")
10
+ end
11
+
12
+ def test_nil_value
13
+ assert_equal(:a, Options.get(:key, {:key => nil}, :a), "Options nil value not returned")
14
+ end
15
+
16
+ def test_false_with_true_default
17
+ assert_equal(false, Options.get(:key, {:key => false}, true), "Option false value with true default doesn't return false")
18
+ end
19
+
20
+ def test_missing
21
+ assert_nil(Options.get(:key, {}), "Missing option incorrectly found")
22
+ end
23
+
24
+ def test_default_value
25
+ assert_equal(:a, Options.get(:key, {}, :a), "Option default value not used")
26
+ end
27
+
28
+ def test_default_block
29
+ assert_equal(:b, Options.get(:key, {}) { :b }, "Option default block not called")
30
+ end
31
+
32
+ def test_symbol
33
+ assert(Options.get(:key, :key), "Option not found in singleton options")
34
+ end
35
+
36
+ def test_array
37
+ assert(Options.get(:b, [:a, :b]), "Option not found in array options")
38
+ end
39
+
40
+ def test_collection_value
41
+ assert_equal([:a], Options.get(:key, {:key => [:a]}, []), "Option array value not returned")
42
+ end
43
+
44
+ def test_empty_to_hash
45
+ assert_equal({}, Options.to_hash(), "Option to_hash with empty argument list not an empty hash")
46
+ end
47
+
48
+ def test_nil_to_hash
49
+ assert_equal({}, Options.to_hash(nil), "Option to_hash with nil argument list not an empty hash")
50
+ end
51
+
52
+ def test_hash_to_hash
53
+ assert_equal({:a => 1}, Options.to_hash({:a => 1}), "Option to_hash with hash argument list not an empty hash")
54
+ end
55
+
56
+ def test_lish_to_hash
57
+ assert_equal({:a => 1, :b => true, :c => [2, 3]}, Options.to_hash(:a, 1, :b, :c, 2, 3), "Option to_hash with list argument list incorrect")
58
+ end
59
+ end
@@ -0,0 +1,41 @@
1
+ $:.unshift 'lib'
2
+
3
+ require "test/unit"
4
+ require 'caruby/util/partial_order'
5
+
6
+ class Queued
7
+ include PartialOrder
8
+
9
+ attr_reader :value, :queue
10
+
11
+ def initialize(value, on)
12
+ @value = value
13
+ @queue = on.push(self)
14
+ end
15
+
16
+ def <=>(other)
17
+ value <=> other.value if queue.equal?(other.queue)
18
+ end
19
+ end
20
+
21
+ class PartialOrderTest < Test::Unit::TestCase
22
+ def test_same_queue
23
+ @a = Queued.new(1, [])
24
+ assert_equal(@a, @a.dup, "Same value, queue not equal")
25
+ end
26
+
27
+ def test_different_eql_queue
28
+ @a = Queued.new(1, [])
29
+ @b = Queued.new(1, [])
30
+ assert_nil(@a <=> @b, "Same value, different queue <=> not nil")
31
+ assert_not_equal(@a, @b, "Same value, different queue is equal")
32
+ end
33
+
34
+ def test_less_than
35
+ @a = Queued.new(1, [])
36
+ @b = Queued.new(2, @a.queue)
37
+ @c = Queued.new(2, [])
38
+ assert(@a < @b, "Comparison incorrect")
39
+ assert_nil(@a < @c, "Comparison incorrect")
40
+ end
41
+ end
@@ -0,0 +1,115 @@
1
+ $:.unshift 'lib'
2
+ $:.unshift '../caruby/lib'
3
+
4
+ require "test/unit"
5
+ require 'caruby/util/person'
6
+
7
+ class PersonTest < Test::Unit::TestCase
8
+ def test_middle_but_no_first
9
+ assert_raises(ValidationError) { || CaRuby::Person::Name.new('Longfellow', nil, 'Wadsworth').validate }
10
+ end
11
+
12
+ def test_empty_middle_and_no_first
13
+ assert_raises(ValidationError) { || CaRuby::Person::Name.new(nil, '').validate }
14
+ end
15
+
16
+ def test_substitute_nil_for_empty
17
+ name = CaRuby::Person::Name.new('Longfellow', '')
18
+ assert_nil(name.first)
19
+ end
20
+
21
+ def test_full_print
22
+ name = CaRuby::Person::Name.new('Longfellow', 'Henry', 'W.')
23
+ assert_equal('Henry W. Longfellow', name.to_s)
24
+ end
25
+
26
+ def test_first_and_last_print
27
+ name = CaRuby::Person::Name.new('Longfellow', 'Henry')
28
+ assert_equal('Henry Longfellow', name.to_s)
29
+ end
30
+
31
+ def test_last_print
32
+ name = CaRuby::Person::Name.new('Longfellow', '')
33
+ assert_equal('Longfellow', name.to_s)
34
+ end
35
+
36
+ def test_first_print
37
+ name = CaRuby::Person::Name.new('', 'Henry')
38
+ assert_equal('Henry', name.to_s)
39
+ end
40
+
41
+ def test_parse_last_first
42
+ name = CaRuby::Person::Name.parse('Longfellow, Henry')
43
+ assert_equal('Henry', name.first)
44
+ assert_equal('Longfellow', name.last)
45
+ assert_nil(name.middle)
46
+ end
47
+
48
+ def test_parse_last_first_middle
49
+ name = CaRuby::Person::Name.parse('Longfellow, Henry Wadsworth')
50
+ assert_equal('Henry', name.first)
51
+ assert_equal('Longfellow', name.last)
52
+ assert_equal('Wadsworth', name.middle)
53
+ end
54
+
55
+ def test_parse_last_first_middles
56
+ name = CaRuby::Person::Name.parse('Longfellow, Henry Gallifant Wadsworth')
57
+ assert_equal('Henry', name.first)
58
+ assert_equal('Longfellow', name.last)
59
+ assert_equal('Gallifant Wadsworth', name.middle)
60
+ end
61
+
62
+ def test_parse_first_last
63
+ name = CaRuby::Person::Name.parse('Henry Longfellow')
64
+ assert_equal('Henry', name.first)
65
+ assert_equal('Longfellow', name.last)
66
+ assert_nil(name.middle)
67
+ end
68
+
69
+ def test_parse_first_middle_Last
70
+ name = CaRuby::Person::Name.parse('Henry Wadsworth Longfellow')
71
+ assert_equal('Henry', name.first)
72
+ assert_equal('Longfellow', name.last)
73
+ assert_equal('Wadsworth', name.middle)
74
+ end
75
+
76
+ def test_salutation_with_middle
77
+ name = CaRuby::Person::Name.parse('Mr. Henry Wadsworth Longfellow')
78
+ assert_equal('Henry', name.first)
79
+ assert_equal('Longfellow', name.last)
80
+ assert_equal('Wadsworth', name.middle)
81
+ assert_equal('Mr.', name.salutation)
82
+ end
83
+
84
+ def test_salutation_without_middle
85
+ name = CaRuby::Person::Name.parse('Mr. Henry Longfellow')
86
+ assert_equal('Henry', name.first)
87
+ assert_equal('Longfellow', name.last)
88
+ assert_equal('Mr.', name.salutation)
89
+ end
90
+
91
+ def test_qualifier
92
+ name = CaRuby::Person::Name.parse('Henry Wadsworth Longfellow III')
93
+ assert_equal('Henry', name.first)
94
+ assert_equal('Longfellow', name.last)
95
+ assert_equal('Wadsworth', name.middle)
96
+ assert_equal('III', name.qualifier)
97
+ end
98
+
99
+ def test_credentials
100
+ name = CaRuby::Person::Name.parse('Henry Wadsworth Longfellow, MD, Ph.D., CPA, Esq')
101
+ assert_equal('Henry', name.first)
102
+ assert_equal('Longfellow', name.last)
103
+ assert_equal('Wadsworth', name.middle)
104
+ assert_equal('MD, Ph.D., CPA, Esq', name.credentials)
105
+ end
106
+
107
+ def test_qualifier_and_credentials
108
+ name = CaRuby::Person::Name.parse('Henry Wadsworth Longfellow III, Ph.D.')
109
+ assert_equal('Henry', name.first)
110
+ assert_equal('Longfellow', name.last)
111
+ assert_equal('Wadsworth', name.middle)
112
+ assert_equal('III', name.qualifier)
113
+ assert_equal('Ph.D.', name.credentials)
114
+ end
115
+ end