Skip to content

Commit

Permalink
Fix output edges; add unit and integration testing. (#11637)
Browse files Browse the repository at this point in the history
Fix output edges; add unit and integration testing for output and input edges.

Fixes #11635.
  • Loading branch information
kazcw authored Nov 26, 2024
1 parent 56e3e1c commit 9ddd841
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 6 additions & 0 deletions app/gui/e2e/project-view/collapsingAndEntering.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { expect } from './customExpect'
import { mockCollapsedFunctionInfo } from './expressionUpdates'
import { CONTROL_KEY } from './keyboard'
import * as locate from './locate'
import { edgesFromNode, edgesToNode } from './locate'
import { mockSuggestion } from './suggestionUpdates'

const MAIN_FILE_NODES = 12
const EDGE_PARTS = 2

const COLLAPSE_SHORTCUT = `${CONTROL_KEY}+G`

Expand Down Expand Up @@ -219,6 +221,8 @@ async function expectInsideFunc1(page: Page) {
await expect(locate.graphNodeByBinding(page, 'f2')).toExist()
await expect(locate.graphNodeByBinding(page, 'result')).toExist()
await expect(locate.outputNode(page)).toHaveCount(1)
await expect(await edgesFromNode(page, locate.inputNode(page))).toHaveCount(EDGE_PARTS)
await expect(await edgesToNode(page, locate.outputNode(page))).toHaveCount(EDGE_PARTS)
}

async function expectInsideFunc2(page: Page) {
Expand All @@ -227,6 +231,8 @@ async function expectInsideFunc2(page: Page) {
await expect(locate.inputNode(page)).toHaveCount(1)
await expect(locate.graphNodeByBinding(page, 'r')).toExist()
await expect(locate.outputNode(page)).toHaveCount(1)
await expect(await edgesFromNode(page, locate.inputNode(page))).toHaveCount(EDGE_PARTS)
await expect(await edgesToNode(page, locate.outputNode(page))).toHaveCount(EDGE_PARTS)
}

async function enterToFunc2(page: Page) {
Expand Down
12 changes: 10 additions & 2 deletions app/gui/e2e/project-view/locate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,22 @@ export const warningsVisualization = visualizationLocator('.WarningsVisualizatio

/** All edges going from a node with given binding. */
export async function edgesFromNodeWithBinding(page: Page, binding: string) {
const node = graphNodeByBinding(page, binding).first()
return edgesFromNode(page, graphNodeByBinding(page, binding).first())
}

/** All edges going from a node. */
export async function edgesFromNode(page: Page, node: Locator) {
const nodeId = await node.getAttribute('data-node-id')
return page.locator(`[data-source-node-id="${nodeId}"]`)
}

/** All edges going to a node with given binding. */
export async function edgesToNodeWithBinding(page: Page, binding: string) {
const node = graphNodeByBinding(page, binding).first()
return edgesToNode(page, graphNodeByBinding(page, binding).first())
}

/** All edges going to a node. */
export async function edgesToNode(page: Page, node: Locator) {
const nodeId = await node.getAttribute('data-node-id')
return page.locator(`[data-target-node-id="${nodeId}"]`)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { asNodeId, GraphDb } from '@/stores/graph/graphDatabase'
import { Ast } from '@/util/ast'
import assert from 'assert'
import * as iter from 'enso-common/src/utilities/data/iter'
import { expect, test } from 'vitest'
import { watchEffect } from 'vue'
import type { AstId } from 'ydoc-shared/ast'
Expand Down Expand Up @@ -37,7 +38,8 @@ test('Reading graph from definition', () => {
const code = `function a =
node1 = a + 4
node2 = node1 + 4
node3 = node2 + 1`
node3 = node2 + 1
node3`
const spans = {
functionName: [0, 8] as [number, number],
parameter: [9, 10] as [number, number],
Expand All @@ -51,13 +53,13 @@ test('Reading graph from definition', () => {
node2RParam: [51, 52] as [number, number],
node3Id: [57, 62] as [number, number],
node3Content: [65, 74] as [number, number],
output: [79, 84] as [number, number],
}

const { ast, id, eid, getSpan } = parseWithSpans(code, spans)

const db = GraphDb.Mock()
const expressions = Array.from(ast.statements())
const func = expressions[0]
const func = iter.first(ast.statements())
assert(func instanceof Ast.FunctionDef)
db.updateExternalIds(ast)
db.updateNodes(func, { watchEffect })
Expand All @@ -68,6 +70,7 @@ test('Reading graph from definition', () => {
eid('node1Content'),
eid('node2Content'),
eid('node3Content'),
eid('output'),
])
expect(db.getExpressionNodeId(id('node1Content'))).toBe(eid('node1Content'))
expect(db.getExpressionNodeId(id('node1LParam'))).toBe(eid('node1Content'))
Expand Down Expand Up @@ -96,9 +99,11 @@ test('Reading graph from definition', () => {
id('parameter'),
id('node1Id'),
id('node2Id'),
id('node3Id'),
])
expect(Array.from(db.connections.lookup(id('parameter')))).toEqual([id('node1LParam')])
expect(Array.from(db.connections.lookup(id('node1Id')))).toEqual([id('node2LParam')])
expect(Array.from(db.connections.lookup(id('node3Id')))).toEqual([id('output')])
expect(db.getOutputPortIdentifier(id('parameter'))).toBe('a')
expect(db.getOutputPortIdentifier(id('node1Id'))).toBe('node1')
expect(Array.from(db.nodeDependents.lookup(asNodeId(eid('node1Content'))))).toEqual([
Expand All @@ -107,5 +112,7 @@ test('Reading graph from definition', () => {
expect(Array.from(db.nodeDependents.lookup(asNodeId(eid('node2Content'))))).toEqual([
eid('node3Content'),
])
expect(Array.from(db.nodeDependents.lookup(asNodeId(eid('node3Content'))))).toEqual([])
expect(Array.from(db.nodeDependents.lookup(asNodeId(eid('node3Content'))))).toEqual([
eid('output'),
])
})
8 changes: 6 additions & 2 deletions app/gui/src/project-view/util/ast/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { visitRecursive } from '@/util/ast/raw'
import { MappedKeyMap, MappedSet } from '@/util/containers'
import type { AstId } from 'ydoc-shared/ast'
import type { SourceDocument } from 'ydoc-shared/ast/sourceDocument'
import { assert } from 'ydoc-shared/util/assert'
import { assert, assertDefined } from 'ydoc-shared/util/assert'
import { type SourceRange, sourceRangeKey, type SourceRangeKey } from 'ydoc-shared/yjsModel'

/** A variable name, and information about its usages. */
Expand Down Expand Up @@ -81,8 +81,12 @@ function rangeMappings(
}
ast.visitRecursive((ast) => {
const span = getSpan(ast.id)
assert(span != null)
assertDefined(span)
// An `ExpressionStatement` may have the same source range as its expression. Descend into the expression that
// contains the reference.
if (ast instanceof Ast.ExpressionStatement) return true
if (bindingRanges.has(span)) {
if (bindingRangeToTree.has(span)) console.warn('Multiple ASTs found for binding range')
bindingRangeToTree.set(span, ast)
return false
}
Expand Down

0 comments on commit 9ddd841

Please sign in to comment.