clamsy 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/HISTORY.txt +7 -0
  2. data/README.rdoc +56 -15
  3. data/VERSION +1 -1
  4. data/clamsy.gemspec +51 -16
  5. data/clamsy.png +0 -0
  6. data/lib/clamsy.rb +26 -123
  7. data/lib/clamsy.yml +8 -0
  8. data/lib/clamsy/base_printer.rb +64 -0
  9. data/lib/clamsy/configuration.rb +128 -0
  10. data/lib/clamsy/cups_pdf_printer.rb +26 -0
  11. data/lib/clamsy/file_system_support.rb +36 -0
  12. data/lib/clamsy/template_open_doc.rb +122 -0
  13. data/spec/clamsy/base_printer_spec.rb +19 -0
  14. data/spec/clamsy/configuration_spec.rb +290 -0
  15. data/spec/clamsy/cups_pdf_printer_spec.rb +40 -0
  16. data/spec/{data → clamsy/data}/clamsy.png +0 -0
  17. data/spec/{data → clamsy/data}/clamsy2.png +0 -0
  18. data/spec/clamsy/data/embedded_ruby_after.odt +0 -0
  19. data/spec/clamsy/data/embedded_ruby_before.odt +0 -0
  20. data/spec/clamsy/data/escaped_text_after.odt +0 -0
  21. data/spec/{data/escaped_text_example.odt → clamsy/data/escaped_text_before.odt} +0 -0
  22. data/spec/clamsy/data/invalid_content_example.odt +0 -0
  23. data/spec/clamsy/data/invalid_zip_example.odt +0 -0
  24. data/spec/{data/multiple_contexts_example.odt → clamsy/data/multiple_contexts_after.odt} +0 -0
  25. data/spec/{data/plain_text_example.odt → clamsy/data/multiple_contexts_before.odt} +0 -0
  26. data/spec/clamsy/data/picture_after.odt +0 -0
  27. data/spec/{data/picture_example.odt → clamsy/data/picture_before.odt} +0 -0
  28. data/spec/clamsy/data/plain_text_after.odt +0 -0
  29. data/spec/clamsy/data/plain_text_before.odt +0 -0
  30. data/spec/clamsy/file_system_support_spec.rb +93 -0
  31. data/spec/clamsy/invalid_printer_spec.rb +21 -0
  32. data/spec/clamsy/template_open_doc_spec.rb +95 -0
  33. data/spec/{clamsy_spec.rb → integration/cups_pdf_printer_spec.rb} +12 -6
  34. data/spec/{data → integration/data}/embedded_ruby_example.odt +0 -0
  35. data/spec/{data → integration/data}/embedded_ruby_example.pdf +0 -0
  36. data/spec/integration/data/escaped_text_example.odt +0 -0
  37. data/spec/{data → integration/data}/escaped_text_example.pdf +0 -0
  38. data/spec/integration/data/multiple_contexts_example.odt +0 -0
  39. data/spec/{data → integration/data}/multiple_contexts_example.pdf +0 -0
  40. data/spec/integration/data/norm_clamsy.png +0 -0
  41. data/spec/integration/data/picture_example.odt +0 -0
  42. data/spec/integration/data/picture_example.pdf +0 -0
  43. data/spec/integration/data/plain_text_example.odt +0 -0
  44. data/spec/{data → integration/data}/plain_text_example.pdf +0 -0
  45. data/spec/integration/data/sunny_clamsy.png +0 -0
  46. data/spec/integration/has_stardand_integration_support_shared_spec.rb +27 -0
  47. data/spec/spec_helper.rb +1 -25
  48. metadata +52 -17
  49. data/spec/data/picture_example.pdf +0 -0
