File size: 3,469 Bytes
402daee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package.path = '../common/libs/?.lua;../common/vendor/?.lua;' .. package.path

app = {}

local lu = require('luaunit')

require('mock_reaper')
require('Polo')
require('source/CSVWriter')
require('source/Transcript')

--

reaper.GetMediaItemTake_Source = function () return {fileName = "test_audio.wav"} end
reaper.GetMediaSourceFileName = function (source) return source.fileName end

TestCSVWriter = {}

function TestCSVWriter:setUp()
  function app:trap(f) return xpcall(f, function(e) print(tostring(e)) end) end

  reaper.__test_setUp()
end

function TestCSVWriter.make_transcript()
  local t = Transcript.new()
  t:add_segment(TranscriptSegment.new {
    data = {start = 0, ['end'] = 1, text = 'hello'},
    item = {},
    take = {}
  })
  t:add_segment(TranscriptSegment.new {
    data = {start = 1, ['end'] = 2, text = 'world'},
    item = {},
    take = {}
  })
  t:add_segment(TranscriptSegment.new {
    data = {start = 2, ['end'] = 3, text = 'something in "quotes"'},
    item = {},
    take = {}
  })
  t:update()

  return t
end

function TestCSVWriter:testFormatTime()
    lu.assertEquals(CSVWriter.format_time(0), '00:00:00,000')
    lu.assertEquals(CSVWriter.format_time(1), '00:00:01,000')
    lu.assertEquals(CSVWriter.format_time(1.5), '00:00:01,500')
    lu.assertEquals(CSVWriter.format_time(60), '00:01:00,000')
    lu.assertEquals(CSVWriter.format_time(60.5), '00:01:00,500')
    lu.assertEquals(CSVWriter.format_time(3600), '01:00:00,000')
    lu.assertEquals(CSVWriter.format_time(3600.5), '01:00:00,500')
  end

function TestCSVWriter:testInit()
  local f = {}
  local writer = CSVWriter.new { file = f }
end

function TestCSVWriter:testInitNoFile()
  lu.assertErrorMsgContains('missing file', CSVWriter.new)
end

function TestCSVWriter:testWrite()
  local t = TestCSVWriter.make_transcript()
  local output = {}
  local f = {
    write = function (self, s)
      table.insert(output, s)
    end
  }
  local writer = CSVWriter.new { file = f }
  writer:write(t)
  local output_str = table.concat(output)
  lu.assertEquals(output_str, '1,"00:00:00,000","00:00:01,000","hello","test_audio.wav"\n2,"00:00:01,000","00:00:02,000","world","test_audio.wav"\n3,"00:00:02,000","00:00:03,000","something in ""quotes""","test_audio.wav"\n')
end

function TestCSVWriter:testCustomDelimiter()
  local t = TestCSVWriter.make_transcript()

  local output = {}
  local f = {
    write = function (self, s)
      table.insert(output, s)
    end
  }
  local writer = CSVWriter.new { file = f, delimiter = '\t' }
  writer:write(t)
  local output_str = table.concat(output)
  lu.assertEquals(output_str, '1\t"00:00:00,000"\t"00:00:01,000"\t"hello"\t"test_audio.wav"\n2\t"00:00:01,000"\t"00:00:02,000"\t"world"\t"test_audio.wav"\n3\t"00:00:02,000"\t"00:00:03,000"\t"something in ""quotes"""\t"test_audio.wav"\n')
end

function TestCSVWriter:testIncludeHeaderRow()
  local t = TestCSVWriter.make_transcript()

  local output = {}
  local f = {
    write = function (self, s)
      table.insert(output, s)
    end
  }
  local writer = CSVWriter.new { file = f, include_header_row = true }
  writer:write(t)
  local output_str = table.concat(output)
  lu.assertEquals(output_str, '"Sequence Number","Start Time","End Time","Text","File"\n1,"00:00:00,000","00:00:01,000","hello","test_audio.wav"\n2,"00:00:01,000","00:00:02,000","world","test_audio.wav"\n3,"00:00:02,000","00:00:03,000","something in ""quotes""","test_audio.wav"\n')
end

--

os.exit(lu.LuaUnit.run())