Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Filter out" bad subs #133

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lunit 0.5-1
108 changes: 108 additions & 0 deletions tests/vlsub_core.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require "lunit"
package.path = package.path .. ";../vlsub.lua"
local vlsub = require("vlsub")

module( "vlsub_core", lunit.testcase, package.seeall)

function test_last_subtitle_time_text_shouldReturnQuestionMark_WhenNoDuration()
-- Given
lang_mock = {int_last_sub="last sub at"}
subtitle_mock = {SubLastTS=nil}
-- When
local result = vlsub.last_subtitle_time_text(subtitle_mock, lang_mock)
-- Then
assert_equal("[last sub at ?]", result)
end

function test_last_subtitle_time_text_shouldReturnQuestionMark_WhenDurationFormatIsWrong()
-- Given
lang_mock = {int_last_sub="last sub at"}
subtitle_mock = {SubLastTS="123456"}
-- When
local result = vlsub.last_subtitle_time_text(subtitle_mock, lang_mock)
-- Then
assert_equal("[last sub at ?]", result)
end

function test_last_subtitle_time_text_shouldReturnDuration_WhenDurationFormatIsRight()
-- Given
lang_mock = {int_last_sub="last sub at"}
subtitle_mock = {SubLastTS="12:34:56"}
-- When
local result = vlsub.last_subtitle_time_text(subtitle_mock, lang_mock)
-- Then
assert_equal("[last sub at 12:34:56]", result)
end

function test_date_string_to_time_shouldReturnTime_FromStringFormatedWithHoursMinutesSeconds()
-- When
local result = vlsub.date_string_to_time("01:02:03")

-- Then
assert_equal(3723, result)
end

function test_date_string_to_time_shouldReturnZero_WhenGivenMalformatedString()
-- When
local result = vlsub.date_string_to_time("01.23azerty")

-- Then
assert_equal(0, result)
end

function test_order_subs_shouldOrderUnorderedSubs_ByDistanceBetweenLastSpokenLineAndMovieDuration()
-- Given
local movie_duration = 60
local unordered_table = {}
local incorrect = {SubLastTS="xxxxx"}
local greater = {SubLastTS="00:01:10"}
local equals = {SubLastTS="00:01:00"}
local lesser = {SubLastTS="00:00:10"}
table.insert(unordered_table, greater)
table.insert(unordered_table, incorrect)
table.insert(unordered_table, equals)
table.insert(unordered_table, lesser)

-- When
local result = vlsub.order_by_ascending_distance_between_last_sub_time_and_movie_duration(unordered_table, movie_duration)

-- Then
assert_equal(table_tostring({equals, greater, lesser, incorrect}), table_tostring(result))
end

-- test-utils
function table_val_to_str ( v )
if "string" == type( v ) then
v = string.gsub( v, "\n", "\\n" )
if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
return "'" .. v .. "'"
end
return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
else
return "table" == type( v ) and table_tostring( v ) or
tostring( v )
end
end

function table_key_to_str ( k )
if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
return k
else
return "[" .. table_val_to_str( k ) .. "]"
end
end

function table_tostring( tbl )
local result, done = {}, {}
for k, v in ipairs( tbl ) do
table.insert( result, table_val_to_str( v ) )
done[ k ] = true
end
for k, v in pairs( tbl ) do
if not done[ k ] then
table.insert( result,
table_key_to_str( k ) .. "=" .. table_val_to_str( v ) )
end
end
return "{" .. table.concat( result, "," ) .. "}"
end
44 changes: 44 additions & 0 deletions tests/vlsub_interface_data.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "lunit"
package.path = package.path .. ";../vlsub.lua"
local vlsub = require("vlsub")

module( "vlsub_interface_data", lunit.testcase, package.seeall)

function test_movie_length_text_shouldReturnNone_WhenNoMovie()
-- Given
function item_mock(string)
return nil
end
vlc_mock = {input={item=item_mock}}
lang_mock = {int_movie_duration="movie duration"}
-- When
local result = vlsub.movie_duration_text(vlc_mock, lang_mock)
-- Then
assert_equal(result, "movie duration : ?")
end

function test_movie_length_text_shouldReturnNone_WhenMovieHasNone()
-- Given
function item_mock(string)
return {duration=function() return -1 end}
end
vlc_mock = {input={item=item_mock}}
lang_mock = {int_movie_duration="movie duration"}
-- When
local result = vlsub.movie_duration_text(vlc_mock, lang_mock)
-- Then
assert_equal(result, "movie duration : ?")
end

function test_movie_length_text_shouldReturnLength_WhenMovieHasOne()
-- Given
function item_mock(string)
return {duration=function() return 70 end}
end
vlc_mock = {input={item=item_mock}}
lang_mock = {int_movie_duration="movie duration"}
-- When
local result = vlsub.movie_duration_text(vlc_mock, lang_mock)
-- Then
assert_equal(result, "movie duration : 00:01:10")
end
Loading