Skip to content

Commit

Permalink
migrated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-taylor committed Nov 15, 2024
1 parent 0fd02a0 commit 1cf96aa
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 2 deletions.
2 changes: 1 addition & 1 deletion srt.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (li LineItem) srtBytes() (c []byte) {
if pos != 0 {
c = append(c, []byte(fmt.Sprintf(`{\an%d}`, pos))...)
}
c = append(c, []byte(escapeWebVTT(li.Text))...)
c = append(c, []byte(li.Text)...)
if u {
c = append(c, []byte("</u>")...)
}
Expand Down
38 changes: 38 additions & 0 deletions srt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package astisub_test
import (
"bytes"
"io/ioutil"
"os"
"testing"

"github.com/asticode/go-astisub"
Expand Down Expand Up @@ -51,3 +52,40 @@ func TestNonUTF8SRT(t *testing.T) {
_, err := astisub.OpenFile("./testdata/example-in-non-utf8.srt")
assert.Error(t, err)
}

func TestSRTStyled(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-styled-in.srt")
assert.NoError(t, err)
assertStyledSubtitleItems(t, s)
assertSRTSubtitleStyles(t, s)

// No subtitles to write
w := &bytes.Buffer{}
err = astisub.Subtitles{}.WriteToSRT(w)
assert.EqualError(t, err, astisub.ErrNoSubtitlesToWrite.Error())

// Write
c, err := os.ReadFile("./testdata/example-styled-out.srt")
assert.NoError(t, err)
err = s.WriteToSRT(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}

func TestReadSRTWriteWebVTTStyled(t *testing.T) {
// Open
s, err := astisub.OpenFile("./testdata/example-styled-in.srt")
assert.NoError(t, err)
assertStyledSubtitleItems(t, s)
assertSRTSubtitleStyles(t, s)

w := &bytes.Buffer{}

// Write
c, err := os.ReadFile("./testdata/example-styled-out.vtt")
assert.NoError(t, err)
err = s.WriteToWebVTT(w)
assert.NoError(t, err)
assert.Equal(t, string(c), w.String())
}
54 changes: 54 additions & 0 deletions subtitles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,60 @@ func assertSubtitleItems(t *testing.T, i *astisub.Subtitles) {
assert.Equal(t, "electronic melody)", i.Items[5].Lines[1].String())
}

func assertStyledSubtitleItems(t *testing.T, i *astisub.Subtitles) {
assert.Len(t, i.Items, 6)
assert.Equal(t, 17*time.Second+985*time.Millisecond, i.Items[0].StartAt)
assert.Equal(t, 20*time.Second+521*time.Millisecond, i.Items[0].EndAt)
assert.Equal(t, "[instrumental music]", i.Items[0].Lines[0].String())
assert.Equal(t, 47*time.Second+115*time.Millisecond, i.Items[1].StartAt)
assert.Equal(t, 48*time.Second+282*time.Millisecond, i.Items[1].EndAt)
assert.Equal(t, "[ticks]", i.Items[1].Lines[0].String())
assert.Equal(t, 58*time.Second+192*time.Millisecond, i.Items[2].StartAt)
assert.Equal(t, 59*time.Second+727*time.Millisecond, i.Items[2].EndAt)
assert.Equal(t, "[instrumental music]", i.Items[2].Lines[0].String())
assert.Equal(t, 1*time.Minute+1*time.Second+662*time.Millisecond, i.Items[3].StartAt)
assert.Equal(t, 1*time.Minute+3*time.Second+63*time.Millisecond, i.Items[3].EndAt)
assert.Equal(t, "[dog barking]", i.Items[3].Lines[0].String())
assert.Equal(t, 1*time.Minute+26*time.Second+787*time.Millisecond, i.Items[4].StartAt)
assert.Equal(t, 1*time.Minute+29*time.Second+523*time.Millisecond, i.Items[4].EndAt)
assert.Equal(t, "[beeping]", i.Items[4].Lines[0].String())
assert.Equal(t, 1*time.Minute+29*time.Second+590*time.Millisecond, i.Items[5].StartAt)
assert.Equal(t, 1*time.Minute+31*time.Second+992*time.Millisecond, i.Items[5].EndAt)
assert.Equal(t, "[automated]", i.Items[5].Lines[0].String())
assert.Equal(t, "'The time is 7:35.'", i.Items[5].Lines[1].String())
}

func assertSRTSubtitleStyles(t *testing.T, i *astisub.Subtitles) {
assert.Len(t, i.Items, 6)
assert.Equal(t, "#00ff00", *i.Items[0].Lines[0].Items[0].InlineStyle.SRTColor)
assert.Zero(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTPosition)
assert.True(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTBold)
assert.False(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTItalics)
assert.False(t, i.Items[0].Lines[0].Items[0].InlineStyle.SRTUnderline)
assert.Equal(t, "#ff00ff", *i.Items[1].Lines[0].Items[0].InlineStyle.SRTColor)
assert.Zero(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTPosition)
assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTBold)
assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTItalics)
assert.False(t, i.Items[1].Lines[0].Items[0].InlineStyle.SRTUnderline)
assert.Equal(t, "#00ff00", *i.Items[2].Lines[0].Items[0].InlineStyle.SRTColor)
assert.Zero(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTPosition)
assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTBold)
assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTItalics)
assert.False(t, i.Items[2].Lines[0].Items[0].InlineStyle.SRTUnderline)
assert.Nil(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTColor)
assert.Zero(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTPosition)
assert.True(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTBold)
assert.False(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTItalics)
assert.True(t, i.Items[3].Lines[0].Items[0].InlineStyle.SRTUnderline)
assert.Nil(t, i.Items[4].Lines[0].Items[0].InlineStyle)
assert.Nil(t, i.Items[5].Lines[0].Items[0].InlineStyle)
assert.Nil(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTColor)
assert.Zero(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTPosition)
assert.False(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTBold)
assert.True(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTItalics)
assert.False(t, i.Items[5].Lines[1].Items[0].InlineStyle.SRTUnderline)
}

