testutil

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindNodeByContent

func FindNodeByContent(node *sitter.Node, content string) *sitter.Node

FindNodeByContent returns a node whose content is the given content string. This could be a descendent of the given node. If there are multiple such nodes, this will return one of them, but it's unspecified which.

func NewSet

func NewSet[T comparable](ss ...T) map[T]struct{}

func SafeYamlPath

func SafeYamlPath(yamlStr string, path string) string

YamlPath returns a subset of the given yaml file, as specified by its path. It uses yamlpath under the hood. See the yamlpath's github page for details, though the package's godocs are easier to read.

tldr: `$.path.to.your[0].subdocument` (the `$` is literally a dollar sign you should use to anchor the path).

This function expects there to be a single node result. If you want a list, select the list's parent instead.

If there are any errors along the way, this will return `// ERROR: ${msg}`.

func UnIndent

func UnIndent(y string) string

UnIndent removes a level of indentation from the given string. The rules are very simple:

  • first, drop any leading newlines from the string
  • then, find the indentation in the first line, which is defined by the leading tabs-or-spaces in that line
  • then, trim that prefix from all lines

The prefix must match exactly, and this method removes it just by invoke strings.CutPrefix -- nothing fancier. In particular, this means that if you're mixing tabs and spaces, you may find yourself in for a bad time: if the first string uses "<space><tab>" and the second uses "<tab><space>", the second does not count as an indentation, and won't be affected.

You can use this to embed yaml within test code as literal strings:

 ...
 SomeField: MyStruct {
 	foo: unIndent(`
 		hello: world
 		counts:
 		  - 1
 		  - 2
 		hello: world`
 	),
...

The resulting string will be:

┌────────────┐ ◁─ no newline
│hello: world│ ◁─╮
│counts:     │ ◁─┤
│  - 1       │ ◁─┼─ no extra indentation
│  - 2       │ ◁─┤
│hello: world│ ◁─╯
└────────────┘

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL