forked from rtomayko/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtilt_csv_test.rb
77 lines (62 loc) · 2.12 KB
/
tilt_csv_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require 'test_helper'
require 'tilt'
begin
require 'tilt/csv'
class CSVTemplateTest < Minitest::Test
test "registered for '.rcsv' files" do
assert_equal Tilt::CSVTemplate, Tilt['rcsv']
end
test "compiles and evaluates the template on #render" do
template = Tilt::CSVTemplate.new { "csv << ['hello', 'world']" }
assert_equal "hello,world\n", template.render
end
test "can be rendered more than once" do
template = Tilt::CSVTemplate.new { "csv << [1,2,3]" }
3.times { assert_equal "1,2,3\n", template.render }
end
test "can pass locals" do
template = Tilt::CSVTemplate.new { 'csv << [1, name]' }
assert_equal "1,Joe\n", template.render(Object.new, :name => 'Joe')
end
test "evaluating in an object scope" do
template = Tilt::CSVTemplate.new { 'csv << [1, @name]' }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
assert_equal "1,Joe\n", template.render(scope)
end
test "backtrace file and line reporting" do
data = File.read(__FILE__).split("\n__END__\n").last
template = Tilt::CSVTemplate.new('test.csv') { data }
begin
template.render
fail 'should have raised an exception'
rescue => boom
assert_kind_of NameError, boom
line = boom.backtrace.grep(/^test\.csv:/).first
assert line, "Backtrace didn't contain test.csv"
_file, line, _meth = line.split(":")
assert_equal '4', line
end
end
test "passing options to engine" do
template = Tilt::CSVTemplate.new(:col_sep => '|') { 'csv << [1,2,3]' }
assert_equal "1|2|3\n", template.render
end
test "outvar option" do
outvar = '@_output'
scope = Object.new
template = Tilt::CSVTemplate.new(:outvar => outvar) { 'csv << [1,2,3]' }
output = template.render(scope)
assert_equal output, scope.instance_variable_get(outvar.to_sym)
end
end
rescue LoadError
warn "Tilt::CSVTemplate (disabled) please install 'fastercsv' if using ruby 1.8.x"
end
__END__
# header
csv << ['Type', 'Age']
raise NameError
# rows
csv << ['Frog', 2]
csv << ['Cat', 5]