-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Literals and operators in Component Browser (#12420)
- Loading branch information
Showing
13 changed files
with
259 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
app/gui/src/project-view/components/ComponentBrowser/__tests__/input.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { useComponentBrowserInput } from '@/components/ComponentBrowser/input' | ||
import { GraphDb, NodeId } from '@/stores/graph/graphDatabase' | ||
import { ComputedValueRegistry } from '@/stores/project/computedValueRegistry' | ||
import { SuggestionDb } from '@/stores/suggestionDatabase' | ||
import { unwrap } from '@/util/data/result' | ||
import { parseAbsoluteProjectPathRaw } from '@/util/projectPath' | ||
import { expect, test } from 'vitest' | ||
import { parseExpression } from 'ydoc-shared/ast' | ||
import { assert, assertUnreachable } from 'ydoc-shared/util/assert' | ||
import { Range } from 'ydoc-shared/util/data/range' | ||
|
||
const aiMock = { query: assertUnreachable } | ||
const operator1Id = '3d0e9b96-3ca0-4c35-a820-7d3a1649de55' as NodeId | ||
const operator2Id = '5eb16101-dd2b-4034-a6e2-476e8bfa1f2b' as NodeId | ||
|
||
function mockGraphDb() { | ||
const computedValueRegistryMock = ComputedValueRegistry.Mock() | ||
computedValueRegistryMock.db.set(operator1Id, { | ||
typename: unwrap(parseAbsoluteProjectPathRaw('Standard.Base.Number')), | ||
rawTypename: 'Standard.Base.Number', | ||
methodCall: undefined, | ||
payload: { type: 'Value' }, | ||
profilingInfo: [], | ||
}) | ||
const db = GraphDb.Mock(computedValueRegistryMock) | ||
db.mockNode('operator1', operator1Id, 'Data.read') | ||
db.mockNode('operator2', operator2Id) | ||
return db | ||
} | ||
|
||
test.each` | ||
inputContent | expectedLiteral | ||
${'read'} | ${undefined} | ||
${'operator1'} | ${undefined} | ||
${'12 + 14'} | ${undefined} | ||
${'12'} | ${'12'} | ||
${'12.6'} | ${'12.6'} | ||
${'-12'} | ${'-12'} | ||
${'-12.6'} | ${'-12.6'} | ||
${'- 12.6'} | ${undefined /* in Enso this is partial OprApp, not negation */} | ||
${'"text"'} | ${'"text"'} | ||
${"'text'"} | ${"'text'"} | ||
${"'text"} | ${"'text'"} | ||
${"'''text"} | ${"'''text"} | ||
`('Reading literal from $inputContent', ({ inputContent, expectedLiteral }) => { | ||
console.log(parseExpression(inputContent)) | ||
const input = useComponentBrowserInput(mockGraphDb(), new SuggestionDb(), aiMock) | ||
input.reset({ type: 'newNode' }) | ||
input.content = { text: inputContent, selection: Range.empty } | ||
assert(input.mode.mode === 'componentBrowsing') | ||
expect(input.mode.literal?.code()).toBe(expectedLiteral) | ||
}) | ||
|
||
test.each` | ||
inputContent | source | expectedCode | ||
${'read'} | ${'operator1'} | ${'operator1.read'} | ||
${'read'} | ${'operator2'} | ${'operator2.read'} | ||
${'read "file"'} | ${'operator2'} | ${'operator2.read "file"'} | ||
${'+ 2'} | ${'operator1'} | ${'operator1 + 2'} | ||
${'+ 3'} | ${'operator2'} | ${'operator2 + 3'} | ||
${'+2'} | ${'operator1'} | ${'operator1+2'} | ||
${'-2'} | ${'operator1'} | ${'operator1-2'} | ||
`( | ||
'Code generated by CB from $inputContent with source node $sourceNode', | ||
({ inputContent, source, expectedCode }) => { | ||
const db = mockGraphDb() | ||
const input = useComponentBrowserInput(db, new SuggestionDb(), aiMock) | ||
const sourcePort = db.getNodeFirstOutputPort(db.getIdentDefiningNode(source)) | ||
assert(sourcePort != null) | ||
input.reset({ type: 'newNode', sourcePort }) | ||
input.content = { text: inputContent, selection: Range.empty } | ||
expect(input.code).toBe(expectedCode) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.