@@ -0,0 +1,40 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'clamsy', 'cups_pdf_printer')
3
+
4
+ class << Clamsy::CupsPdfPrinter
5
+ public :tmp_pdf_path
6
+ attr_accessor :config
7
+ end
8
+
9
+ describe 'Cups pdf printer' do
10
+
11
+ before do
12
+ @printer = Clamsy::CupsPdfPrinter
13
+ @printer.config = Class.new { attr_accessor :cups_output_dir, :cups_output_file }.new
14
+ end
15
+
16
+ describe '> generating of tmp pdf path (using config.cups_output_file)' do
17
+
18
+ should 'return config.cups_output_file if it is not a Proc' do
19
+ @printer.config.cups_output_file = '/a/b/c'
20
+ @printer.tmp_pdf_path('dummy').should.equal '/a/b/c'
21
+ end
22
+
23
+ should "return evaluated config.cups_output_file if it is a Proc" do
24
+ @printer.config.cups_output_file = lambda { "/a/b/#{1+2}" }
25
+ @printer.tmp_pdf_path('dummy').should.equal '/a/b/3'
26
+ end
27
+
28
+ end
29
+
30
+ describe '> generating of tmp pdf path (using config.cups_output_file)' do
31
+
32
+ should "use config.cups_output_dir, basename of specified file & pdf file extension" do
33
+ @printer.config.cups_output_dir = '/a/b/c'
34
+ @printer.tmp_pdf_path('/e/f/g/document').should.equal '/a/b/c/document.pdf'
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+
File without changes
File without changes
@@ -0,0 +1,93 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ class FSSInstance
4
+ include Clamsy::FileSystemSupport
5
+ def trashable_tmp_files
6
+ @trashable_tmp_files || []
7
+ end
8
+ end
9
+
10
+ describe 'File system support (ensuring file exists)' do
11
+
12
+ before do
13
+ @instance = FSSInstance.new
14
+ @missing_file = "#{__FILE__}.x"
15
+ @check_test_passes_and_completes_in_n_secs = lambda do |seconds_to_wait, test_proc|
16
+ started_at = Time.now
17
+ test_proc.call
18
+ (Time.now - started_at).to_i.should.equal seconds_to_wait
19
+ end
20
+ end
21
+
22
+ describe '> when given N seconds to wait' do
23
+
24
+ it 'should wait & raise Clamsy::FileNotFoundError if file eventually does not exist' do
25
+ @check_test_passes_and_completes_in_n_secs[
26
+ seconds_to_wait = 2, lambda do
27
+ lambda { @instance.file_must_exist!(@missing_file, seconds_to_wait) }.
28
+ should.raise(Clamsy::FileNotFoundError).
29
+ message.should.equal("File '#{@missing_file}' not found.")
30
+ end
31
+ ]
32
+ end
33
+
34
+ it 'should wait & not raise Clamsy::FileNotFoundError if file eventually exists' do
35
+ # Hmm ... don't really know how to test this yet.
36
+ true.should.be.true
37
+ end
38
+
39
+ end
40
+
41
+ describe '> when not given any seconds to wait' do
42
+
43
+ it 'should immediately raise Clamsy::FileNotFoundError if file does not exist' do
44
+ @check_test_passes_and_completes_in_n_secs[
45
+ seconds_to_wait = 0, lambda do
46
+ lambda { @instance.file_must_exist!(@missing_file) }.
47
+ should.raise(Clamsy::FileNotFoundError).
48
+ message.should.equal("File '#{@missing_file}' not found.")
49
+ end
50
+ ]
51
+ end
52
+
53
+ it 'should not raise Clamsy::FileNotFoundError if file exists' do
54
+ @check_test_passes_and_completes_in_n_secs[
55
+ seconds_to_wait = 0,
56
+ lambda { @instance.file_must_exist!(__FILE__) }
57
+ ]
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
64
+ describe 'File system support (trashable temp files)' do
65
+
66
+ before do
67
+ @instance = FSSInstance.new
68
+ end
69
+
70
+ it 'should always return a new temp file upon request' do
71
+ (file = @instance.tmp_file).class.should.equal Tempfile
72
+ end
73
+
74
+ it 'should support customizing of name of requested temp file' do
75
+ (file = @instance.tmp_file('zzyyxx')).class.should.equal Tempfile
76
+ file.path.should.match(/zzyyxx/)
77
+ end
78
+
79
+ it 'should cache newly requested temp file' do
80
+ orig_count = @instance.trashable_tmp_files.size
81
+ file = @instance.tmp_file
82
+ @instance.trashable_tmp_files.size.should.equal orig_count.succ
83
+ file.path.should.equal @instance.trashable_tmp_files[-1].path
84
+ end
85
+
86
+ it 'should support trashing/deleting all previously requested temp files' do
87
+ (0..1).each {|_| @instance.tmp_file }
88
+ orig_count = @instance.trashable_tmp_files.size
89
+ @instance.trash_tmp_files
90
+ @instance.trashable_tmp_files.should.be.empty
91
+ end
92
+
93
+ end
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ class InvalidPrinter < Clamsy::BasePrinter ; end
4
+
5
+ describe 'Invalid printer' do
6
+
7
+ describe '> converting docs to pdf' do
8
+
9
+ behaves_like 'has standard files support'
10
+
11
+ it 'should raise Clamsy::ImplementationNotFoundError' do
12
+ tmp_doc = tmp_file('doc')
13
+ lambda { InvalidPrinter.docs_to_pdf([tmp_doc], "#{__FILE__}.pdf") }.
14
+ should.raise(Clamsy::ImplementationNotFoundError).
15
+ message.should.equal("InvalidPrinter.doc_to_pdf not implemented.")
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,95 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+
3
+ describe 'Template open doc' do
4
+
5
+ before do
6
+ @data_file = lambda {|file| File.join(File.dirname(__FILE__), 'data', file) }
7
+ end
8
+
9
+ describe '> initializing' do
10
+
11
+ it 'should raise Clamsy::FileNotFoundError template doc cannot be found' do
12
+ missing_template = @data_file['missing.odt']
13
+ lambda { Clamsy::TemplateOpenDoc.new(missing_template) }.
14
+ should.raise(Clamsy::FileNotFoundError).
15
+ message.should.equal("File '#{missing_template}' not found.")
16
+ end
17
+
18
+ it 'should raise Clamsy::TemplateDocIsCorruptedError if template doc is not a valid document' do
19
+ invalid_template = @data_file['invalid_zip_example.odt']
20
+ lambda { Clamsy::TemplateOpenDoc.new(invalid_template) }.
21
+ should.raise(Clamsy::TemplateDocIsCorruptedError).
22
+ message.should.equal("Template doc '#{invalid_template}' is corrupted.")
23
+ end
24
+
25
+ it 'should raise Clamsy::TemplateDocContentIsCorruptedError if template doc has invalid content' do
26
+ invalid_template = @data_file['invalid_content_example.odt']
27
+ lambda { Clamsy::TemplateOpenDoc.new(invalid_template) }.
28
+ should.raise(Clamsy::TemplateDocContentIsCorruptedError).
29
+ message.should.equal("Template doc content '#{invalid_template}'/'content.xml' is corrupted.")
30
+ end
31
+
32
+ end
33
+
34
+ describe '> rendering' do
35
+
36
+ before do
37
+ @zip_content = lambda {|file| Zip::ZipFile.open(file).map {|e| e.get_input_stream.read } }
38
+ @template = lambda {|name| Clamsy::TemplateOpenDoc.new(@data_file["#{name}_before.odt"]) }
39
+ @check_rendering_yields_expected_doc = lambda do |context, example|
40
+ generated_content = @zip_content[@template[example].render(context).path]
41
+ expected_content = @zip_content[@data_file["#{example}_after.odt"]]
42
+ (generated_content - expected_content).should.be.empty
43
+ (expected_content - generated_content).should.be.empty
44
+ end
45
+ end
46
+
47
+ it 'should return instance of Tempfile for generated doc' do
48
+ file = @template[:plain_text].render({})
49
+ file.class.should.equal Tempfile
50
+ end
51
+
52
+ it 'should do #{...} plain text replacement' do
53
+ @check_rendering_yields_expected_doc[
54
+ context = {:someone => 'Peter', :mood => 'Happy'},
55
+ example = :plain_text
56
+ ]
57
+ end
58
+
59
+ it 'should do ${...} escaped (santized) replacement' do
60
+ @check_rendering_yields_expected_doc[
61
+ context = {:someone => '<Peter>', :mood => '<Happy>'},
62
+ example = :escaped_text
63
+ ]
64
+ end
65
+
66
+ it 'should do {? ... ?} embedded ruby statements processing' do
67
+ @check_rendering_yields_expected_doc[
68
+ context = {
69
+ :everyone => [
70
+ {:name => 'Peter', :mood => 'Happy'},
71
+ {:name => 'Jane', :mood => 'Sad'}
72
+ ]
73
+ },
74
+ example = :embedded_ruby
75
+ ]
76
+ end
77
+
78
+ it 'should do picture replacement for pictures with matching names' do
79
+ @check_rendering_yields_expected_doc[
80
+ context = {:_pictures => {:to_be_replaced_pic => @data_file['clamsy.png']}},
81
+ example = :picture
82
+ ]
83
+ end
84
+
85
+ it 'should raise Clamsy::RenderedDocContentIsCorruptedError if rendering yields invalid content' do
86
+ lambda {
87
+ @template[:plain_text].render(:someone => '<Peter>', :mood => '<Happy>')
88
+ }.should.raise(Clamsy::RenderedDocContentIsCorruptedError).message.should.equal(
89
+ 'Rendered doc content is corrupted, use ${ ... } where text escaping is needed.'
90
+ )
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -1,13 +1,16 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
1
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
2
+ require File.join(File.dirname(__FILE__), 'has_stardand_integration_support_shared_spec')
2
3
 
