Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50640 views
1
###############################################################################
2
#
3
# CoCalc: Collaborative web-based calculation
4
# Copyright (C) 2017, Sagemath Inc.
5
# AGPLv3
6
#
7
###############################################################################
8
9
###
10
Test opts defaults handling code
11
###
12
13
opts = require('../opts')
14
underscore = require('underscore')
15
16
# ATTN: the order of these require statements is important,
17
# such that should & sinon work well together
18
sinon = require('sinon')
19
should = require('should')
20
require('should-sinon')
21
22
# Returns a new object with properties determined by those of obj1 and
23
# obj2. The properties in obj1 *must* all also appear in obj2. If an
24
# obj2 property has value "defaults.required", then it must appear in
25
# obj1. For each property P of obj2 not specified in obj1, the
26
# corresponding value obj1[P] is set (all in a new copy of obj1) to
27
# be obj2[P].
28
29
describe "default", ->
30
d = opts.defaults
31
required = opts.required
32
33
before =>
34
@debug_orig = global.DEBUG
35
global.DEBUG = true
36
37
after =>
38
global.DEBUG = @debug_orig
39
40
beforeEach =>
41
@console_debug_stub = sinon.stub(global.console, "warn")
42
@console_trace_stub = sinon.stub(global.console, "trace")
43
44
afterEach =>
45
@console_trace_stub.restore()
46
@console_debug_stub.restore()
47
48
it "returns a new object", ->
49
o1 = {}; o2 = {}
50
d(o1, o2).should.not.be.exactly(o1).and.not.exactly(o2)
51
52
it "properties of obj1 must appear in obj2", ->
53
obj1 =
54
foo: 1
55
bar: [1, 2, 3]
56
baz:
57
foo: "bar"
58
obj2 =
59
foo: 2
60
bar: [1, 2, 3]
61
baz:
62
foo: "bar"
63
exp =
64
foo: 1
65
bar: [1, 2, 3]
66
baz:
67
foo: "bar"
68
d(obj1, obj2).should.be.eql exp
69
70
it "raises exception for extra arguments", =>
71
obj1 = extra: true
72
obj2 = {}
73
(-> d(obj1, obj2)).should.throw /got an unexpected argument 'extra'/
74
@console_trace_stub.calledOnce.should.be.false()
75
#@console_debug_stub.getCall(0).args[0].should.match /(obj1={"extra":true}, obj2={})/
76
77
it "doesn't raises exception if extra arguments are allowed", =>
78
obj1 = extra: true
79
obj2 = {}
80
d(obj1, obj2, true)
81
@console_trace_stub.should.have.callCount 0
82
@console_debug_stub.should.have.callCount 0
83
84
it "raises an exception if obj2 has a `required` property but nothing in obj1", =>
85
obj1 = {}
86
obj2 =
87
r: required
88
(-> d(obj1, obj2)).should.throw /property \'r\' must be specified/
89
@console_trace_stub.calledOnce.should.be.false()
90
#@console_debug_stub.getCall(0).args[0].should.match /(obj1={}, obj2={"r":"__!!!!!!this is a required property!!!!!!__"})/
91
92
it "raises an exception if obj2 has a `required` property but is undefined in obj1", =>
93
obj1 =
94
r: undefined
95
obj2 =
96
r: required
97
(-> d(obj1, obj2)).should.throw /property \'r\' must be specified/
98
@console_trace_stub.calledOnce.should.be.false()
99
#@console_debug_stub.getCall(0).args[0].should.match /(obj1={}, obj2={"r":"__!!!!!!this is a required property!!!!!!__"})/
100
101
102
103