general 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +166 -20
  3. data/Rakefile +11 -2
  4. data/exp/expected/applied1.txt +1 -0
  5. data/exp/expected/applied2.txt +3 -0
  6. data/exp/expected/applied3.txt +9 -0
  7. data/exp/expected/default1.txt +1 -0
  8. data/exp/expected/default2.txt +3 -0
  9. data/exp/expected/default3.txt +9 -0
  10. data/exp/out/sample1.txt +1 -0
  11. data/exp/out/sample2.txt +3 -0
  12. data/exp/out/sample3.txt +9 -0
  13. data/exp/templates/sample0.general +7 -0
  14. data/exp/templates/sample1.general +1 -0
  15. data/exp/templates/sample2.general +3 -0
  16. data/exp/templates/sample3.general +4 -0
  17. data/exp/templates/sampleE0.general +7 -0
  18. data/exp/templates/sampleE1.general +3 -0
  19. data/exp/templates/sampleE2.general +3 -0
  20. data/exp/templates/sampleE3.general +7 -0
  21. data/lib/general.rb +3 -3
  22. data/lib/gerror.rb +33 -0
  23. data/lib/goperations.rb +95 -27
  24. data/lib/gpartials/garrayplaceholder.rb +87 -0
  25. data/lib/gpartials/gfullplaceholder.rb +63 -0
  26. data/lib/gpartials/gpartial.rb +11 -1
  27. data/lib/gpartials/gplaceholder.rb +1 -52
  28. data/lib/gpartials/gspecial.rb +81 -0
  29. data/lib/gpartials/gtext.rb +5 -33
  30. data/lib/gprepartials/gextend.rb +42 -0
  31. data/lib/gprepartials/ginclude.rb +47 -0
  32. data/lib/gprepartials/gprepartial.rb +45 -0
  33. data/lib/gprepartials/gpretext.rb +33 -0
  34. data/lib/gprepartials/gyield.rb +33 -0
  35. data/lib/{templates → gtemplates}/gbasetemplate.rb +46 -3
  36. data/lib/gtemplates/gio.rb +143 -0
  37. data/lib/gtemplates/gmeta.rb +106 -0
  38. data/lib/{templates → gtemplates}/gtemplate.rb +10 -30
  39. data/lib/{templates → gtemplates}/gtimeformat.rb +6 -12
  40. data/spec/gdothash_spec.rb +6 -10
  41. data/spec/goperations_spec.rb +112 -82
  42. data/spec/gpartial_garrayplaceholder_spec.rb +165 -0
  43. data/spec/gpartial_gfullplaceholder_spec.rb +82 -0
  44. data/spec/gpartial_gplaceholder_spec.rb +237 -0
  45. data/spec/gpartial_gspecial_spec.rb +88 -0
  46. data/spec/gpartial_gtext_spec.rb +87 -0
  47. data/spec/gtamplate_gtemplate_spec.rb +266 -0
  48. data/spec/gtemplate_gio_spec.rb +147 -0
  49. data/spec/gtemplate_gtimeformat_spec.rb +77 -0
  50. data/spec/spec_require.rb +7 -4
  51. metadata +41 -13
  52. data/exp/future.general +0 -11
  53. data/exp/out.txt +0 -1
  54. data/exp/sample.general +0 -1
  55. data/lib/templates/gio.rb +0 -68
  56. data/spec/garrayplaceholder_spec.rb +0 -163
  57. data/spec/gplaceholder_spec.rb +0 -300
  58. data/spec/gtemplates_spec.rb +0 -480
  59. data/spec/gtext_spec.rb +0 -150
