Add test/go/perm.js.

This commit is contained in:
David Braun 2017-10-11 11:37:57 -04:00
parent 17a2688291
commit a54414ac42
No known key found for this signature in database
GPG key ID: 5694EEC4D129BDCF

78
test/go/perm.js Normal file
View file

@ -0,0 +1,78 @@
// errorcheck
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test various correct and incorrect permutations of send-only,
// receive-only, and bidirectional channels.
'use strict'
const assert = require(`@nodeguy/assert`)
const Channel = require('../../lib')
it(`perm`, function () {
const c = Channel()
const cr = Channel().readOnly()
const cs = Channel().writeOnly()
const n = 0
assert.throws(() => {
Channel.shift(n) // ERROR "receive from non-chan"
})
assert.throws(() => {
Channel.push(2, n) // ERROR "send to non-chan"
})
c.push(0) // ok
c.shift() // ok
assert.throws(() => {
cr.push(0) // ERROR "send"
})
cr.shift() // ok
cs.push(0) // ok
assert.throws(() => {
cs.shift() // ERROR "receive"
})
Channel.select(
c.push(0), // ok
c.shift() // ok
)
assert.throws(() => {
Channel.select(
cr.push(0) // ERROR "send"
)
})
Channel.select(cr.shift()) // ok
Channel.select(cs.push(0)) // ok
assert.throws(() => {
Channel.select(cs.shift()) // ERROR "receive"
})
assert.throws(() => {
cs.forEach(() => {}) // ERROR "receive"
})
c.close()
cs.close()
assert.throws(() => {
cr.close() // ERROR "receive"
})
assert.throws(() => {
Channel.close(n) // ERROR "invalid operation.*non-chan type"
})
})