Back to Repositories

Testing Text Transformation Compiler Features in uni-app

This test suite validates text transformation functionality in the uni-app UTS compiler, focusing on element node creation and text interpolation. It verifies how different text patterns and template expressions are compiled into virtual DOM nodes.

Test Coverage Overview

The test suite provides comprehensive coverage of text transformation scenarios in view and text elements.

  • Basic text node creation for view and text elements
  • Nested element structures with text content
  • Template expression interpolation with dynamic values
  • Complex combinations of static text and multiple interpolations
  • Special character handling including newlines and escape sequences

Implementation Analysis

The testing approach uses a custom assert utility to compare input template strings against expected compiled output.

Tests utilize Jest’s describe/test pattern with specific focus on compiler transformation validation. The implementation verifies correct creation of virtual DOM nodes using createElementVNode with proper nesting and text interpolation handling.

Technical Details

  • Testing Framework: Jest
  • Custom Utilities: testUtils assert function
  • Key Functions Tested: createElementVNode, toDisplayString
  • Test Environment: TypeScript
  • Compiler Features: Template compilation, text interpolation, element nesting

Best Practices Demonstrated

The test suite exhibits strong testing practices through comprehensive coverage of edge cases and transformation scenarios.

  • Systematic testing of basic and complex text patterns
  • Explicit verification of compiler output structure
  • Clear test case organization and naming
  • Thorough escape sequence handling validation
  • Comprehensive interpolation testing

dcloudio/uni-app

packages/uni-app-uts/__tests__/android/transforms/transformText.spec.ts

            
import { assert } from '../testUtils'

describe('compiler: transform text', () => {
  test('transform text', () => {
    assert(`<text>hello</text>`, `createElementVNode("text", null, "hello")`)
    assert(`<view>hello</view>`, `createElementVNode("view", null, "hello")`)
    assert(
      `<view><text>hello</text></view>`,
      `createElementVNode("view", null, [
  createElementVNode("text", null, "hello")
])`
    )
    assert(
      `<view>aaa{{bbb}}ccc</view>`,
      `createElementVNode("view", null, "aaa" + toDisplayString(_ctx.bbb) + "ccc", 1 /* TEXT */)`
    )
    assert(
      `<view>aaa{{bbb}}<view>ccc{{ddd}}</view>{{eee}}fff<text>{{ggg}}</text></view>`,
      `createElementVNode("view", null, [
  "aaa" + toDisplayString(_ctx.bbb),
  createElementVNode("view", null, "ccc" + toDisplayString(_ctx.ddd), 1 /* TEXT */),
  toDisplayString(_ctx.eee) + "fff",
  createElementVNode("text", null, toDisplayString(_ctx.ggg), 1 /* TEXT */)
])`
    )
  })
  test('\n', () => {
    assert(
      `<view>
  <text>\\\\\n 换行</text>
  <text>\\\\n 换行</text>
  <text>\\\n 换行</text>
  <text>\\n 换行</text>
  <text>\n 换行</text>
  <text>\n 换行 \\n 换行 \\\n 换行 \\\\n 换行 \\\\\n 换行</text>
</view>`,
      `createElementVNode(\"view\", null, [
  createElementVNode(\"text\", null, \"\\\\\\\\ 换行\"),
  createElementVNode(\"text\", null, \"\\\\n 换行\"),
  createElementVNode(\"text\", null, \"\\\\ 换行\"),
  createElementVNode(\"text\", null, \"\\n 换行\"),
  createElementVNode(\"text\", null, \" 换行\"),
  createElementVNode(\"text\", null, \" 换行 \\n 换行 \\\\ 换行 \\\\\\n 换行 \\\\\\\\ 换行\")
])`
    )
  })
})