6a7f0679 by David LaPalomento

Add method to test m3u8s directly against expected JSON output

There are a ton of tests in m3u8_test.js currently and it's getting a bit difficult to manage. Instead of hand-writing checks for each property of the parsed output, automatically convert manifests to javscript strings during the build process and then compare the parse of that string against a JSON file with the same name.
1 parent 69be9f80
1 'use strict'; 1 'use strict';
2 2
3 var basename = require('path').basename;
4
3 module.exports = function(grunt) { 5 module.exports = function(grunt) {
4 6
5 // Project configuration. 7 // Project configuration.
...@@ -12,7 +14,7 @@ module.exports = function(grunt) { ...@@ -12,7 +14,7 @@ module.exports = function(grunt) {
12 ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n', 14 ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */\n',
13 // Task configuration. 15 // Task configuration.
14 clean: { 16 clean: {
15 files: ['build', 'dist'] 17 files: ['build', 'dist', 'tmp']
16 }, 18 },
17 concat: { 19 concat: {
18 options: { 20 options: {
...@@ -93,11 +95,56 @@ module.exports = function(grunt) { ...@@ -93,11 +95,56 @@ module.exports = function(grunt) {
93 grunt.loadNpmTasks('grunt-contrib-jshint'); 95 grunt.loadNpmTasks('grunt-contrib-jshint');
94 grunt.loadNpmTasks('grunt-contrib-watch'); 96 grunt.loadNpmTasks('grunt-contrib-watch');
95 97
98 grunt.registerTask('manifests-to-js', 'Wrap the test fixtures and output' +
99 ' so they can be loaded in a browser',
100 function() {
101 var
102 jsManifests = 'window.manifests = {\n',
103 jsExpected = 'window.expected = {\n';
104 grunt.file.recurse('test/manifest/',
105 function(abspath, root, sub, filename) {
106 if ((/\.m3u8$/).test(abspath)) {
107
108 // translate this manifest
109 jsManifests += ' \'' + basename(filename, '.m3u8') + '\': ' +
110 grunt.file.read(abspath)
111 .split('\n')
112
113 // quote and concatenate
114 .map(function(line) {
115 return ' \'' + line + '\\n\' +\n';
116 }).join('')
117
118 // strip leading spaces and the trailing '+'
119 .slice(4, -3);
120 jsManifests += ',\n';
121 }
122
123 if ((/\.json$/).test(abspath)) {
124
125 // append the JSON
126 jsExpected += ' "' + basename(filename, '.json') + '": ' +
127 grunt.file.read(abspath) + ',\n';
128 }
129 });
130
131 // clean up and close the objects
132 jsManifests = jsManifests.slice(0, -2);
133 jsManifests += '\n};\n';
134 jsExpected = jsExpected.slice(0, -2);
135 jsExpected += '\n};\n';
136
137 // write out the manifests
138 grunt.file.write('tmp/manifests.js', jsManifests);
139 grunt.file.write('tmp/expected.js', jsExpected);
140 });
141
96 // Default task. 142 // Default task.
97 grunt.registerTask('default', 143 grunt.registerTask('default',
98 ['jshint', 144 ['clean',
145 'jshint',
146 'manifests-to-js',
99 'qunit', 147 'qunit',
100 'clean',
101 'concat', 148 'concat',
102 'uglify']); 149 'uglify']);
103 150
......
...@@ -1126,4 +1126,19 @@ ...@@ -1126,4 +1126,19 @@
1126 'the byterange offset was defaulted'); 1126 'the byterange offset was defaulted');
1127 }); 1127 });
1128 1128
1129 module('m3u8s');
1130
1131 test('parses the example manifests as expected', function() {
1132 var key;
1133 for (key in window.manifests) {
1134 if (window.expected[key]) {
1135 parser = new Parser();
1136 parser.push(window.manifests[key]);
1137 deepEqual(parser.manifest,
1138 window.expected[key],
1139 key + '.m3u8 was parsed correctly');
1140 }
1141 }
1142 });
1143
1129 })(window, window.console); 1144 })(window, window.console);
......
1 {
2 "allowCache": true,
3 "targetDuration": 10,
4 "mediaSequence": 0,
5 "playlistType": "VOD",
6 "segments": [{
7 "duration": 10,
8 "byterange": {
9 "length": 522828,
10 "offset": 0
11 },
12 "uri": "hls_450k_video.ts"
13 }, {
14 "duration": 10,
15 "byterange": {
16 "length": 587500,
17 "offset": 522828
18 },
19 "uri": "hls_450k_video.ts"
20 }, {
21 "duration": 10,
22 "byterange": {
23 "length": 713084,
24 "offset": 1110328
25 },
26 "uri": "hls_450k_video.ts"
27 }, {
28 "duration": 10,
29 "byterange": {
30 "length": 476580,
31 "offset": 1823412
32 },
33 "uri": "hls_450k_video.ts"
34 }, {
35 "duration": 10,
36 "byterange": {
37 "length": 535612,
38 "offset": 2299992
39 },
40 "uri": "hls_450k_video.ts"
41 }, {
42 "duration": 10,
43 "byterange": {
44 "length": 207176,
45 "offset": 2835604
46 },
47 "uri": "hls_450k_video.ts"
48 }, {
49 "duration": 10,
50 "byterange": {
51 "length": 455900,
52 "offset": 3042780
53 },
54 "uri": "hls_450k_video.ts"
55 }, {
56 "duration": 10,
57 "byterange": {
58 "length": 657248,
59 "offset": 3498680
60 },
61 "uri": "hls_450k_video.ts"
62 }, {
63 "duration": 10,
64 "byterange": {
65 "length": 571708,
66 "offset": 4155928
67 },
68 "uri": "hls_450k_video.ts"
69 }, {
70 "duration": 10,
71 "byterange": {
72 "length": 485040,
73 "offset": 4727636
74 },
75 "uri": "hls_450k_video.ts"
76 }, {
77 "duration": 10,
78 "byterange": {
79 "length": 709136,
80 "offset": 5212676
81 },
82 "uri": "hls_450k_video.ts"
83 }, {
84 "duration": 10,
85 "byterange": {
86 "length": 730004,
87 "offset": 5921812
88 },
89 "uri": "hls_450k_video.ts"
90 }, {
91 "duration": 10,
92 "byterange": {
93 "length": 456276,
94 "offset": 6651816
95 },
96 "uri": "hls_450k_video.ts"
97 }, {
98 "duration": 10,
99 "byterange": {
100 "length": 468684,
101 "offset": 7108092
102 },
103 "uri": "hls_450k_video.ts"
104 }, {
105 "duration": 10,
106 "byterange": {
107 "length": 444996,
108 "offset": 7576776
109 },
110 "uri": "hls_450k_video.ts"
111 }, {
112 "duration": 10,
113 "byterange": {
114 "length": 331444,
115 "offset": 8021772
116 },
117 "uri": "hls_450k_video.ts"
118 }, {
119 "duration": 1.4167,
120 "byterange": {
121 "length": 44556,
122 "offset": 8353216
123 },
124 "uri": "hls_450k_video.ts"
125 }]
126 }
...@@ -34,11 +34,13 @@ ...@@ -34,11 +34,13 @@
34 <script src="manifest/playlist_allow_cache_template.js"></script> 34 <script src="manifest/playlist_allow_cache_template.js"></script>
35 <script src="manifest/playlist_byte_range_template.js"></script> 35 <script src="manifest/playlist_byte_range_template.js"></script>
36 36
37 <script src="../tmp/manifests.js"></script>
38 <script src="../tmp/expected.js"></script>
39
37 <!-- M3U8 --> 40 <!-- M3U8 -->
38 <!-- SEGMENT --> 41 <!-- SEGMENT -->
39 <script src="tsSegment-bc.js"></script> 42 <script src="tsSegment-bc.js"></script>
40 <script src="../src/segment-controller.js"></script> 43 <script src="../src/segment-controller.js"></script>
41
42 <script src="../src/bin-utils.js"></script> 44 <script src="../src/bin-utils.js"></script>
43 45
44 <!-- Test cases --> 46 <!-- Test cases -->
......