func mockSubtitles() *astisub.Subtitles {
return &astisub.Subtitles{Items: []*astisub.Item{{EndAt: 3 * time.Second, StartAt: time.Second, Lines: []astisub.Line{{Items: []astisub.LineItem{{Text: "subtitle-1"}}}}}, {EndAt: 7 * time.Second, StartAt: 3 * time.Second, Lines: []astisub.Line{{Items: []astisub.LineItem{{Text: "subtitle-2"}}}}}}}
}
Expand Down
24 changes: 24 additions & 0 deletions testdata/example-styled-in.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1
00:00:17,985 --> 00:00:20,521
<font color="#00ff00"><b>[instrumental music]</b></font>

2
00:00:47,115 --> 00:00:48,282
<font color="#ff00ff">[ticks]</font>

3
00:00:58,192 --> 00:00:59,727
<font color="#00ff00">[instrumental music]</font>

4
00:01:01,662 --> 00:01:03,063
<b><u>[dog barking]</u></b>

5
00:01:26,787 --> 00:01:29,523
[beeping]

6
00:01:29,590 --> 00:01:31,992
[automated]
<i>'The time is 7:35.'</i>
24 changes: 24 additions & 0 deletions testdata/example-styled-out.srt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
1
00:00:17,985 --> 00:00:20,521
<font color="#00ff00"><b>[instrumental music]</b></font>

2
00:00:47,115 --> 00:00:48,282
<font color="#ff00ff">[ticks]</font>

3
00:00:58,192 --> 00:00:59,727
<font color="#00ff00">[instrumental music]</font>

4
00:01:01,662 --> 00:01:03,063
<b><u>[dog barking]</u></b>

5
00:01:26,787 --> 00:01:29,523
[beeping]

6
00:01:29,590 --> 00:01:31,992
[automated]
<i>'The time is 7:35.'</i>
26 changes: 26 additions & 0 deletions testdata/example-styled-out.vtt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
WEBVTT
1
00:00:17.985 --> 00:00:20.521
<c.lime><b>[instrumental music]</b></c>

2
00:00:47.115 --> 00:00:48.282
<c.magenta>[ticks]</c>

3
00:00:58.192 --> 00:00:59.727
<c.lime>[instrumental music]</c>

4
00:01:01.662 --> 00:01:03.063
<b><u>[dog barking]</u></b>

5
00:01:26.787 --> 00:01:29.523
[beeping]

6
00:01:29.590 --> 00:01:31.992
[automated]
<i>'The time is 7:35.'</i>
4 changes: 3 additions & 1 deletion webvtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ const (

// Vars
var (
bytesWebVTTItalicEndTag = []byte("</i>")
bytesWebVTTItalicStartTag = []byte("<i>")
bytesWebVTTTimeBoundariesSeparator = []byte(webvttTimeBoundariesSeparator)
webVTTRegexpInlineTimestamp = regexp.MustCompile(`<((?:\d{2,}:)?\d{2}:\d{2}\.\d{3})>`)
webVTTRegexpTag = regexp.MustCompile(`(</*\s*([^\.\s]+)(\.[^\s/]*)*\s*([^/]*)\s*/*>)`)
webVTTEscaper = strings.NewReplacer("&", "&amp;", "<", "&lt;")
webVTTUnescaper = strings.NewReplacer("&nbsp;", " ", "&amp;", "&", "&lt;", "<")
webVTTUnescaper = strings.NewReplacer("&amp;", "&", "&lt;", "<")
)

// parseDurationWebVTT parses a .vtt duration
Expand Down

0 comments on commit 1cf96aa

Please sign in to comment.