linebreak 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ # TODO: If user-choices patch gets accepted, use :upcase for environment variables
19
+
20
+ require 'lib/aef/linebreak' rescue LoadError require 'aef/linebreak'
21
+
22
+ class Aef::Linebreak::EncodeCommand < UserChoices::Command
23
+ include UserChoices
24
+
25
+ # Prepare configuration sources
26
+ def add_sources(builder)
27
+ builder.add_source(
28
+ CommandLineSource, :usage,
29
+ "Usage: #$PROGRAM_NAME encode [OPTIONS] [INPUT] [OUTPUT]\n\n",
30
+ "Convert all linebreak encodings of a file.\n",
31
+ "Use the environment variable LINEBREAK_SYSTEM to specify a default output encoding.\n"
32
+ )
33
+
34
+ builder.add_source(EnvironmentSource, :mapping,
35
+ :system => 'LINEBREAK_SYSTEM'
36
+ )
37
+ end
38
+
39
+ # Define configuration options
40
+ def add_choices(builder)
41
+ systems = Aef::Linebreak::BREAK_BY_SYSTEM.keys.map{|key| key.to_s}
42
+ builder.add_choice(:system, :default => 'unix', :type => systems) do |cli|
43
+ cli.uses_option('-s', '--system SYSTEM',
44
+ "Output encoding system. Possible settings: #{systems.join(', ')}")
45
+ end
46
+
47
+ builder.add_choice(:files, :length => 0..2, :type => :pathname) {|cli| cli.uses_arglist}
48
+ end
49
+
50
+ # Manual option post processing
51
+ def postprocess_user_choices
52
+ if @user_choices[:files][0] and @user_choices[:files][0] != Pathname('-')
53
+ @user_choices[:input] = @user_choices[:files][0].open('r')
54
+ else
55
+ @user_choices[:input] = STDIN
56
+ end
57
+
58
+ if @user_choices[:files][1] and @user_choices[:files][1] != Pathname('-')
59
+ @user_choices[:output] = @user_choices[:files][1].open('w')
60
+ else
61
+ @user_choices[:output] = STDOUT
62
+ end
63
+ end
64
+
65
+ # Main program
66
+ def execute
67
+ @user_choices[:input].each_line do |line|
68
+ @user_choices[:output] << Aef::Linebreak.encode(line, @user_choices[:system].to_sym)
69
+ end
70
+
71
+ [:input, :output].each {|stream| @user_choices[stream].close }
72
+ end
73
+ end
@@ -0,0 +1,88 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify it under the
6
+ # terms of the GNU General Public License as published by the Free Software
7
+ # Foundation, either version 3 of the License, or (at your option) any later
8
+ # version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT
11
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
+ # details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along with
16
+ # this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'lib/aef/linebreak' rescue LoadError require 'aef/linebreak'
19
+
20
+ class Aef::Linebreak::EncodingsCommand < UserChoices::Command
21
+ include UserChoices
22
+
23
+ # Prepare configuration sources
24
+ def add_sources(builder)
25
+ builder.add_source(CommandLineSource, :usage,
26
+ "Usage: #$PROGRAM_NAME encodings [OPTIONS] [FILE]\n\n",
27
+ "Detect linebreak encoding systems of a file.\n"
28
+ )
29
+ end
30
+
31
+ # Define configuration options
32
+ def add_choices(builder)
33
+ builder.add_choice(:ensure, :type => [:string]) do |cli|
34
+ cli.uses_option('-e', '--ensure SYSTEMLIST',
35
+ "Verify that given encodings are existent. Separated by commas without spaces. Possible settings: #{Aef::Linebreak::BREAK_BY_SYSTEM.keys.join(', ')}.")
36
+ end
37
+
38
+ builder.add_choice(:files, :type => :pathname) {|cli| cli.uses_arglist}
39
+ end
40
+
41
+ # Manual option post processing
42
+ def postprocess_user_choices
43
+ if @user_choices[:ensure]
44
+ @user_choices[:ensure] = @user_choices[:ensure].map{|string| string.to_sym }.to_set
45
+ end
46
+
47
+ if @user_choices[:files].empty? or @user_choices[:files].include?(Pathname('-'))
48
+ @user_choices[:files] = [STDIN]
49
+ end
50
+ end
51
+
52
+ # Main program
53
+ def execute
54
+ systems_by_file = {}
55
+
56
+ @user_choices[:files].each do |file|
57
+ unless systems_by_file[file]
58
+ systems_by_file[file] = Set.new
59
+
60
+ file.each_line do |line|
61
+ systems_by_file[file] = Aef::Linebreak.encodings(line) + systems_by_file[file]
62
+ end
63
+ end
64
+ end
65
+
66
+ failed = false
67
+
68
+ systems_by_file.each do |file, systems|
69
+ print "#{file}: " unless file == STDIN
70
+
71
+ sorted_systems = systems.sort do |a,b|
72
+ order = [:unix, :windows, :mac]
73
+ order.index(a) <=> order.index(b)
74
+ end
75
+
76
+ print sorted_systems.join(',')
77
+
78
+ if @user_choices[:ensure] and systems != @user_choices[:ensure]
79
+ failed = true
80
+ puts ' (failed)'
81
+ else
82
+ puts
83
+ end
84
+ end
85
+
86
+ exit false if failed
87
+ end
88
+ end
@@ -0,0 +1,38 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify it under the
6
+ # terms of the GNU General Public License as published by the Free Software
7
+ # Foundation, either version 3 of the License, or (at your option) any later
8
+ # version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT
11
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
+ # details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along with
16
+ # this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'lib/aef/linebreak' rescue LoadError require 'aef/linebreak'
19
+
20
+ class Aef::Linebreak::VersionCommand
21
+ def execute
22
+ name = 'Linebreak'
23
+ puts "#{name} #{Aef::Linebreak::VERSION}"
24
+ puts
25
+ puts 'Project: https://rubyforge.org/projects/aef/'
26
+ puts "RDoc: http://aef.rubyforge.org/#{name.downcase}/"
27
+ puts "Github: http://github.com/aef/#{name.downcase}/"
28
+ puts
29
+ puts 'Copyright 2009 Alexander E. Fischer <aef@raxys.net>'
30
+ # Read and print licensing information from the top of this file
31
+ if Gem::Version.new(RUBY_VERSION) <= Gem::Version.new('1.8.6')
32
+ puts File.read(__FILE__).map{|line| line[2..-1]}[3..15]
33
+ else
34
+ puts File.read(__FILE__).lines.map{|line| line[2..-1]}[3..15]
35
+ end
36
+ exit false
37
+ end
38
+ end
@@ -0,0 +1,122 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'pathname'
19
+ require 'set'
20
+ require 'rubygems'
21
+
22
+ require 'lib/aef' rescue LoadError require 'aef'
23
+
24
+ # Linebreak is Ruby library and commandline tool for conversion of text
25
+ # between linebreak encoding formats of unix, windows or mac.
26
+ #
27
+ # If you want to use the String extension methods, simply use the following
28
+ # command:
29
+ #
30
+ # require 'aef/linebreak/string_extension'
31
+ module Aef::Linebreak
32
+ VERSION = '1.3.0'
33
+
34
+ BREAK_BY_SYSTEM = {
35
+ :unix => "\n",
36
+ :windows => "\r\n",
37
+ :mac => "\r"
38
+ }
39
+
40
+ SYSTEM_BY_BREAK = BREAK_BY_SYSTEM.invert
41
+
42
+ BREAK_REGEXP = /(\r\n|[\r\n])/
43
+
44
+ # Detects encoding systems of a string.
45
+ #
46
+ # Returns a Set with symbols of all encoding systems existent in the string.
47
+ def self.encodings(input)
48
+ if input.respond_to?(:to_s) then input = input.to_s
49
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
50
+
51
+ occurences = Set.new
52
+
53
+ input.scan(BREAK_REGEXP).each do |linebreak|
54
+ occurences << SYSTEM_BY_BREAK[linebreak.first]
55
+ end
56
+
57
+ occurences
58
+ end
59
+
60
+ # Checks whether a string includes linebreaks of all the given encoding
61
+ # systems.
62
+ #
63
+ # One or more systems can be given as an array or an argument list of symbols.
64
+ #
65
+ # Returns true or false.
66
+ def self.encoding?(input, *encodings)
67
+ systems = BREAK_BY_SYSTEM.keys
68
+
69
+ encodings.flatten!
70
+ encodings.each do |encoding|
71
+ unless systems.include?(encoding)
72
+ raise ArgumentError,
73
+ %{Invalid encoding system. Available systems: #{systems.join(', ')}. Arguments are expected as symbols or an array of symbols.}
74
+ end
75
+ end
76
+
77
+ Aef::Linebreak.encodings(input) == Set.new(encodings)
78
+ end
79
+
80
+ # Create a copy of a string with all the string's linebreaks replaced by
81
+ # linebreaks of a specific system or a given replacement.
82
+ #
83
+ # If given output_encoding is not a key of BREAK_BY_SYSTEM, all linebreaks
84
+ # are replaced with output_encoding's content itself.
85
+ def self.encode(input, system_or_replacement = :unix)
86
+ if input.respond_to?(:to_s) then input = input.to_s
87
+ else raise ArgumentError, 'Input needs to be a string or must support to_s' end
88
+
89
+ input.gsub(BREAK_REGEXP,
90
+ BREAK_BY_SYSTEM[system_or_replacement] || system_or_replacement)
91
+ end
92
+
93
+ # Detects encoding systems of the string.
94
+ #
95
+ # Returns a Set with symbols of all encoding systems existent in the string.
96
+ #
97
+ # This method is supposed to be used as a method of String.
98
+ def linebreak_encodings
99
+ Aef::Linebreak.encodings(self)
100
+ end
101
+
102
+ # Checks whether the string includes linebreaks of all the given encoding
103
+ # systems.
104
+ #
105
+ # One or more systems can be given as an array or an argument list of symbols.
106
+ #
107
+ # This method is supposed to be used as a method of String.
108
+ def linebreak_encoding?(*encodings)
109
+ Aef::Linebreak.encoding?(self, encodings)
110
+ end
111
+
112
+ # Create a copy of the string with all the string's linebreaks replaced by
113
+ # linebreaks of a specific system or a given replacement.
114
+ #
115
+ # If given output_encoding is not a key of BREAK_BY_SYSTEM, all linebreaks
116
+ # are replaced with output_encoding's content itself.
117
+ #
118
+ # This method is supposed to be used as a method of String.
119
+ def linebreak_encode(system_or_replacement = :unix)
120
+ Aef::Linebreak.encode(self, system_or_replacement)
121
+ end
122
+ end
@@ -0,0 +1,44 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ # This class adds the type :pathname to user-choices
19
+ class ConversionToPathname < UserChoices::Conversion
20
+ def self.described_by?(conversion_tag)
21
+ conversion_tag == :pathname
22
+ end
23
+
24
+ def description
25
+ "a pathname"
26
+ end
27
+
28
+ def suitable?(actual)
29
+ true
30
+ end
31
+
32
+ def convert(value)
33
+ case value
34
+ when Array
35
+ pathnames = []
36
+
37
+ value.each {|path| pathnames << Pathname(path) }
38
+
39
+ pathnames
40
+ else
41
+ Pathname(value)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify
6
+ # it under the terms of the GNU General Public License as published by
7
+ # the Free Software Foundation, either version 3 of the License, or
8
+ # (at your option) any later version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ # GNU General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License
16
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'rubygems'
19
+
20
+ require 'lib/aef/linebreak' rescue LoadError require 'aef/linebreak'
21
+
22
+ class String
23
+ include Aef::Linebreak
24
+ end
@@ -0,0 +1 @@
1
+ Abcdef
@@ -0,0 +1,2 @@
1
+ Abcdef
2
+ Abcdef
@@ -0,0 +1,3 @@
1
+ Abcdef
2
+ Abcdef
3
+ Abcdef
@@ -0,0 +1,3 @@
1
+ Abcdef
2
+ Abcdef
3
+ Abcdef
@@ -0,0 +1,3 @@
1
+ Abcdef
2
+ Abcdef
3
+ Abcdef
@@ -0,0 +1,3 @@
1
+ Abcdef
2
+ Abcdef
3
+ Abcdef
@@ -0,0 +1,2 @@
1
+ Abcdef
2
+ Abcdef
@@ -0,0 +1,380 @@
1
+ # Copyright 2009 Alexander E. Fischer <aef@raxys.net>
2
+ #
3
+ # This file is part of Linebreak.
4
+ #
5
+ # Linebreak is free software: you can redistribute it and/or modify it under the
6
+ # terms of the GNU General Public License as published by the Free Software
7
+ # Foundation, either version 3 of the License, or (at your option) any later
8
+ # version.
9
+ #
10
+ # This program is distributed in the hope that it will be useful, but WITHOUT
11
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12
+ # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13
+ # details.
14
+ #
15
+ # You should have received a copy of the GNU General Public License along with
16
+ # this program. If not, see <http://www.gnu.org/licenses/>.
17
+
18
+ require 'spec/spec_helper'
19
+
20
+ describe Aef::Linebreak do
21
+ include LinebreakSpecHelper
22
+
23
+ context 'library' do
24
+ describe 'method "encode"' do
25
+ it 'should correctly work from unix to windows format' do
26
+ Aef::Linebreak.encode(unix_fixture, :windows).should eql(windows_fixture)
27
+ end
28
+
29
+ it 'should correctly work from unix to mac format' do
30
+ Aef::Linebreak.encode(unix_fixture, :mac).should eql(mac_fixture)
31
+ end
32
+
33
+ it 'should correctly work from unix to a custom format' do
34
+ Aef::Linebreak.encode(unix_fixture, 'fnord').should eql(custom_fixture)
35
+ end
36
+
37
+ it 'should correctly work from windows to unix format' do
38
+ Aef::Linebreak.encode(windows_fixture, :unix).should eql(unix_fixture)
39
+ end
40
+
41
+ it 'should correctly work from windows to mac format' do
42
+ Aef::Linebreak.encode(windows_fixture, :mac).should eql(mac_fixture)
43
+ end
44
+
45
+ it 'should correctly work from unix to a custom format' do
46
+ Aef::Linebreak.encode(windows_fixture, 'fnord').should eql(custom_fixture)
47
+ end
48
+
49
+ it 'should correctly work from mac to unix format' do
50
+ Aef::Linebreak.encode(mac_fixture, :unix).should eql(unix_fixture)
51
+ end
52
+
53
+ it 'should correctly work from mac to windows format' do
54
+ Aef::Linebreak.encode(mac_fixture, :windows).should eql(windows_fixture)
55
+ end
56
+
57
+ it 'should correctly work from unix to a custom format' do
58
+ Aef::Linebreak.encode(mac_fixture, 'fnord').should eql(custom_fixture)
59
+ end
60
+ end
61
+
62
+ describe 'method "encodings"' do
63
+ it 'should detect unix format' do
64
+ Aef::Linebreak.encodings(unix_fixture).should eql([:unix].to_set)
65
+ end
66
+
67
+ it 'should detect windows format' do
68
+ Aef::Linebreak.encodings(windows_fixture).should eql([:windows].to_set)
69
+ end
70
+
71
+ it 'should detect mac format' do
72
+ Aef::Linebreak.encodings(mac_fixture).should eql([:mac].to_set)
73
+ end
74
+
75
+ it 'should detect mixed unix and windows format' do
76
+ Aef::Linebreak.encodings(unix_windows_fixture).should eql([:unix, :windows].to_set)
77
+ end
78
+
79
+ it 'should detect mixed windows and mac format' do
80
+ Aef::Linebreak.encodings(windows_mac_fixture).should eql([:windows, :mac].to_set)
81
+ end
82
+
83
+ it 'should detect mixed mac and unix format' do
84
+ Aef::Linebreak.encodings(mac_unix_fixture).should eql([:mac, :unix].to_set)
85
+ end
86
+
87
+ it 'should detect mixed unix, windows and mac format' do
88
+ Aef::Linebreak.encodings(unix_windows_mac_fixture).should eql([:unix, :windows, :mac].to_set)
89
+ end
90
+
91
+ it 'should detect correctly strings without linebreaks' do
92
+ Aef::Linebreak.encodings(none_fixture).should eql(Set.new)
93
+ end
94
+ end
95
+
96
+ describe 'method "encoding?"' do
97
+ system_combinations = [
98
+ :unix,
99
+ :windows,
100
+ :mac,
101
+ :unix_windows,
102
+ :windows_mac,
103
+ :mac_unix,
104
+ :unix_windows_mac
105
+ ]
106
+
107
+ system_combinations.each do |system_combination|
108
+ system_combinations.each do |fixture_systems|
109
+
110
+ expected_systems = system_combination.to_s.split('_').inject([]) do |systems, system|
111
+ systems << system.to_sym
112
+ end
113
+
114
+ it "should correctly ensure #{expected_systems.join(' and ')} encoding (input: #{fixture_systems.to_s.split('_').join(' and ')})" do
115
+ fixture = send "#{fixture_systems}_fixture"
116
+
117
+ result = Aef::Linebreak.encoding?(fixture, expected_systems)
118
+
119
+ if system_combination == fixture_systems
120
+ result.should be_true
121
+ else
122
+ result.should be_false
123
+ end
124
+ end
125
+
126
+ if expected_systems.size > 1
127
+ it "should correctly ensure #{expected_systems.join(' and ')} encoding (input: #{fixture_systems.to_s.split('_').join(' and ')}; using argument list)" do
128
+ fixture = send "#{fixture_systems}_fixture"
129
+
130
+ result = Aef::Linebreak.encoding?(fixture, *expected_systems)
131
+
132
+ if system_combination == fixture_systems
133
+ result.should be_true
134
+ else
135
+ result.should be_false
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ end
143
+ end
144
+
145
+ context 'string extension' do
146
+ describe 'method "linebreak_encode"' do
147
+ it 'should correctly work from unix to windows format' do
148
+ unix_fixture.linebreak_encode(:windows).should eql(windows_fixture)
149
+ end
150
+
151
+ it 'should correctly work from unix to mac format' do
152
+ unix_fixture.linebreak_encode(:mac).should eql(mac_fixture)
153
+ end
154
+
155
+ it 'should correctly work from unix to a custom format' do
156
+ unix_fixture.linebreak_encode('fnord').should eql(custom_fixture)
157
+ end
158
+
159
+ it 'should correctly work from windows to unix format' do
160
+ windows_fixture.linebreak_encode(:unix).should eql(unix_fixture)
161
+ end
162
+
163
+ it 'should correctly work from windows to mac format' do
164
+ windows_fixture.linebreak_encode(:mac).should eql(mac_fixture)
165
+ end
166
+
167
+ it 'should correctly work from unix to a custom format' do
168
+ windows_fixture.linebreak_encode('fnord').should eql(custom_fixture)
169
+ end
170
+
171
+ it 'should correctly work from mac to unix format' do
172
+ mac_fixture.linebreak_encode(:unix).should eql(unix_fixture)
173
+ end
174
+
175
+ it 'should correctly work from mac to windows format' do
176
+ mac_fixture.linebreak_encode(:windows).should eql(windows_fixture)
177
+ end
178
+
179
+ it 'should correctly work from unix to a custom format' do
180
+ mac_fixture.linebreak_encode('fnord').should eql(custom_fixture)
181
+ end
182
+ end
183
+
184
+ describe 'method "linebreak_encodings"' do
185
+ it 'should detect unix format' do
186
+ unix_fixture.linebreak_encodings.should eql([:unix].to_set)
187
+ end
188
+
189
+ it 'should detect windows format' do
190
+ windows_fixture.linebreak_encodings.should eql([:windows].to_set)
191
+ end
192
+
193
+ it 'should detect mac format' do
194
+ mac_fixture.linebreak_encodings.should eql([:mac].to_set)
195
+ end
196
+
197
+ it 'should detect mixed unix and windows format' do
198
+ unix_windows_fixture.linebreak_encodings.should eql([:unix, :windows].to_set)
199
+ end
200
+
201
+ it 'should detect mixed windows and mac format' do
202
+ windows_mac_fixture.linebreak_encodings.should eql([:windows, :mac].to_set)
203
+ end
204
+
205
+ it 'should detect mixed mac and unix format' do
206
+ mac_unix_fixture.linebreak_encodings.should eql([:mac, :unix].to_set)
207
+ end
208
+
209
+ it 'should detect mixed unix, windows and mac format' do
210
+ unix_windows_mac_fixture.linebreak_encodings.should eql([:unix, :windows, :mac].to_set)
211
+ end
212
+
213
+ it 'should detect correctly strings without linebreaks' do
214
+ none_fixture.linebreak_encodings.should eql(Set.new)
215
+ end
216
+ end
217
+
218
+ describe 'method "linebreak_encoding?"' do
219
+ system_combinations = [
220
+ :unix,
221
+ :windows,
222
+ :mac,
223
+ :unix_windows,
224
+ :windows_mac,
225
+ :mac_unix,
226
+ :unix_windows_mac
227
+ ]
228
+
229
+ system_combinations.each do |system_combination|
230
+ system_combinations.each do |fixture_systems|
231
+
232
+ expected_systems = system_combination.to_s.split('_').inject([]) do |systems, system|
233
+ systems << system.to_sym
234
+ end
235
+
236
+ it "should correctly ensure #{expected_systems.join(' and ')} encoding (input: #{fixture_systems.to_s.split('_').join(' and ')})" do
237
+ fixture = send "#{fixture_systems}_fixture"
238
+
239
+ result = fixture.linebreak_encoding?(expected_systems)
240
+
241
+ if system_combination == fixture_systems
242
+ result.should be_true
243
+ else
244
+ result.should be_false
245
+ end
246
+ end
247
+
248
+ if expected_systems.size > 1
249
+ it "should correctly ensure #{expected_systems.join(' and ')} encoding (input: #{fixture_systems.to_s.split('_').join(' and ')}; using argument list)" do
250
+ fixture = send "#{fixture_systems}_fixture"
251
+
252
+ result = fixture.linebreak_encoding?(*expected_systems)
253
+
254
+ if system_combination == fixture_systems
255
+ result.should be_true
256
+ else
257
+ result.should be_false
258
+ end
259
+ end
260
+ end
261
+ end
262
+ end
263
+
264
+ end
265
+ end
266
+
267
+ context 'commandline tool' do
268
+ describe '"version" command' do
269
+ it 'should display correct version and licensing information with the version argument' do
270
+ `#{executable_path} version`.should eql(version_message)
271
+ end
272
+
273
+ it 'should display correct version and licensing information with the --version argument' do
274
+ `#{executable_path} --version`.should eql(version_message)
275
+ end
276
+
277
+ it 'should display correct version and licensing information with the -v argument' do
278
+ `#{executable_path} -v`.should eql(version_message)
279
+ end
280
+
281
+ it 'should display correct version and licensing information with the -V argument' do
282
+ `#{executable_path} -V`.should eql(version_message)
283
+ end
284
+ end
285
+
286
+ describe '"encode" command' do
287
+ it 'should use unix as default format' do
288
+ `#{executable_path} encode #{fixture_path('windows.txt')}`.should eql(unix_fixture)
289
+ end
290
+
291
+ it 'should accept -s option to specify output format' do
292
+ `#{executable_path} encode -s mac #{fixture_path('unix.txt')}`.should eql(mac_fixture)
293
+ end
294
+
295
+ it 'should also accept --system option to specify output format' do
296
+ `#{executable_path} encode --system windows #{fixture_path('mac.txt')}`.should eql(windows_fixture)
297
+ end
298
+
299
+ it 'should abort on invalid output formats' do
300
+ POpen4.popen4("#{executable_path} encode -s fnord #{fixture_path('mac.txt')}") do |stdout, stderr, stdin, pid|
301
+ stdout.read.should be_empty
302
+ stderr.read.should_not be_empty
303
+ end
304
+ end
305
+
306
+ it 'should accept LINEBREAK_SYSTEM environment variable to specify output format' do
307
+ env(:LINEBREAK_SYSTEM => 'mac') do
308
+ `#{executable_path} encode #{fixture_path('windows.txt')}`.should eql(mac_fixture)
309
+ end
310
+ end
311
+
312
+ it 'should use output format specified with -s even if LINEBREAK_SYSTEM environment variable is set' do
313
+ env(:LINEBREAK_SYSTEM => 'windows') do
314
+ `#{executable_path} encode -s mac #{fixture_path('unix.txt')}`.should eql(mac_fixture)
315
+ end
316
+ end
317
+
318
+ it 'should use a second argument as target file' do
319
+ temp_file = Tempfile.open('linebreak_spec')
320
+ location = Pathname(temp_file.path)
321
+ temp_file.close
322
+ location.delete
323
+
324
+ `#{executable_path} encode --system windows #{fixture_path('unix.txt')} #{location}`.should be_empty
325
+
326
+ location.read.should eql(windows_fixture)
327
+ location.delete
328
+ end
329
+ end
330
+
331
+ describe '"encodings" command' do
332
+ it "should correctly detect the linebreak encodings of a file" do
333
+ POpen4.popen4("#{executable_path} encodings #{fixture_path('mac.txt')}") do |stdout, stderr, stdin, pid|
334
+ stdout.read.should eql("#{fixture_path('mac.txt')}: mac\n")
335
+ end.exitstatus.should eql(0)
336
+ end
337
+
338
+ it "should correctly detect the linebreak encodings of multiple files" do
339
+ files = %w{unix_windows.txt windows_mac.txt mac_unix.txt unix_windows_mac.txt}
340
+ files = files.map{|file| fixture_path(file)}
341
+
342
+ POpen4.popen4("#{executable_path} encodings #{files.join(' ')}") do |stdout, stderr, stdin, pid|
343
+ output = stdout.read
344
+ output.should include("#{files[0]}: unix,windows\n")
345
+ output.should include("#{files[1]}: windows,mac\n")
346
+ output.should include("#{files[2]}: unix,mac\n")
347
+ output.should include("#{files[3]}: unix,windows,mac\n")
348
+ end.exitstatus.should eql(0)
349
+ end
350
+
351
+ it "should correctly ensure the linebreak encodings of a valid file" do
352
+ POpen4.popen4("#{executable_path} encodings --ensure unix #{fixture_path('unix.txt')}") do |stdout, stderr, stdin, pid|
353
+ output = stdout.read
354
+ output.should eql("#{fixture_path('unix.txt')}: unix\n")
355
+ end.exitstatus.should eql(0)
356
+ end
357
+
358
+ it "should correctly ensure the linebreak encodings of an invalid file" do
359
+ POpen4.popen4("#{executable_path} encodings --ensure mac #{fixture_path('unix_windows.txt')}") do |stdout, stderr, stdin, pid|
360
+ output = stdout.read
361
+ output.should eql("#{fixture_path('unix_windows.txt')}: unix,windows (failed)\n")
362
+ end.exitstatus.should eql(1)
363
+ end
364
+
365
+ it "should correctly ensure the linebreak encodings of multiple files" do
366
+ files = %w{unix_windows.txt windows_mac.txt mac_unix.txt unix_windows_mac.txt}
367
+ files = files.map{|file| fixture_path(file)}
368
+
369
+ POpen4.popen4("#{executable_path} encodings --ensure windows,unix #{files.join(' ')}") do |stdout, stderr, stdin, pid|
370
+ output = stdout.read
371
+ output.should include("#{files[0]}: unix,windows\n")
372
+ output.should_not include("#{files[0]}: unix,windows (failed)\n")
373
+ output.should include("#{files[1]}: windows,mac (failed)\n")
374
+ output.should include("#{files[2]}: unix,mac (failed)\n")
375
+ output.should include("#{files[3]}: unix,windows,mac (failed)\n")
376
+ end.exitstatus.should eql(1)
377
+ end
378
+ end
379
+ end
380
+ end