corefoundation 0.2.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,158 +0,0 @@
1
- # Rubyinteger
2
- class Integer
3
- # Converts the Integer to a {CF::Number} using {CF::Number.from_i}
4
- # @return [CF::Number]
5
- def to_cf
6
- CF::Number.from_i(self)
7
- end
8
- end
9
-
10
- # Ruby float class
11
- class Float
12
- # Converts the Float to a {CF::Number} using {CF::Number.from_f}
13
- # @return [CF::Number]
14
- def to_cf
15
- CF::Number.from_f(self)
16
- end
17
- end
18
-
19
- # Ruby array class
20
- class Array
21
- # Converts the Array to an immutable {CF::Array} by calling `to_cf` on each element it contains
22
- # @return [CF::Number]
23
- def to_cf
24
- CF::Array.immutable(collect(&:to_cf))
25
- end
26
- end
27
-
28
- # Ruby true class
29
- class TrueClass
30
- # Returns a CF::Boolean object representing true
31
- # @return [CF::Boolean]
32
- def to_cf
33
- CF::Boolean::TRUE
34
- end
35
- end
36
-
37
- # Ruby false class
38
- class FalseClass
39
- # Returns a CF::Boolean object representing false
40
- # @return [CF::Boolean]
41
- def to_cf
42
- CF::Boolean::FALSE
43
- end
44
- end
45
-
46
- # Ruby String class
47
- class String
48
- # Returns a {CF::String} or {CF::Data} representing the string.
49
- # If {#binary?} returns true a {CF::Data} is returned, if not a {CF::String} is returned
50
- #
51
- # If you want a {CF::Data} with the contents of a non binary string, use {#to_cf_data}
52
- #
53
- # @return [CF::String, CF::Data]
54
- def to_cf
55
- self.binary? ? self.to_cf_data : self.to_cf_string
56
- end
57
-
58
- # @!method binary?
59
- #
60
- # used to determine whether {#to_cf} should return a {CF::String} or a {CF::Data}. On ruby 1.9 and above this simply
61
- # checks whether the encoding is ascii-8bit or not.
62
- #
63
- # On ruby 1.8.7
64
- #
65
- # - A string is binary if you call {#binary!} with the default argument of true
66
- # - A string is not binary if you call {#binary!} with the argument false
67
- #
68
- # If you have never called {#binary!} then a string is binary if Iconv does not think it is valid utf-8
69
- # @return whether the string is handled as binary data or not
70
- #
71
- if '<3'.respond_to? :encoding
72
- def binary?
73
- encoding == Encoding::ASCII_8BIT
74
- end
75
- else
76
- def binary?
77
- unless defined? @cf_is_binary
78
- begin
79
- ::Iconv.conv('UTF-8', 'UTF-8', self)
80
- return false if self.frozen?
81
- @cf_is_binary = false
82
- rescue Iconv::IllegalSequence
83
- return true if self.frozen?
84
- @cf_is_binary = true
85
- end
86
- end
87
- @cf_is_binary
88
- end
89
- end
90
-
91
- if '<3'.respond_to? :encoding
92
- # On ruby 1.8.7 sets or clears the flag used by {#binary?}. On ruby 1.9 the string's encoding is forced.
93
- # @see #binary?
94
- #
95
- # @note There is no advantage to using this over the standard encoding methods unless you wish to retain 1.8.7 compatibility
96
- #
97
- # @param [optional, Boolean, Encoding] bin On ruby 1.8.7 only boolean values are admissible. On ruby 1.9 you can pass a specific encoding to force.
98
- # If you pass `true` then `Encoding::ASCII_BIT` is used, if you pass `false` then `Encoding::UTF_8`
99
- #
100
- def binary!(bin=true)
101
- if bin == true
102
- self.force_encoding Encoding::ASCII_8BIT
103
- else
104
- # default to utf-8
105
- self.force_encoding( (bin == false) ? "UTF-8" : bin)
106
- end
107
- self
108
- end
109
- else
110
- # On ruby 1.8.7 sets or clears the flag used by {#binary?}. On ruby 1.9 the string's encoding is forced.
111
- # @see #binary?
112
- #
113
- # @note There is no advantage to using this over the standard encoding methods unless you wish to retain 1.8.7 compatibility
114
- #
115
- # @param [optional, Boolean, Encoding] bin On ruby 1.8.7 only boolean values are admissible. On ruby 1.9 you can pass a specific encoding to force.
116
- # If you pass `true` then `Encoding::ASCII_BIT` is used, if you pass `false` then `Encoding::UTF_8`
117
- #
118
- def binary!(bin=true)
119
- @cf_is_binary = !! bin
120
- self
121
- end
122
- end
123
-
124
- # Returns a {CF::String} representing the string
125
- # @return [CF::String]
126
- def to_cf_string
127
- CF::String.from_string self
128
- end
129
-
130
- # Returns a {CF::Data} representing the string
131
- # @return [CF::Data]
132
- def to_cf_data
133
- CF::Data.from_string self
134
- end
135
-
136
- end
137
-
138
- # Ruby Time class
139
- class Time
140
- # Returns a {CF::Date} representing the time.
141
- # @return [CF::Date]
142
- def to_cf
143
- CF::Date.from_time(self)
144
- end
145
- end
146
-
147
- # Ruby Hash class
148
- class Hash
149
- # Converts the Hash to an mutable {CF::Dictionary} by calling `to_cf` on each key and value it contains
150
- # @return [CF::Dictionary]
151
- def to_cf
152
- CF::Dictionary.mutable.tap do |r|
153
- each do |k,v|
154
- r[k.to_cf] = v.to_cf
155
- end
156
- end
157
- end
158
- end
data/spec/array_spec.rb DELETED
@@ -1,92 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Array do
4
- describe 'mutable' do
5
- subject { CF::Array.mutable}
6
-
7
- it { should be_a(CF::Array)}
8
- it { should be_mutable}
9
-
10
- describe '[]=' do
11
- it 'should raise when trying to store a non cf value' do
12
- expect {subject[0] = 123}.to raise_error(TypeError)
13
- end
14
- end
15
-
16
- describe '<<' do
17
- it 'should raise when trying to store a non cf value' do
18
- expect {subject << 123}.to raise_error(TypeError)
19
- end
20
- end
21
-
22
- end
23
-
24
- describe 'immutable' do
25
- it 'should raise if all of the array elements are not cf values' do
26
- expect {CF::Array.immutable([CF::Boolean::TRUE, 1])}.to raise_error(TypeError)
27
- end
28
-
29
- it 'should return an immutable cfarray' do
30
- CF::Array.immutable([CF::Boolean::TRUE]).should be_a(CF::Array)
31
- end
32
-
33
- context 'with an immutable array' do
34
- subject { CF::Array.immutable([CF::Boolean::TRUE, CF::String.from_string('123')])}
35
-
36
- describe '[]=' do
37
- it 'should raise TypeError' do
38
- expect {subject[0] = CF::Boolean::TRUE}.to raise_error(TypeError)
39
- end
40
- end
41
-
42
- describe '<<' do
43
- it 'should raise TypeError' do
44
- expect {subject << CF::Boolean::TRUE}.to raise_error(TypeError)
45
- end
46
- end
47
- end
48
- end
49
-
50
- context "with an array" do
51
- subject { CF::Array.immutable([CF::Boolean::TRUE, CF::String.from_string('123')])}
52
-
53
- describe '[]' do
54
- it 'should return the typecast value at the index' do
55
- subject[1].should be_a(CF::String)
56
- subject[1].should == CF::String.from_string('123')
57
- end
58
- end
59
-
60
-
61
- describe 'length' do
62
- it 'should return the count of items in the dictionary' do
63
- subject.length.should == 2
64
- end
65
- end
66
-
67
- describe 'to_ruby' do
68
- it 'should return the result of calling to ruby on its contents' do
69
- subject.to_ruby.should == [true, '123']
70
- end
71
- end
72
-
73
- describe 'each' do
74
- it 'should iterate over each value' do
75
- values = []
76
- subject.each do |v|
77
- values << v
78
- end
79
- values[0].should == CF::Boolean::TRUE
80
- values[1].should == CF::String.from_string('123')
81
- end
82
- end
83
-
84
- it 'should be enumerable' do
85
- values = {}
86
- subject.each_with_index do |value, index|
87
- values[index] = value
88
- end
89
- values.should == {0 => CF::Boolean::TRUE, 1 => CF::String.from_string('123')}
90
- end
91
- end
92
- end
data/spec/boolean_spec.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF do
4
-
5
-
6
- describe CF::Boolean do
7
- describe 'value' do
8
- it 'should return true for CF::Boolean::TRUE' do
9
- CF::Boolean::TRUE.value.should == true
10
- end
11
- it 'should return false for CF::Boolean::FALSE' do
12
- CF::Boolean::FALSE.value.should == false
13
- end
14
- end
15
-
16
- describe 'to_ruby' do
17
- it 'should behave like value' do
18
- CF::Boolean::FALSE.to_ruby.should == false
19
- end
20
- end
21
-
22
- end
23
-
24
- end
data/spec/data_spec.rb DELETED
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe CF::Data do
5
- subject {CF::Data.from_string('A CF string')}
6
- describe '#to_s' do
7
- it 'should return a binary ruby string' do
8
- ruby_string = subject.to_s
9
- ruby_string.should == 'A CF string'
10
- if CF::String::HAS_ENCODING
11
- ruby_string.encoding.should == Encoding::ASCII_8BIT
12
- end
13
- end
14
- end
15
-
16
- describe '#size' do
17
- it 'should return the size in bytes of the cfdata' do
18
- subject.size.should == 11
19
- end
20
- end
21
-
22
- describe 'to_ruby' do
23
- it 'should behave like to_s' do
24
- subject.to_ruby.should == 'A CF string'
25
- if 'A CF string'.respond_to? "encoding"
26
- subject.to_ruby.encoding.should == Encoding::ASCII_8BIT
27
- end
28
- end
29
- end
30
- end
data/spec/date_spec.rb DELETED
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Date do
4
- describe('from_time') do
5
- it 'should create a cf date from a time' do
6
- CF::Date.from_time(Time.now).should be_a(CF::Date)
7
- end
8
- end
9
-
10
- describe('to_time') do
11
- it 'should return a time' do
12
- t = CF::Date.from_time(Time.now).to_time
13
- t.should be_a(Time)
14
- t.should be_within(0.01).of(Time.now)
15
- end
16
- end
17
-
18
- describe 'to_ruby' do
19
- it 'should behave like to_time' do
20
- t = CF::Date.from_time(Time.now).to_ruby
21
- t.should be_a(Time)
22
- end
23
- end
24
- end
25
-
@@ -1,81 +0,0 @@
1
- require 'spec_helper'
2
-
3
-
4
- describe CF::Dictionary do
5
- describe 'mutable' do
6
- it 'should return a cf dictionary' do
7
- CF::Dictionary.mutable.should be_a(CF::Dictionary)
8
- end
9
- end
10
-
11
- describe 'hash access' do
12
- subject {CF::Dictionary.mutable}
13
-
14
- it 'should return nil when the key does not exist' do
15
- subject['doesnotexist'].should be_nil
16
- end
17
-
18
- it 'should raise when trying to store a non cf value' do
19
- expect {subject[CF::String.from_string('key')]=1}.to raise_error(TypeError)
20
- end
21
-
22
- it 'should raise when trying to store a non cf key' do
23
- expect {subject[1]=CF::String.from_string('value')}.to raise_error(TypeError)
24
- end
25
-
26
- it 'should allow storing and retrieving a cf key pair' do
27
- subject[CF::String.from_string('key')] = CF::String.from_string('value')
28
- end
29
-
30
- it 'should instantiate the correct type on retrieval' do
31
- subject[CF::String.from_string('key')] = CF::String.from_string('value')
32
- subject[CF::String.from_string('key')].should be_a(CF::String)
33
- end
34
-
35
- it 'should coerce string keys' do
36
- subject['key'] = CF::String.from_string('value')
37
- subject['key'].to_s.should == 'value'
38
- end
39
- end
40
-
41
- describe 'merge!' do
42
- subject { CF::Dictionary.mutable.tap {|dict| dict['1'] = CF::Boolean::TRUE; dict['2'] = CF::Boolean::FALSE}}
43
- it 'should merge the argument into the receiver' do
44
- argument = {'1' => false, 'foo' => 'bar'}.to_cf
45
- subject.merge! argument
46
- subject.to_ruby.should == {'1' => false, '2' => false, 'foo' => 'bar'}
47
- end
48
- end
49
-
50
- describe 'length' do
51
- it 'should return the count of items in the dictionary' do
52
- dict = CF::Dictionary.mutable
53
- dict['one'] = CF::Boolean::TRUE
54
- dict['two'] = CF::Boolean::TRUE
55
-
56
- dict.length.should == 2
57
- end
58
- end
59
-
60
- describe 'enumeration' do
61
- subject { CF::Dictionary.mutable.tap {|dict| dict['1'] = CF::Boolean::TRUE; dict['2'] = CF::Boolean::FALSE}}
62
-
63
- it 'should yield each key value pair in the dictionary' do
64
- hash = {}
65
- subject.each do |k,v|
66
- hash[k] = v
67
- end
68
- hash.should == {CF::String.from_string('1') => CF::Boolean::TRUE,
69
- CF::String.from_string('2') => CF::Boolean::FALSE}
70
- end
71
- end
72
-
73
- describe 'to_ruby' do
74
- subject { CF::Dictionary.mutable.tap {|dict| dict['1'] = CF::Boolean::TRUE; dict['2'] = CF::Array.immutable([CF::Boolean::FALSE])}}
75
-
76
- it 'should return a ruby hash where keys and values have been converted to ruby types' do
77
- subject.to_ruby.should == {'1' => true, '2' => [false]}
78
- end
79
- end
80
-
81
- end
@@ -1,127 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'spec_helper'
3
-
4
- describe 'extensions' do
5
- context 'with an integer' do
6
- it 'should return a cfnumber' do
7
- 1.to_cf.should be_a(CF::Number)
8
- end
9
- end
10
-
11
- context 'with a float' do
12
- it 'should return a cfnumber' do
13
- (1.0).to_cf.should be_a(CF::Number)
14
- end
15
- end
16
-
17
- describe String do
18
- uber = [195, 188, 98, 101, 114]
19
-
20
- describe 'to_cf' do
21
-
22
- unless defined? RUBY_ENGINE and RUBY_ENGINE == 'jruby'
23
- context 'with data incorrectly marked as non binary' do
24
- it 'returns nil' do
25
- [0xff, 0xff, 0xff].pack("C*").binary!(false).to_cf.should be_nil
26
- end
27
- end
28
- end
29
-
30
- context 'with a non binary string' do
31
- it 'should return a cf string' do
32
- '123'.to_cf.should be_a(CF::String)
33
- end
34
- end
35
-
36
- context 'with a string marked as binary' do
37
- it 'should return a cf data' do
38
- '123'.binary!.to_cf.should be_a(CF::Data)
39
- end
40
- end
41
-
42
- context 'with a binary string' do
43
- it 'should return a cf data' do
44
- [0xff, 0xff, 0xff].pack("C*").to_cf.should be_a(CF::Data)
45
- end
46
- end
47
-
48
- context 'with a utf-8 string' do
49
- it 'should return a cfstring' do
50
- uber.pack("C*").should_not be_a(CF::String)
51
- end
52
- end
53
- end
54
-
55
- describe 'binary?' do
56
-
57
- it 'should return false on a plain ascii string' do
58
- '123'.should_not be_binary
59
- end
60
-
61
- if CF::String::HAS_ENCODING
62
- it 'should return false on valid utf data' do
63
- "\xc3\xc9".should_not be_binary
64
- end
65
- else
66
- it 'should return false on valid utf data' do
67
- uber.pack("C*").should_not be_binary
68
- end
69
- end
70
-
71
- it 'should return false on string forced to non binary' do
72
- "\xc3\xc9".binary!(false).should_not be_binary
73
- end
74
-
75
- it 'should return true if binary! has been called' do
76
- '123'.binary!.should be_binary
77
- end
78
-
79
- if CF::String::HAS_ENCODING
80
- it 'should return true for ascii 8bit strings' do
81
- '123'.encode(Encoding::ASCII_8BIT).should be_binary
82
- end
83
- else
84
- it 'should return true for data that cannot be converted to utf8' do
85
- "\xff\xff\xff".should be_binary
86
- end
87
- end
88
- end
89
-
90
- end
91
-
92
- context 'with true' do
93
- it 'should return CF::Boolean::TRUE' do
94
- true.to_cf.should == CF::Boolean::TRUE
95
- end
96
- end
97
-
98
- context 'with false' do
99
- it 'should return CF::Boolean::FALSE' do
100
- false.to_cf.should == CF::Boolean::FALSE
101
- end
102
- end
103
-
104
- context 'with a time' do
105
- it 'should return a CFDate' do
106
- Time.now.to_cf.should be_a(CF::Date)
107
- end
108
- end
109
-
110
- context 'with an array' do
111
- it 'should return a cfarray containing cf objects' do
112
- cf_array = [true, 1, 'hello'].to_cf
113
- cf_array.should be_a(CF::Array)
114
- cf_array[0].should == CF::Boolean::TRUE
115
- cf_array[1].should be_a(CF::Number)
116
- cf_array[2].should == CF::String.from_string('hello')
117
- end
118
- end
119
-
120
- context 'with a dictionary' do
121
- it 'should return a cfdictionary containing cf objects' do
122
- cf_hash = {'key_1' => true, 'key_2' => false}.to_cf
123
- cf_hash['key_1'].should == CF::Boolean::TRUE
124
- cf_hash['key_2'].should == CF::Boolean::FALSE
125
- end
126
- end
127
- end
data/spec/null_spec.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Null do
4
- it 'should be null' do
5
- CF::NULL.should be_null
6
- end
7
- end
data/spec/number_spec.rb DELETED
@@ -1,52 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::Number do
4
-
5
- describe 'to_ruby' do
6
- context 'with a number created from a float' do
7
- subject {CF::Number.from_f('3.1415')}
8
- it 'should return a float' do
9
- subject.to_ruby.should be_within(0.0000001).of(3.14150)
10
- end
11
- end
12
- context 'with a number created from a int' do
13
- subject {CF::Number.from_i('31415')}
14
- it 'should return a int' do
15
- subject.to_ruby.should == 31415
16
- subject.to_ruby.should be_a(Integer)
17
- end
18
- end
19
- end
20
-
21
- it 'should be comparable' do
22
- (CF::Number.from_f('3.1415') <= CF::Number.from_i(4)).should be_true
23
- end
24
-
25
- describe('from_f') do
26
- it 'should create a cf number from a float' do
27
- CF::Number.from_f('3.1415').should be_a(CF::Number)
28
- end
29
- end
30
-
31
- describe('from_i') do
32
- it 'should create a cf number from a 32 bit sized int' do
33
- CF::Number.from_i(2**30).should be_a(CF::Number)
34
- end
35
-
36
- it 'should create a cf number from a 64 bit sized int' do
37
- CF::Number.from_i(2**60).should be_a(CF::Number)
38
- end
39
- end
40
-
41
- describe('to_i') do
42
- it 'should return a ruby integer representing the cfnumber' do
43
- CF::Number.from_i(2**60).to_i.should == 2**60
44
- end
45
- end
46
-
47
- describe('to_f') do
48
- it 'should return a ruby float representing the cfnumber' do
49
- CF::Number.from_f(3.1415).to_f.should be_within(0.0000001).of(3.14150)
50
- end
51
- end
52
- end
data/spec/spec_helper.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
-
4
- $: << File.dirname(__FILE__) + '/../lib'
5
-
6
- require 'corefoundation'
7
-
8
- RSpec.configure do |config|
9
-
10
- end
data/spec/string_spec.rb DELETED
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe CF::String do
4
-
5
- describe 'from_string' do
6
- it 'should return a CF::String' do
7
- CF::String.from_string('A CF string').should be_a(CF::String)
8
- end
9
-
10
- # The intent is to force feed CF::String with an invalid utf-8 string
11
- # but jruby doesn't seem to allow this to be constructed
12
- unless defined? RUBY_ENGINE and RUBY_ENGINE == 'jruby'
13
- context 'with invalid data' do
14
- it 'returns nil' do
15
- if CF::String::HAS_ENCODING
16
- CF::String.from_string("\xff\xff\xff".force_encoding('UTF-8')).should be_nil
17
- else
18
- CF::String.from_string("\xff\xff\xff").should be_nil
19
- end
20
- end
21
- end
22
- end
23
- end
24
-
25
- describe '#to_s' do
26
- it 'should return a utf ruby string' do
27
- ruby_string = CF::String.from_string('A CF string').to_s
28
- ruby_string.should == 'A CF string'
29
- if CF::String::HAS_ENCODING
30
- ruby_string.encoding.should == Encoding::UTF_8
31
- else
32
- end
33
- end
34
- end
35
-
36
- describe 'to_ruby' do
37
- it 'should behave like to_s' do
38
- CF::String.from_string('A CF string').to_ruby.should == 'A CF string'
39
- if CF::String::HAS_ENCODING
40
- CF::String.from_string('A CF string').to_ruby.encoding.should == Encoding::UTF_8
41
- end
42
- end
43
- end
44
-
45
- it 'should be comparable' do
46
- CF::String.from_string('aaa').should <= CF::String.from_string('zzz')
47
- end
48
- end