|
| 1 | +/* eslint-env mocha */ |
| 2 | +import { expect } from 'chai' |
| 3 | + |
| 4 | +import Literal from '../../src/literal' |
| 5 | +import XSD from '../../src/xsd' |
| 6 | + |
| 7 | +describe('Literal', () => { |
| 8 | + describe('fromValue', () => { |
| 9 | + describe('for numbers', () => { |
| 10 | + it('detects integers', () => { |
| 11 | + expect(Literal.fromValue(0)).to.eql(new Literal('0', null, XSD.integer)) |
| 12 | + expect(Literal.fromValue(1)).to.eql(new Literal('1', null, XSD.integer)) |
| 13 | + expect(Literal.fromValue(Number.MAX_SAFE_INTEGER)) |
| 14 | + .to.eql(new Literal(Number.MAX_SAFE_INTEGER.toString(), null, XSD.integer)) |
| 15 | + expect(Literal.fromValue(Number.MIN_SAFE_INTEGER)) |
| 16 | + .to.eql(new Literal(Number.MIN_SAFE_INTEGER.toString(), null, XSD.integer)) |
| 17 | + }) |
| 18 | + |
| 19 | + it('detects decimals', () => { |
| 20 | + expect(Literal.fromValue(1.1)).to.eql(new Literal('1.1', null, XSD.decimal)) |
| 21 | + }) |
| 22 | + |
| 23 | + it('detects doubles', () => { |
| 24 | + expect(Literal.fromValue(Number.MAX_SAFE_INTEGER + 1)) |
| 25 | + .to.eql(new Literal((Number.MAX_SAFE_INTEGER + 1).toString(), null, XSD.double)) |
| 26 | + expect(Literal.fromValue(Number.MIN_SAFE_INTEGER - 1)) |
| 27 | + .to.eql(new Literal((Number.MIN_SAFE_INTEGER - 1).toString(), null, XSD.double)) |
| 28 | + expect(Literal.fromValue(Number.MAX_VALUE)) |
| 29 | + .to.eql(new Literal(Number.MAX_VALUE.toString(), null, XSD.double)) |
| 30 | + expect(Literal.fromValue(-Number.MAX_VALUE)) |
| 31 | + .to.eql(new Literal((-Number.MAX_VALUE).toString(), null, XSD.double)) |
| 32 | + expect(Literal.fromValue(Number.MIN_VALUE)) |
| 33 | + .to.eql(new Literal(Number.MIN_VALUE.toString(), null, XSD.double)) |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + it('detects string values', () => { |
| 38 | + expect(Literal.fromValue('foo')).to.eql(new Literal('foo', null, null)) |
| 39 | + }) |
| 40 | + |
| 41 | + it('detects boolean values', () => { |
| 42 | + expect(Literal.fromValue(true)).to.eql(new Literal('1', null, XSD.boolean)) |
| 43 | + expect(Literal.fromValue(false)).to.eql(new Literal('0', null, XSD.boolean)) |
| 44 | + }) |
| 45 | + |
| 46 | + it('constructs a literal representing a date value', () => { |
| 47 | + const date = new Date(Date.UTC(2010, 5, 10, 1, 2, 3)) |
| 48 | + expect(Literal.fromValue(date)) |
| 49 | + .to.eql(new Literal('2010-06-10T01:02:03Z', null, XSD.dateTime)) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('toNT', () => { |
| 54 | + describe('for strings', () => { |
| 55 | + it('serializes strings with no language', () => { |
| 56 | + const node = Literal.fromValue('foo') |
| 57 | + expect(node.toNT()).to.equal('"foo"') |
| 58 | + }) |
| 59 | + |
| 60 | + it('serializes strings with a language', () => { |
| 61 | + const node = new Literal('foo', 'en') |
| 62 | + expect(node.toNT()).to.equal('"foo"@en') |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + describe('for numbers', () => { |
| 67 | + it('serializes integers', () => { |
| 68 | + const node = Literal.fromValue(1) |
| 69 | + expect(node.toNT()).to.equal('"1"^^<http://www.w3.org/2001/XMLSchema#integer>') |
| 70 | + }) |
| 71 | + |
| 72 | + it('serializes decimals', () => { |
| 73 | + const node = Literal.fromValue(1.5) |
| 74 | + expect(node.toNT()).to.equal('"1.5"^^<http://www.w3.org/2001/XMLSchema#decimal>') |
| 75 | + }) |
| 76 | + |
| 77 | + it('serializes doubles', () => { |
| 78 | + const largeDouble = Number.MAX_VALUE |
| 79 | + const node = Literal.fromValue(largeDouble) |
| 80 | + expect(node.toNT()).to.equal(`"${largeDouble.toString()}"^^<http://www.w3.org/2001/XMLSchema#double>`) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('serializes boolean values', () => { |
| 85 | + expect(Literal.fromValue(true).toNT()).to.equal('"1"^^<http://www.w3.org/2001/XMLSchema#boolean>') |
| 86 | + expect(Literal.fromValue(false).toNT()).to.equal('"0"^^<http://www.w3.org/2001/XMLSchema#boolean>') |
| 87 | + }) |
| 88 | + |
| 89 | + it('serializes date values', () => { |
| 90 | + const date = new Date(Date.UTC(2010, 5, 10, 1, 2, 3)) |
| 91 | + expect(Literal.fromValue(date).toNT()) |
| 92 | + .to.equal('"2010-06-10T01:02:03Z"^^<http://www.w3.org/2001/XMLSchema#dateTime>') |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + describe('copy', () => { |
| 97 | + it('creates an identical copy of a node', () => { |
| 98 | + const node = Literal.fromValue('foo') |
| 99 | + expect(node).to.eql(node.copy()) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('equals', () => { |
| 104 | + it('compares termType, value, language, and datatype', () => { |
| 105 | + const a = new Literal('hello world', 'en', XSD.langString) |
| 106 | + const b = new Literal('', '', null) |
| 107 | + expect(a.equals(b)).to.be.false |
| 108 | + expect(b.equals(a)).to.be.false |
| 109 | + b.value = 'hello world' |
| 110 | + expect(a.equals(b)).to.be.false |
| 111 | + expect(b.equals(a)).to.be.false |
| 112 | + b.language = 'en' |
| 113 | + expect(a.equals(b)).to.be.false |
| 114 | + expect(b.equals(a)).to.be.false |
| 115 | + b.datatype = XSD.langString |
| 116 | + expect(a.equals(b)).to.be.true |
| 117 | + expect(b.equals(a)).to.be.true |
| 118 | + }) |
| 119 | + }) |
| 120 | +}) |
0 commit comments