MCPcopy
hub / github.com/go-yaml/yaml / TestNodeRoundtrip

Method TestNodeRoundtrip

node_test.go:2556–2607  ·  view source on GitHub ↗
(c *C)

Source from the content-addressed store, hash-verified

2554}
2555
2556func (s *S) TestNodeRoundtrip(c *C) {
2557 defer os.Setenv("TZ", os.Getenv("TZ"))
2558 os.Setenv("TZ", "UTC")
2559 for i, item := range nodeTests {
2560 c.Logf("test %d: %q", i, item.yaml)
2561
2562 if strings.Contains(item.yaml, "#") {
2563 var buf bytes.Buffer
2564 fprintComments(&buf, &item.node, " ")
2565 c.Logf(" expected comments:\n%s", buf.Bytes())
2566 }
2567
2568 decode := true
2569 encode := true
2570
2571 testYaml := item.yaml
2572 if s := strings.TrimPrefix(testYaml, "[decode]"); s != testYaml {
2573 encode = false
2574 testYaml = s
2575 }
2576 if s := strings.TrimPrefix(testYaml, "[encode]"); s != testYaml {
2577 decode = false
2578 testYaml = s
2579 }
2580
2581 if decode {
2582 var node yaml.Node
2583 err := yaml.Unmarshal([]byte(testYaml), &node)
2584 c.Assert(err, IsNil)
2585 if strings.Contains(item.yaml, "#") {
2586 var buf bytes.Buffer
2587 fprintComments(&buf, &node, " ")
2588 c.Logf(" obtained comments:\n%s", buf.Bytes())
2589 }
2590 c.Assert(&node, DeepEquals, &item.node)
2591 }
2592 if encode {
2593 node := deepCopyNode(&item.node, nil)
2594 buf := bytes.Buffer{}
2595 enc := yaml.NewEncoder(&buf)
2596 enc.SetIndent(2)
2597 err := enc.Encode(node)
2598 c.Assert(err, IsNil)
2599 err = enc.Close()
2600 c.Assert(err, IsNil)
2601 c.Assert(buf.String(), Equals, testYaml)
2602
2603 // Ensure there were no mutations to the tree.
2604 c.Assert(node, DeepEquals, &item.node)
2605 }
2606 }
2607}
2608
2609func deepCopyNode(node *yaml.Node, cache map[*yaml.Node]*yaml.Node) *yaml.Node {
2610 if n, ok := cache[node]; ok {

Callers

nothing calls this directly

Calls 6

SetIndentMethod · 0.95
EncodeMethod · 0.95
CloseMethod · 0.95
fprintCommentsFunction · 0.85
deepCopyNodeFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected