aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/attachment_to_html/adapters/text_spec.rb
blob: b2e8141e0c5f4ba357470da8e8b15c7b59525346 (plain)
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
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')

describe AttachmentToHTML::Adapters::Text do

    let(:attachment) { FactoryGirl.build(:body_text) }
    let(:adapter) { AttachmentToHTML::Adapters::Text.new(attachment) }

    describe :title do

        it 'uses the attachment filename for the title' do
            adapter.title.should == attachment.display_filename
        end
 
    end

    describe :body do

        it 'extracts the body from the document' do
            adapter.body.should == attachment.body
        end

        it 'strips the body of trailing whitespace' do
            attachment = FactoryGirl.build(:body_text, :body => ' Hello ')
            adapter = AttachmentToHTML::Adapters::Text.new(attachment) 
            adapter.body.should == 'Hello'
        end

        it 'escapes special characters' do
            attachment = FactoryGirl.build(:body_text, :body => 'Usage: foo "bar" >baz<')
            adapter = AttachmentToHTML::Adapters::Text.new(attachment)
            expected = %Q(Usage: foo &quot;bar&quot; &gt;baz&lt;)
            adapter.body.should == expected
        end

        it 'creates hyperlinks for text that looks like a url' do
            attachment = FactoryGirl.build(:body_text, :body => 'http://www.whatdotheyknow.com')
            adapter = AttachmentToHTML::Adapters::Text.new(attachment)
            expected = %Q(<a href='http://www.whatdotheyknow.com'>http://www.whatdotheyknow.com</a>)
            adapter.body.should == expected
        end

        it 'substitutes newlines for br tags' do
            attachment = FactoryGirl.build(:body_text, :body => "A\nNewline")
            adapter = AttachmentToHTML::Adapters::Text.new(attachment)
            expected = %Q(A<br>Newline)
            adapter.body.should == expected
        end

    end

    describe :success? do

        it 'is successful if the body has content excluding the tags' do
            adapter.stub(:body).and_return('<p>some content</p>')
            adapter.success?.should be_true
        end

        it 'is successful if the body contains images' do
            adapter.stub(:body).and_return(%Q(<img src="logo.png" />))
            adapter.success?.should be_true
        end

        it 'is not successful if the body has no content other than tags' do
            adapter.stub(:body).and_return('<p></p>')
            adapter.success?.should be_false
        end

    end

end