73894036656cdf6f464a57b974b05756d9fe8b7d
[platform/framework/web/crosswalk-tizen.git] /
1 "use strict"
2
3 import test from 'tape'
4 import protochain from './'
5
6 test('protochain', t => {
7   t.test('finds correct prototype chain', t => {
8     let obj = {}
9     strictEqualArray(t, protochain(obj), [Object.prototype])
10     strictEqualArray(t, protochain(Object.create(obj)), [obj, Object.prototype])
11     strictEqualArray(t, protochain(new Error('message')), [Error.prototype, Object.prototype])
12     strictEqualArray(t, protochain(new TypeError('message')), [TypeError.prototype, Error.prototype, Object.prototype])
13     strictEqualArray(t, protochain(new String()), [String.prototype, Object.prototype])
14     strictEqualArray(t, protochain(new Number()), [Number.prototype, Object.prototype])
15     strictEqualArray(t, protochain(new RegExp('abc')), [RegExp.prototype, Object.prototype])
16     strictEqualArray(t, protochain(new Date()), [Date.prototype, Object.prototype])
17     t.end()
18   })
19
20   t.test('null prototype is handled correctly', t => {
21     let noProtoObject = Object.create(null)
22     strictEqualArray(t, protochain(noProtoObject), [])
23     strictEqualArray(t, protochain(Object.create(noProtoObject)), [noProtoObject])
24     t.end()
25   })
26
27   t.test('non-object values cooerce to object counterparts correctly', t => {
28     strictEqualArray(t, protochain('abc'), [String.prototype, Object.prototype])
29     strictEqualArray(t, protochain(123), [Number.prototype, Object.prototype])
30     strictEqualArray(t, protochain(/abc/), [RegExp.prototype, Object.prototype])
31     strictEqualArray(t, protochain(true), [Boolean.prototype, Object.prototype])
32     strictEqualArray(t, protochain(false), [Boolean.prototype, Object.prototype])
33     strictEqualArray(t, protochain(''), [String.prototype, Object.prototype])
34     strictEqualArray(t, protochain(0), [Number.prototype, Object.prototype])
35     t.end()
36   })
37
38   t.test('null values produce empty list', t => {
39     strictEqualArray(t, protochain(), [])
40     strictEqualArray(t, protochain(undefined), [])
41     strictEqualArray(t, protochain(null), [])
42     t.end()
43   })
44
45   t.test('examples', t => {
46     t.test('ES5', t => {
47       function Person() {}
48       function FancyPerson() {
49         Person.call(this)
50       }
51       FancyPerson.prototype = Object.create(Person.prototype)
52
53       strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype])
54       strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype])
55       t.end()
56     })
57     t.test('ES6', t => {
58       // note this will in-fact be compiled to ES5
59       class Person {}
60       strictEqualArray(t, protochain(new Person()), [Person.prototype, Object.prototype])
61
62       class FancyPerson extends Person {}
63       strictEqualArray(t, protochain(new FancyPerson()), [FancyPerson.prototype, Person.prototype, Object.prototype])
64       t.end()
65     })
66   })
67
68   // new native types which may not be supported
69
70   if (typeof Symbol !== 'undefined') {
71     t.test('symbol support', t => {
72       let foo = Symbol('foo')
73       strictEqualArray(t, protochain(foo), [Symbol.prototype, Object.prototype])
74       t.end()
75     })
76   }
77
78   if (typeof Promise !== 'undefined') {
79     t.test('promise support', t => {
80       let foo = new Promise((Y, N) => Y())
81       strictEqualArray(t, protochain(foo), [Promise.prototype, Object.prototype])
82       t.end()
83     })
84   }
85
86   t.end()
87 })
88
89 function strictEqualArray(t, a, b) {
90   a.forEach((item, index) => t.strictEqual(a[index], b[index], `strictEqual at index ${index}`))
91   t.equal(a.length, b.length, 'same length')
92 }