Skip to main content

twk_test 0.1.0 copy "twk_test: ^0.1.0" to clipboard
twk_test: ^0.1.0 copied to clipboard

Tests for twk apps: the real app on a terminal in memory under a fake clock, input as a terminal's bytes, the screen read by an emulator.

example/twk_test_example.dart

// A counter and its tests: the app runs on a terminal in memory, the keys
// go in as a terminal sends them, and the screen is read back.
//
//   dart test example/twk_test_example.dart
import 'package:test/test.dart';
import 'package:twk/twk.dart';
import 'package:twk_test/twk_test.dart';

final class Counter extends StatefulWidget {
  const Counter({super.key});

  @override
  State<Counter> createState() => _CounterState();
}

final class _CounterState extends State<Counter> {
  var _n = 0;

  @override
  Widget build(BuildContext context) => KeyBindings(
    {
      'up': () => setState(() => _n++),
      'q': () => AppBinding.of(context).quit(),
    },
    child: Box(
      border: const .all(type: .rounded),
      padding: const .all(1),
      child: Text.rich(['Count: '.dim, '$_n'.bold.fg(.green)]),
    ),
  );
}

void main() {
  testTwk('Up counts', (t) async {
    await t.pumpWidget(const Counter(), size: (20, 5));
    expect(
      t,
      matchesScreen('''
╭──────────────────╮
│                  │
│ Count: 0         │
│                  │
╰──────────────────╯
'''),
    );

    await t.press('up up up');
    await t.pump();
    expect(t, containsLine('│ Count: 3         │'));
    expect(t, styleAt(9, 2, allOf(hasAttr(Attr.bold), hasFg(Color.green))));
  });

  testTwk('q quits', (t) async {
    await t.pumpWidget(const Counter());
    await t.press('q');
    expect(t.isRunning, isFalse);
  });
}
1
likes
0
points
1.18k
downloads

Publisher

verified publisherpro100.dev

Weekly Downloads

Tests for twk apps: the real app on a terminal in memory under a fake clock, input as a terminal's bytes, the screen read by an emulator.

Repository (GitHub)
View/report issues

Topics

#terminal #tui #testing #widget

License

unknown (license)

Dependencies

meta, test, twk, twk_core

More

Packages that depend on twk_test