mustParseGolden parses a file as a set of key-value pairs. The syntax is simple and looks something like: <<< Key1 value1a value1b >>> Key1 <<< Key2 value2 >>> Key2 It is the user's responsibility to choose a sufficiently unique key name such that it never appears in the body of the value
(path string)
| 58 | // It is the user's responsibility to choose a sufficiently unique key name |
| 59 | // such that it never appears in the body of the value itself. |
| 60 | func mustParseGolden(path string) map[string]string { |
| 61 | b, err := ioutil.ReadFile(path) |
| 62 | if err != nil { |
| 63 | panic(err) |
| 64 | } |
| 65 | s := string(b) |
| 66 | |
| 67 | out := map[string]string{} |
| 68 | for len(s) > 0 { |
| 69 | // Identify the next header. |
| 70 | i := strings.Index(s, "\n") + len("\n") |
| 71 | header := s[:i] |
| 72 | if !strings.HasPrefix(header, goldenHeaderPrefix) { |
| 73 | panic(fmt.Sprintf("invalid header: %q", header)) |
| 74 | } |
| 75 | |
| 76 | // Locate the next footer. |
| 77 | footer := goldenFooterPrefix + header[len(goldenHeaderPrefix):] |
| 78 | j := strings.Index(s, footer) |
| 79 | if j < 0 { |
| 80 | panic(fmt.Sprintf("missing footer: %q", footer)) |
| 81 | } |
| 82 | |
| 83 | // Store the name and data. |
| 84 | name := header[len(goldenHeaderPrefix) : len(header)-len("\n")] |
| 85 | if _, ok := out[name]; ok { |
| 86 | panic(fmt.Sprintf("duplicate name: %q", name)) |
| 87 | } |
| 88 | out[name] = s[len(header):j] |
| 89 | s = s[j+len(footer):] |
| 90 | } |
| 91 | return out |
| 92 | } |
| 93 | func mustFormatGolden(path string, in []struct{ Name, Data string }) { |
| 94 | var b []byte |
| 95 | for _, v := range in { |