@@ -0,0 +1,143 @@
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 "gtemplate"
18
+ require_relative "gmeta"
19
+ require_relative "../gerror"
20
+ require_relative "../gprepartials/gpretext"
21
+ require_relative "../gprepartials/ginclude"
22
+ require_relative "../gprepartials/gextend"
23
+ require_relative "../gprepartials/gyield"
24
+
25
+ # General is a templating system in ruby
26
+ #
27
+ # Author: Anshul Kharbanda
28
+ # Created: 3 - 4 - 2016
29
+ module General
30
+ # Implements the general IO writer template
31
+ #
32
+ # Author: Anshul Kharbanda
33
+ # Created: 3 - 4 - 2016
34
+ class GIO < GTemplate
35
+ # The general file extension
36
+ EXTENSION = ".general"
37
+
38
+ # Public read source
39
+ attr :source
40
+
41
+ # Loads a GIO from a file with the given path
42
+ #
43
+ # Parameter: path - the path of the file to load
44
+ #
45
+ # Return: GIO loaded from the file
46
+ def self.load path
47
+ # Get current working directory
48
+ cwd = Dir.getwd
49
+
50
+ # Change to path of file
51
+ Dir.chdir File.dirname(path)
52
+
53
+ # Read raw text
54
+ string = IO.read File.basename(path)
55
+
56
+ # Prepartial types
57
+ ptypes = [
58
+ General::GExtend,
59
+ General::GInclude,
60
+ General::GYield,
61
+ General::GPretext
62
+ ]
63
+
64
+ # Breakdown algorithm
65
+ preparts = []
66
+ m = nil
67
+ while string.length > 0
68
+ # Match the front of the string to a preprocessor
69
+ prepart = ptypes.find { |ptype| m = ptype::REGEX.match(string) }
70
+
71
+ # Raise error if no matching prepart can be found
72
+ if prepart.nil?
73
+ Dir.chdir cwd # Make sure to change back to current directory
74
+ raise GError.new "Unmatched prepartial at #{(string.length <= 5 ? string : string[0..5] + "...").inspect}"
75
+ end
76
+
77
+ # Add the partial to the array
78
+ preparts << prepart.new(m)
79
+
80
+ # Trim the front of the string
81
+ string = string[m.end(0)..-1]
82
+ end
83
+
84
+ # Find an extend
85
+ extindex = preparts.index{ |prepart| prepart.is_a? General::GExtend }
86
+
87
+ # Run extend algorithm (throw error if extend is found elsewhere)
88
+ if extindex == 0
89
+ begin
90
+ preparts = GMeta.load(preparts[extindex].filename+General::GIO::EXTENSION).gextend(preparts[1..-1])
91
+ rescue GError => e
92
+ Dir.chdir cwd # Make sure to change back to current directory
93
+ raise e
94
+ end
95
+ elsif !extindex.nil?
96
+ Dir.chdir cwd # Make sure to change back to current directory
97
+ raise GError.new "@@extend prepartial needs to be at beginning of template."
98
+ end
99
+
100
+ # Find a yield
101
+ yindex = preparts.index{ |prepart| prepart.is_a? General::GYield }
102
+
103
+ # Raise error if yield is found
104
+ unless yindex.nil?
105
+ Dir.chdir cwd # Make sure to change back to current directory
106
+ raise GError.new "#{path} is a meta template and cannot be parsed. Must be extended by other GIO template"
107
+ end
108
+
109
+ # Combine text
110
+ text = preparts.collect{ |prepart| prepart.apply }.join
111
+
112
+ # Change to current directory
113
+ Dir.chdir cwd
114
+
115
+ # Return new GIO
116
+ return self.new text, path
117
+ end
118
+
119
+ # Creates a GIO with the given template string and source filename
120
+ #
121
+ # Parameter: string - the string being converted to a template
122
+ # Parameter: source - the name of the source file of the template
123
+ def initialize string, source=nil
124
+ super string
125
+ @source = source
126
+ end
127
+
128
+ # Writes the template with the given data applied to the target stream
129
+ #
130
+ # Parameter: ios - if String, is the name of the file to write to
131
+ # if IO, is the stream to write to
132
+ # Parameter: data - the data to be applied (merges with defaults)
133
+ def write ios, data={}
134
+ if ios.is_a? String
135
+ IO.write ios, apply(data)
136
+ elsif ios.is_a? IO
137
+ ios.write apply(data)
138
+ else
139
+ raise TypeError.new "Expected IO or String, got: #{ios.class}"
140
+ end
141
+ end
142
+ end
143
+ end
@@ -0,0 +1,106 @@
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 "gtemplate"
18
+
19
+ # General is a templating system in ruby
20
+ #
21
+ # Author: Anshul Kharbanda
22
+ # Created: 3 - 4 - 2016
23
+ module General
24
+ # Represents an abstract meta file
25
+ #
26
+ # Author: Anshul Kharbanda
27
+ # Created: 1 - 31 - 2016
28
+ class GMeta < GTemplate
29
+ def self.load filename
30
+ # Read file
31
+ string = IO.read filename
32
+
33
+ # Prepartial types
34
+ ptypes = [
35
+ General::GExtend,
36
+ General::GInclude,
37
+ General::GYield,
38
+ General::GPretext
39
+ ]
40
+
41
+ # Breakdown algorithm
42
+ preparts = []
43
+ m = nil
44
+ loop do
45
+ # Match the front of the string to a preprocessor
46
+ prepart = ptypes.find { |ptype| m = ptype::REGEX.match(string) }
47
+
48
+ # Raise error if no matching prepart can be found
49
+ raise GError.new("Unmatched prepartial at #{(string.length <= 5 ? string : string[0..5] + "...").inspect}") if prepart.nil?
50
+
51
+ # Add the partial to the array
52
+ preparts << prepart.new(m)
53
+
54
+ # Trim the front of the string
55
+ string = string[m.end(0)..-1]
56
+
57
+ # End when string is empty
58
+ break if string.length == 0
59
+ end
60
+
61
+ # Find an extend
62
+ extindex = preparts.index{ |prepart| prepart.is_a? General::GExtend }
63
+
64
+ # Run extend algorithm (throw error if extend is found elsewhere)
65
+ if extindex == 0
66
+ GMeta.load(extindex.filename).gextend(preparts)
67
+ elsif !extindex.nil? && extindex > 0
68
+ raise GError.new "@@extend prepartial needs to be at beginning of template."
69
+ end
70
+
71
+ # Check for multiple yields
72
+ yields = preparts.find_all{ |prepart| prepart.is_a? General::GYield }
73
+ raise GError.new "@@yield prepartial can only appear ONCE!" if yields.length > 1
74
+
75
+ # Find the yield
76
+ yindex = preparts.index{ |prepart| prepart.is_a? General::GYield }
77
+
78
+ # Return a new meta file if yield is found. Else default is end.
79
+ unless yindex.nil?
80
+ return self.new preparts[0...yindex], preparts[(yindex + 1)..-1], filename
81
+ else
82
+ return self.new preparts, [], filename
83
+ end
84
+ end
85
+
86
+ # Creates a new GMeta
87
+ #
88
+ # Parameter: top - the top end of the meta (in preparts)
89
+ # Parameter: bottom - the bottom end of the data (in preparts)
90
+ # Parameter: source - the source of the file
91
+ def initialize top, bottom, source=nil
92
+ @top = top
93
+ @bottom = bottom
94
+ @source = source
95
+ end
96
+
97
+ # Extends the given preparts from the GMeta
98
+ #
99
+ # Parameter: preparts - the preparts array to extend
100
+ #
101
+ # Returns: the extended preparts
102
+ def gextend preparts
103
+ return @top + preparts + [General::GPretext.new("\r\n")] + @bottom
104
+ end
105
+ end
106
+ end
@@ -16,7 +16,10 @@
16
16
 
