opencolor
Version:
A collection of functions to parse Open Color files, construct them via code and write them
152 lines (140 loc) • 3.86 kB
JavaScript
/* eslint-env mocha */
'use strict'
import {expect} from 'chai'
import * as oco from '../src/index'
/** @test {parse} */
describe('Parsing of References', () => {
it('should parse a reference', () => {
var test = `
color:
ref color: =color
`
var tree = oco.parse(test)
// Only simple, same level references for now
expect(tree.children[1].refName).to.equal('color')
expect(tree.children[1].resolved().hexcolor()).to.equal('#FFFFFF')
})
it('should parse a deep reference', () => {
var test = `
color:
group:
group color:
ref color: =color
test:
another color:
var tree = oco.parse(test)
// Only simple, same level references for now
var refColor = tree.get('group').get('ref color')
expect(refColor.refName).to.equal('color')
expect(refColor.resolved().hexcolor()).to.equal('#FFFFFF')
})
it('should return a absolute ref name', () => {
var test = `
one:
three:
group:
one:
two:
refOne: =one
refTwo: =group.two
refThree: =three
refThreeRef: =refThree
`
var tree = oco.parse(test)
var refOne = tree.get('group').get('refOne')
var refTwo = tree.get('group').get('refTwo')
var refThree = tree.get('group').get('refThree')
var refThreeRef = tree.get('group').get('refThreeRef')
expect(refOne.absoluteRefName).to.equal('group.one')
expect(refTwo.absoluteRefName).to.equal('group.two')
expect(refThree.absoluteRefName).to.equal('three')
expect(refThreeRef.absoluteRefName).to.equal('group.refThree')
})
it('should parse a tree reference', () => {
var test = `
color:
group:
group color:
ref color: =group.another color
another color:
`
var tree = oco.parse(test)
// Only simple, same level references for now
var refColor = tree.get('group').get('ref color')
expect(refColor.refName).to.equal('group.another color')
expect(refColor.resolved().hexcolor()).to.equal('#AAFFAA')
})
it('should parse a non obvious tree reference', () => {
var test = `
a:
b:
a:
c:
subgroup ref color: =a.b
`
var tree = oco.parse(test)
// Only simple, same level references for now
var refColor = tree.get('a').get('a').get('subgroup ref color')
expect(refColor.refName).to.equal('a.b')
expect(refColor.resolved().hexcolor()).to.equal('#FFFFFF')
})
it('should resolve references of references', () => {
var test = `
a: =b
b: =c
c:
`
var tree = oco.parse(test)
// Only simple, same level references for now
expect(tree.get('a').resolved().hexcolor()).to.equal('#AAFFAA')
})
it('should break on resolving single circular references', () => {
var test = `
a: =b
b: =a
`
var tree = oco.parse(test)
// Only simple, same level references for now
expect(tree.get('a').resolved).to.throw()
})
it('should break on resolving multi circular references', () => {
var test = `
a: =b
b: =c
c: =a
`
var tree = oco.parse(test)
// Only simple, same level references for now
expect(tree.get('a').resolved).to.throw()
})
it('should resolve references with number names', () => {
var test = `
800:
a: =800
`
var tree = oco.parse(test)
// Only simple, same level references for now
expect(tree.get('a').resolved().hexcolor()).to.equal('#CC0000')
})
})
/** @test {parse} */
describe('Parsing of Metadata on References', () => {
it('should parse a local ref metadata', () => {
var test = `
a:
b: =a
oct/meta: foo
`
var tree = oco.parse(test)
expect(tree.get('b').getMetadata('oct/meta')).to.equal('foo')
})
it('should find metadata on ref target', () => {
var test = `
a:
oct/meta: foo
b: =a
`
var tree = oco.parse(test)
expect(tree.get('b').getMetadata('oct/meta')).to.equal('foo')
})
})