Ben Hsieh 8 лет назад
Родитель
Сommit
cb859f996c
1 измененных файлов: 111 добавлений и 0 удалений
  1. 111
    0
      test/test-blob.js

+ 111
- 0
test/test-blob.js Просмотреть файл

@@ -154,3 +154,114 @@ describe('create blob using FormData', (report, done) => {
154 154
         done()
155 155
       })
156 156
 })
157
+
158
+// since 0.9.2
159
+// test case from :
160
+// https://github.com/w3c/web-platform-tests/blob/master/FileAPI/blob/Blob-slice.html
161
+describe('#89 Blob.slice test', (report, done) => {
162
+
163
+  let blob1, blob2
164
+  let count = 0
165
+  let testData
166
+
167
+  Blob
168
+    .build(["squiggle"])
169
+    .then((b) => {
170
+      blob1 = b
171
+      return Blob.build(["steak"], {type: "content/type"})
172
+    })
173
+    .then((b) => {
174
+      blob2 = b
175
+      setTestData()
176
+      startTest()
177
+    })
178
+
179
+  function setTestData() {
180
+    testData = [
181
+      [
182
+        ["PASSSTRING"],
183
+        [{start:  -6, contents: "STRING"},
184
+         {start: -12, contents: "PASSSTRING"},
185
+         {start:   4, contents: "STRING"},
186
+         {start:  12, contents: ""},
187
+         {start: 0, end:  -6, contents: "PASS"},
188
+         {start: 0, end: -12, contents: ""},
189
+         {start: 0, end:   4, contents: "PASS"},
190
+         {start: 0, end:  12, contents: "PASSSTRING"},
191
+         {start: 7, end:   4, contents: ""}]
192
+      ],
193
+      // Test 3 strings
194
+      [
195
+        ["foo", "bar", "baz"],
196
+        [{start:  0, end:  9, contents: "foobarbaz"},
197
+         {start:  0, end:  3, contents: "foo"},
198
+         {start:  3, end:  9, contents: "barbaz"},
199
+         {start:  6, end:  9, contents: "baz"},
200
+         {start:  6, end: 12, contents: "baz"},
201
+         {start:  0, end:  9, contents: "foobarbaz"},
202
+         {start:  0, end: 11, contents: "foobarbaz"},
203
+         {start: 10, end: 15, contents: ""}]
204
+      ],
205
+      // Test string, Blob, string
206
+      [
207
+        ["foo", blob1, "baz"],
208
+        [{start:  0, end:  3, contents: "foo"},
209
+         {start:  3, end: 11, contents: "squiggle"},
210
+         {start:  2, end:  4, contents: "os"},
211
+         {start: 10, end: 12, contents: "eb"}]
212
+      ],
213
+      // Test blob, string, blob
214
+      [
215
+        [blob1, "foo", blob1],
216
+        [{start:  0, end:  8, contents: "squiggle"},
217
+         {start:  7, end:  9, contents: "ef"},
218
+         {start: 10, end: 12, contents: "os"},
219
+         {start:  1, end:  4, contents: "qui"},
220
+         {start: 12, end: 15, contents: "qui"},
221
+         {start: 40, end: 60, contents: ""}]
222
+      ],
223
+      // Test blobs all the way down
224
+      [
225
+        [blob2, blob1, blob2],
226
+        [{start: 0,  end:  5, contents: "steak"},
227
+         {start: 5,  end: 13, contents: "squiggle"},
228
+         {start: 13, end: 18, contents: "steak"},
229
+         {start:  1, end:  3, contents: "te"},
230
+         {start:  6, end: 10, contents: "quig"}]
231
+      ]
232
+    ]
233
+  }
234
+
235
+  function startTest() {
236
+    Promise.all(testData.map(assert)).then(done)
237
+  }
238
+
239
+  function assert(d):Promise {
240
+    let content = d[0]
241
+    let assertions = d[1]
242
+    console.log('create blob content = ', content)
243
+    Blob.build(content).then((b) => {
244
+      for(let i in assertions) {
245
+        let args = assertions[i]
246
+        let target = b.slice(args.start, args.end)
247
+        target.onCreated((b2) => {
248
+          let raw = null
249
+          fs.readFile(b.blobPath, 'utf8').then((data) => {
250
+            raw = data
251
+            fs.readFile(b2.blobPath, 'utf8')
252
+              .then(function(actual){
253
+                console.log('---')
254
+                console.log('raw',data)
255
+                console.log('expect', this.contents)
256
+                console.log('actual', actual)
257
+                report(<Assert key={`assertion ${++count}`} expect={this.contents} actual={actual}/>)
258
+              }.bind(args))
259
+          })
260
+
261
+        })
262
+      }
263
+    })
264
+
265
+  }
266
+
267
+})