17
17
  require_relative "gbasetemplate"
18
18
  require_relative "../gpartials/gtext"
19
+ require_relative "../gpartials/gspecial"
19
20
  require_relative "../gpartials/gplaceholder"
21
+ require_relative "../gpartials/garrayplaceholder"
22
+ require_relative "../gpartials/gfullplaceholder"
20
23
 
21
24
  # General is a templating system in ruby
22
25
  #
@@ -32,36 +35,13 @@ module General
32
35
  #
33
36
  # Parameter: string - the string being converted to a template
34
37
  def initialize string
35
- super(string)
36
- @defaults = General::GDotHash.new
37
-
38
- # The string gets split into partials by placeholder and array template
39
- loop do
40
- if match = General::GText::REGEX.match(string)
41
- @partials << General::GText.new(match)
42
- elsif match = General::GSpecial::REGEX.match(string)
43
- @partials << General::GSpecial.new(match)
44
- elsif match = General::GArrayPlaceholder::REGEX.match(string)
45
- @partials << General::GArrayPlaceholder.new(match)
46
- elsif match = General::GPlaceholder::REGEX.match(string)
47
- @partials << General::GPlaceholder.new(match, @defaults)
48
- else
49
- return
50
- end
51
- string = string[match.end(0)..-1]
52
- end
53
- end
54
-
55
- # Returns a string representation of the string
56
- #
57
- # Return: a string representation of the string
58
- def to_s
59
- first = Hash.new(true); str = ""
60
- @partials.each do |part|
61
- str += part.string(first[part.name])
62
- first[part.name] &&= false
63
- end
64
- return str
38
+ super(string, [
39
+ General::GText,
40
+ General::GSpecial,
41
+ General::GArrayPlaceholder,
42
+ General::GPlaceholder,
43
+ General::GFullPlaceholder
44
+ ])
65
45
  end
