File size: 1,668 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package twiml

import (
	"encoding/xml"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestSay(t *testing.T) {
	checkExp := func(exp Say, doc, expDoc string) {
		t.Helper()
		var s Say
		err := xml.Unmarshal([]byte(doc), &s)
		require.NoError(t, err)
		assert.Equal(t, exp, s)

		data, err := xml.Marshal(s)
		require.NoError(t, err)
		assert.Equal(t, expDoc, string(data))
	}
	check := func(exp Say, doc string) {
		t.Helper()
		checkExp(exp, doc, doc)
	}

	check(Say{Content: "hi"}, `<Say>hi</Say>`)
	checkExp(Say{Content: "hi"}, `<Say loop="">hi</Say>`, `<Say>hi</Say>`)
	checkExp(Say{Content: "hi", LoopCount: 1000}, `<Say loop="0">hi</Say>`, `<Say loop="1000">hi</Say>`)
	check(Say{Content: "hi", LoopCount: 1}, `<Say loop="1">hi</Say>`)
	check(Say{Content: "hi", LoopCount: 1000}, `<Say loop="1000">hi</Say>`)
	check(Say{Content: "hi", Voice: "foo"}, `<Say voice="foo">hi</Say>`)
}

func TestPause(t *testing.T) {
	checkExp := func(exp Pause, doc, expDoc string) {
		t.Helper()
		var s Pause
		err := xml.Unmarshal([]byte(doc), &s)
		require.NoError(t, err)
		assert.Equal(t, exp, s)

		data, err := xml.Marshal(s)
		require.NoError(t, err)
		assert.Equal(t, expDoc, string(data))
	}
	check := func(exp Pause, doc string) {
		t.Helper()
		checkExp(exp, doc, doc)
	}

	checkExp(Pause{Dur: time.Second}, `<Pause></Pause>`, `<Pause length="1"></Pause>`)
	checkExp(Pause{Dur: time.Second}, `<Pause/>`, `<Pause length="1"></Pause>`)
	check(Pause{Dur: 2 * time.Second}, `<Pause length="2"></Pause>`)
	check(Pause{Dur: time.Second}, `<Pause length="1"></Pause>`)
	check(Pause{}, `<Pause length="0"></Pause>`)
}