wip style docs and test

This commit is contained in:
Mike Farah 2020-11-18 20:42:37 +11:00
parent dcacad1e7e
commit bb3b08e648
5 changed files with 243 additions and 65 deletions

View File

@ -1,17 +1,149 @@
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
## Examples ## Examples
### Example 0 ### Set tagged style
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
b: 5
c: 3.2
e: true
``` ```
then then
```bash ```bash
yq eval '.a style="single"' sample.yml yq eval '.. style="tagged"' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: 'cat'} !!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
```
### Set double quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="double"' sample.yml
```
will output
```yaml
a: "cat"
b: "5"
c: "3.2"
e: "true"
```
### Set single quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="single"' sample.yml
```
will output
```yaml
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
```
### Set literal quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="literal"' sample.yml
```
will output
```yaml
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
```
### Set folded quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="folded"' sample.yml
```
will output
```yaml
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
```
### Set flow quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="flow"' sample.yml
```
will output
```yaml
{a: cat, b: 5, c: 3.2, e: true}
```
### Set empty (default) quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style=""' sample.yml
```
will output
```yaml
a: cat
b: 5
c: 3.2
e: true
``` ```
### Set style using a path ### Set style using a path
@ -26,10 +158,11 @@ yq eval '.a style=.b' sample.yml
``` ```
will output will output
```yaml ```yaml
{a: "cat", b: double} a: "cat"
b: double
``` ```
### Example 2 ### Example 8
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
@ -45,7 +178,7 @@ a: cat
b: dog b: dog
``` ```
### Example 3 ### Example 9
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat
@ -57,12 +190,12 @@ yq eval '.. | style' sample.yml
``` ```
will output will output
```yaml ```yaml
flow
double
single
``` ```
### Example 4 ### Example 10
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
a: cat a: cat

View File

@ -0,0 +1 @@
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)

View File

@ -0,0 +1,98 @@
package yqlib
import (
"testing"
)
var styleOperatorScenarios = []expressionScenario{
{
description: "Set tagged style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="tagged"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set double quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="double"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set single quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="single"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set literal quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="literal"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set folded quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="folded"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set flow quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style="flow"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set empty (default) quote style",
document: `{a: cat, b: 5, c: 3.2, e: true}`,
expression: `.. style=""`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
skipDoc: true,
document: `{a: cat, b: double}`,
expression: `.a style=.b`,
expected: []string{
"D0, P[], (doc)::{a: \"cat\", b: double}\n",
},
},
{
description: "Read style",
document: `{a: "cat", b: 'thing'}`,
expression: `.. | style`,
expected: []string{
"D0, P[], (!!str)::flow\n",
"D0, P[a], (!!str)::double\n",
"D0, P[b], (!!str)::single\n",
},
},
{
skipDoc: true,
document: `a: cat`,
expression: `.. | style`,
expected: []string{
"D0, P[], (!!str)::\"\"\n",
"D0, P[a], (!!str)::\"\"\n",
},
},
}
func TestStyleOperatorScenarios(t *testing.T) {
for _, tt := range styleOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Style Operator", styleOperatorScenarios)
}

View File

@ -1,54 +0,0 @@
package yqlib
import (
"testing"
)
var styleOperatorScenarios = []expressionScenario{
{
document: `{a: cat}`,
expression: `.a style="single"`,
expected: []string{
"D0, P[], (doc)::{a: 'cat'}\n",
},
},
{
description: "Set style using a path",
document: `{a: cat, b: double}`,
expression: `.a style=.b`,
expected: []string{
"D0, P[], (doc)::{a: \"cat\", b: double}\n",
},
},
{
document: `{a: "cat", b: 'dog'}`,
expression: `.. style=""`,
expected: []string{
"D0, P[], (!!map)::a: cat\nb: dog\n",
},
},
{
document: `{a: "cat", b: 'thing'}`,
expression: `.. | style`,
expected: []string{
"D0, P[], (!!str)::flow\n",
"D0, P[a], (!!str)::double\n",
"D0, P[b], (!!str)::single\n",
},
},
{
document: `a: cat`,
expression: `.. | style`,
expected: []string{
"D0, P[], (!!str)::\"\"\n",
"D0, P[a], (!!str)::\"\"\n",
},
},
}
func TestStyleOperatorScenarios(t *testing.T) {
for _, tt := range styleOperatorScenarios {
testScenario(t, &tt)
}
documentScenarios(t, "Style Operator", styleOperatorScenarios)
}