66
46
 
67
47
  # Returns the string as a regex
@@ -16,6 +16,7 @@
16
16
 
17
17
  require_relative "gbasetemplate"
18
18
  require_relative "../gpartials/gtext"
19
+ require_relative "../gpartials/gspecial"
19
20
  require_relative "../gpartials/gtimeformatplaceholder"
20
21
 
21
22
  # General is a templating system in ruby
@@ -32,18 +33,11 @@ module General
32
33
  #
33
34
  # Parameter: string - the template string
34
35
  def initialize string
35
- super string
36
-
37
- loop do
38
- if match = General::GText::REGEX.match(string)
39
- @partials << General::GText.new(match)
40
- elsif match = General::GTimeFormatPlaceholder::REGEX.match(string)
41
- @partials << General::GTimeFormatPlaceholder.new(match)
42
- else
43
- return
44
- end
45
- string = string[match.end(0)..-1]
46
- end
36
+ super(string, [
37
+ General::GSpecial,
38
+ General::GText,
39
+ General::GTimeFormatPlaceholder
40
+ ])
47
41
  end
48
42
 
49
43
  # Applies the given integer value to the template and returns the generated string
@@ -70,18 +70,14 @@ describe General::GDotHash do
70
70
  # Parameter: key - the key to set
71
71
  # Parameter: value - the value to set
72
72
  describe '#[]=' do
73
- context 'if given position exists' do
74
- it 'sets the position addressed by the given dot notation key to the given value' do
75
- expect { @gdothash[@key1] = @newvalue1 }.not_to raise_error
76
- expect(@gdothash[@key1]).to eql @newvalue1
77
- end
73
+ it 'sets the position addressed by the given dot notation key (if it exists) to the given value' do
74
+ expect { @gdothash[@key1] = @newvalue1 }.not_to raise_error
75
+ expect(@gdothash[@key1]).to eql @newvalue1
78
76
  end
79
77
 
80
- context 'if given position does not exist' do
81
- it 'creates position addressed by the given dot notation key in the given hash and sets it to the given value' do
82
- expect { @gdothash[@key2] = @newvalue2 }.not_to raise_error
83
- expect(@gdothash[@key2]).to eql @newvalue2
84
- end
78
+ it 'creates position addressed by the given dot notation key (if it does not exist) and sets it to the given value' do
79
+ expect { @gdothash[@key2] = @newvalue2 }.not_to raise_error
80
+ expect(@gdothash[@key2]).to eql @newvalue2
85
81
  end
86
82
  end
87
83
  end
@@ -39,41 +39,31 @@ describe General::GOperations do
39
39
  @out_all = "Joe Schmoe"
40
40
  end
41
41
 
42
- context 'with string argument given' do
42
+ context 'with no second argument given' do
43
43
  it 'capitalizes the first letter in the string' do
44
44
  expect(General::GOperations.capitalize(@input)).to eql @out_first
45
45
  end
46
- end
47
46
 
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
47
+ it 'raises GOperationError with no string argument' do
48
+ expect { General::GOperations.capitalize }.to raise_error General::GOperationError
53
49
  end
54
50
 
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
51
+ it 'raises GOperationError with argument of another type' do
52
+ expect { General::GOperations.capitalize(0) }.to raise_error General::GOperationError
59
53
  end
