mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Add basic repository tests
This commit is contained in:
@@ -35,9 +35,8 @@ module Linguist
|
|||||||
def initialize(paths)
|
def initialize(paths)
|
||||||
@paths = paths
|
@paths = paths
|
||||||
|
|
||||||
@stats = nil
|
@stats = nil
|
||||||
@languages = Hash.new { 0 }
|
@sizes = Hash.new { 0 }
|
||||||
@sizes = Hash.new { 0 }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](path)
|
def [](path)
|
||||||
@@ -45,7 +44,16 @@ module Linguist
|
|||||||
end
|
end
|
||||||
|
|
||||||
def language
|
def language
|
||||||
stats[:primary]
|
Language[stats[:primary]]
|
||||||
|
end
|
||||||
|
|
||||||
|
def languages
|
||||||
|
stats
|
||||||
|
@sizes
|
||||||
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
stats[:total_size]
|
||||||
end
|
end
|
||||||
|
|
||||||
def stats
|
def stats
|
||||||
@@ -59,8 +67,7 @@ module Linguist
|
|||||||
language = blob.language
|
language = blob.language
|
||||||
|
|
||||||
if language.common?
|
if language.common?
|
||||||
@languages[language.name] += 1
|
@sizes[language.name] += blob.size
|
||||||
@sizes[language.name] += blob.size
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
59
test/test_repository.rb
Normal file
59
test/test_repository.rb
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
require 'linguist/repository'
|
||||||
|
|
||||||
|
require 'test/unit'
|
||||||
|
|
||||||
|
class TestRepository < Test::Unit::TestCase
|
||||||
|
include Linguist
|
||||||
|
|
||||||
|
class FixtureBlob
|
||||||
|
def initialize(name, path)
|
||||||
|
@name = name
|
||||||
|
@path = path
|
||||||
|
end
|
||||||
|
|
||||||
|
def name
|
||||||
|
@name
|
||||||
|
end
|
||||||
|
|
||||||
|
def data
|
||||||
|
File.read(@path)
|
||||||
|
end
|
||||||
|
|
||||||
|
def size
|
||||||
|
File.size(@path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def repo(base_path)
|
||||||
|
paths = Dir["#{base_path}/**/*"].inject({}) do |h, path|
|
||||||
|
if File.file?(path)
|
||||||
|
name = path.sub("#{base_path}/", '')
|
||||||
|
h[name] = Blob.new(FixtureBlob.new(name, path))
|
||||||
|
end
|
||||||
|
h
|
||||||
|
end
|
||||||
|
Repository.new(paths)
|
||||||
|
end
|
||||||
|
|
||||||
|
def linguist_repo
|
||||||
|
repo(File.expand_path("../..", __FILE__))
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_lookup_path
|
||||||
|
assert linguist_repo['lib/linguist.rb']
|
||||||
|
assert_equal Language['Ruby'], linguist_repo['lib/linguist.rb'].language
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_linguist_language
|
||||||
|
assert_equal Language['Ruby'], linguist_repo.language
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_linguist_languages
|
||||||
|
assert linguist_repo.languages['Ruby'] > 30_000
|
||||||
|
assert linguist_repo.languages['Python'] < 1000
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_linguist_size
|
||||||
|
assert linguist_repo.size > 30_000
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user