implementation of minimum, maximum, hasAny and hasAll for bitflags
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
import { FlagDefinition, FlagsDictionary } from '~'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { ReusedFlagAliasError, ReusedFlagValueError } from '../../src/errors'
|
||||
|
||||
class TestDefinition implements FlagDefinition<unknown, unknown> {
|
||||
private readonly _value: number
|
||||
|
||||
public constructor(value: number) {
|
||||
this._value = value
|
||||
}
|
||||
|
||||
public get values(): unknown {
|
||||
return this._value
|
||||
}
|
||||
|
||||
public hasSameValue(other: FlagDefinition<unknown, unknown>): boolean {
|
||||
return other instanceof TestDefinition && this._value === other._value
|
||||
}
|
||||
}
|
||||
|
||||
describe(FlagsDictionary, () => {
|
||||
test('define then look up', () => {
|
||||
const dict = new FlagsDictionary()
|
||||
const def = new TestDefinition(1)
|
||||
|
||||
dict.define('test', def)
|
||||
|
||||
expect(dict.findByAlias('test')).toBe(def)
|
||||
expect(dict.findByAlias('undefined')).toBe(undefined)
|
||||
})
|
||||
|
||||
test("can't use the same alias twice", () => {
|
||||
const dict = new FlagsDictionary()
|
||||
|
||||
dict.define('test', new TestDefinition(1))
|
||||
expect(() => dict.define('test', new TestDefinition(2))).toThrow(
|
||||
ReusedFlagAliasError,
|
||||
)
|
||||
})
|
||||
|
||||
test("can't use the same value twice", () => {
|
||||
const dict = new FlagsDictionary()
|
||||
|
||||
dict.define('test A', new TestDefinition(1))
|
||||
expect(() => dict.define('test B', new TestDefinition(1))).toThrow(
|
||||
ReusedFlagValueError,
|
||||
)
|
||||
})
|
||||
})
|
||||
52
node/tests/definitions/graph.test.ts
Normal file
52
node/tests/definitions/graph.test.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { FlagsGraph } from '~/definitions/graph'
|
||||
|
||||
describe(FlagsGraph, () => {
|
||||
test('put a definition in the graph', () => {
|
||||
const graph = new FlagsGraph<number>()
|
||||
|
||||
const A = graph.put({ alias: 'A', value: 1 })
|
||||
|
||||
expect(graph.definitions).toHaveLength(1)
|
||||
expect(graph.definitions).toContain(A)
|
||||
|
||||
expect(A.isPlaceholder).toBe(false)
|
||||
expect(A.alias).toBe('A')
|
||||
expect(A.value).toBe(1)
|
||||
})
|
||||
|
||||
test('create a definition with a reference to an existing definition', () => {
|
||||
const graph = new FlagsGraph<number>()
|
||||
|
||||
const A = graph.put({ alias: 'A', value: 1 })
|
||||
|
||||
expect(graph.definitions).toHaveLength(1)
|
||||
expect(graph.definitions).toContain(A)
|
||||
|
||||
const B = graph.put({ alias: 'B', value: 2, parents: [{ alias: 'A' }] })
|
||||
|
||||
expect(graph.definitions).toHaveLength(2)
|
||||
expect(graph.definitions).toContain(B)
|
||||
|
||||
expect(B.parents).toContain(A)
|
||||
expect(A.children).toContain(B)
|
||||
})
|
||||
|
||||
test('create a definition with a forward reference', () => {
|
||||
const graph = new FlagsGraph<number>()
|
||||
|
||||
const B = graph.put({ alias: 'B', value: 2, parents: [{ alias: 'A' }] })
|
||||
|
||||
expect(graph.definitions).toHaveLength(1)
|
||||
expect(graph.definitions).toContain(B)
|
||||
|
||||
const A = graph.put({ alias: 'A', value: 1 })
|
||||
|
||||
expect(graph.definitions).toHaveLength(2)
|
||||
expect(graph.definitions).toContain(A)
|
||||
expect(A.value).toBe(1)
|
||||
|
||||
expect(B.parents).toContain(A)
|
||||
expect(A.children).toContain(B)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user