54
+ end
60
55
 
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
56
+ context 'with string argument and second string argument is given' do
57
+ it 'capitalizes the first letter in the string when the second argument is "first"' do
58
+ expect(General::GOperations.capitalize @input, "first").to eql @out_first
65
59
  end
66
- end
67
60
 
68
- context 'with no string argument' do
69
- it 'raises Error' do
70
- expect { General::GOperations.capitalize }.to raise_error ArgumentError
61
+ it 'capitalizes all words in the string when the second argument is "all"' do
62
+ expect(General::GOperations.capitalize @input, "all").to eql @out_all
71
63
  end
72
- end
73
64
 
74
- context 'with argument of another type' do
75
- it 'raises Error' do
76
- expect { General::GOperations.capitalize(0) }.to raise_error TypeError
65
+ it 'raises GOperationError when the second argument is neither "first" or "all"' do
66
+ expect{General::GOperations.capitalize @input, "foo"}.to raise_error General::GOperationError
77
67
  end
78
68
  end
79
69
  end
@@ -91,22 +81,16 @@ describe General::GOperations do
91
81
  @output = "JOE SCHMOE"
92
82
  end
93
83
 
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
84
+ it 'converts the string to uppercase' do
85
+ expect(General::GOperations.uppercase @input).to eql @output
98
86
  end
99
87
 
100
- context 'with no string argument' do
101
- it 'raises Error' do
102
- expect { General::GOperations.uppercase }.to raise_error ArgumentError
103
- end
88
+ it 'raises GOperationError with no string argument' do
89
+ expect { General::GOperations.uppercase }.to raise_error General::GOperationError
104
90
  end
105
91
 
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
92
+ it 'raises GOperationError with argument of another type' do
93
+ expect { General::GOperations.capitalize(0) }.to raise_error General::GOperationError
110
94
  end
111
95
  end
112
96
 
@@ -123,22 +107,16 @@ describe General::GOperations do
123
107
  @output = "joe schmoe"
124
108
  end
125
109
 
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
110
+ it 'converts the string to lowercase' do
111
+ expect(General::GOperations.lowercase @input).to eql @output
130
112
  end
131
113
 
132
- context 'with no string argument' do
133
- it 'raises Error' do
134
- expect { General::GOperations.lowercase }.to raise_error ArgumentError
135
- end
114
+ it 'raises GOperationError with no string argument' do
115
+ expect { General::GOperations.lowercase }.to raise_error General::GOperationError
136
116
  end
137
117
 
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
118
+ it 'raises GOperationError with argument of another type' do
119
+ expect { General::GOperations.capitalize(0) }.to raise_error General::GOperationError
142
120
  end
143
121
  end
144
122
 
@@ -162,37 +140,29 @@ describe General::GOperations do
162
140
  @output4 = "-€3.44"
163
141
  end
164
142
 
165
- context 'with an integer value given' do
143
+ context 'with no second string value given' do
166
144
  it 'formats the monetary value into USD' do
167
145
  expect(General::GOperations.money @money1).to eql @output1
168
146
  expect(General::GOperations.money @money2).to eql @output2
169
147
  end
170
- end
171
148
 
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
149
+ it 'raises GOperationError with no integer value given' do
150
+ expect { General::GOperations.money }.to raise_error General::GOperationError
178
151
  end
179
152
 
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
153
+ it 'raises GOperationError with argument of another type' do
154
+ expect { General::GOperations.money "" }.to raise_error General::GOperationError
184
155
  end
185
156
  end
186
157
 
187
- context 'with no integer value given' do
188
- it 'raises Error' do
189
- expect { General::GOperations.money }.to raise_error ArgumentError
158
+ context 'with an integer value given and another string value given' do
159
+ it 'formats the monetary value according to the money type' do
160
+ expect(General::GOperations.money @money1, "EUR").to eql @output3
161
+ expect(General::GOperations.money @money2, "EUR").to eql @output4
190
162
  end
191
- end
192
163
 
193
- context 'with argument of another type' do
194
- it 'raises Error' do
195
- expect { General::GOperations.money "" }.to raise_error TypeError
164
+ it 'raises GOperationError if the money type is not supported' do
165
+ expect { General::GOperations.money(@money1, "RUE") }.to raise_error General::GOperationError
196
166
  end
