Fix refs to a renamed constant
[gitorious:mainline.git] / test / unit / helpers / blobs_helper_test.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2012 Gitorious AS
4 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
5 #
6 #   This program is free software: you can redistribute it and/or modify
7 #   it under the terms of the GNU Affero General Public License as published by
8 #   the Free Software Foundation, either version 3 of the License, or
9 #   (at your option) any later version.
10 #
11 #   This program is distributed in the hope that it will be useful,
12 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #   GNU Affero General Public License for more details.
15 #
16 #   You should have received a copy of the GNU Affero General Public License
17 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #++
19
20 require "test_helper"
21
22 class BlobsHelperTest < ActionView::TestCase
23   include ERB::Util
24
25   def included_modules
26     (class << self; self; end).send(:included_modules)
27   end
28
29   should "includes the RepostoriesHelper" do
30     assert included_modules.include?(RepositoriesHelper)
31   end
32
33   should "includes the TreesHelper" do
34     assert included_modules.include?(TreesHelper)
35   end
36
37   context "render_highlighted()" do
38     should "html escape the line & add the proper class" do
39       res = render_highlighted("puts '<foo>'", "foo.rb")
40       assert res.include?(%Q{<td class="code"><pre class="prettyprint lang-rb">puts &#x27;&lt;foo&gt;&#x27;</pre></td>}), "Res: #{res}"
41     end
42
43     should "add line numbers" do
44       res = render_highlighted("alert('moo')\nalert('moo')", "foo.js")
45       assert res.include?(%Q{<td class="line-numbers"><a href="#line2" name="line2">2</a></td>} ), "Res: #{res}"
46     end
47   end
48
49   context "too_big_to_render" do
50     should "knows when a blob is too big to be rendered within reasonable time" do
51       assert !too_big_to_render?(1.kilobyte)
52       assert too_big_to_render?(350.kilobyte+1)
53     end
54   end
55
56   context "ascii/binary detection" do
57     should "know that a plain text file is fine" do
58       assert textual?(blob_with_name("jack.txt", "all work and no play"))
59     end
60
61     should "know that text files are fine" do
62       assert textual?(blob_with_name("foo.rb", "class Lulz; end"))
63       assert textual?(blob_with_name("foo.py", "class Lulz: pass"))
64       assert textual?(blob_with_name("foo.c", "void lulz()"))
65       assert textual?(blob_with_name("foo.m", "[lulz isForTehLaffs:YES]"))
66     end
67
68     should "know that binary are not textual" do
69       image = File.read(Rails.root + "public/images" + Gitorious::View::AvatarHelper::DEFAULT_USER_AVATAR_FILE)
70       assert !textual?(blob_with_name("foo.png", image))
71       assert !textual?(blob_with_name("foo.gif", "GIF89a\v\x00\r\x00\xD5!\x00\xBD"))
72       assert !textual?(blob_with_name("foo.exe", "rabuf\06si\000ezodniw"))
73       assert !textual?(blob_with_name("foo", "a"*1024 + "\000"))
74
75       assert image?(blob_with_name("foo.png"))
76       assert image?(blob_with_name("foo.gif"))
77       assert image?(blob_with_name("foo.jpg"))
78       assert image?(blob_with_name("foo.jpeg"))
79       assert !image?(blob_with_name("foo.exe"))
80     end
81   end
82
83   context "highlightable?" do
84     should "be highlightable if it is codeish" do
85       assert highlightable?(blob_with_name("foo.rb"))
86       assert highlightable?(blob_with_name("foo.c"))
87       assert highlightable?(blob_with_name("foo.h"))
88       assert highlightable?(blob_with_name("foo.py"))
89       assert highlightable?(blob_with_name("foo.css"))
90     end
91
92     should "not be highlightable if there is no file name extension" do
93       assert !highlightable?(blob_with_name("README"))
94     end
95
96     should "not be highlightable if it is a plaintext file" do
97       assert !highlightable?(blob_with_name("info.txt"))
98       assert !highlightable?(blob_with_name("info.textile"))
99     end
100   end
101
102   def blob_with_name(name, data = "")
103     repo = mock("grit repo")
104     Grit::Blob.create(repo, {:name => name, :data => data})
105   end
106
107   context "language highlightinh of a given filename" do
108     should "return the name of a known file type" do
109       assert_equal "list", language_of_file("foo.lisp")
110       assert_equal "css", language_of_file("foo.css")
111       assert_equal "lua", language_of_file("foo.lua")
112       assert_equal "scala", language_of_file("foo.scala")
113     end
114
115     should "return nil if the filename cannot be highlighted" do
116       assert_nil language_of_file("foo.bar")
117     end
118   end
119
120 end