Skip to content

Commit

Permalink
add one more test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChromeGG committed Mar 14, 2024
1 parent f7b400e commit dd7deb2
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 142 deletions.
3 changes: 2 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@

- [ ] I have performed a self-review of my code
- [ ] I have added tests (if possible)

- [ ] I have updated the readme (if necessary)
- [ ] I have updated the demo (if necessary)
7 changes: 4 additions & 3 deletions demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ import { evaluate, parse } from 'cel-js'
console.log(`${arrayExpr} => ${evaluate(arrayExpr)}`) // => [1, 2]

// Map expressions
const mapExpr = '{"a": 1, "b": 2}'
console.log(`${mapExpr} => ${evaluate(mapExpr)}`) // => { a: 1, b: 2 }
// TODO - bump version to 0.1.5 and uncomment
// const mapExpr = '{"a": 1, "b": {"c": 2} }'
// console.log(`${mapExpr} => ${evaluate(mapExpr)}`) // => { a: 1, b: { c: 2 } }

// Macro expressions
const macroExpr = 'size([1, 2])'
console.log(`${arrayExpr} => ${evaluate(macroExpr)}`) // => 2
console.log(`${macroExpr} => ${evaluate(macroExpr)}`) // => 2
}

// Parse an expression, useful for validation purposes before persisting
Expand Down
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A simple usage of the CEL library",
"type": "module",
"dependencies": {
"cel-js": "0.1.2"
"cel-js": "0.1.4"
},
"devDependencies": {
"tsx": "4.7.0"
Expand Down
8 changes: 4 additions & 4 deletions demo/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

265 changes: 135 additions & 130 deletions src/spec/maps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,157 +2,162 @@ import { expect, describe, it } from 'vitest'
import { CelEvaluationError, evaluate } from '..'

describe('maps expressions', () => {
describe('creation', () => {
it('should create a empty map', () => {
const expr = '{}'

const result = evaluate(expr)

expect(result).toStrictEqual({})
})

it('should create a one element map', () => {
const expr = '{"a": 1}'

const result = evaluate(expr)

expect(result).toStrictEqual({ a: 1 })
})

it('should create a many element map', () => {
const expr = '{"a": 1, "b": 2, "c": 3}'

describe('literal', () => {
it('should create a empty map', () => {
const expr = '{}'

const result = evaluate(expr)

expect(result).toStrictEqual({})
})

it('should create a one element map', () => {
const expr = '{"a": 1}'

const result = evaluate(expr)

expect(result).toStrictEqual({ a: 1 })
})

it('should create a many element map', () => {
const expr = '{"a": 1, "b": 2, "c": 3}'

const result = evaluate(expr)

expect(result).toStrictEqual({ a: 1, b: 2, c: 3 })
})
it('should handle nested map', () => {
const expr = '{"a": {"a":1, "b":2}}'

const result = evaluate(expr)

expect(result).toStrictEqual({ a: { a: 1, b: 2 } })
})

it('should throw an error if maps have different types', () => {
const expr = '{"a": 1, "b": true}'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('invalid_argument: true'))
})
})

describe('index', () => {
describe('dot expression', () => {
it('should get the value of a key', () => {
const expr = '{"a": 1}.a'

const result = evaluate(expr)
expect(result).toStrictEqual({ "a": 1, "b": 2, "c": 3 })
})
it('should throw an error if maps have different types', () => {
const expr = '{"a": 1, "b": true}'

expect(result).toStrictEqual(1)
})

it('should throw an error if the key does not exist', () => {
const expr = '{"a": 1}.b'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('invalid_argument: true'))
})

expect(result).toThrow(
new CelEvaluationError('Identifier "b" not found, no context passed')
)
})

it.todo('should throw an error if the key is not a string', () => {
const expr = '{"a": 1}.1'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('invalid_argument: 1'))
})
})
describe('index expression', () => {
it('should get the value of a key', () => {
const expr = '{"a": 1}["a"]'

const result = evaluate(expr)

expect(result).toStrictEqual(1)
})

describe('index', () => {

describe('dot expression', () => {
it('should get the value of a key', () => {
const expr = '{"a": 1}.a'

const result = evaluate(expr)

expect(result).toStrictEqual(1)
})

it('should throw an error if the key does not exist', () => {
const expr = '{"a": 1}.b'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('Identifier "b" not found, no context passed'))
})

it.todo('should throw an error if the key is not a string', () => {
const expr = '{"a": 1}.1'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('invalid_argument: 1'))
})
})
describe('index expression', () => {
it('should get the value of a key', () => {
const expr = '{"a": 1}["a"]'

const result = evaluate(expr)

expect(result).toStrictEqual(1)
})

it('should throw an error if the key does not exist', () => {
const expr = '{"a": 1}["b"]'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('Identifier "b" not found, no context passed'))
})

it('should throw an error if the key is not a string', () => {
const expr = '{"a": 1}[1]'

const result = () => evaluate(expr)

expect(result).toThrow(new CelEvaluationError('Identifier "1" not found, no context passed'))
})
})
it('should throw an error if the key does not exist', () => {
const expr = '{"a": 1}["b"]'

const result = () => evaluate(expr)

expect(result).toThrow(
new CelEvaluationError('Identifier "b" not found, no context passed')
)
})

it('should throw an error if the key is not a string', () => {
const expr = '{"a": 1}[1]'

const result = () => evaluate(expr)

expect(result).toThrow(
new CelEvaluationError('Identifier "1" not found, no context passed')
)
})
})
})

describe('addition', () => {
it('should add to values accessed by index', () => {
const expr = '{"a": 1, "b": 2}["a"] + {"a": 1, "b": 2}["b"]'

const result = evaluate(expr)
describe('addition', () => {
it('should add to values accessed by index', () => {
const expr = '{"a": 1, "b": 2}["a"] + {"a": 1, "b": 2}["b"]'

expect(result).toStrictEqual(3)
const result = evaluate(expr)

})
expect(result).toStrictEqual(3)
})

describe('equal', () => {
it('should compare two equal maps', () => {
const expr = '{"c": 1, "a": 1, "b": 2} == {"a": 1, "b": 2, "c": 1}'

const result = evaluate(expr)
})

describe('equal', () => {
it('should compare two equal maps', () => {
const expr = '{"c": 1, "a": 1, "b": 2} == {"a": 1, "b": 2, "c": 1}'

expect(result).toStrictEqual(true)
const result = evaluate(expr)

})
it('should compare two different maps', () => {
const expr = '{"a": 1, "b": 2} == {"a": 1, "b": 2, "c": 1}'

const result = evaluate(expr)
expect(result).toStrictEqual(true)
})
it('should compare two different maps', () => {
const expr = '{"a": 1, "b": 2} == {"a": 1, "b": 2, "c": 1}'

expect(result).toStrictEqual(false)
const result = evaluate(expr)

})
expect(result).toStrictEqual(false)
})
describe('not equal', () => {
it('should compare two equal maps', () => {
const expr = '{"c": 1, "a": 1, "b": 2} != {"a": 1, "b": 2, "c": 1}'

const result = evaluate(expr)
})
describe('not equal', () => {
it('should compare two equal maps', () => {
const expr = '{"c": 1, "a": 1, "b": 2} != {"a": 1, "b": 2, "c": 1}'

expect(result).toStrictEqual(false)
const result = evaluate(expr)

})
it('should compare two different maps', () => {
const expr = '{"a": 1, "b": 2} != {"a": 1, "b": 2, "c": 1}'

const result = evaluate(expr)
expect(result).toStrictEqual(false)
})
it('should compare two different maps', () => {
const expr = '{"a": 1, "b": 2} != {"a": 1, "b": 2, "c": 1}'

expect(result).toStrictEqual(true)
const result = evaluate(expr)

})
expect(result).toStrictEqual(true)
})
describe('in', () => {
it('should find a key in the map', () => {
const expr = '"c" in {"c": 1, "a": 1, "b": 2}'

const result = evaluate(expr)
})
describe('in', () => {
it('should find a key in the map', () => {
const expr = '"c" in {"c": 1, "a": 1, "b": 2}'

expect(result).toStrictEqual(true)
const result = evaluate(expr)

})
it('should not find a key in the map', () => {
const expr = '"z" in {"c": 1, "a": 1, "b": 2}'

const result = evaluate(expr)
expect(result).toStrictEqual(true)
})
it('should not find a key in the map', () => {
const expr = '"z" in {"c": 1, "a": 1, "b": 2}'

expect(result).toStrictEqual(false)
const result = evaluate(expr)

})
expect(result).toStrictEqual(false)
})
})
})
})
6 changes: 3 additions & 3 deletions src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ export class CelVisitor
}

mapExpression(ctx: MapExpressionCstChildren) {
const mapExpression: { [key: string]: unknown } = {}
const mapExpression: Record<string, unknown> = {}
if (!ctx.keyValues) {
return {}
}
let valueType: string = ''
let valueType = ''
for (const keyValuePair of ctx.keyValues) {
const [key, value] = this.visit(keyValuePair)
if (valueType === '') {
Expand Down Expand Up @@ -240,7 +240,7 @@ export class CelVisitor

macrosExpression(ctx: MacrosExpressionCstChildren): unknown {
const macrosIdentifier = ctx.MacrosIdentifier[0]
// eslint-disable-next-line sonarjs/no-small-switch
// eslint-disable-next-line sonarjs/no-small-switch -- there will be more macros in the future, remove me when that happens
switch (macrosIdentifier.image) {
case 'size': // todo type it
return ctx.arg ? size(this.visit(ctx.arg)) : 0
Expand Down

0 comments on commit dd7deb2

Please sign in to comment.