reportbuilder 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_html.rb CHANGED
@@ -3,45 +3,66 @@ $:.unshift(File.dirname(__FILE__)+"/../lib")
3
3
  require "reportbuilder"
4
4
  require 'fileutils'
5
5
  require 'tmpdir'
6
+ require 'hpricot'
7
+ require 'tempfile'
6
8
  class TestReportbuilderHtml < Test::Unit::TestCase
7
9
  def setup
8
10
  @tmpdir=Dir::mktmpdir
9
- @rp=ReportBuilder.new("Test Html", @tmpdir)
11
+ @title="Test #{rand(100)}"
12
+ @rp=ReportBuilder.new(:name=>@title, :directory=>@tmpdir)
10
13
  @datadir=File.dirname(__FILE__)+"/../data"
11
14
  end
12
15
  def teardown
13
- FileUtils.remove_entry_secure @tmpdir
16
+ FileUtils.remove_entry_secure @tmpdir
17
+ end
18
+ def test_empty_document
19
+ doc=Hpricot(@rp.to_html)
20
+ assert_equal(@title, doc.search("head/title").inner_html)
21
+ assert_equal(@title, doc.search("body/h1").inner_html)
22
+ assert_match(/^\s+$/, doc.search("body").inner_html.gsub(/<h1>.+<\/h1>/,""))
23
+
24
+ end
25
+ def test_generate
26
+ html=ReportBuilder.generate(:format=>:html, :name=>@title, :directory=>@tmpdir) do
27
+ text("hola")
28
+ end
29
+ doc=Hpricot(html)
30
+ assert_equal(@title, doc.search("head/title").inner_html)
31
+ assert_equal(@title, doc.search("body/h1").inner_html)
32
+ assert_equal("hola", doc.search("body/p").inner_html)
33
+
14
34
  end
15
35
  def test_include_js
