refactor: rename toValue to toValueSync

This commit is contained in:
Harttle
2022-07-10 00:24:00 +08:00
parent c3e51caa70
commit 4289b4e804
7 changed files with 77 additions and 37 deletions
+18 -1
View File
@@ -2,7 +2,7 @@ import { Tokenizer } from '../../../src/parser/tokenizer'
import { expect } from 'chai'
import { Drop } from '../../../src/drop/drop'
import { Context } from '../../../src/context/context'
import { toPromise } from '../../../src/util/async'
import { toPromise, toValueSync } from '../../../src/util/async'
import { defaultOperators } from '../../../src/render/operator'
import { createTrie } from '../../../src/util/operator-trie'
@@ -158,4 +158,21 @@ describe('Expression', function () {
expect(await toPromise(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).to.equal('FOO')
})
})
describe('sync', function () {
it('should eval literal', function () {
expect(toValueSync(create('2.4').evaluate(ctx, false))).to.equal(2.4)
})
it('should return false for "1==2"', () => {
expect(toValueSync(create('1==2').evaluate(ctx, false))).to.equal(false)
})
it('should escape quote', function () {
const ctx = new Context({ quote: '"' })
expect(toValueSync(create('"\\"" == quote').evaluate(ctx, false))).to.equal(true)
})
it('should allow nested property access', function () {
const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } })
expect(toValueSync(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).to.equal('FOO')
})
})
})
+1 -1
View File
@@ -76,6 +76,6 @@ describe('filter', function () {
it('should support key value pairs', async function () {
filters.set('add', (a, b) => b[0] + ':' + (a + b[1]))
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
expect(await toPromise((filters.create('add', [['num', two]]).render(3, ctx)))).to.equal('num:5')
expect(await toPromise(filters.create('add', [['num', two]]).render(3, ctx))).to.equal('num:5')
})
})
+8 -5
View File
@@ -1,4 +1,4 @@
import { toPromise, toValue } from '../../../src/util/async'
import { toPromise, toValueSync } from '../../../src/util/async'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
@@ -75,7 +75,7 @@ describe('utils/async', () => {
expect(ret).to.equal('barfoo')
})
})
describe('#toValue()', function () {
describe('#toValueSync()', function () {
it('should throw Error if dependency throws syncly', () => {
function * foo (): Generator<Generator<never>> {
return yield bar()
@@ -83,7 +83,7 @@ describe('utils/async', () => {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(() => toValue(foo())).to.throw('bar')
expect(() => toValueSync(foo())).to.throw('bar')
})
it('should resume yield after catch', () => {
function * foo (): Generator<unknown, never, never> {
@@ -95,7 +95,7 @@ describe('utils/async', () => {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValue(foo())).to.equal('foo')
expect(toValueSync(foo())).to.equal('foo')
})
it('should resume return after catch', () => {
function * foo (): Generator<Generator<never>, string> {
@@ -107,7 +107,10 @@ describe('utils/async', () => {
function * bar (): Generator<never> {
throw new Error('bar')
}
expect(toValue(foo())).to.equal('foo')
expect(toValueSync(foo())).to.equal('foo')
})
it('should return non iterator value as it is', () => {
expect(toValueSync('foo')).to.equal('foo')
})
})
})