example_syncer_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func Test_syncExamples(t *testing.T) {
  19. var podExample = `apiVersion: v1
  20. kind: Pod
  21. metadata:
  22. name: nginx
  23. spec:
  24. containers:
  25. - name: nginx
  26. image: nginx
  27. ports:
  28. - containerPort: 80
  29. `
  30. var textExample = `some text
  31. `
  32. var cases = []struct {
  33. in string
  34. expected string
  35. }{
  36. {"", ""},
  37. {
  38. "<!-- BEGIN MUNGE: EXAMPLE testdata/pod.yaml -->\n<!-- END MUNGE: EXAMPLE testdata/pod.yaml -->\n",
  39. "<!-- BEGIN MUNGE: EXAMPLE testdata/pod.yaml -->\n\n```yaml\n" + podExample + "```\n\n[Download example](testdata/pod.yaml?raw=true)\n<!-- END MUNGE: EXAMPLE testdata/pod.yaml -->\n",
  40. },
  41. {
  42. "<!-- BEGIN MUNGE: EXAMPLE ../mungedocs/testdata/pod.yaml -->\n<!-- END MUNGE: EXAMPLE ../mungedocs/testdata/pod.yaml -->\n",
  43. "<!-- BEGIN MUNGE: EXAMPLE ../mungedocs/testdata/pod.yaml -->\n\n```yaml\n" + podExample + "```\n\n[Download example](../mungedocs/testdata/pod.yaml?raw=true)\n<!-- END MUNGE: EXAMPLE ../mungedocs/testdata/pod.yaml -->\n",
  44. },
  45. {
  46. "<!-- BEGIN MUNGE: EXAMPLE testdata/example.txt -->\n<!-- END MUNGE: EXAMPLE testdata/example.txt -->\n",
  47. "<!-- BEGIN MUNGE: EXAMPLE testdata/example.txt -->\n\n```\n" + textExample + "```\n\n[Download example](testdata/example.txt?raw=true)\n<!-- END MUNGE: EXAMPLE testdata/example.txt -->\n",
  48. },
  49. }
  50. repoRoot = ""
  51. for _, c := range cases {
  52. in := getMungeLines(c.in)
  53. expected := getMungeLines(c.expected)
  54. actual, err := syncExamples("filename.md", in)
  55. assert.NoError(t, err)
  56. if !expected.Equal(actual) {
  57. t.Errorf("Expected example \n'%q' but got \n'%q'", expected.String(), actual.String())
  58. }
  59. }
  60. }