UNPKG

2.37 kBJavaScriptView Raw
1'use strict'
2/* eslint-env node, mocha */
3/* eslint-disable no-unused-expressions */
4
5const expect = require('chai').expect
6const path = require('path')
7const RangeAttribute = require(path.join(__dirname, '..', 'lib', 'client', 'RangeAttribute'))
8
9describe('Range Attribute Class', function () {
10 it('should return determine if ranges are present', function (done) {
11 let attrs = {}
12 let hasRanges = RangeAttribute.hasRangeAttributes(attrs)
13 expect(hasRanges).to.be.false
14
15 attrs = {
16 'member;range=0-5': {}
17 }
18 hasRanges = RangeAttribute.hasRangeAttributes(attrs)
19 expect(hasRanges).to.be.true
20
21 done()
22 })
23
24 it('should parse an attribute string with a range', function (done) {
25 const attr = 'member;range=0-5'
26 const rrsa = RangeAttribute(attr)
27 expect(rrsa.attributeName).to.equal('member')
28 expect(rrsa.low).to.equal(0)
29 expect(rrsa.high).to.equal(5)
30 done()
31 })
32
33 it('should parse multiple range attributes', function (done) {
34 const attrs = {
35 'member;range=0-5': {},
36 'foo;range=100-200': {}
37 }
38 const ranges = RangeAttribute.getRangeAttributes(attrs)
39 expect(ranges).to.not.be.null
40 expect(ranges).to.be.an.instanceof(Array)
41 expect(ranges.length).to.equal(2)
42
43 const range1 = ranges[0]
44 expect(range1).to.be.an.instanceof(RangeAttribute)
45 expect(range1.attributeName).to.equal('member')
46 expect(range1.low).to.equal(0)
47 expect(range1.high).to.equal(5)
48
49 const range2 = ranges[1]
50 expect(range2).to.be.an.instanceof(RangeAttribute)
51 expect(range2.attributeName).to.equal('foo')
52 expect(range2.low).to.equal(100)
53 expect(range2.high).to.equal(200)
54 done()
55 })
56
57 it('should get the next range for an attribute', function (done) {
58 const range = RangeAttribute.fromString('member;range=0-2')
59 const range2 = range.next()
60 expect(range2).to.not.be.null
61 expect(range2.attributeName).to.equal('member')
62 expect(range2.low).to.equal(3)
63 expect(range2.high).to.equal(6)
64 done()
65 })
66
67 it('should recognize the end of a range', function (done) {
68 const range = RangeAttribute.fromString('member;range=5-*')
69 expect(range).to.not.be.undefined
70 expect(range).to.be.an.instanceof(RangeAttribute)
71 expect(range.low).to.equal(5)
72 expect(range.high).to.be.null
73 expect(range.isComplete()).to.be.true
74 done()
75 })
76})