forked from tlconnor/activerecord-postgres-array
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_ext_spec.rb
109 lines (83 loc) · 3.35 KB
/
string_ext_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'spec_helper'
require 'activerecord-postgres-array/string'
describe "String" do
describe "#valid_postgres_array?" do
it 'returns true for an empty string' do
"".should be_valid_postgres_array
end
it 'returns true for a string consisting only of whitespace' do
" ".should be_valid_postgres_array
end
it 'returns true for a valid postgres integer array' do
"{10000, 10000, 10000, 10000}".should be_valid_postgres_array
end
it 'returns true for a valid postgres float array' do
"{10000.2, .5, 10000, 10000.9}".should be_valid_postgres_array
end
it 'returns true for a valid postgres numerical array with irregular whitespace' do
"{ 10000, 10000 , 10000,10000}".should be_valid_postgres_array
end
it 'returns false for an array with invalid commas' do
"{213,}".should_not be_valid_postgres_array
end
it 'allows enclosing single quotes' do
'\'{"ruby", "on", "rails"}\''.should be_valid_postgres_array
end
it 'returns false for an array without enclosing curly brackets' do
"213, 1234".should_not be_valid_postgres_array
end
it 'returns true for a valid postgres string array' do
'{"ruby", "on", "rails"}'.should be_valid_postgres_array
end
it 'returns true for a postgres string array with escaped double quote' do
'{"ruby", "on", "ra\"ils"}'.should be_valid_postgres_array
end
it 'returns false for a postgres string array with wrong quotation' do
'{"ruby", "on", "ra"ils"}'.should_not be_valid_postgres_array
end
it 'returns true for string array without quotes' do
"{ruby, on, rails}".should be_valid_postgres_array
end
it 'returns false for array consisting of commas' do
"{,,}".should_not be_valid_postgres_array
end
it 'returns false for concatenated strings' do
'{"ruby""on""rails"}'.should_not be_valid_postgres_array
end
it "returns false if single quotes are not closed" do
'\'{"ruby", "on", "rails"}'.should_not be_valid_postgres_array
end
it "returns true for an empty postgres array" do
"{}".should be_valid_postgres_array
end
it "returns false for postgres array beginning with ," do
"{,ruby,on,rails}".should_not be_valid_postgres_array
end
end
describe "#from_postgres_array" do
it 'returns an empty array if string is empty' do
"".from_postgres_array.should == []
end
it 'returns an empty array if empty postgres array is given' do
"{}".from_postgres_array.should == []
end
it 'returns an correct array if a valid postgres array is given' do
"{Ruby,on,Rails}".from_postgres_array.should == ["Ruby", "on", "Rails"]
end
it 'correctly handles commas' do
'{Ruby,on,"Rails,"}'.from_postgres_array.should == ["Ruby", "on", "Rails,"]
end
it 'correctly handles single quotes' do
"{Ruby,on,Ra'ils}".from_postgres_array.should == ["Ruby", "on", "Ra'ils"]
end
it 'correctly handles double quotes' do
"{Ruby,on,\"Ra\\\"ils\"}".from_postgres_array.should == ["Ruby", "on", 'Ra"ils']
end
it 'correctly handles backslashes' do
'\'{"\\\\","\\""}\''.from_postgres_array.should == ["\\","\""]
end
it 'correctly handles multi line content' do
"{A\nB\nC,X\r\nY\r\nZ}".from_postgres_array.should == ["A\nB\nC", "X\r\nY\r\nZ"]
end
end
end