Skip to content

Commit

Permalink
Allow a TypeError instance to be stored in a Record field with typed.…
Browse files Browse the repository at this point in the history
…Any and add unit tests to verify the fix. (#42)
  • Loading branch information
jharris4 authored and lukesneeringer committed Feb 16, 2017
1 parent 06da956 commit 44c2a0a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/record.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Typed, typeOf, construct} from "./typed"
import {Typed, typeOf, construct, Any} from "./typed"
import {Seq, Iterable, Map} from 'immutable'

const {Keyed} = Iterable
Expand Down Expand Up @@ -67,7 +67,7 @@ class TypedRecord extends IterableKeyedBase {
const value = seq.has(key) ? seq.get(key) : this.get(key)
const result = fieldType[$read](value)

if (result instanceof TypeError) {
if (fieldType !== Any && result instanceof TypeError) {
return TypeError(`Invalid value for "${key}" field:\n ${result.message}`)
}

Expand Down Expand Up @@ -147,7 +147,7 @@ class TypedRecord extends IterableKeyedBase {

const result = fieldType[$read](value)

if (result instanceof TypeError) {
if (fieldType !== Any && result instanceof TypeError) {
throw TypeError(`Invalid value for ${key} field: ${result.message}`)
}

Expand Down
41 changes: 40 additions & 1 deletion src/test/record.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import test from "./test"
import * as Immutable from "immutable"
import {Record} from "../record"
import {Typed, typeOf, Union, Maybe} from "../typed"
import {Typed, typeOf, Any} from "../typed"

const Point = Record({
x: Number(0),
y: Number(0)
}, "Point")

const PointAndAny = Record({
x: Number(0),
y: Number(0),
any: Any
}, "PointAndAny")

test("reading record short-cirquits", assert => {
const v = Point()
const reader = typeOf(Point)
Expand Down Expand Up @@ -40,6 +46,39 @@ test("reading records", assert => {
assert.deepEqual(v4.toJSON(), {x:1,y:2})
})

test("reading records with TypeError in Any field", assert => {
const reader = typeOf(PointAndAny)

const aTypeError = new TypeError('error message')

const v1 = reader[Typed.read]()
const v2 = reader[Typed.read]({x:10})
const v3 = reader[Typed.read]({y:10})
const v4 = reader[Typed.read]({x:1, y:2})
const v5 = reader[Typed.read]({x:1, y:2, any: 'any'})
const v6 = reader[Typed.read]({x:1, y:2, any: aTypeError})

assert.ok(v1 instanceof Record)
assert.ok(v2 instanceof Record)
assert.ok(v3 instanceof Record)
assert.ok(v4 instanceof Record)
assert.ok(v5 instanceof Record)
assert.ok(v6 instanceof Record)

assert.ok(v1 instanceof PointAndAny)
assert.ok(v2 instanceof PointAndAny)
assert.ok(v3 instanceof PointAndAny)
assert.ok(v4 instanceof PointAndAny)
assert.ok(v5 instanceof PointAndAny)
assert.ok(v6 instanceof PointAndAny)

assert.deepEqual(v1.toJSON(), {x:0,y:0,any:undefined})
assert.deepEqual(v2.toJSON(), {x:10,y:0,any:undefined})
assert.deepEqual(v3.toJSON(), {x:0,y:10,any:undefined})
assert.deepEqual(v4.toJSON(), {x:1,y:2,any:undefined})
assert.deepEqual(v5.toJSON(), {x:1,y:2,any:'any'})
assert.deepEqual(v6.toJSON(), {x:1,y:2,any:aTypeError})
})

const identity = x => x
test("identical on no change", assert => {
Expand Down

0 comments on commit 44c2a0a

Please sign in to comment.