instructionsPage.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { instructionArgumentNode, instructionNode, programNode, stringTypeNode } from '@kinobi-so/nodes';
  2. import { visit } from '@kinobi-so/visitors-core';
  3. import { test } from 'vitest';
  4. import { getRenderMapVisitor } from '../src';
  5. import { codeContains } from './_setup';
  6. test('it renders a public instruction data struct', () => {
  7. // Given the following program with 1 instruction.
  8. const node = programNode({
  9. instructions: [instructionNode({ name: 'mintTokens' })],
  10. name: 'splToken',
  11. publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  12. });
  13. // When we render it.
  14. const renderMap = visit(node, getRenderMapVisitor());
  15. // Then we expect the following pub struct.
  16. codeContains(renderMap.get('instructions/mint_tokens.rs'), [`pub struct MintTokensInstructionData`, `pub fn new(`]);
  17. });
  18. test('it renders an instruction with a remainder str', () => {
  19. // Given the following program with 1 instruction.
  20. const node = programNode({
  21. instructions: [
  22. instructionNode({
  23. arguments: [
  24. instructionArgumentNode({
  25. name: 'memo',
  26. type: stringTypeNode('utf8'),
  27. }),
  28. ],
  29. name: 'addMemo',
  30. }),
  31. ],
  32. name: 'splToken',
  33. publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  34. });
  35. // When we render it.
  36. const renderMap = visit(node, getRenderMapVisitor());
  37. // Then we expect the following pub struct.
  38. codeContains(renderMap.get('instructions/add_memo.rs'), [
  39. `use kaigan::types::RemainderStr`,
  40. `pub memo: RemainderStr`,
  41. ]);
  42. });
  43. test('it renders a default impl for instruction data struct', () => {
  44. // Given the following program with 1 instruction.
  45. const node = programNode({
  46. instructions: [instructionNode({ name: 'mintTokens' })],
  47. name: 'splToken',
  48. publicKey: 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
  49. });
  50. // When we render it.
  51. const renderMap = visit(node, getRenderMapVisitor());
  52. // Then we expect the following Default trait to be implemented.
  53. codeContains(renderMap.get('instructions/mint_tokens.rs'), [
  54. `impl Default for MintTokensInstructionData`,
  55. `fn default(`,
  56. ]);
  57. });