glue 0.0.1 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+ require 'glue/attribute'
5
+
6
+ class TC_Attribute < Test::Unit::TestCase # :nodoc: all
7
+
8
+ class Dummy
9
+ cattr_accessor :value, 'Default'
10
+ cattr_reader :reader, 3
11
+ end
12
+
13
+ def test_attr
14
+ assert_equal 'Default', Dummy.value
15
+ assert_nothing_raised { Dummy.value = 2 }
16
+ assert_equal 2, Dummy.value
17
+
18
+ assert_equal 3, Dummy.reader
19
+ assert_raise(NoMethodError) { Dummy.reader = 19 }
20
+ end
21
+
22
+ end
@@ -0,0 +1,45 @@
1
+ require "test/unit"
2
+ require "glue/cache"
3
+
4
+ class TC_Glue_Cache < Test::Unit::TestCase
5
+ # :nodoc: all
6
+
7
+ class Dummy
8
+ include N::LRUCache::Item
9
+ end
10
+
11
+ def setup
12
+ @cache = N::LRUCache.new(maxitems = 3)
13
+ end
14
+
15
+ def teardown
16
+ @cache = nil
17
+ end
18
+
19
+ def test_safe_cache
20
+ d1 = Dummy.new
21
+ d2 = Dummy.new
22
+ d3 = Dummy.new
23
+ d4 = Dummy.new
24
+
25
+ @cache[1] = d1
26
+ @cache[2] = d2
27
+ @cache[3] = d3
28
+
29
+ assert_equal(d1, @cache.last)
30
+ assert_equal(d3, @cache.first)
31
+
32
+ val = @cache[2]
33
+ assert_equal(d2, @cache.first)
34
+
35
+ @cache[4] = d4
36
+ assert_equal(3, @cache.size)
37
+ assert_equal(d4, @cache.first)
38
+
39
+ # insert the same key
40
+ @cache[4] = d4
41
+ assert_equal(3, @cache.size)
42
+ assert_equal(d4, @cache.first)
43
+ end
44
+
45
+ end
@@ -0,0 +1,38 @@
1
+ # :nodoc: all
2
+
3
+ require "test/unit"
4
+ require "glue/hash"
5
+
6
+ class TC_N2_Utils_Cache < Test::Unit::TestCase
7
+ # :nodoc: all
8
+
9
+ def setup
10
+ @safe_cache = N::SafeHash.new(Hash.new)
11
+ end
12
+
13
+ def teardown
14
+ @safe_cache = nil
15
+ end
16
+
17
+ def test_safe_cache
18
+ assert_equal(nil, @safe_cache["no_item"])
19
+ item1 = "item1"
20
+ @safe_cache["key1"] = item1
21
+ assert_equal(item1, @safe_cache["key1"])
22
+
23
+ @safe_cache["rem1"] = item1
24
+ @safe_cache.delete("rem1")
25
+ assert_equal(nil, @safe_cache["rem1"])
26
+
27
+ @safe_cache.clear()
28
+ assert_equal(0, @safe_cache.size)
29
+
30
+ @safe_cache["i1"] = "i1"
31
+ @safe_cache["i2"] = "i1"
32
+ @safe_cache["i3"] = "i1"
33
+ assert_equal(3, @safe_cache.size)
34
+
35
+ assert_equal(3, @safe_cache.values.size)
36
+ end
37
+
38
+ end
@@ -0,0 +1,39 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'vendor')
3
+
4
+ require 'stringio'
5
+
6
+ require 'test/unit'
7
+ require 'glue/logger'
8
+
9
+ class TC_Logger < Test::Unit::TestCase # :nodoc: all
10
+
11
+ def setup
12
+ @io = StringIO.new('')
13
+ Logger.set(Logger.new(@io))
14
+ end
15
+
16
+ def test_logger
17
+ Logger.info 'hello'
18
+ assert_equal(" INFO: hello\n", @io.string)
19
+
20
+ # bug: Binding.of_caller thinks Logger is the caller.
21
+ a = 14
22
+ b = 13
23
+ Logger.trace 'a * b'
24
+ assert_equal(" INFO: hello\nDEBUG: a * b = 182\n", @io.string)
25
+
26
+ @io.string = ''
27
+ Logger.debug 'hello'
28
+ assert_equal("DEBUG: hello\n", @io.string)
29
+
30
+ # test logging level.
31
+ Logger.get.level = Logger::INFO
32
+ Logger.debug 'world'
33
+ assert_equal("DEBUG: hello\n", @io.string)
34
+
35
+ # bug:
36
+ Logger.error 'Have forgotten that :)'
37
+ end
38
+
39
+ end
@@ -0,0 +1,20 @@
1
+ # :nodoc: all
2
+
3
+ require "test/unit"
4
+ require "glue/number"
5
+
6
+ class TC_N_NumberUtils < Test::Unit::TestCase
7
+ # :nodoc: all
8
+
9
+ def setup
10
+ end
11
+
12
+ def teardown
13
+ end
14
+
15
+ def test_ceil_multiple
16
+ assert_equal(20, N::NumberUtils.ceil_multiple(15, 10))
17
+ assert_equal(10, N::NumberUtils.ceil_multiple(1, 10))
18
+ end
19
+
20
+ end
@@ -0,0 +1,91 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+
5
+ require 'og'
6
+ require 'glue/logger'
7
+ require 'glue/property'
8
+
9
+ include N
10
+
11
+ def VarChar(size)
12
+ return String, :sql => "VARCHAR(#{ size })"
13
+ end
14
+ NotNull = {:sql => "NOT NULL"}.freeze
15
+ Null = {:sql => "NULL"}.freeze
16
+
17
+ Property.type_checking = false
18
+
19
+ module Test # :nodoc: all
20
+
21
+ class Msg
22
+ include Og::Unmanageable
23
+
24
+ prop Fixnum, :owner_oid
25
+ prop_accessor :val1, :val2, :val3, Fixnum, :sql => "smallint"
26
+ prop_accessor :title, :body, String
27
+ prop_accessor :test, String, :sql => "char(10) NOT NULL"
28
+ prop_accessor :count, Fixnum
29
+ prop_accessor :create_time, Time
30
+
31
+ # a marshaled property
32
+ prop_accessor Array, :options
33
+
34
+ # property with macro arguments!
35
+ prop_accessor :address, VarChar(30), NotNull
36
+
37
+ def initialize
38
+ @create_time = Time.now
39
+ @options = []
40
+ end
41
+
42
+ end
43
+
44
+ class SubMsg < Msg
45
+ # to avoid conflicts with tc_og.rb
46
+ include Og::Unmanageable
47
+
48
+ # duplicate definition with different type!
49
+ prop_accessor :count, Float
50
+ end
51
+
52
+ class TC_N_Properties < Test::Unit::TestCase
53
+
54
+ def setup
55
+ @msg1 = Msg.new
56
+ end
57
+
58
+ def teardown
59
+ @msg1 = nil
60
+ end
61
+
62
+ def test_props
63
+
64
+ # bug: props for subclasses.
65
+
66
+ assert(SubMsg.__props)
67
+ assert_equal(Msg.__props.size(), SubMsg.__props.size())
68
+
69
+ # bug: duplicate definition
70
+
71
+ assert_equal(Float, SubMsg.__props.find { |p| :count == p.symbol }.klass)
72
+
73
+ # dont force conversion
74
+
75
+ @msg1.count = 2.4
76
+ assert_equal(Float, @msg1.count.class)
77
+
78
+ # force conversion
79
+
80
+ @msg1.__force_count(2.4)
81
+ assert_equal(Fixnum, @msg1.count.class)
82
+ end
83
+
84
+ def test_macro_params
85
+ sql = Msg.__props.find { |p| :address == p.symbol }.meta[:sql]
86
+ assert_equal 'VARCHAR(30) NOT NULL', sql
87
+ end
88
+
89
+ end
90
+
91
+ end # module
@@ -0,0 +1,93 @@
1
+ #! /usr/bin/env ruby
2
+ # vim:sw=2:ai
3
+
4
+ #--
5
+ # Thomas Quas <tquas@yahoo.com>
6
+ # George Moschovitis <gm@navel.gr>
7
+ # $Id$
8
+ #++
9
+
10
+ $LOAD_PATH.unshift 'lib'
11
+
12
+ require "test/unit"
13
+ require 'glue/property'
14
+
15
+ module Mixin
16
+ prop_accessor :date
17
+ meta :dummy, [123]
18
+ end
19
+
20
+ class MixedOnly
21
+ include Mixin
22
+ __meta[:dummy] << 5
23
+ __meta[:dummy] << 3
24
+ end
25
+
26
+ class MixedOnly2
27
+ include Mixin
28
+ end
29
+
30
+ class MixedWithProp
31
+ include Mixin
32
+
33
+ prop_accessor :dummy
34
+ end
35
+
36
+ class Pure
37
+ prop_accessor :dummy
38
+ end
39
+
40
+ class Empty
41
+ end
42
+
43
+ class Base
44
+ prop_accessor :date
45
+ meta :dummy, [123]
46
+ end
47
+
48
+ class Child1 < Base
49
+ __meta[:dummy].first << 5
50
+ __meta[:dummy].first << 3
51
+ end
52
+
53
+ class Child2 < Base
54
+ end
55
+
56
+ # Tests auto management.
57
+ class TC_MixinsTest < ::Test::Unit::TestCase
58
+ def test_the_unmanaged
59
+ assert( Empty.respond_to?( :__props ) == false )
60
+ assert_respond_to( Pure.new, :dummy )
61
+ end
62
+
63
+ def test_manage_entities
64
+ assert_respond_to( Pure, :__props )
65
+ assert_respond_to( Pure.new, :dummy )
66
+ end
67
+
68
+ def test_managing_mixedonly_classes
69
+ assert_respond_to( MixedOnly.new, :date )
70
+ end
71
+
72
+ def test_managing_mixins
73
+ assert_respond_to( Mixin, :__props )
74
+ end
75
+
76
+ def test_managing_mixed_classes
77
+ obj = MixedWithProp.new
78
+ assert( obj.respond_to?( :date ) )
79
+ assert( obj.respond_to?( :dummy ) )
80
+ end
81
+
82
+ def test_crosspolination
83
+ assert_equal 3, MixedOnly.__meta[:dummy].size
84
+ assert_equal 1, MixedOnly2.__meta[:dummy].size
85
+ assert_equal 1, Mixin.__meta[:dummy].size
86
+ =begin
87
+ gmosx: THINK!
88
+ assert_equal 3, Child1.__meta[:dummy].first.size
89
+ assert_equal 1, Child2.__meta[:dummy].first.size
90
+ assert_equal 1, Base.__meta[:dummy].first.size
91
+ =end
92
+ end
93
+ end
@@ -0,0 +1,35 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
2
+
3
+ require 'test/unit'
4
+
5
+ require 'glue/logger'
6
+ require 'glue/property'
7
+
8
+ N::Property.type_checking = true
9
+
10
+ module Test # :nodoc: all
11
+
12
+ class Person
13
+ prop_accessor :age, Fixnum
14
+
15
+ def initialize(age = nil)
16
+ @age = age
17
+ end
18
+
19
+ end
20
+
21
+ class TC_PropertiesTypeChecking < Test::Unit::TestCase
22
+
23
+ def test_all
24
+ per = Person.new
25
+ =begin
26
+ FIXME: does not work when run in the full test suite.
27
+ assert_raises(RuntimeError) {
28
+ per.age = 'Hello'
29
+ }
30
+ =end
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -0,0 +1,103 @@
1
+ # :nodoc: all
2
+
3
+ require "test/unit"
4
+ require "glue/string"
5
+
6
+ class TC_StringUtilsUtils < Test::Unit::TestCase
7
+ # :nodoc: all
8
+
9
+ def setup
10
+ @s = "this is any cool test"
11
+ end
12
+
13
+ def test_valid?
14
+ assert_equal(true, N::StringUtils.valid?("test"))
15
+ assert_equal(false, N::StringUtils.valid?(""))
16
+ assert_equal(false, N::StringUtils.valid?(nil))
17
+ end
18
+
19
+ def test_head
20
+ assert_equal(nil, N::StringUtils.head(nil))
21
+ assert_equal("this...", N::StringUtils.head(@s, 3))
22
+ assert_equal("this+++", N::StringUtils.head(@s, 3, false, "+++"))
23
+ assert_equal("thi...", N::StringUtils.head(@s, 3, true))
24
+ assert_equal("this is...", N::StringUtils.head(@s, 8))
25
+ assert_equal(nil, N::StringUtils.head(@s, 0))
26
+ assert_equal(nil, N::StringUtils.head(@s, 0, true))
27
+ assert_equal("this is...", N::StringUtils.head(@s, 8, true))
28
+ end
29
+
30
+ def test_rewrite
31
+ rules = [
32
+ [/\/\$/, "../n1"],
33
+ [/\$/, "../n1"],
34
+ [/^games/, "../../games"],
35
+ [/^tmp/, "/n/theseas/var/tmp"],
36
+ ]
37
+ string = N::StringUtils.rewrite("games/arkanoid.html", rules)
38
+ assert_equal("../../games/arkanoid.html", string)
39
+ string = N::StringUtils.rewrite("tmp/garbage.html", rules)
40
+ assert_equal("/n/theseas/var/tmp/garbage.html", string)
41
+ string = N::StringUtils.rewrite("root/index.html", rules)
42
+ assert_equal("root/index.html", string)
43
+ assert_equal(nil, N::StringUtils.rewrite(nil, rules))
44
+ assert_equal("", N::StringUtils.rewrite("", rules))
45
+
46
+ assert_raises(ArgumentError) {
47
+ assert_equal("koko", N::StringUtils.rewrite("koko", nil))
48
+ }
49
+
50
+ # bug: should keep order
51
+ s = N::StringUtils.rewrite("/$/koko.html", rules)
52
+ assert_equal("../n1/koko.html", s)
53
+ end
54
+
55
+ def test_wrap
56
+ s = "1234567890abcdefghijklmnopqrstu"
57
+ r = "1234 5678 90ab cdef ghij klmn opqr stu"
58
+ assert_equal(r, N::StringUtils::wrap(s, 4, " "))
59
+
60
+ s = "111111111111111111111111111111111111111111111111"
61
+ r = "1111111111 1111111111 1111111111 1111111111 11111111"
62
+ assert_equal(r, N::StringUtils::wrap(s, 10, " "))
63
+
64
+ s = "jackdaws love my big sphinx of quartz"
65
+ r = "jackdaws love my big sphinx of quartz"
66
+ assert_equal(r, N::StringUtils::wrap(s, 10, " "))
67
+
68
+ s = "jackdaws love my big sphinx of quartz"
69
+ r = "jack daws love my big sphi nx of quar tz"
70
+ assert_equal(r, N::StringUtils::wrap(s, 4, " "))
71
+
72
+ s = "jack.daws love my big sphinx of quartz"
73
+ r = "jack .daw s love my big sphi nx of quar tz"
74
+ assert_equal(r, N::StringUtils::wrap(s, 4, " "))
75
+
76
+ assert_equal("", N::StringUtils::wrap("", 4, " "))
77
+ assert_equal(nil, N::StringUtils::wrap(nil, 4, " "))
78
+ end
79
+ =begin
80
+ def test_rationalize_filename
81
+ filename = N::StringUtils.rationalize_filename("hello my friend!.gif")
82
+ assert_equal("hello-my-friend.gif", filename)
83
+ filename = N::StringUtils.rationalize_filename("���� ����.gif")
84
+ assert_equal("pame-pali.gif", filename)
85
+ filename = N::StringUtils.rationalize_filename("�� ���.gif")
86
+ assert_equal("ti-les.gif", filename)
87
+
88
+ # bug:
89
+ filename = N::StringUtils.rationalize_filename("image-(10).gif")
90
+ assert_equal("image-10.gif", filename)
91
+ end
92
+ =end
93
+ def test_random_string
94
+ s1 = N::StringUtils.random()
95
+ s2 = N::StringUtils.random()
96
+ assert_not_equal(s1, s2)
97
+ assert(s1.size == s2.size)
98
+ end
99
+
100
+ def teardown
101
+ @s = nil
102
+ end
103
+ end