Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add test for top-level interface

  • Loading branch information...
commit 4ec734d958edee8dc4fb7d4921c41ecc5b25f08a 1 parent b5ef6d8
@grosser authored
View
3  lib/language_sniffer.rb
@@ -1,10 +1,11 @@
require 'language_sniffer/blob_helper'
+require 'language_sniffer/file_blob'
require 'language_sniffer/language'
require 'language_sniffer/pathname'
module LanguageSniffer
def self.detect(options)
raise "I need :path" if not options[:path]
- FileProxy.new(options[:path])
+ FileBlob.new(options[:path], options[:pwd], options[:content])
end
end
View
12 lib/language_sniffer/file_blob.rb
@@ -13,9 +13,10 @@ class FileBlob
# base_path - Optional base to relativize the path
#
# Returns a FileBlob.
- def initialize(path, base_path = nil)
+ def initialize(path, base_path = nil, data=nil)
@path = path
@name = base_path ? path.sub("#{base_path}/", '') : path
+ @data = data
end
# Public: Filename
@@ -36,14 +37,7 @@ def initialize(path, base_path = nil)
#
# Returns a String.
def data
- File.read(@path)
- end
-
- # Public: Get byte size
- #
- # Returns an Integer.
- def size
- File.size(@path)
+ @data ||= File.read(@path)
end
end
end
View
4 test/test_blob.rb
@@ -29,10 +29,6 @@ def test_lines
assert_equal ["module Foo", "end", ""], blob("foo.rb").lines
end
- def test_size
- assert_equal 15, blob("foo.rb").size
- end
-
def test_loc
assert_equal 3, blob("foo.rb").loc
end
View
2  test/test_cli.rb
@@ -1,6 +1,6 @@
require 'test/unit'
-class TestBlob < Test::Unit::TestCase
+class TestCLI < Test::Unit::TestCase
def test_cli
assert_equal "test/fixtures/bar.h: 11 lines (7 sloc)\n extension: .h\n language: C++\n", `bundle exec ruby bin/language_sniffer test/fixtures/bar.h`
assert_equal "usage: language_sniffer <file>\n", `bundle exec ruby bin/language_sniffer 2>&1`
View
18 test/test_sniffer.rb
@@ -0,0 +1,18 @@
+require 'language_sniffer'
+
+require 'test/unit'
+class TestSniffer < Test::Unit::TestCase
+ def test_sniffer
+ # path and content given -> does not read file
+ assert_equal 'Ruby', LanguageSniffer.detect(:path => 'foo/bar', :content => '#!/usr/bin/env ruby').language.name
+
+ # pure un-descriptive path -> reads file
+ assert_equal 'Ruby', LanguageSniffer.detect(:path => 'bin/language_sniffer').language.name
+
+ # pure descriptive path -> does not need to read file
+ assert_equal 'Ruby', LanguageSniffer.detect(:path => 'foo/bar.rb').language.name
+
+ # empty
+ assert_equal nil, LanguageSniffer.detect(:path => 'test/fixtures/blank').language
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.