3
- describe "Clamsy" do
4
+ describe "Clamsy (using Cups-PDF printer)" do
4
5
 
5
6
  behaves_like 'has standard files support'
7
+ behaves_like 'has standard integration support'
6
8
 
7
9
  before do
8
10
  @check_processing_yields_content = lambda do |contexts, example|
9
11
  generated_pdf = tmp_file('clamsy_pdf').path
10
- Clamsy.process(contexts, template_odt(example), generated_pdf)
12
+ Clamsy.process(contexts, template_doc(example), generated_pdf)
13
+ `cp #{generated_pdf} /tmp/abc.pdf`
11
14
  expected_content = comparable_content(expected_pdf(example))
12
15
  generated_content = comparable_content(generated_pdf)
13
16
  generated_content.size.should.equal expected_content.size
@@ -19,9 +22,12 @@ describe "Clamsy" do
19
22
  trash_tmp_files
20
23
  end
21
24
 
22
- it 'should do picture replacement for matching <draw:frame draw:name="..." />' do
25
+ it 'should do picture replacement for pictures with matching names' do
23
26
  @check_processing_yields_content[
24
- context = {:_pictures => {:to_be_replaced_pic => data_file('clamsy.png')}},
27
+ context = {:_pictures => {
28
+ :sunny_clamsy => data_file('norm_clamsy.png'),
29
+ :norm_clamsy => data_file('sunny_clamsy.png'),
30
+ }},
25
31
  example = :picture
26
32
  ]
