Files
liquidjs/src/template/hash.spec.ts
T
JamesandGitHub 3492ff63f4 feat: static variable analysis (#770)
* feat: static variable analysis

* Accept any iterable from `children`, `arguments`, etc.

* Test analysis of standard tags

* Use `TagToken.tokenizer` instead of creating a new one

* Test analysis of netsted tags

* Group variables by their root value

* Test analysis of nested globals and locals

* Analyze included and rendered templates WIP

* Use existing tokenizer when constructing `Hash`

* Improve test coverage

* Analyze variables from `layout` and `block` tags

* Test analysis of Jekyll style includes

* Handle variables that start with a nested variable

* Async analysis

* Test non-standard tag end to end

* Implement convenience analysis methods on the `Liquid` class

* More analysis convenience methods

* Accept string or template array

* Draft static analysis docs

* Deduplicate variables names

* Fix isolated scope global variable map

* Coerce variables to strings instead of extending String

* Private map instead of extending Map

* Fix e2e test

* Tentatively implement analysis of aliased variables

* Fix nested variable segments array

* Update docs sidebar
2024-12-28 21:35:28 +08:00

54 lines
2.0 KiB
TypeScript

import { toPromise } from '../util'
import { Hash } from './hash'
import { Context } from '../context'
import { Tokenizer } from '../parser'
describe('Hash', function () {
it('should parse "reverse"', async function () {
const hash = await toPromise(new Hash('reverse').render(new Context({ foo: 3 })))
expect(hash).toHaveProperty('reverse')
expect(hash.reverse).toBeTruthy()
})
it('should parse "num:foo"', async function () {
const hash = await toPromise(new Hash('num:foo').render(new Context({ foo: 3 })))
expect(hash.num).toBe(3)
})
it('should parse "num:3"', async function () {
const hash = await toPromise(new Hash('num:3').render(new Context()))
expect(hash.num).toBe(3)
})
it('should parse "num: arr[0]"', async function () {
const hash = await toPromise(new Hash('num:3').render(new Context({ arr: [3] })))
expect(hash.num).toBe(3)
})
it('should parse "num: 2.3"', async function () {
const hash = await toPromise(new Hash('num:2.3').render(new Context()))
expect(hash.num).toBe(2.3)
})
it('should parse "num:bar.coo"', async function () {
const pending = new Hash('num:bar.coo').render(new Context({ bar: { coo: 3 } }))
const hash = await toPromise(pending)
expect(hash.num).toBe(3)
})
it('should parse "num1:2.3 reverse,num2:bar.coo\n num3: arr[0]"', async function () {
const ctx = new Context({ bar: { coo: 3 }, arr: [4] })
const hash = await toPromise(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
expect(hash).toEqual({
num1: 2.3,
reverse: true,
num2: 3,
num3: 4
})
})
it('should support custom separator', async function () {
const hash = await toPromise(new Hash('num=2.3', '=').render(new Context()))
expect(hash.num).toBe(2.3)
})
it('should accept an existing tokenizer', async function () {
const tokenizer = new Tokenizer('a:1, b:2')
const hash = await toPromise(new Hash(tokenizer).render(new Context()))
expect(hash.a).toBe(1)
expect(hash.b).toBe(2)
})
})