Skip to content

Commit 6c7d4d1

Browse files
author
Ciro S. Costa
committed
Starts adding the first bounds checking asserts for cut
1 parent 3b3a893 commit 6c7d4d1

File tree

6 files changed

+112
-19
lines changed

6 files changed

+112
-19
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ fmt:
55
go fmt ./...
66

77
test:
8-
go test ./cast -v
8+
go test ./... -v

editor/cut.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package editor
2+
3+
import (
4+
"github.com/cirocosta/asciinema-edit/cast"
5+
"github.com/pkg/errors"
6+
)
7+
8+
// Cut removes a piece of the cast event stream as specified by
9+
// `from` and `to`.
10+
//
11+
// It assumes that the provided `cast` is entirely valid (see
12+
// `github.com/cirocosta/asciinema-edit/cast#Validate`).
13+
//
14+
// 1. search a time that is close to `from`; then
15+
// 2. search a time that is close to `to`; then
16+
// 3. remove all in between; then
17+
// 4. adjust the time of the remaining.
18+
func Cut(c *cast.Cast, from, to float64) (err error) {
19+
if c == nil {
20+
err = errors.Errorf("a cast must be specified")
21+
return
22+
}
23+
24+
if len(c.EventStream) == 0 {
25+
err = errors.Errorf(
26+
"a cast with non-empty event stream must be supplied")
27+
return
28+
}
29+
30+
if from > to {
31+
err = errors.Errorf(
32+
"`from` cant be bigger than `to`")
33+
return
34+
}
35+
36+
return
37+
}

editor/cut_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package editor_test
2+
3+
import (
4+
. "github.com/onsi/ginkgo"
5+
. "github.com/onsi/gomega"
6+
7+
"github.com/cirocosta/asciinema-edit/cast"
8+
"github.com/cirocosta/asciinema-edit/editor"
9+
)
10+
11+
var (
12+
event1 = &cast.Event{
13+
Time: 1,
14+
Data: "event1",
15+
}
16+
event1_2 = &cast.Event{
17+
Time: 1.2,
18+
Data: "event1_2",
19+
}
20+
event1_6 = &cast.Event{
21+
Time: 1.6,
22+
Data: "event1_6",
23+
}
24+
event2 = &cast.Event{
25+
Time: 2,
26+
Data: "event2",
27+
}
28+
)
29+
30+
var _ = Describe("Cut", func() {
31+
Context("with nil cast", func() {
32+
It("fails", func() {
33+
err := editor.Cut(nil, 1, 2)
34+
Expect(err).ToNot(Succeed())
35+
})
36+
})
37+
38+
Context("with an empty event stream", func() {
39+
var data = &cast.Cast{
40+
EventStream: []*cast.Event{},
41+
}
42+
43+
It("errors", func() {
44+
err := editor.Cut(data, 1, 2)
45+
Expect(err).ToNot(Succeed())
46+
})
47+
})
48+
49+
Context("with non-empty event stream", func() {
50+
var (
51+
err error
52+
data *cast.Cast
53+
)
54+
55+
BeforeEach(func() {
56+
data = &cast.Cast{
57+
EventStream: []*cast.Event{
58+
event1,
59+
event1_2,
60+
event1_6,
61+
event2,
62+
},
63+
}
64+
})
65+
66+
It("fails if `from` > `to`", func() {
67+
err = editor.Cut(data, 3, 2)
68+
Expect(err).ToNot(Succeed())
69+
})
70+
})
71+
})

editor/doc.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package editor provides a set of functions to transform and
2+
// deal with the post production of asciinema casts.
3+
package editor

editor/editor.go

-1
This file was deleted.

editor/editor_test.go

-17
This file was deleted.

0 commit comments

Comments
 (0)