|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "testing" |
| 8 | + |
| 9 | + jd "github.com/josephburnett/jd/lib" |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +type TestOptions struct { |
| 15 | + defaultResponse string |
| 16 | + description string |
| 17 | + testProto string |
| 18 | + title string |
| 19 | + version string |
| 20 | + include string |
| 21 | + ignore string |
| 22 | +} |
| 23 | + |
| 24 | +type TestSuite struct { |
| 25 | + suite.Suite |
| 26 | + rawDoc []byte |
| 27 | + options TestOptions |
| 28 | +} |
| 29 | + |
| 30 | +func (s *TestSuite) YAMLEqual(expected any, actual any, msgAndArgs ...any) { |
| 31 | + var expectedYAML, actualYAML string |
| 32 | + |
| 33 | + switch v := expected.(type) { |
| 34 | + case string: |
| 35 | + expectedYAML = v |
| 36 | + case []byte: |
| 37 | + expectedYAML = string(v) |
| 38 | + default: |
| 39 | + expectedBytes, err := yaml.Marshal(v) |
| 40 | + if err != nil { |
| 41 | + s.FailNow(fmt.Sprintf("failed to marshal expected: %#v", v), msgAndArgs...) |
| 42 | + } |
| 43 | + expectedYAML = string(expectedBytes) |
| 44 | + } |
| 45 | + |
| 46 | + switch v := actual.(type) { |
| 47 | + case string: |
| 48 | + actualYAML = v |
| 49 | + case []byte: |
| 50 | + actualYAML = string(v) |
| 51 | + default: |
| 52 | + actualBytes, err := yaml.Marshal(v) |
| 53 | + if err != nil { |
| 54 | + s.FailNow(fmt.Sprintf("failed to marshal actual: %#v", v), msgAndArgs...) |
| 55 | + } |
| 56 | + actualYAML = string(actualBytes) |
| 57 | + } |
| 58 | + |
| 59 | + expectedNode, _ := jd.ReadYamlString(expectedYAML) |
| 60 | + actualNode, _ := jd.ReadYamlString(actualYAML) |
| 61 | + |
| 62 | + diff := expectedNode.Diff(actualNode, jd.SET).Render(jd.COLOR) |
| 63 | + |
| 64 | + if diff != "" { |
| 65 | + s.Fail(fmt.Sprintf("Not Equal:\nexpected:\n%s\nactual:\n%s\n\n%s", expectedYAML, actualYAML, diff), msgAndArgs...) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (s *TestSuite) SetupSuite() { |
| 70 | + s.options = TestOptions{ |
| 71 | + defaultResponse: "test.api.Error", |
| 72 | + description: "test description", |
| 73 | + testProto: "basic_test.proto", |
| 74 | + title: "test title", |
| 75 | + version: "1.1.0", |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func (s *TestSuite) BeforeTest(suite, name string) { |
| 80 | + var filename string |
| 81 | + |
| 82 | + switch name { |
| 83 | + case "TestBasic": |
| 84 | + filename = "basic_test.proto" |
| 85 | + case "TestFile": |
| 86 | + filename = "file_test.proto" |
| 87 | + case "TestService": |
| 88 | + filename = "service_test.proto" |
| 89 | + case "TestMethod": |
| 90 | + filename = "method_test.proto" |
| 91 | + case "TestField": |
| 92 | + filename = "field_test.proto" |
| 93 | + default: |
| 94 | + s.FailNow("invalid test name") |
| 95 | + } |
| 96 | + |
| 97 | + err := exec.Command("rm", "-f", "../../test/openapi.yaml").Run() |
| 98 | + if err != nil { |
| 99 | + s.FailNow(err.Error()) |
| 100 | + } |
| 101 | + |
| 102 | + out, err := exec.Command("protoc", |
| 103 | + "-I=../../proto/technicallyjosh/oapi", |
| 104 | + "-I=../../test", |
| 105 | + "--openapi_out=../../test", |
| 106 | + "--openapi_opt=version="+s.options.version, |
| 107 | + "--openapi_opt=title="+s.options.title, |
| 108 | + "--openapi_opt=description="+s.options.description, |
| 109 | + "--openapi_opt=default_response="+s.options.defaultResponse, |
| 110 | + "--openapi_opt=include="+s.options.include, |
| 111 | + "--openapi_opt=ignore="+s.options.ignore, |
| 112 | + "../../test/"+filename, |
| 113 | + ).CombinedOutput() |
| 114 | + if err != nil { |
| 115 | + s.FailNow(err.Error(), string(out)) |
| 116 | + } |
| 117 | + |
| 118 | + s.rawDoc, err = os.ReadFile("../../test/openapi.yaml") |
| 119 | + if err != nil { |
| 120 | + s.FailNow(err.Error()) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func (s *TestSuite) TestBasic() { |
| 125 | + s.YAMLEqual(readFile("basic_test_openapi.yaml"), string(s.rawDoc)) |
| 126 | +} |
| 127 | + |
| 128 | +func (s *TestSuite) TestFile() { |
| 129 | + s.YAMLEqual(readFile("file_test_openapi.yaml"), string(s.rawDoc)) |
| 130 | +} |
| 131 | + |
| 132 | +func (s *TestSuite) TestService() { |
| 133 | + s.YAMLEqual(readFile("service_test_openapi.yaml"), string(s.rawDoc)) |
| 134 | +} |
| 135 | + |
| 136 | +func (s *TestSuite) TestMethod() { |
| 137 | + s.YAMLEqual(readFile("method_test_openapi.yaml"), string(s.rawDoc)) |
| 138 | +} |
| 139 | + |
| 140 | +func (s *TestSuite) TestField() { |
| 141 | + s.YAMLEqual(readFile("field_test_openapi.yaml"), string(s.rawDoc)) |
| 142 | +} |
| 143 | + |
| 144 | +func TestSuites(t *testing.T) { |
| 145 | + suite.Run(t, new(TestSuite)) |
| 146 | +} |
| 147 | + |
| 148 | +func readFile(name string) string { |
| 149 | + data, _ := os.ReadFile("../../test/" + name) |
| 150 | + |
| 151 | + return string(data) |
| 152 | +} |
0 commit comments