Skip to content

Commit

Permalink
Add tests for Perl and Turning detection
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 21, 2011
1 parent 0b5a265 commit e4fe1d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/linguist/blob_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,20 @@ def guess_r_language

# Internal: Guess language of .t files.
#
# Makes fairly sure that it is Turing.
# Turing is not very popular so it would not be good to have perl users' files being confused.
#
# Returns a Language.
def guess_t_language
if lines.grep(/:=/).any? && lines.grep(/proc |procedure |fcn |function /).any? && lines.grep(/var/).any?
score = 0
score += 1 if lines.grep(/^% /).any?
score += data.gsub(/ := /).count
score += data.gsub(/proc |procedure |fcn |function /).count
score += data.gsub(/var \w+: \w+/).count

# Tell-tale signs its gotta be Perl
if lines.grep(/^(my )?(sub |\$|@|%)\w+/).any?
score = 0
end

if score >= 3
Language['Turing']
else
Language['Perl']
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/perl-test.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use strict;
use warnings;

use Foo::Bar

$n = 42;
$name = "world";
@array = ("1","2","3");
%hash = ("foo":"bar");
my $name = "josh";
19 changes: 19 additions & 0 deletions test/fixtures/turing.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
% Accepts a number and calculates its factorial

function factorial (n: int) : real
if n = 0 then
result 1
else
result n * factorial (n - 1)
end if
end factorial

var n: int
loop
put "Please input an integer: " ..
get n
exit when n >= 0
put "Input must be a non-negative integer."
end loop

put "The factorial of ", n, " is ", factorial (n)
4 changes: 4 additions & 0 deletions test/test_blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ def test_language
assert_equal Language['R'], blob("hello-r.R").language
assert_equal Language['Rebol'], blob("hello-rebol.r").language

# .t disambiguation
assert_equal Language['Perl'], blob("perl-test.t").language
assert_equal Language['Turing'], blob("turing.t").language

# ML
assert_equal Language['OCaml'], blob("Foo.ml").language
assert_equal Language['Standard ML'], blob("Foo.sig").language
Expand Down
3 changes: 3 additions & 0 deletions test/test_language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ def test_ambiguous_extensions

assert Language.ambiguous?('.r')
assert_equal Language['R'], Language.find_by_extension('r')

assert Language.ambiguous?('.t')
assert_equal Language['Perl'], Language.find_by_extension('t')
end

def test_lexer
Expand Down

0 comments on commit e4fe1d1

Please sign in to comment.