github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/boilingcore/output_test.go (about) 1 package boilingcore 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "os" 8 "strings" 9 "testing" 10 11 "github.com/google/go-cmp/cmp" 12 ) 13 14 type NopWriteCloser struct { 15 io.Writer 16 } 17 18 func (NopWriteCloser) Close() error { 19 return nil 20 } 21 22 func nopCloser(w io.Writer) io.WriteCloser { 23 return NopWriteCloser{w} 24 } 25 26 func TestWriteFile(t *testing.T) { 27 // t.Parallel() cannot be used 28 29 // set the function pointer back to its original value 30 // after we modify it for the test 31 saveTestHarnessWriteFile := testHarnessWriteFile 32 defer func() { 33 testHarnessWriteFile = saveTestHarnessWriteFile 34 }() 35 36 var output []byte 37 testHarnessWriteFile = func(_ string, in []byte, _ os.FileMode) error { 38 output = in 39 return nil 40 } 41 42 buf := &bytes.Buffer{} 43 writePackageName(buf, "pkg") 44 fmt.Fprintf(buf, "func hello() {}\n\n\nfunc world() {\nreturn\n}\n\n\n\n") 45 46 if err := writeFile("", "", buf, true); err != nil { 47 t.Error(err) 48 } 49 50 if string(output) != "package pkg\n\nfunc hello() {}\n\nfunc world() {\n\treturn\n}\n" { 51 t.Errorf("Wrong output: %q", output) 52 } 53 } 54 55 func TestFormatBuffer(t *testing.T) { 56 t.Parallel() 57 58 buf := &bytes.Buffer{} 59 60 fmt.Fprintf(buf, "package pkg\n\nfunc() {a}\n") 61 62 // Only test error case - happy case is taken care of by template test 63 _, err := formatBuffer(buf) 64 if err == nil { 65 t.Error("want an error") 66 } 67 68 if txt := err.Error(); !strings.Contains(txt, ">>>> func() {a}") { 69 t.Error("got:\n", txt) 70 } 71 } 72 73 func TestOutputFilenameParts(t *testing.T) { 74 t.Parallel() 75 76 tests := []struct { 77 Filename string 78 79 FirstDir string 80 Normalized string 81 IsSingleton bool 82 IsGo bool 83 UsePkg bool 84 }{ 85 {"templates/00_struct.go.tpl", "templates", "struct.go", false, true, true}, 86 {"templates/singleton/00_struct.go.tpl", "templates", "struct.go", true, true, true}, 87 {"templates/notpkg/00_struct.go.tpl", "templates", "notpkg/struct.go", false, true, false}, 88 {"templates/js/singleton/00_struct.js.tpl", "templates", "js/struct.js", true, false, false}, 89 {"templates/js/00_struct.js.tpl", "templates", "js/struct.js", false, false, false}, 90 } 91 92 for i, test := range tests { 93 normalized, isSingleton, isGo, usePkg := outputFilenameParts(test.Filename) 94 95 if normalized != test.Normalized { 96 t.Errorf("%d) normalized wrong, want: %s, got: %s", i, test.Normalized, normalized) 97 } 98 if isSingleton != test.IsSingleton { 99 t.Errorf("%d) isSingleton wrong, want: %t, got: %t", i, test.IsSingleton, isSingleton) 100 } 101 if isGo != test.IsGo { 102 t.Errorf("%d) isGo wrong, want: %t, got: %t", i, test.IsGo, isGo) 103 } 104 if usePkg != test.UsePkg { 105 t.Errorf("%d) usePkg wrong, want: %t, got: %t", i, test.UsePkg, usePkg) 106 } 107 } 108 } 109 110 func TestGetOutputFilename(t *testing.T) { 111 t.Parallel() 112 113 tests := map[string]struct { 114 TableName string 115 IsTest bool 116 IsGo bool 117 Expected string 118 }{ 119 "regular": { 120 TableName: "hello", 121 IsTest: false, 122 IsGo: true, 123 Expected: "hello", 124 }, 125 "begins with underscore": { 126 TableName: "_hello", 127 IsTest: false, 128 IsGo: true, 129 Expected: "und_hello", 130 }, 131 "ends with _test": { 132 TableName: "hello_test", 133 IsTest: false, 134 IsGo: true, 135 Expected: "hello_test_model", 136 }, 137 "ends with _js": { 138 TableName: "hello_js", 139 IsTest: false, 140 IsGo: true, 141 Expected: "hello_js_model", 142 }, 143 "ends with _windows": { 144 TableName: "hello_windows", 145 IsTest: false, 146 IsGo: true, 147 Expected: "hello_windows_model", 148 }, 149 "ends with _arm64": { 150 TableName: "hello_arm64", 151 IsTest: false, 152 IsGo: true, 153 Expected: "hello_arm64_model", 154 }, 155 "non-go ends with _arm64": { 156 TableName: "hello_arm64", 157 IsTest: false, 158 IsGo: false, 159 Expected: "hello_arm64", 160 }, 161 } 162 163 for name, tc := range tests { 164 t.Run(name, func(t *testing.T) { 165 notTest := getOutputFilename(tc.TableName, false, tc.IsGo) 166 if diff := cmp.Diff(tc.Expected, notTest); diff != "" { 167 t.Fatalf(diff) 168 } 169 170 isTest := getOutputFilename(tc.TableName, true, tc.IsGo) 171 if diff := cmp.Diff(tc.Expected+"_test", isTest); diff != "" { 172 t.Fatalf(diff) 173 } 174 }) 175 } 176 }