16
- mock_element = ""
17
- eval(" class << mock_element
18
- def to_reportbuilder_html(generator)
19
- generator.add_js('"+@datadir+"/reportbuilder.js')
36
+ mock = ""
37
+ eval(" class << mock
38
+ def report_building_html(generator)
39
+ generator.js('"+@datadir+"/reportbuilder.js')
20
40
  end
21
- end ")
22
- @rp.add(mock_element)
23
- assert_match(/script.+js\/reportbuilder.js/, @rp.to_html)
24
- assert(File.exists? @tmpdir+"/js/reportbuilder.js")
41
+ end ")
42
+ @rp.add(mock)
43
+ assert_match(/script.+js\/reportbuilder.js/, @rp.to_html)
44
+ assert(File.exists? @tmpdir+"/js/reportbuilder.js")
25
45
  end
26
46
  def test_save_html
47
+ tf=Tempfile.new("sdsd.html")
27
48
  exp=@rp.to_html
28
- @rp.save_html("test.html")
49
+ @rp.save_html(tf.path)
29
50
  obt=""
30
- File.open(@tmpdir+"/test.html","r") {|fp|
51
+ File.open(tf.path,"r") {|fp|
31
52
  obt=fp.readlines(nil)[0]
32
53
  }
33
54
  assert_equal(exp,obt)
34
55
  end
35
56
  def test_include_css
36
57
  mock_element = ""
37
- eval(" class << mock_element
38
- def to_reportbuilder_html(generator)
39
- generator.add_css('"+@datadir+"/reportbuilder.css')
40
- end
41
- end ")
42
- @rp.add(mock_element)
43
- assert_match(/link rel='stylesheet'.+css\/reportbuilder.css/, @rp.to_html)
44
- assert(File.exists? @tmpdir+"/css/reportbuilder.css")
45
- end
46
-
58
+ eval(" class << mock_element
59
+ def report_building_html(generator)
60
+ generator.css('"+@datadir+"/reportbuilder.css')
61
+ end
62
+ end ")
63
+ @rp.add(mock_element)
64
+ assert_match(/link rel='stylesheet'.+css\/reportbuilder.css/, @rp.to_html)
65
+ assert(File.exists? @tmpdir+"/css/reportbuilder.css")
66
+ end
67
+
47
68
  end
data/test/test_image.rb CHANGED
@@ -1,20 +1,20 @@
1
- require "test/unit"
2
1
  $:.unshift(File.dirname(__FILE__)+"/../lib")
2
+ require 'test/unit'
3
+ require 'tmpdir'
3
4
  require "reportbuilder"
4
-
5
5
  class TestReportbuilderImage < Test::Unit::TestCase
6
6
  def setup
7
7
  @tmpdir=Dir::mktmpdir
8
- @rp=ReportBuilder.new("Test", @tmpdir)
8
+ @rp=ReportBuilder.new(:no_name=>true, :directory=>@tmpdir)
9
9
  @datadir=File.dirname(__FILE__)+"/../data"
10
- @rp.add(@rp.image(@datadir+"/sheep.jpg"))
10
+ @rp.add(ReportBuilder::Image.new(@datadir+"/sheep.jpg"))
11
11
  end
12
12
  def teardown
13
- FileUtils.remove_entry_secure @tmpdir
13
+ FileUtils.remove_entry_secure @tmpdir
14
14
  end
15
15
  def test_image_text
16
16
  expected= <<-HERE
17
- Report: Test
17
+ Test
18
18
  +--------------------------------+
19
19
  | *********#** |
20
20
  | ****#********# * |
@@ -31,12 +31,13 @@ Report: Test
31
31
  | ****#** *#***** |
32
32
  | **#***** |
33
33
  +--------------------------------+
34
- HERE
35
- puts @rp.to_s
36
- assert_equal(expected, @rp.to_s)
34
+ HERE
35
+ real=@rp.to_s
36
+ #expected=expected.gsub(/[^ ]/,'-')
37
+ assert_match(/[^\s]{12}$/,real)
37
38
  end
38
39
  def test_image_html
39
40
  assert_match(/img src='images\/sheep.jpg'/, @rp.to_html)
40
41
  end
41
-
42
+
42
43
  end
@@ -1,22 +1,81 @@
1
1
  require "test/unit"
2
2
  $:.unshift(File.dirname(__FILE__)+"/../lib")
3
3
  require "reportbuilder"
4
-
4
+ require 'hpricot'
5
+ require 'tempfile'
5
6
  class TestReportbuilder < Test::Unit::TestCase
7
+ def setup
8
+ @datadir=File.dirname(__FILE__)+"/../data"
9
+ @image=@datadir+"/sheep.jpg"
10
+ end
6
11
  # adding basic object
7
12
  def test_basic_generators
8
13
  # basic_classes = Array,String,Integer, Float,Time,Hash
9
- ary=[1,2,3]
10
- str="1,2,3"
11
- int=1
12
- flt=1.0
13
- rat=1.quo(2)
14
- tim=Time.new
15
- has={'a'=>1,'b'=>2}
14
+ args=[]
15
+ args << [1,2,3]
16
+ args << "1,2,3"
17
+ args << 1
18
+ args << 1.0
19
+ args << 1.quo(2)
20
+ args << Time.new
21
+ args << {'a'=>1,'b'=>2}
22
+ rb=ReportBuilder.new
23
+ args.each do |a|
24
+ assert_nothing_raised do
25
+ rb.add(a)
26
+ end
27
+ end
28
+ end
29
+ def test_using_block
16
30
  rb=ReportBuilder.new
17
- rb.add(ary,str,int,flt,rat,tim,has)
18
- assert_nothing_raised do
19
- rb.to_s
31
+ a=lambda {|g|
32
+ g.text "para"
33
+ g.preformatted "pre"
34
+ g.table(:header=>%w{th1 th2}) {row([1,2]); row([1,3])}
35
+ g.image @image
36
+
37
+ }
38
+ rb.add(a)
39
+ out=rb.to_text
40
+ ["para","pre","th1","th2"].each do |t|
41
+ assert_match(/#{t}/,out)
42
+ end
43
+
44
+ rb=ReportBuilder.new {|g|
45
+ g.text "para"
46
+ g.preformatted "pre"
47
+ g.table(:header=>%w{th1 th2}) {row([1,2]); row([1,3])}
48
+ g.image @image
49
+ }
50
+
51
+ out=rb.to_text
52
+
53
+ ["para","pre","th1","th2"].each do |t|
54
+ assert_match(/#{t}/,out)
55
+ end
56
+ end
57
+ def test_empty
58
+ rp=ReportBuilder.new
59
+ assert_match(/^Report.+\n$/,rp.to_s)
60
+ rp=ReportBuilder.new(:no_title=>true)
61
+ assert_equal("",rp.to_s)
62
+ rp.add("hola")
63
+ assert_equal("hola\n",rp.to_s)
64
+ end
65
+ def test_generate
66
+ res=ReportBuilder.generate(:format=>:text,:name=>"Test") do
67
+ text("hola")
68
+ end
69
+ assert_match(/^Test\nhola$/,res)
70
+ html_file=Tempfile.new("test.html")
71
+ html=ReportBuilder.generate(:name=>"Test", :format=>:html) do
72
+ text("hola")
73
+ end
74
+ ReportBuilder.generate(:name=>"Test", :filename=>html_file,:format=>:html) do
75
+ text("hola")
20
76
  end
77
+
78
+ assert_equal(html,File.read(html_file.path))
79
+
21
80
  end
22
81
  end
data/test/test_section.rb CHANGED
@@ -1,64 +1,80 @@
1
1
  require "test/unit"
2
2
  $:.unshift(File.dirname(__FILE__)+"/../lib")
3
3
  require "reportbuilder"
4
+ require 'hpricot'
4
5
 
5
6
  class TestReportbuilderSection < Test::Unit::TestCase
6
7
  def setup
7
- @rp=ReportBuilder.new("Test Report")
8
- s1=@rp.section(:name=>"Section 1")
8
+ @name="Test Section"
9
+ @rp=ReportBuilder.new(:name=>"Test Section")
10
+ s1=ReportBuilder::Section.new(:name=>"Section 1")
9
11
  @rp.add s1
10
12
  s1.add("Texto 1")
11
13
  s1.add(2)
12
- s11=@rp.section(:name=>"Section 1.1")
14
+ s11=ReportBuilder::Section.new(:name=>"Section 1.1")
13
15
  s1.add s11
14
- s111=@rp.section(:name=>"Section 1.1.1")
16
+ s111=ReportBuilder::Section.new(:name=>"Section 1.1.1")
15
17
  s11.add s111
16
18
  s1.add("Texto 1.1")
17
- s2=@rp.section(:name=>"Section 2")
19
+ s2=ReportBuilder::Section.new(:name=>"Section 2")
18
20
  @rp.add s2
19
21
  end
22
+ def test_section_generate
23
+ text=ReportBuilder.generate(:name=>@name,:format=>:text) {
24
+ section(:name=>"Section 1") {
25
+ text("Texto 1")
26
+ text("2")
27
+ section(:name=>"Section 1.1") {
28
+ section(:name=>"Section 1.1.1") {
29
+ }
30
+ }
31
+ text("Texto 1.1")
32
+ }
33
+ section(:name=>"Section 2")
34
+ }
35
+ assert_equal(@rp.to_text,text)
36
+ end
20
37
  def test_section_text
21
38
  expected= <<-HERE
22
- Report: Test Report
23
- = Section 1
24
- Texto 1
25
- 2
26
- == Section 1.1
27
- === Section 1.1.1
28
- Texto 1.1
29
- = Section 2
30
-
31
- HERE
32
- assert_equal(expected, @rp.to_s)
39
+ Test Section
40
+ = Section 1
41
+ Texto 1
42
+ 2
43
+ == Section 1.1
44
+ === Section 1.1.1
45
+ Texto 1.1
46
+ = Section 2
47
+ HERE
48
+ assert_equal(expected, @rp.to_s)
33
49
  end
34
50
  def test_section_html
35
- expected= <<-HERE
36
- <html><head><title>Test Report</title></head><body>
37
- <h1>Test Report</h1><div class='toc'><div class='title'>List of contents</div></div><ul>
38
- <li><a href='#toc_1'>Section 1</a></li>
39
- <ul>
40
- <li><a href='#toc_2'>Section 1.1</a></li>
41
- <ul>
42
- <li><a href='#toc_3'>Section 1.1.1</a></li>
43
- </ul>
44
- </ul>
45
- <li><a href='#toc_4'>Section 2</a></li>
46
- </ul>
47
- </div>
48
- <div class='section'><h2>Section 1</h2><a name='toc_1'></a>
49
- <pre>Texto 1</pre>
50
- <pre>2</pre>
51
- <div class='section'><h3>Section 1.1</h3><a name='toc_2'></a>
52
- <div class='section'><h4>Section 1.1.1</h4><a name='toc_3'></a>
53
- </div>
54
- </div>
55
- <pre>Texto 1.1</pre>
56
- </div>
57
- <div class='section'><h2>Section 2</h2><a name='toc_4'></a>
58
- </div>
59
- </body></html>
60
- HERE
61
- assert_equal(expected.gsub(/\s/,""), @rp.to_html.gsub(/\s/,""))
51
+ real=@rp.to_html
52
+ #puts real
53
+ doc = Hpricot(real)
54
+ # Sanity
55
+ assert_equal("Test Section", doc.search("head/title").inner_html)
56
+ # Test toc
57
+ base_ul=doc.search("/html/body/div[@id='toc']")
58
+ assert(base_ul!="")
59
+ #puts base_ul
60
+ [
61
+ ["li/a[@href='#toc_1']","Section 1"],
62
+ ["li/a[@href='#toc_2']","Section 1.1"],
63
+ ["li/a[@href='#toc_3']","Section 1.1.1"],
64
+ ["li/a[@href='#toc_4']","Section 2"]].each do |path, expected|
65
+ inner=base_ul.search(path).inner_html
66
+ assert_equal(expected, inner, "On #{path}")
67
+ end
68
+
69
+ [
70
+ ["div[class='section']/h2", ["Section 1","Section 2"]],
71
+ ["div[class='section']/h3",["Section 1.1"]],
72
+ ["div[class='section']/h4",["Section 1.1.1"]]
73
+ ].each do |path, expected|
74
+ base_ul.search(path).each do |el|
75
+ assert_equal(expected, el.inner_html, "On #{path} #{el}")
76
+ end
77
+ end
62
78
  end
63
-
79
+
64
80
  end
data/test/test_table.rb CHANGED
@@ -1,40 +1,55 @@
1
1
  require "test/unit"
2
2
  $:.unshift(File.dirname(__FILE__)+"/../lib")
3
3
  require "reportbuilder"
4
- require "reportbuilder/table/htmlgenerator"
5
- require "reportbuilder/table/textgenerator"
4
+ require 'reportbuilder/table/htmlgenerator'
5
+ require 'reportbuilder/table/textgenerator'
6
+ require 'hpricot'
6
7
 
7
8
  class TestReportbuilderTable < Test::Unit::TestCase
8
9
  def setup
9
10
  super
10
- table=ReportBuilder::Table.new(:header=>%w{a bb ccc dddd eeee fff gggg hh i})
11
- table.add_row(["a","b","c","d","e","f","g","h","i"])
12
- table.add_row([table.colspan("a",2),nil,"c",table.rowspan("d",2),"e","f","g","h","i"])
13
- table.add_row([table.colspan("a",3),nil,nil,nil,"e","f","g","h","i"])
14
- table.add_row([table.colspan("a",4),nil,nil,nil,"e","f","g","h","i"])
15
- table.add_row([table.colspan("a",5),nil,nil,nil,nil,table.colspan("f",3),nil,nil,"i"])
16
- table.add_row([table.colspan("a",6),nil,nil,nil,nil,nil,"g","h","i"])
17
- @table=table
11
+ @name="Table Test"
12
+ @header=%w{a bb ccc dddd eeee fff gggg hh i}
13
+ table=ReportBuilder::Table.new(:name=>"Table Test", :header=>@header) do
14
+ row(["a","b","c","d","e","f","g","h","i"])
15
+ row([colspan("a",2),nil,"c",rowspan("d",2),"e","f","g","h","i"])
16
+ row([colspan("a",3),nil,nil,nil,"e","f","g","h","i"])
17
+ row([colspan("a",4),nil,nil,nil,"e","f","g","h","i"])
18
+ row([colspan("a",5),nil,nil,nil,nil,colspan("f",3),nil,nil,"i"])
19
+ row([colspan("a",6),nil,nil,nil,nil,nil,"g","h","i"])
20
+ end
18
21
 
22
+ @table=table
23
+
19
24
  @mock_generator = ""
20
25
  class << @mock_generator
21
- def add_raw(t)
26
+ def preformatted(t)
22
27
  replace(t)
23
28
  end
24
- def add_text(t)
29
+ def text(t)
25
30
  replace(t)
26
31
  end
27
- def add_table_entry(t)
32
+ def table_entry(t)
28
33
  "MOCK"
29
34
  end
35
+ def html(t)
36
+ replace(t)
37
+ end
30
38
  end
31
-
39
+
40
+ end
41
+ def test_empty_table
42
+ text=ReportBuilder.generate(:no_title=>true,:format=>:text) do
43
+ table(:name=>"Table")
44
+ end
45
+ assert_match(/^Table\s+$/ , text)
32
46
  end
33
47
  def test_table_text
34
-
48
+
35
49
  tg=ReportBuilder::Table::TextGenerator.new(@mock_generator,@table)
36
50
  tg.generate
37
- expected= <<HEREDOC
51
+ expected= <<-HEREDOC
52
+ Table Test
38
53
  ----------------------------------------------------
39
54
  | a | bb | ccc | dddd | eeee | fff | gggg | hh | i |
40
55
  ----------------------------------------------------
@@ -47,14 +62,15 @@ class TestReportbuilderTable < Test::Unit::TestCase
47
62
  ----------------------------------------------------
48
63
  HEREDOC
49
64
 
50
- assert_equal(expected,@mock_generator)
65
+ assert_equal(expected,@mock_generator)
51
66
 
52
67
  end
53
68
  def test_table_html
54
-
69
+
55
70
  tg=ReportBuilder::Table::HtmlGenerator.new(@mock_generator,@table)
56
71
  tg.generate
57
- expected= <<HEREDOC
72
+
73
+ expected= <<HEREDOC
58
74
  <a name='MOCK'> </a>
59
75
  <table>
60
76
  <thead>
@@ -82,6 +98,22 @@ assert_equal(expected,@mock_generator)
82
98
  </table>
83
99
  HEREDOC
84
100
 
85
- assert_equal(expected.gsub(/\s/m,""), @mock_generator.gsub(/\s/m,""))
101
+ doc=Hpricot(@mock_generator)
102
+
103
+ assert(doc.search("a[@name='MOCK']")!="")
104
+ assert(doc.search("table"!=""))
105
+ assert_equal(@header, doc.search("table/thead/th").map {|m| m.inner_html} )
106
+ [[2,%w{a}],
107
+ [3,%w{a f}],
108
+ [4,%w{a}],
109
+ [5,%w{a}],
110
+ [6,%w{a}],
111
+
112
+ ].each do |m,exp|
113
+ real=doc.search("table/tbody/tr/td[@colspan='#{m}']").map {|x| x.inner_html}
114
+ assert_equal(exp, real, "On table/tbody/tr/td[@colspan='#{m}']"
115
+ )
116
+ end
117
+
86
118
  end
87
119
  end