197
167
  end
198
168
  end
@@ -219,28 +189,88 @@ describe General::GOperations do
219
189
  @out2 = General::GTimeFormat.new(@formatter).apply(@time)
220
190
  end
221
191
 
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
192
+ it 'formats the given time value to the default time format' do
193
+ expect(General::GOperations.time(@time)).to eql @out1
226
194
  end
227
195
 
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
196
+ it 'formats the given time value to the given time format' do
197
+ expect(General::GOperations.time(@time, @formatter)).to eql @out2
232
198
  end
233
199
 
234
- context 'with no integer value given' do
235
- it 'raises Error' do
236
- expect { General::GOperations.time }.to raise_error ArgumentError
237
- end
200
+ it 'raises GOperationError with argument of another type' do
201
+ expect { General::GOperations.time "" }.to raise_error General::GOperationError
238
202
  end
203
+ end
239
204
 
240
- context 'with argument of another type' do
241
- it 'raises Error' do
242
- expect { General::GOperations.time "" }.to raise_error TypeError
243
- end
205
+ #----------------------------------TO ARRAY OPERATIONS------------------------------------
206
+
207
+ # Splits the string by the given delimeter (or by newline if no delimeter is given)
208
+ #
209
+ # Parameter: string - the string to split
210
+ # Parameter: delimeter - the delimeter to split by (defaults to newline)
211
+ #
212
+ # Return: the array containing the split string chunks
213
+ describe '::split' do
214
+ before(:all) do
215
+ @string1 = "Hello World\nI am dog"
216
+ @array1 = ["Hello World", "I am dog"]
217
+
218
+ @string2 = "Butter, Milk, Eggs, Bread"
219
+ @delim2 = ",\s*"
220
+ @array2 = ["Butter", "Milk", "Eggs", "Bread"]
221
+
222
+ @string4 = 3
223
+ end
224
+
225
+ it 'splits the string by whitespace if no delimeter is given' do
226
+ expect(General::GOperations.split(@string1)).to eql @array1
227
+ end
228
+
229
+ it 'splits the string using the given delimeter' do
230
+ expect(General::GOperations.split(@string2, @delim2)).to eql @array2
231
+ end
232
+
233
+ it 'raises GOperationError with no string value given' do
234
+ expect { General::GOperations.split }.to raise_error General::GOperationError
235
+ end
236
+
237
+ it 'raises GOperationError with value of other type given' do
238
+ expect { General::GOperations.split @string4 }.to raise_error General::GOperationError
239
+ end
240
+ end
241
+
242
+ # Splits a sequence by a set number of words (defaults to 10)
243
+ #
244
+ # Parameter: string - the string to split
245
+ # Parameter: words - the number of words to split by (defaults to 10)
246
+ #
247
+ # Return: an array containing hashes representing the chunks of the string split by words
248
+ describe '::splitwords' do
249
+ before(:all) do
250
+ @string1 = "The number of words in this string is greater than ten!"
251
+ @array1 = ["The number of words in this string is greater than", "ten!"]
252
+
253
+ @string2 = "The number of words (in this string) is greater than five, so every \"fifth\" word is on Joe's new line!"
254
+ @words2 = 5
255
+ @array2 = ["The number of words (in", "this string) is greater than", "five, so every \"fifth\" word", "is on Joe's new line!"]
256
+
257
+ @string3 = 4
258
+ end
259
+
260
+ it 'splits the string by ten words if no word count is given' do
261
+ expect(General::GOperations.splitwords @string1).to eql @array1
262
+ end
263
+
264
+ it 'splits the string by the given number of words' do
265
+ expect(General::GOperations.splitwords @string2, @words2).to eql @array2
266
+ end
267
+
268
+ it 'raises GOperationError with no string value given' do
269
+ expect { General::GOperations.splitwords }.to raise_error General::GOperationError
270
+ end
271
+
272
+ it 'raises GOperationError with value of other type given' do
273
+ expect { General::GOperations.splitwords @string3 }.to raise_error General::GOperationError
244
274
  end
245
275
  end
246
276
  end