This repository was archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest-value.js
133 lines (122 loc) · 4.35 KB
/
test-value.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
'use strict'
import test from 'tape'
import Immutable from 'immutable'
import {JSONLDValue} from '../ImmutableJSONLD'
test('test JSONLDValue construction via factory method', t => {
const value = JSONLDValue()
t.plan(9)
t.ok(value instanceof JSONLDValue,
'is a JSONLDValue')
t.ok(value instanceof Immutable.Map,
'is an Immutable.Map')
t.ok(value instanceof Immutable.Collection.Keyed,
'is an Immutable.Collection.Keyed')
t.ok(value instanceof Immutable.Collection,
'is an Immutable.Collection')
t.ok(value instanceof Immutable.Iterable,
'is an Immutable.Iterable')
t.ok(JSONLDValue.isJSONLDValue(value), 'isJSONLDValue()')
t.ok(Immutable.Map.isMap(value), 'isMap()')
t.ok(Immutable.Iterable.isIterable(value), 'isIterable()')
t.ok(Immutable.Iterable.isKeyed(value), 'isKeyed()')
})
test('test JSONLDValue @value is always set', t => {
const empty = JSONLDValue()
, string = JSONLDValue('hello')
, number = JSONLDValue(69)
, truth = JSONLDValue(true)
, falsity = JSONLDValue(false)
, nullity = JSONLDValue(null)
t.plan(9)
t.equal(empty.get('@value'), '')
t.equal(string.get('@value'), 'hello')
t.equal(number.get('@value'), 69)
t.equal(truth.get('@value'), true)
t.equal(falsity.get('@value'), false)
t.equal(nullity, empty)
try {JSONLDValue({})} catch (e) {
t.ok(e, 'obj without @value throws exception')
}
try {JSONLDValue([])} catch (e) {
t.ok(e, 'array throws exception')
}
try {JSONLDValue(x => x)} catch (e) {
t.ok(e, 'function throws exception')
}
})
test('test JSONLDValue.toString()', t => {
const empty = JSONLDValue()
, value = JSONLDValue({"@value": "Moby Dick"})
t.plan(2)
t.equals(empty.toString(), 'JSONLDValue { "@value": "" }',
'works for empty value')
t.equals(value.toString(), 'JSONLDValue { "@value": "Moby Dick" }',
'works for non-empty value')
})
test('test JSONLDValue.language', t => {
const val1 = JSONLDValue({'@value': 'Moby Dick'})
, val2 = JSONLDValue({'@value': 'Moby Dick', '@language': 'en'})
t.plan(6)
t.equals(val1.language, undefined)
t.equals(val2.language, 'en')
t.equals(val1.set('@language', 'ja').language, 'ja')
t.equals(val2.set('@language', 'ja').language, 'ja')
try {val1.language = 'en'} catch (e) {
t.ok(e instanceof TypeError, 'set throws TypeError')
t.ok(/^Cannot set property language/.test(e.message), 'with message')
}
})
test('test JSONLDValue.type', t => {
const val1 = JSONLDValue({'@value': 'Moby Dick'})
, val2 = JSONLDValue(
{ '@value': 'Moby Dick'
, '@type': 'http://www.w3.org/2001/XMLSchema#string'})
t.plan(6)
t.equals(val1.type, undefined)
t.equals(val2.type, 'http://www.w3.org/2001/XMLSchema#string')
t.equals(val1.set('@type', 'http://schema.org/Text').type,
'http://schema.org/Text')
t.equals(val2.set('@type', 'http://schema.org/Text').type,
'http://schema.org/Text')
try {val1.type = 'http://schema.org/Text'} catch (e) {
t.ok(e instanceof TypeError, 'set throws TypeError')
t.ok(/^Cannot set property type/.test(e.message), 'with message')
}
})
test('test JSONLDValue.value', t => {
const val1 = JSONLDValue({'@value': 'Moby Dick'})
t.plan(4)
t.equals(val1.value, 'Moby Dick')
t.equals(val1.set('@value', 'foo').value, 'foo')
try {val1.value = ''} catch (e) {
t.ok(e instanceof TypeError, 'set throws TypeError')
t.ok(/^Cannot set property value/.test(e.message), 'with message')
}
})
test('test JSONLDValue.delete()', t => {
const value = JSONLDValue({'@value': 'x'})
, expected = JSONLDValue({'@value': ''})
t.plan(1)
t.ok(value.delete('@value').equals(expected), 'returns expected value')
})
test('test JSONLDValue.remove()', t => {
const value = JSONLDValue({'@value': 'x'})
, expected = JSONLDValue({'@value': ''})
t.plan(1)
t.ok(value.remove('@value').equals(expected), 'returns expected value')
})
test('test JSONLDValue.set(foo, bar)', t => {
let value = JSONLDValue()
t.plan(1)
t.throws(() => value.set('foo', 'bar'),
/invalid value object keypath: \[ foo \]/)
})
test('test JSONLDValue.set(foo, bar) in production', t => {
let node_env = process.env['NODE_ENV']
process.env['NODE_ENV'] = 'production'
let value = JSONLDValue()
t.plan(2)
t.doesNotThrow(() => value.set('foo', 'bar'))
t.equals(value.set('foo', 'bar').get('foo'), 'bar')
process.env['NODE_ENV'] = node_env
})