27
33
  end
@@ -40,7 +46,7 @@ describe "Clamsy" do
40
46
  ]
41
47
  end
42
48
 
43
- it 'should do {? ... ?} embedded ruby statements' do
49
+ it 'should do {? ... ?} embedded ruby statements processing' do
44
50
  @someone = Class.new do
45
51
  attr_reader :name, :mood
46
52
  def initialize(name, mood)
@@ -0,0 +1,27 @@
1
+ shared 'has standard integration support' do
2
+ class << self
3
+
4
+ INSIGNIFICANT_AND_UNMATCHABLE_PATTERNS = [
5
+ /^q\[\-?\d+\.?\d*( \-?\d+\.?\d*){5}\]concat\n$/,
6
+ /^\d+\.?\d*( \d+\.?\d*){3} re\n$/
7
+ ]
8
+
9
+ def data_file(file)
10
+ File.join(File.dirname(__FILE__), 'data', file)
11
+ end
12
+
13
+ def template_doc(name)
14
+ data_file("#{name}_example.odt")
15
+ end
16
+
17
+ def expected_pdf(name)
18
+ data_file("#{name}_example.pdf")
19
+ end
20
+
21
+ def comparable_content(file)
22
+ RGhost::Convert.new(file).to(:ps).read.grep(/^[^%][^%]?/).
23
+ reject {|line| INSIGNIFICANT_AND_UNMATCHABLE_PATTERNS.any? {|regexp| regexp =~ line } }
24
+ end
25
+
26
+ end
27
+ end
@@ -3,6 +3,7 @@ require 'bacon'
3
3
  require 'differ'
4
4
  require 'tempfile'
5
5
  require 'digest/md5'
6
+ require 'yaml'
6
7
 
7
8
  $LOAD_PATH.unshift(File.dirname(__FILE__))
8
9
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -10,37 +11,12 @@ require 'clamsy'
10
11
 
11
12
  shared 'has standard files support' do
12
13
  class << self
13
-
14
- INSIGNIFICANT_AND_UNMATCHABLE_PATTERNS = [
15
- /^q\[\-?\d+\.?\d*( \-?\d+\.?\d*){5}\]concat\n$/,
16
- /^\d+\.?\d*( \d+\.?\d*){3} re\n$/
17
- ]
18
-
19
- def data_file(file)
20
- File.join(File.dirname(__FILE__), 'data', file)
21
- end
22
-
23
- def template_odt(name)
24
- data_file("#{name}_example.odt")
25
- end
26
-
27
- def expected_pdf(name)
28
- data_file("#{name}_example.pdf")
29
- end
30
-
31
- def comparable_content(file)
32
- RGhost::Convert.new(file).to(:ps).read.grep(/^[^%][^%]?/).
33
- reject {|line| INSIGNIFICANT_AND_UNMATCHABLE_PATTERNS.any? {|regexp| regexp =~ line } }
34
- end
35
-
36
14
  def trash_tmp_files
37
15
  (@trashable_tmp_files || []).select {|f| f.path }.map(&:unlink)
38
16
  end
39
-
40
17
  def tmp_file(file_name)
41
18
  ((@trashable_tmp_files ||= []) << Tempfile.new(file_name))[-1]
