general 1.4.2 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,246 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "spec_require"
18
+
19
+ # Describe General::GOperations
20
+ #
21
+ # Implements placeholder operations
22
+ #
23
+ # Author: Anshul Kharbanda
24
+ # Created: 6 - 3 - 2016
25
+ describe General::GOperations do
26
+ #-----------------------------------STRING OPERATIONS------------------------------------
27
+
28
+ # Describe General::GOperations::capitalize
29
+ #
30
+ # Capitalizes every word in the string
31
+ #
32
+ # Parameter: string - the string being capitalized
33
+ #
34
+ # Return: the capitalized string
35
+ describe '::capitalize' do
36
+ before(:all) do
37
+ @input = "joe schmoe"
38
+ @out_first = "Joe schmoe"
39
+ @out_all = "Joe Schmoe"
40
+ end
41
+
42
+ context 'with string argument given' do
43
+ it 'capitalizes the first letter in the string' do
44
+ expect(General::GOperations.capitalize(@input)).to eql @out_first
45
+ end
46
+ end
47
+
48
+ context 'with string argument and second string argument is given' do
49
+ context 'when the second argument is "first"' do
50
+ it 'capitalizes the first letter in the string' do
51
+ expect(General::GOperations.capitalize @input, "first").to eql @out_first
52
+ end
53
+ end
54
+
55
+ context 'when the second argument is "all"' do
56
+ it 'capitalizes all words in the string' do
57
+ expect(General::GOperations.capitalize @input, "all").to eql @out_all
58
+ end
59
+ end
60
+
61
+ context 'when the second argument is neither "first" or "all"' do
62
+ it 'raises TypeError' do
63
+ expect{General::GOperations.capitalize @input, "foo"}.to raise_error TypeError
64
+ end
65
+ end
66
+ end
67
+
68
+ context 'with no string argument' do
69
+ it 'raises Error' do
70
+ expect { General::GOperations.capitalize }.to raise_error ArgumentError
71
+ end
72
+ end
73
+
74
+ context 'with argument of another type' do
75
+ it 'raises Error' do
76
+ expect { General::GOperations.capitalize(0) }.to raise_error TypeError
77
+ end
78
+ end
79
+ end
80
+
81
+ # Describe General::GOperations::uppercase
82
+ #
83
+ # Converts every letter in the string to uppercase
84
+ #
85
+ # Parameter: string - the string being uppercased
86
+ #
87
+ # Return: the uppercased string
88
+ describe '::uppercase' do
89
+ before :all do
90
+ @input = "Joe Schmoe"
91
+ @output = "JOE SCHMOE"
92
+ end
93
+
94
+ context 'with string argument given' do
95
+ it 'converts the string to uppercase' do
96
+ expect(General::GOperations.uppercase @input).to eql @output
97
+ end
98
+ end
99
+
100
+ context 'with no string argument' do
101
+ it 'raises Error' do
102
+ expect { General::GOperations.uppercase }.to raise_error ArgumentError
103
+ end
104
+ end
105
+
106
+ context 'with argument of another type' do
107
+ it 'raises Error' do
108
+ expect { General::GOperations.capitalize(0) }.to raise_error TypeError
109
+ end
110
+ end
111
+ end
112
+
113
+ # Describe General::GOperations::lowercase
114
+ #
115
+ # Converts every letter in the string to lowercase
116
+ #
117
+ # Parameter: string - the string being lowercased
118
+ #
119
+ # Return: the lowercased string
120
+ describe '::lowercase' do
121
+ before :all do
122
+ @input = "Joe Schmoe"
123
+ @output = "joe schmoe"
124
+ end
125
+
126
+ context 'with string argument given' do
127
+ it 'converts the string to lowercase' do
128
+ expect(General::GOperations.lowercase @input).to eql @output
129
+ end
130
+ end
131
+
132
+ context 'with no string argument' do
133
+ it 'raises Error' do
134
+ expect { General::GOperations.lowercase }.to raise_error ArgumentError
135
+ end
136
+ end
137
+
138
+ context 'with argument of another type' do
139
+ it 'raises Error' do
140
+ expect { General::GOperations.capitalize(0) }.to raise_error TypeError
141
+ end
142
+ end
143
+ end
144
+
145
+ #-----------------------------------INTEGER OPERATIONS------------------------------------
146
+
147
+ # Describe General::GOperations::money
148
+ #
149
+ # Returns the integer monetary value formatted to the given money type
150
+ #
151
+ # Parameter: integer - the monetary amount being formatted
152
+ # Parameter: type - the type of money (defaults to USD)
153
+ #
154
+ # Return: the formatted money amount
155
+ describe '::money' do
156
+ before :all do
157
+ @money1 = 233
158
+ @money2 = -344
159
+ @output1 = "$2.33"
160
+ @output2 = "-$3.44"
161
+ @output3 = "€2.33"
162
+ @output4 = "-€3.44"
163
+ end
164
+
165
+ context 'with an integer value given' do
166
+ it 'formats the monetary value into USD' do
167
+ expect(General::GOperations.money @money1).to eql @output1
168
+ expect(General::GOperations.money @money2).to eql @output2
169
+ end
170
+ end
171
+
172
+ context 'with an integer value given and another string value given' do
173
+ context 'if string money value is supported' do
174
+ it 'formats the monetary value according the the money type' do
175
+ expect(General::GOperations.money @money1, "EUR").to eql @output3
176
+ expect(General::GOperations.money @money2, "EUR").to eql @output4
177
+ end
178
+ end
179
+
180
+ context 'if string money value is not supported' do
181
+ it 'raises TypeError' do
182
+ expect { General::GOperations.money(@money1, "RUE") }.to raise_error TypeError
183
+ end
184
+ end
185
+ end
186
+
187
+ context 'with no integer value given' do
188
+ it 'raises Error' do
189
+ expect { General::GOperations.money }.to raise_error ArgumentError
190
+ end
191
+ end
192
+
193
+ context 'with argument of another type' do
194
+ it 'raises Error' do
195
+ expect { General::GOperations.money "" }.to raise_error TypeError
196
+ end
197
+ end
198
+ end
199
+
200
+ # Describe General::GOperations::time
201
+ #
202
+ # Returns the integer time value (in seconds) formatted with the given formatter
203
+ #
204
+ # Parameter: integer - the integer being formatted (representing the time in seconds)
205
+ # Parameter: format - the format being used (defaults to @I:@MM:@SS @A)
206
+ #
207
+ # Return: the time formatted with the given formatter
208
+ describe '::time' do
209
+ before :all do
210
+ @formatter = '@SS <- @MM <- @HH'
211
+ @hrs = 3
212
+ @min = 14
213
+ @sec = 12
214
+ @pm = true
215
+
216
+ @time = (((@pm ? 11 : 0) + @hrs)*3600 + @min*60 + @sec)
217
+
218
+ @out1 = General::GTimeFormat.new(General::GOperations::DEFAULT_TIME).apply(@time)
219
+ @out2 = General::GTimeFormat.new(@formatter).apply(@time)
220
+ end
221
+
222
+ context 'with integer value given' do
223
+ it 'formats the given time value to the default time format' do
224
+ expect(General::GOperations.time(@time)).to eql @out1
225
+ end
226
+ end
227
+
228
+ context 'with integer value given and time format given' do
229
+ it 'formats the given time value to the given time format' do
230
+ expect(General::GOperations.time(@time, @formatter)).to eql @out2
231
+ end
232
+ end
233
+
234
+ context 'with no integer value given' do
235
+ it 'raises Error' do
236
+ expect { General::GOperations.time }.to raise_error ArgumentError
237
+ end
238
+ end
239
+
240
+ context 'with argument of another type' do
241
+ it 'raises Error' do
242
+ expect { General::GOperations.time "" }.to raise_error TypeError
243
+ end
244
+ end
245
+ end
246
+ end
@@ -0,0 +1,54 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "spec_require"
18
+
19
+ # Describe General::GPartial
20
+ #
21
+ # A unit of the GTemplate. Returns a string based on an argument hash
22
+ # When GTemplate is applied
23
+ #
24
+ # Author: Anshul Kharbanda
25
+ # Created: 7 - 29 - 2016
26
+ describe General::GPartial do
27
+ before :all do
28
+ @name = :partial_name
29
+ @partial = General::GPartial.new name: @name
30
+ end
31
+
32
+ # Describe General::GPartial::new
33
+ #
34
+ # Creates the GPartial with the given object
35
+ #
36
+ # Parameter: obj - the object containing information for the partial
37
+ describe '::new' do
38
+ it 'creates the GPartial wih the given object' do
39
+ expect(@partial).to be_an_instance_of General::GPartial
40
+ expect(@partial.instance_variable_get :@name).to eql @name
41
+ end
42
+ end
43
+
44
+ # Describe General::GPartial#name
45
+ #
46
+ # Returns the name of the partial
47
+ #
48
+ # Return: the name of the partial
49
+ describe '#name' do
50
+ it 'returns the name of the GPartial' do
51
+ expect(@partial.name).to eql @name
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,300 @@
1
+ # General is a templating system in ruby
2
+ # Copyright (C) 2016 Anshul Kharbanda
3
+ #
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+ #
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+ #
14
+ # You should have received a copy of the GNU General Public License
15
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
+
17
+ require_relative "spec_require"
18
+
19
+ # Represents a placeholder partial in a GTemplate
20
+ #
21
+ # Author: Anshul Kharbanda
22
+ # Created: 7 - 1 - 2016
23
+ describe General::GPlaceholder do
24
+ before :all do
25
+ # Placename
26
+ @placename1 = :plac1
27
+ @placename2 = :plac2
28
+ @placename3 = :plac3
29
+ @placename4 = :plac4
30
+ @placename5 = :"plac5.subarg"
31
+
32
+ # Hash
33
+ @hash = General::GDotHash.new
34
+ @hash[@placename1] = "foobar"
35
+ @hash[@placename3] = "barbaz"
36
+ @hash[@placename4] = "jujar"
37
+ @hash[@placename5] = "dudar"
38
+
39
+ # Default hash
40
+ @defaults = General::GDotHash.new
41
+
42
+ # ------------PLACEHOLDER------------
43
+ @result1 = @hash[@placename1]
44
+ @partial1 = General::GPlaceholder.new [
45
+ [:name, @placename1]
46
+ ].to_h, @defaults
47
+ @string1 = "@(#{@placename1})"
48
+ @string_first1 = "@(#{@placename1})"
49
+ @regex1 = "\\k<#{@placename1}>"
50
+ @regex_first1 = "(?<#{@placename1}>.*)"
51
+
52
+ # --------------DEFAULT--------------
53
+ @default2 = "foo2"
54
+ @result2 = "foo2"
55
+ @partial2 = General::GPlaceholder.new [
56
+ [:name, @placename2],
57
+ [:default, @default2]
58
+ ].to_h, @defaults
59
+ @string2 = "@(#{@placename2})"
60
+ @string_first2 = "@(#{@placename2}:" \
61
+ + " #{@default2})"
62
+ @regex2 = "\\k<#{@placename2}>"
63
+ @regex_first2 = "(?<#{@placename2}>.*)"
64
+
65
+ # -------------OPERATION-------------
66
+ @operation3 = "capitalize"
67
+ @result3 = General::GOperations.send(
68
+ @operation3.to_sym,
69
+ @hash[@placename3]
70
+ )
71
+ @partial3 = General::GPlaceholder.new [
72
+ [:name, @placename3],
73
+ [:operation, @operation3]
74
+ ].to_h, @defaults
75
+ @string3 = "@(#{@placename3})"
76
+ @string_first3 = "@(#{@placename3}" \
77
+ + " -> #{@operation3})"
78
+ @regex3 = "\\k<#{@placename3}>"
79
+ @regex_first3 = "(?<#{@placename3}>.*)"
80
+
81
+ # -------------ARGUMENTS-------------
82
+ @operation4 = "capitalize"
83
+ @arguments4 = ["all"]
84
+ @result4 = General::GOperations.send(
85
+ @operation4.to_sym,
86
+ @hash[@placename4],
87
+ *@arguments4
88
+ )
89
+ @partial4 = General::GPlaceholder.new [
90
+ [:name, @placename4],
91
+ [:operation, @operation4],
92
+ [:arguments, @arguments4.join(" ")]
93
+ ].to_h, @defaults
94
+ @string4 = "@(#{@placename4})"
95
+ @string_first4 = "@(#{@placename4}" \
96
+ + " -> #{@operation4} " \
97
+ + "#{@arguments4.collect {|s| "\"#{s}\""}.join " "})"
98
+ @regex4 = "\\k<#{@placename4}>"
99
+ @regex_first4 = "(?<#{@placename4}>.*)"
100
+
101
+ # -----------DOT NOTATION------------
102
+ keys = @placename5.to_s.split(".").collect(&:to_sym)
103
+ @result5 = @hash[keys[0]][keys[1]]
104
+ @partial5 = General::GPlaceholder.new [
105
+ [:name, @placename5]
106
+ ].to_h, @defaults
107
+ @string5 = "@(#{@placename5})"
108
+ @string_first5 = "@(#{@placename5})"
109
+ @regex5 = "\\k<#{@placename5}>"
110
+ @regex_first5 = "(?<#{@placename5}>.*)"
111
+ end
112
+
113
+ # Describe General::GPlaceholder::new
114
+ #
115
+ # Initializes the GPlaceholder with the given match
116
+ #
117
+ # Parameter: match - the match data from the string being parsed
118
+ describe '::new' do
119
+ # -------------------------------------PLACEHOLDER---------------------------------------
120
+ context 'with name given' do
121
+ it 'creaes a GPlaceholder with the given name' do
122
+ expect(@partial1).to be_an_instance_of General::GPlaceholder
123
+ expect(@partial1.instance_variable_get :@name).to eql @placename1
124
+ expect(@partial1.instance_variable_get :@operation).to be_nil
125
+ expect(@partial1.instance_variable_get :@arguments).to be_empty
126
+ end
127
+ end
128
+
129
+ # ---------------------------------------DEFAULT-----------------------------------------
130
+ context 'with name and default in match object' do
131
+ it 'creates a GPlaceholder with the given name and default' do
132
+ expect(@partial2).to be_an_instance_of General::GPlaceholder
133
+ expect(@partial2.instance_variable_get :@name).to eql @placename2
134
+ expect(@partial2.instance_variable_get :@operation).to be_nil
135
+ expect(@partial2.instance_variable_get :@arguments).to be_empty
136
+ expect(@defaults[@placename2]).to eql @default2
137
+ end
138
+ end
139
+
140
+ # --------------------------------------OPERATION----------------------------------------
141
+ context 'with name, default, and operation in match object' do
142
+ it 'creates a GPlaceholder with the given name and operation' do
143
+ expect(@partial3).to be_an_instance_of General::GPlaceholder
144
+ expect(@partial3.instance_variable_get :@name).to eql @placename3
145
+ expect(@partial3.instance_variable_get :@operation).to eql @operation3
146
+ expect(@partial3.instance_variable_get :@arguments).to be_empty
147
+ end
148
+ end
149
+
150
+ # --------------------------------------ARGUMENTS----------------------------------------
151
+ context 'with name, default, operation, and arguments in match object' do
152
+ it 'creates a GPlaceholder with the given name, operation, and arguments' do
153
+ expect(@partial4).to be_an_instance_of General::GPlaceholder
154
+ expect(@partial4.instance_variable_get :@name).to eql @placename4
155
+ expect(@partial4.instance_variable_get :@operation).to eql @operation4
156
+ expect(@partial4.instance_variable_get :@arguments).to eql @arguments4
157
+ end
158
+ end
159
+
160
+ # --------------------------------------ARGUMENTS----------------------------------------
161
+ context 'with dot notation name' do
162
+ it 'creates a GPlaceholder with the given dot notation name' do
163
+ expect(@partial5).to be_an_instance_of General::GPlaceholder
164
+ expect(@partial5.instance_variable_get :@name).to eql @placename5
165
+ expect(@partial5.instance_variable_get :@operation).to be_nil
166
+ expect(@partial5.instance_variable_get :@arguments).to be_empty
167
+ end
168
+ end
169
+ end
170
+
171
+ # Describe General::GPlaceholder#apply
172
+ #
173
+ # Returns the value of the placeholder in the given data
174
+ # with the given operation performed on it
175
+ #
176
+ # Parameter: data - the data being applied
177
+ #
178
+ # Return: the value of the placeholder in the given data
179
+ # with the given operation performed on it
180
+ describe '#apply' do
181
+ # -------------------------------------PLACEHOLDER---------------------------------------
182
+ context 'when name is defined in given data' do
183
+ it 'returns the value in the data at the name' do
184
+ expect(@partial1.apply @hash).to eql @result1
185
+ end
186
+ end
187
+
188
+ # ---------------------------------------DEFAULT-----------------------------------------
189
+ context 'when name is not defined in given data, but defined in defaults' do
190
+ it 'returns the value in the default at the name' do
191
+ expect(@partial2.apply @hash).to eql @result2
192
+ end
193
+ end
194
+
195
+ # --------------------------------------OPERATION----------------------------------------
196
+ context 'when an operation is defined' do
197
+ it 'returns the value in the data/default at the name with the operation applied' do
198
+ expect(@partial3.apply @hash).to eql @result3
199
+ end
200
+ end
201
+
202
+ # --------------------------------------ARGUMENTS----------------------------------------
203
+ context 'when an operation and arguments are defined' do
204
+ it 'returns the value in the data/default at the name with the operation and arguments applied' do
205
+ expect(@partial4.apply @hash).to eql @result4
206
+ end
207
+ end
208
+
209
+ # -------------------------------------DOT NOTATION--------------------------------------
210
+ context 'when dot notation name is given' do
211
+ it 'returns the value in the data/default at the dot notation name' do
212
+ expect(@partial5.apply @hash).to eql @result5
213
+ end
214
+ end
215
+ end
216
+
217
+ # Describe General::GTemplate#string
218
+ #
219
+ # Returns the string representation of the placeholder
220
+ #
221
+ # Parameter: first - true if the placeholder is the first of its kind in the GTemplate
222
+ #
223
+ # Return: the string representation of the placeholder
224
+ describe '#string' do
225
+ # -------------------------------------NO FIRST---------------------------------------
226
+ context 'when no first argument specified' do
227
+ it 'returns the first string representation of the placeholder' do
228
+ expect(@partial1.string).to eql @string_first1
229
+ expect(@partial2.string).to eql @string_first2
230
+ expect(@partial3.string).to eql @string_first3
231
+ expect(@partial4.string).to eql @string_first4
232
+ end
233
+ end
234
+
235
+ # --------------------------------------FIRST-----------------------------------------
236
+ context 'when first argument is specified' do
237
+ # --------------------------------TRUE--------------------------------
238
+ context 'when first is true' do
239
+ it 'returns the first string representation of the placeholder' do
240
+ expect(@partial1.string true).to eql @string_first1
241
+ expect(@partial2.string true).to eql @string_first2
242
+ expect(@partial3.string true).to eql @string_first3
243
+ expect(@partial4.string true).to eql @string_first4
244
+ end
245
+ end
246
+
247
+ # --------------------------------FALSE-------------------------------
248
+ context 'when first is false' do
249
+ it 'returns the string representation of the placeholder' do
250
+ expect(@partial1.string false).to eql @string1
251
+ expect(@partial2.string false).to eql @string2
252
+ expect(@partial3.string false).to eql @string3
253
+ expect(@partial4.string false).to eql @string4
254
+ end
255
+ end
256
+ end
257
+ end
258
+
259
+ # Describe General::GTemplate#regex
260
+ #
261
+ # Returns the string as a regex
262
+ #
263
+ # Parameter: first - true if the placeholder is the first of its kind in the GTemplate
264
+ #
265
+ # Returns: the string as a regex
266
+ describe '#regex' do
267
+ # -------------------------------------NO FIRST---------------------------------------
268
+ context 'when no first argument specified' do
269
+ it 'returns the first regex representation of the placeholder' do
270
+ expect(@partial1.regex).to eql @regex_first1
271
+ expect(@partial2.regex).to eql @regex_first2
272
+ expect(@partial3.regex).to eql @regex_first3
273
+ expect(@partial4.regex).to eql @regex_first4
274
+ end
275
+ end
276
+
277
+ # --------------------------------------FIRST-----------------------------------------
278
+ context 'when first argument is specified' do
279
+ # --------------------------------TRUE--------------------------------
280
+ context 'when first is true' do
281
+ it 'returns the first regex representation of the placeholder' do
282
+ expect(@partial1.regex true).to eql @regex_first1
283
+ expect(@partial2.regex true).to eql @regex_first2
284
+ expect(@partial3.regex true).to eql @regex_first3
285
+ expect(@partial4.regex true).to eql @regex_first4
286
+ end
287
+ end
288
+
289
+ # --------------------------------FALSE-------------------------------
290
+ context 'when first is false' do
291
+ it 'returns the regex representation of the placeholder' do
292
+ expect(@partial1.regex false).to eql @regex1
293
+ expect(@partial2.regex false).to eql @regex2
294
+ expect(@partial3.regex false).to eql @regex3
295
+ expect(@partial4.regex false).to eql @regex4
296
+ end
297
+ end
298
+ end
299
+ end
300
+ end