42
19
  end
43
-
44
20
  end
45
21
  end
46
22
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 3
9
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - NgTzeYang
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-25 00:00:00 +08:00
17
+ date: 2010-05-04 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -92,21 +92,49 @@ files:
92
92
  - Rakefile
93
93
  - VERSION
94
94
  - clamsy.gemspec
95
+ - clamsy.png
95
96
  - lib/clamsy.rb
97
+ - lib/clamsy.yml
98
+ - lib/clamsy/base_printer.rb
99
+ - lib/clamsy/configuration.rb
100
+ - lib/clamsy/cups_pdf_printer.rb
101
+ - lib/clamsy/file_system_support.rb
102
+ - lib/clamsy/template_open_doc.rb
96
103
  - lib/clamsy/tenjin.rb
97
- - spec/clamsy_spec.rb
98
- - spec/data/clamsy.png
99
- - spec/data/clamsy2.png
100
- - spec/data/embedded_ruby_example.odt
101
- - spec/data/embedded_ruby_example.pdf
102
- - spec/data/escaped_text_example.odt
103
- - spec/data/escaped_text_example.pdf
104
- - spec/data/multiple_contexts_example.odt
105
- - spec/data/multiple_contexts_example.pdf
106
- - spec/data/picture_example.odt
107
- - spec/data/picture_example.pdf
108
- - spec/data/plain_text_example.odt
109
- - spec/data/plain_text_example.pdf
104
+ - spec/clamsy/base_printer_spec.rb
105
+ - spec/clamsy/configuration_spec.rb
106
+ - spec/clamsy/cups_pdf_printer_spec.rb
107
+ - spec/clamsy/data/clamsy.png
108
+ - spec/clamsy/data/clamsy2.png
109
+ - spec/clamsy/data/embedded_ruby_after.odt
110
+ - spec/clamsy/data/embedded_ruby_before.odt
111
+ - spec/clamsy/data/escaped_text_after.odt
112
+ - spec/clamsy/data/escaped_text_before.odt
113
+ - spec/clamsy/data/invalid_content_example.odt
114
+ - spec/clamsy/data/invalid_zip_example.odt
115
+ - spec/clamsy/data/multiple_contexts_after.odt
116
+ - spec/clamsy/data/multiple_contexts_before.odt
117
+ - spec/clamsy/data/picture_after.odt
118
+ - spec/clamsy/data/picture_before.odt
119
+ - spec/clamsy/data/plain_text_after.odt
120
+ - spec/clamsy/data/plain_text_before.odt
121
+ - spec/clamsy/file_system_support_spec.rb
122
+ - spec/clamsy/invalid_printer_spec.rb
123
+ - spec/clamsy/template_open_doc_spec.rb
124
+ - spec/integration/cups_pdf_printer_spec.rb
125
+ - spec/integration/data/embedded_ruby_example.odt
126
+ - spec/integration/data/embedded_ruby_example.pdf
127
+ - spec/integration/data/escaped_text_example.odt
128
+ - spec/integration/data/escaped_text_example.pdf
129
+ - spec/integration/data/multiple_contexts_example.odt
130
+ - spec/integration/data/multiple_contexts_example.pdf
131
+ - spec/integration/data/norm_clamsy.png
132
+ - spec/integration/data/picture_example.odt
133
+ - spec/integration/data/picture_example.pdf
134
+ - spec/integration/data/plain_text_example.odt
135
+ - spec/integration/data/plain_text_example.pdf
136
+ - spec/integration/data/sunny_clamsy.png
137
+ - spec/integration/has_stardand_integration_support_shared_spec.rb
110
138
  - spec/spec_helper.rb
111
139
  has_rdoc: true
112
140
  homepage: http://github.com/ngty/clamsy
@@ -139,5 +167,12 @@ signing_key:
139
167
  specification_version: 3
140
168
  summary: A clumsily shellish way to generate a single pdf for multiple contexts from an odt template
141
169
  test_files:
142
- - spec/clamsy_spec.rb
170
+ - spec/integration/cups_pdf_printer_spec.rb
171
+ - spec/integration/has_stardand_integration_support_shared_spec.rb
172
+ - spec/clamsy/file_system_support_spec.rb
173
+ - spec/clamsy/base_printer_spec.rb
174
+ - spec/clamsy/cups_pdf_printer_spec.rb
175
+ - spec/clamsy/invalid_printer_spec.rb
176
+ - spec/clamsy/configuration_spec.rb
177
+ - spec/clamsy/template_open_doc_spec.rb
143
178
  - spec/spec_helper.rb