How to use Write method of output Package

Best Testkube code snippet using output.Write

writeCloser_test.go

Source:writeCloser_test.go Github

copy

Full Screen

...5 "testing"6)7type errClose struct{}8func (ew errClose) Error() string { return "test close error" }9type errWrite struct{}10func (ew errWrite) Error() string { return "test write error" }11type errOnClose struct{}12func (eoc *errOnClose) Write(p []byte) (int, error) { return len(p), nil }13func (eoc *errOnClose) Close() error { return errClose{} }14type errOnWrite struct{}15func (eoc *errOnWrite) Write(p []byte) (int, error) { return 0, errWrite{} }16func (eoc *errOnWrite) Close() error { return errClose{} }17func TestWriteCloser(t *testing.T) {18 // initially empty vs not empty19 // buf newline: (none, single: (at end, not at end), multiple: (at end, not at end))20 // data newline: (none, single: (at end, not at end), multiple: (at end, not at end))21 // flush vs not-flush22 // write error vs no write error23 t.Run("NewWriteCloser", func(t *testing.T) {24 _, err := NewWriteCloser(NopCloseWriter(io.Discard), 0)25 ensureError(t, err, "flushThreshold")26 _, err = NewWriteCloser(NopCloseWriter(io.Discard), -1)27 ensureError(t, err, "flushThreshold")28 })29 t.Run("Close", func(t *testing.T) {30 t.Run("no error", func(t *testing.T) {31 wc, err := NewWriteCloser(NopCloseWriter(io.Discard), 16)32 ensureError(t, err)33 ensureWrite(t, wc, "line 1\n")34 ensureError(t, wc.Close())35 })36 t.Run("write returns error", func(t *testing.T) {37 wc, err := NewWriteCloser(&errOnWrite{}, 16)38 ensureError(t, err)39 ensureWrite(t, wc, "line 1")40 ensureError(t, wc.Close(), "test write error")41 })42 t.Run("close returns error", func(t *testing.T) {43 wc, err := NewWriteCloser(&errOnClose{}, 16)44 ensureError(t, err)45 ensureWrite(t, wc, "line 1")46 ensureError(t, wc.Close(), "test close error")47 })48 t.Run("write error during close", func(t *testing.T) {49 wc, err := NewWriteCloser(NopCloseWriter(io.Discard), 16)50 ensureError(t, err)51 ensureError(t, wc.Close())52 })53 })54 t.Run("flushCompleted", func(t *testing.T) {55 t.Run("buf has no newlines", func(t *testing.T) {56 wc, err := NewWriteCloser(NopCloseWriter(io.Discard), 16)57 ensureError(t, err)58 ensureWrite(t, wc, "line 1")59 n, err := wc.flushCompleted(0, 0, 0)60 if got, want := n, 0; got != want {61 t.Errorf("GOT: %v; WANT: %v", got, want)62 }63 ensureError(t, err)64 })65 t.Run("buf has newlines", func(t *testing.T) {66 output := new(bytes.Buffer)67 wc, err := NewWriteCloser(NopCloseWriter(output), 16)68 ensureError(t, err)69 ensureWrite(t, wc, "line 1\n")70 ensureWrite(t, wc, "line 2")71 const tokenInt = 4272 n, err := wc.flushCompleted(13, tokenInt, 7)73 if got, want := n, tokenInt; got != want {74 t.Errorf("GOT: %v; WANT: %v", got, want)75 }76 ensureError(t, err)77 ensureBuffer(t, output, "line 1\n")78 })79 })80 t.Run("Write", func(t *testing.T) {81 t.Run("buf empty | data no newline | no flush", func(t *testing.T) {82 output := new(bytes.Buffer)83 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)84 ensureError(t, err)85 p := "unterminated line"86 ensureWriteResponse(t, lbf, p, wantState{87 buf: "unterminated line",88 n: len(p),89 indexOfFinalNewline: -1,90 })91 ensureBuffer(t, output, "")92 })93 t.Run("buf empty | data single newline | at end | no flush", func(t *testing.T) {94 output := new(bytes.Buffer)95 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)96 ensureError(t, err)97 p := "terminated line\n"98 ensureWriteResponse(t, lbf, p, wantState{99 buf: p,100 n: len(p),101 indexOfFinalNewline: len(p) - 1,102 })103 ensureBuffer(t, output, "")104 })105 t.Run("buf empty | data single newline | at end | flush | no write error", func(t *testing.T) {106 output := new(bytes.Buffer)107 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)108 ensureError(t, err)109 p := "terminated line\n"110 ensureWriteResponse(t, lbf, p, wantState{111 n: len(p),112 indexOfFinalNewline: -1,113 })114 ensureBuffer(t, output, p)115 })116 t.Run("buf empty | data single newline | at end | flush | write error", func(t *testing.T) {117 output := new(bytes.Buffer)118 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)119 ensureError(t, err)120 p := "terminated line\n"121 ensureWriteResponse(t, lbf, p, wantState{122 isShortWrite: true,123 n: 4,124 indexOfFinalNewline: -1,125 })126 ensureBuffer(t, output, "term")127 })128 t.Run("buf empty | data single newline | not at end | no flush", func(t *testing.T) {129 output := new(bytes.Buffer)130 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)131 ensureError(t, err)132 p := "terminated\nline"133 ensureWriteResponse(t, lbf, p, wantState{134 buf: "terminated\nline",135 n: len(p),136 indexOfFinalNewline: 10,137 })138 ensureBuffer(t, output, "")139 })140 t.Run("buf empty | data single newline | not at end | flush | no write error", func(t *testing.T) {141 output := new(bytes.Buffer)142 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)143 ensureError(t, err)144 p := "terminated\nline"145 ensureWriteResponse(t, lbf, p, wantState{146 buf: "line",147 n: len(p),148 indexOfFinalNewline: -1,149 })150 ensureBuffer(t, output, "terminated\n")151 })152 t.Run("buf empty | data single newline | not at end | flush | write error", func(t *testing.T) {153 output := new(bytes.Buffer)154 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)155 ensureError(t, err)156 p := "terminated\nline"157 ensureWriteResponse(t, lbf, p, wantState{158 isShortWrite: true,159 n: 4,160 indexOfFinalNewline: -1,161 })162 ensureBuffer(t, output, "term")163 })164 t.Run("buf empty | data multiple newlines | at end | no flush", func(t *testing.T) {165 output := new(bytes.Buffer)166 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)167 ensureError(t, err)168 p := "terminated\nline\n"169 ensureWriteResponse(t, lbf, p, wantState{170 buf: p,171 n: len(p),172 indexOfFinalNewline: len(p) - 1,173 })174 ensureBuffer(t, output, "")175 })176 t.Run("buf empty | data multiple newlines | at end | flush | no write error", func(t *testing.T) {177 output := new(bytes.Buffer)178 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)179 ensureError(t, err)180 p := "terminated\nline\n"181 ensureWriteResponse(t, lbf, p, wantState{182 n: len(p),183 indexOfFinalNewline: -1,184 })185 ensureBuffer(t, output, "terminated\nline\n")186 })187 t.Run("buf empty | data multiple newlines | at end | flush | write error", func(t *testing.T) {188 output := new(bytes.Buffer)189 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)190 ensureError(t, err)191 p := "terminated\nline\n"192 ensureWriteResponse(t, lbf, p, wantState{193 isShortWrite: true,194 n: 4,195 indexOfFinalNewline: -1,196 })197 ensureBuffer(t, output, "term")198 })199 t.Run("buf empty | data multiple newlines | not at end | no flush", func(t *testing.T) {200 output := new(bytes.Buffer)201 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)202 ensureError(t, err)203 p := "terminated\nline\nhere"204 ensureWriteResponse(t, lbf, p, wantState{205 buf: p,206 n: len(p),207 indexOfFinalNewline: 15,208 })209 ensureBuffer(t, output, "")210 })211 t.Run("buf empty | data multiple newlines | not at end | flush | no write error", func(t *testing.T) {212 output := new(bytes.Buffer)213 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)214 ensureError(t, err)215 p := "terminated\nline\nhere"216 ensureWriteResponse(t, lbf, p, wantState{217 buf: "here",218 n: len(p),219 indexOfFinalNewline: -1,220 })221 ensureBuffer(t, output, "terminated\nline\n")222 })223 t.Run("buf empty | data multiple newlines | not at end | flush | write error", func(t *testing.T) {224 output := new(bytes.Buffer)225 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)226 ensureError(t, err)227 p := "terminated\nline\nhere"228 ensureWriteResponse(t, lbf, p, wantState{229 isShortWrite: true,230 n: 4,231 indexOfFinalNewline: -1,232 })233 ensureBuffer(t, output, "term")234 })235 //236 // buf not empty237 //238 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | at end | no flush", func(t *testing.T) {239 output := new(bytes.Buffer)240 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)241 ensureError(t, err)242 ensureWrite(t, lbf, "line 1\nline 2\n")243 p := "line 3\nline 4\n"244 ensureWriteResponse(t, lbf, p, wantState{245 buf: "line 1\nline 2\nline 3\nline 4\n",246 n: len(p),247 indexOfFinalNewline: len("line 1\nline 2\n") + len(p) - 1,248 })249 ensureBuffer(t, output, "")250 })251 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | at end | flush | no write error", func(t *testing.T) {252 output := new(bytes.Buffer)253 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)254 ensureError(t, err)255 ensureWrite(t, lbf, "line 1\nline 2\n")256 p := "line 3\nline 4\nline 5\n"257 ensureWriteResponse(t, lbf, p, wantState{258 n: len(p),259 indexOfFinalNewline: -1,260 })261 ensureBuffer(t, output, "line 1\nline 2\nline 3\nline 4\nline 5\n")262 })263 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | at end | flush | write error", func(t *testing.T) {264 output := new(bytes.Buffer)265 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)266 ensureError(t, err)267 ensureWrite(t, lbf, "line 1\nline 2\n")268 p := "line 3\nline 4\nline 5\n"269 ensureWriteResponse(t, lbf, p, wantState{270 buf: " 1\nline 2\n",271 isShortWrite: true,272 n: 0,273 indexOfFinalNewline: 9,274 })275 ensureBuffer(t, output, "line")276 })277 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | not at end | no flush", func(t *testing.T) {278 output := new(bytes.Buffer)279 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)280 ensureError(t, err)281 ensureWrite(t, lbf, "line 1\nline 2\n")282 p := "line 3\nline 4"283 ensureWriteResponse(t, lbf, p, wantState{284 buf: "line 1\nline 2\nline 3\nline 4",285 n: len(p),286 indexOfFinalNewline: 20,287 })288 ensureBuffer(t, output, "")289 })290 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | not at end | flush | no write error", func(t *testing.T) {291 output := new(bytes.Buffer)292 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)293 ensureError(t, err)294 ensureWrite(t, lbf, "line 1\nline 2\n")295 p := "line 3\nline 4"296 ensureWriteResponse(t, lbf, p, wantState{297 buf: "line 1\nline 2\nline 3\nline 4",298 n: len(p),299 indexOfFinalNewline: len("line 1\nline 2\nline 3\n") - 1,300 })301 ensureBuffer(t, output, "")302 })303 t.Run("buf not empty | buf multiple newlines | at end | data multiple newlines | not at end | flush | write error", func(t *testing.T) {304 output := new(bytes.Buffer)305 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)306 ensureError(t, err)307 ensureWrite(t, lbf, "line 1\nline 2\n")308 p := "line 3\nline 4"309 ensureWriteResponse(t, lbf, p, wantState{310 isShortWrite: true,311 buf: " 1\nline 2\n",312 n: 0,313 indexOfFinalNewline: len(" 1\nline 2\n") - 1,314 })315 ensureBuffer(t, output, "line")316 })317 t.Run("buf not empty | buf multiple newlines | at end | data no newline | no flush", func(t *testing.T) {318 output := new(bytes.Buffer)319 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)320 ensureError(t, err)321 ensureWrite(t, lbf, "line 1\nline 2\n")322 p := "line 3"323 ensureWriteResponse(t, lbf, p, wantState{324 buf: "line 1\nline 2\nline 3",325 n: 6,326 indexOfFinalNewline: 13,327 })328 ensureBuffer(t, output, "")329 })330 t.Run("buf not empty | buf multiple newlines | at end | data no newline | flush | no write error", func(t *testing.T) {331 output := new(bytes.Buffer)332 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)333 ensureError(t, err)334 ensureWrite(t, lbf, "line 1\nline 2\n")335 p := "line 3"336 ensureWriteResponse(t, lbf, p, wantState{337 buf: p,338 n: len(p),339 indexOfFinalNewline: -1,340 })341 ensureBuffer(t, output, "line 1\nline 2\n")342 })343 t.Run("buf not empty | buf multiple newlines | at end | data no newline | flush | write error", func(t *testing.T) {344 output := new(bytes.Buffer)345 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)346 ensureError(t, err)347 ensureWrite(t, lbf, "line 1\nline 2\n")348 p := "line 3"349 ensureWriteResponse(t, lbf, p, wantState{350 isShortWrite: true,351 buf: " 1\nline 2\n",352 n: 0,353 indexOfFinalNewline: 9,354 })355 ensureBuffer(t, output, "line")356 })357 t.Run("buf not empty | buf multiple newlines | at end | data single newline | at end | no flush", func(t *testing.T) {358 output := new(bytes.Buffer)359 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)360 ensureError(t, err)361 ensureWrite(t, lbf, "line 1\nline 2\n")362 p := "line 3\n"363 ensureWriteResponse(t, lbf, p, wantState{364 buf: "line 1\nline 2\nline 3\n",365 n: 7,366 indexOfFinalNewline: 20,367 })368 ensureBuffer(t, output, "")369 })370 t.Run("buf not empty | buf multiple newlines | at end | data single newline | at end | flush | no write error", func(t *testing.T) {371 output := new(bytes.Buffer)372 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)373 ensureError(t, err)374 ensureWrite(t, lbf, "line 1\nline 2\n")375 p := "line 3\n"376 ensureWriteResponse(t, lbf, p, wantState{377 n: len(p),378 indexOfFinalNewline: -1,379 })380 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")381 })382 t.Run("buf not empty | buf multiple newlines | at end | data single newline | at end | flush | write error", func(t *testing.T) {383 output := new(bytes.Buffer)384 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)385 ensureError(t, err)386 ensureWrite(t, lbf, "line 1\nline 2\n")387 p := "line 3\n"388 ensureWriteResponse(t, lbf, p, wantState{389 buf: " 1\nline 2\n",390 isShortWrite: true,391 n: 0,392 indexOfFinalNewline: 9,393 })394 ensureBuffer(t, output, "line")395 })396 t.Run("buf not empty | buf multiple newlines | at end | data single newline | not at end | no flush", func(t *testing.T) {397 output := new(bytes.Buffer)398 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)399 ensureError(t, err)400 ensureWrite(t, lbf, "line 1\nline 2\n")401 p := "line 3\nline 4"402 ensureWriteResponse(t, lbf, p, wantState{403 buf: "line 1\nline 2\nline 3\nline 4",404 n: len(p),405 indexOfFinalNewline: 20,406 })407 ensureBuffer(t, output, "")408 })409 t.Run("buf not empty | buf multiple newlines | at end | data single newline | not at end | flush | no write error", func(t *testing.T) {410 output := new(bytes.Buffer)411 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)412 ensureError(t, err)413 ensureWrite(t, lbf, "line 1\nline 2\n")414 p := "line 3\nline 4"415 ensureWriteResponse(t, lbf, p, wantState{416 buf: "line 4",417 n: len(p),418 indexOfFinalNewline: -1,419 })420 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")421 })422 t.Run("buf not empty | buf multiple newlines | at end | data single newline | not at end | flush | write error", func(t *testing.T) {423 output := new(bytes.Buffer)424 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)425 ensureError(t, err)426 ensureWrite(t, lbf, "line 1\nline 2\n")427 p := "line 3\nline 4"428 ensureWriteResponse(t, lbf, p, wantState{429 isShortWrite: true,430 buf: " 1\nline 2\n",431 n: 0,432 indexOfFinalNewline: 9,433 })434 ensureBuffer(t, output, "line")435 })436 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | at end | no flush", func(t *testing.T) {437 output := new(bytes.Buffer)438 lbf, err := NewWriteCloser(NopCloseWriter(output), 64)439 ensureError(t, err)440 ensureWrite(t, lbf, "line 1\nline 2\nline 3")441 p := "\nline 4\nline 5\n"442 ensureWriteResponse(t, lbf, p, wantState{443 buf: "line 1\nline 2\nline 3\nline 4\nline 5\n",444 n: len(p),445 indexOfFinalNewline: 34,446 })447 ensureBuffer(t, output, "")448 })449 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | at end | flush | no write error", func(t *testing.T) {450 output := new(bytes.Buffer)451 lbf, err := NewWriteCloser(NopCloseWriter(output), 24)452 ensureError(t, err)453 ensureWrite(t, lbf, "line 1\nline 2\nline 3")454 p := "\nline 4\nline 5\n"455 ensureWriteResponse(t, lbf, p, wantState{456 n: len(p),457 indexOfFinalNewline: -1,458 })459 ensureBuffer(t, output, "line 1\nline 2\nline 3\nline 4\nline 5\n")460 })461 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | at end | flush | write error", func(t *testing.T) {462 output := new(bytes.Buffer)463 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)464 ensureError(t, err)465 ensureWrite(t, lbf, "line 1\nline 2\nline 3")466 p := "\nline 4\nline 5\n"467 ensureWriteResponse(t, lbf, p, wantState{468 isShortWrite: true,469 buf: " 1\nline 2\nline 3",470 n: 0,471 indexOfFinalNewline: 9,472 })473 ensureBuffer(t, output, "line")474 })475 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | not at end | no flush", func(t *testing.T) {476 output := new(bytes.Buffer)477 lbf, err := NewWriteCloser(NopCloseWriter(output), 64)478 ensureError(t, err)479 ensureWrite(t, lbf, "line 1\nline 2\nline 3")480 p := "\nline 4\nline 5"481 ensureWriteResponse(t, lbf, p, wantState{482 buf: "line 1\nline 2\nline 3\nline 4\nline 5",483 n: len(p),484 indexOfFinalNewline: 27,485 })486 ensureBuffer(t, output, "")487 })488 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | not at end | flush | no write error", func(t *testing.T) {489 output := new(bytes.Buffer)490 lbf, err := NewWriteCloser(NopCloseWriter(output), 24)491 ensureError(t, err)492 ensureWrite(t, lbf, "line 1\nline 2\nline 3")493 p := "\nline 4\nline 5"494 ensureWriteResponse(t, lbf, p, wantState{495 buf: "line 5",496 n: len(p),497 indexOfFinalNewline: -1,498 })499 ensureBuffer(t, output, "line 1\nline 2\nline 3\nline 4\n")500 })501 t.Run("buf not empty | buf multiple newlines | not at end | data multiple newlines | not at end | flush | write error", func(t *testing.T) {502 output := new(bytes.Buffer)503 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)504 ensureError(t, err)505 ensureWrite(t, lbf, "line 1\nline 2\nline 3")506 p := "\nline 4\nline 5"507 ensureWriteResponse(t, lbf, p, wantState{508 isShortWrite: true,509 buf: " 1\nline 2\nline 3",510 n: 0,511 indexOfFinalNewline: 9,512 })513 ensureBuffer(t, output, "line")514 })515 t.Run("buf not empty | buf multiple newlines | not at end | data no newline | no flush", func(t *testing.T) {516 output := new(bytes.Buffer)517 lbf, err := NewWriteCloser(NopCloseWriter(output), 64)518 ensureError(t, err)519 ensureWrite(t, lbf, "line 1\nline 2\nline 3")520 p := "line 4"521 ensureWriteResponse(t, lbf, p, wantState{522 buf: "line 1\nline 2\nline 3line 4",523 n: len(p),524 indexOfFinalNewline: 13,525 })526 ensureBuffer(t, output, "")527 })528 t.Run("buf not empty | buf multiple newlines | not at end | data no newline | flush | no write error", func(t *testing.T) {529 output := new(bytes.Buffer)530 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)531 ensureError(t, err)532 ensureWrite(t, lbf, "line 1\nline 2\nline 3")533 p := "line 4"534 ensureWriteResponse(t, lbf, p, wantState{535 buf: "line 3line 4",536 n: len(p),537 indexOfFinalNewline: -1,538 })539 ensureBuffer(t, output, "line 1\nline 2\n")540 })541 t.Run("buf not empty | buf multiple newlines | not at end | data no newline | flush | write error", func(t *testing.T) {542 output := new(bytes.Buffer)543 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)544 ensureError(t, err)545 ensureWrite(t, lbf, "line 1\nline 2\nline 3")546 p := "line 4"547 ensureWriteResponse(t, lbf, p, wantState{548 isShortWrite: true,549 buf: " 1\nline 2\nline 3",550 n: 0,551 indexOfFinalNewline: 9,552 })553 ensureBuffer(t, output, "line")554 })555 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | at end | no flush", func(t *testing.T) {556 output := new(bytes.Buffer)557 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)558 ensureError(t, err)559 ensureWrite(t, lbf, "line 1\nline 2\nline 3")560 p := "line 4\n"561 ensureWriteResponse(t, lbf, p, wantState{562 buf: "line 1\nline 2\nline 3line 4\n",563 n: len(p),564 indexOfFinalNewline: 26,565 })566 ensureBuffer(t, output, "")567 })568 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | at end | flush | no write error", func(t *testing.T) {569 output := new(bytes.Buffer)570 lbf, err := NewWriteCloser(NopCloseWriter(output), 24)571 ensureError(t, err)572 ensureWrite(t, lbf, "line 1\nline 2\nline 3")573 p := "line 4\n"574 ensureWriteResponse(t, lbf, p, wantState{575 n: len(p),576 indexOfFinalNewline: -1,577 })578 ensureBuffer(t, output, "line 1\nline 2\nline 3line 4\n")579 })580 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | at end | flush | write error", func(t *testing.T) {581 output := new(bytes.Buffer)582 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)583 ensureError(t, err)584 ensureWrite(t, lbf, "line 1\nline 2\nline 3")585 p := "line 4\n"586 ensureWriteResponse(t, lbf, p, wantState{587 isShortWrite: true,588 buf: " 1\nline 2\nline 3",589 n: 0,590 indexOfFinalNewline: 9,591 })592 ensureBuffer(t, output, "line")593 })594 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | not at end | no flush", func(t *testing.T) {595 output := new(bytes.Buffer)596 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)597 ensureError(t, err)598 ensureWrite(t, lbf, "line 1\nline 2\nline 3")599 p := "\nline 4"600 ensureWriteResponse(t, lbf, p, wantState{601 isShortWrite: true,602 buf: " 1\nline 2\nline 3",603 n: 0,604 indexOfFinalNewline: 9,605 })606 ensureBuffer(t, output, "line")607 })608 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | not at end | flush | no write error", func(t *testing.T) {609 output := new(bytes.Buffer)610 lbf, err := NewWriteCloser(NopCloseWriter(output), 24)611 ensureError(t, err)612 ensureWrite(t, lbf, "line 1\nline 2\nline 3")613 p := "\nline 4"614 ensureWriteResponse(t, lbf, p, wantState{615 buf: "line 4",616 n: len(p),617 indexOfFinalNewline: -1,618 })619 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")620 })621 t.Run("buf not empty | buf multiple newlines | not at end | data single newline | not at end | flush | write error", func(t *testing.T) {622 output := new(bytes.Buffer)623 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)624 ensureError(t, err)625 ensureWrite(t, lbf, "line 1\nline 2\nline 3")626 p := "\nline 4"627 ensureWriteResponse(t, lbf, p, wantState{628 isShortWrite: true,629 buf: " 1\nline 2\nline 3",630 n: 0,631 indexOfFinalNewline: 9,632 })633 ensureBuffer(t, output, "line")634 })635 t.Run("buf not empty | buf no newline | data multiple newlines | not at end | no flush", func(t *testing.T) {636 output := new(bytes.Buffer)637 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)638 ensureError(t, err)639 ensureWrite(t, lbf, "line 1")640 p := "\nline 2\nline 3\nline 4"641 ensureWriteResponse(t, lbf, p, wantState{642 buf: "line 1\nline 2\nline 3\nline 4",643 n: len(p),644 indexOfFinalNewline: 20,645 })646 ensureBuffer(t, output, "")647 })648 t.Run("buf not empty | buf no newline | data multiple newlines | not at end | flush | no write error", func(t *testing.T) {649 output := new(bytes.Buffer)650 lbf, err := NewWriteCloser(NopCloseWriter(output), 24)651 ensureError(t, err)652 ensureWrite(t, lbf, "line 1")653 p := "\nline 2\nline 3\nline 4"654 ensureWriteResponse(t, lbf, p, wantState{655 buf: "line 4",656 n: len(p),657 indexOfFinalNewline: -1,658 })659 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")660 })661 t.Run("buf not empty | buf no newline | data multiple newlines | not at end | flush | write error", func(t *testing.T) {662 output := new(bytes.Buffer)663 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 24)664 ensureError(t, err)665 ensureWrite(t, lbf, "line 1")666 p := "\nline 2\nline 3\nline 4"667 ensureWriteResponse(t, lbf, p, wantState{668 isShortWrite: true,669 buf: " 1",670 n: 0,671 indexOfFinalNewline: -1,672 })673 ensureBuffer(t, output, "line")674 })675 t.Run("buf not empty | buf no newline | data no newline | no flush", func(t *testing.T) {676 output := new(bytes.Buffer)677 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)678 ensureError(t, err)679 ensureWrite(t, lbf, "line 1")680 p := "line 2"681 ensureWriteResponse(t, lbf, p, wantState{682 buf: "line 1line 2",683 n: len(p),684 indexOfFinalNewline: -1,685 })686 ensureBuffer(t, output, "")687 })688 // t.Run("buf not empty | buf no newline | data no newline | flush | no write error", func(t *testing.T) {689 // })690 // t.Run("buf not empty | buf no newline | data no newline | flush | write error", func(t *testing.T) {691 // })692 t.Run("buf not empty | buf no newline | data single newline | at end | no flush", func(t *testing.T) {693 output := new(bytes.Buffer)694 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)695 ensureError(t, err)696 ensureWrite(t, lbf, "line 1")697 p := "line 2\n"698 ensureWriteResponse(t, lbf, p, wantState{699 buf: "line 1line 2\n",700 n: len(p),701 indexOfFinalNewline: 12,702 })703 ensureBuffer(t, output, "")704 })705 t.Run("buf not empty | buf no newline | data single newline | at end | flush | no write error", func(t *testing.T) {706 output := new(bytes.Buffer)707 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)708 ensureError(t, err)709 ensureWrite(t, lbf, "line 1")710 p := "line 2\n"711 ensureWriteResponse(t, lbf, p, wantState{712 n: len(p),713 indexOfFinalNewline: -1,714 })715 ensureBuffer(t, output, "line 1line 2\n")716 })717 t.Run("buf not empty | buf no newline | data single newline | at end | flush | write error", func(t *testing.T) {718 output := new(bytes.Buffer)719 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)720 ensureError(t, err)721 ensureWrite(t, lbf, "line 1")722 p := "line 2\n"723 ensureWriteResponse(t, lbf, p, wantState{724 isShortWrite: true,725 buf: " 1",726 n: 0,727 indexOfFinalNewline: -1,728 })729 ensureBuffer(t, output, "line")730 })731 t.Run("buf not empty | buf no newline | data single newline | not at end | no flush", func(t *testing.T) {732 output := new(bytes.Buffer)733 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)734 ensureError(t, err)735 ensureWrite(t, lbf, "line 1")736 p := "\nline 2"737 ensureWriteResponse(t, lbf, p, wantState{738 buf: "line 1\nline 2",739 n: len(p),740 indexOfFinalNewline: 6,741 })742 ensureBuffer(t, output, "")743 })744 t.Run("buf not empty | buf no newline | data single newline | not at end | flush | no write error", func(t *testing.T) {745 output := new(bytes.Buffer)746 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)747 ensureError(t, err)748 ensureWrite(t, lbf, "line 1")749 p := "\nline 2"750 ensureWriteResponse(t, lbf, p, wantState{751 buf: "line 2",752 n: len(p),753 indexOfFinalNewline: -1,754 })755 ensureBuffer(t, output, "line 1\n")756 })757 t.Run("buf not empty | buf no newline | data single newline | not at end | flush | write error", func(t *testing.T) {758 output := new(bytes.Buffer)759 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)760 ensureError(t, err)761 ensureWrite(t, lbf, "line 1")762 p := "\nline 2"763 ensureWriteResponse(t, lbf, p, wantState{764 isShortWrite: true,765 buf: " 1",766 n: 0,767 indexOfFinalNewline: -1,768 })769 ensureBuffer(t, output, "line")770 })771 t.Run("buf not empty | buf single newline | at end | data multiple newlines | at end | no flush", func(t *testing.T) {772 output := new(bytes.Buffer)773 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)774 ensureError(t, err)775 ensureWrite(t, lbf, "line 1\n")776 p := "line 2\nline 3\n"777 ensureWriteResponse(t, lbf, p, wantState{778 buf: "line 1\nline 2\nline 3\n",779 n: len(p),780 indexOfFinalNewline: 20,781 })782 ensureBuffer(t, output, "")783 })784 t.Run("buf not empty | buf single newline | at end | data multiple newlines | at end | flush | no write error", func(t *testing.T) {785 output := new(bytes.Buffer)786 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)787 ensureError(t, err)788 ensureWrite(t, lbf, "line 1\n")789 p := "line 2\nline 3\n"790 ensureWriteResponse(t, lbf, p, wantState{791 n: len(p),792 indexOfFinalNewline: -1,793 })794 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")795 })796 t.Run("buf not empty | buf single newline | at end | data multiple newlines | at end | flush | write error", func(t *testing.T) {797 output := new(bytes.Buffer)798 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)799 ensureError(t, err)800 ensureWrite(t, lbf, "line 1\n")801 p := "line 2\nline 3\n"802 ensureWriteResponse(t, lbf, p, wantState{803 isShortWrite: true,804 buf: " 1\n",805 n: 0,806 indexOfFinalNewline: 2,807 })808 ensureBuffer(t, output, "line")809 })810 t.Run("buf not empty | buf single newline | at end | data multiple newlines | not at end | no flush", func(t *testing.T) {811 output := new(bytes.Buffer)812 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)813 ensureError(t, err)814 ensureWrite(t, lbf, "line 1\n")815 p := "line 2\nline 3\nline 4"816 ensureWriteResponse(t, lbf, p, wantState{817 buf: "line 1\nline 2\nline 3\nline 4",818 n: len(p),819 indexOfFinalNewline: 20,820 })821 ensureBuffer(t, output, "")822 })823 t.Run("buf not empty | buf single newline | at end | data multiple newlines | not at end | flush | no write error", func(t *testing.T) {824 output := new(bytes.Buffer)825 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)826 ensureError(t, err)827 ensureWrite(t, lbf, "line 1\n")828 p := "line 2\nline 3\nline 4"829 ensureWriteResponse(t, lbf, p, wantState{830 buf: "line 4",831 n: len(p),832 indexOfFinalNewline: -1,833 })834 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")835 })836 t.Run("buf not empty | buf single newline | at end | data multiple newlines | not at end | flush | write error", func(t *testing.T) {837 output := new(bytes.Buffer)838 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)839 ensureError(t, err)840 ensureWrite(t, lbf, "line 1\n")841 p := "line 2\nline 3\nline 4"842 ensureWriteResponse(t, lbf, p, wantState{843 buf: " 1\n",844 n: 0,845 indexOfFinalNewline: 2,846 isShortWrite: true,847 })848 ensureBuffer(t, output, "line")849 })850 t.Run("buf not empty | buf single newline | at end | data no newline | no flush", func(t *testing.T) {851 output := new(bytes.Buffer)852 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)853 ensureError(t, err)854 ensureWrite(t, lbf, "line 1\n")855 p := "line 2"856 ensureWriteResponse(t, lbf, p, wantState{857 buf: "line 1\nline 2",858 n: len(p),859 indexOfFinalNewline: 6,860 })861 ensureBuffer(t, output, "")862 })863 t.Run("buf not empty | buf single newline | at end | data no newline | flush | no write error", func(t *testing.T) {864 output := new(bytes.Buffer)865 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)866 ensureError(t, err)867 ensureWrite(t, lbf, "line 1\n")868 p := "line 2"869 ensureWriteResponse(t, lbf, p, wantState{870 buf: "line 2",871 n: len(p),872 indexOfFinalNewline: -1,873 })874 ensureBuffer(t, output, "line 1\n")875 })876 t.Run("buf not empty | buf single newline | at end | data no newline | flush | write error", func(t *testing.T) {877 output := new(bytes.Buffer)878 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)879 ensureError(t, err)880 ensureWrite(t, lbf, "line 1\n")881 p := "line 2"882 ensureWriteResponse(t, lbf, p, wantState{883 buf: " 1\n",884 n: 0,885 indexOfFinalNewline: 2,886 isShortWrite: true,887 })888 ensureBuffer(t, output, "line")889 })890 t.Run("buf not empty | buf single newline | at end | data single newline | at end | no flush", func(t *testing.T) {891 output := new(bytes.Buffer)892 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)893 ensureError(t, err)894 ensureWrite(t, lbf, "line 1\n")895 p := "line 2\n"896 ensureWriteResponse(t, lbf, p, wantState{897 buf: "line 1\nline 2\n",898 n: len(p),899 indexOfFinalNewline: 13,900 })901 ensureBuffer(t, output, "")902 })903 t.Run("buf not empty | buf single newline | at end | data single newline | at end | flush | no write error", func(t *testing.T) {904 output := new(bytes.Buffer)905 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)906 ensureError(t, err)907 ensureWrite(t, lbf, "line 1\n")908 p := "line 2\n"909 ensureWriteResponse(t, lbf, p, wantState{910 n: len(p),911 indexOfFinalNewline: -1,912 })913 ensureBuffer(t, output, "line 1\nline 2\n")914 })915 t.Run("buf not empty | buf single newline | at end | data single newline | at end | flush | write error", func(t *testing.T) {916 output := new(bytes.Buffer)917 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)918 ensureError(t, err)919 ensureWrite(t, lbf, "line 1\n")920 p := "line 2\n"921 ensureWriteResponse(t, lbf, p, wantState{922 buf: " 1\n",923 n: 0,924 indexOfFinalNewline: 2,925 isShortWrite: true,926 })927 ensureBuffer(t, output, "line")928 })929 t.Run("buf not empty | buf single newline | at end | data single newline | not at end | no flush", func(t *testing.T) {930 output := new(bytes.Buffer)931 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)932 ensureError(t, err)933 ensureWrite(t, lbf, "line 1\n")934 p := "line 2\nline 3"935 ensureWriteResponse(t, lbf, p, wantState{936 buf: "line 1\nline 2\nline 3",937 n: len(p),938 indexOfFinalNewline: 13,939 })940 ensureBuffer(t, output, "")941 })942 t.Run("buf not empty | buf single newline | at end | data single newline | not at end | flush | no write error", func(t *testing.T) {943 output := new(bytes.Buffer)944 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)945 ensureError(t, err)946 ensureWrite(t, lbf, "line 1\n")947 p := "line 2\nline 3"948 ensureWriteResponse(t, lbf, p, wantState{949 buf: "line 3",950 n: len(p),951 indexOfFinalNewline: -1,952 })953 ensureBuffer(t, output, "line 1\nline 2\n")954 })955 t.Run("buf not empty | buf single newline | at end | data single newline | not at end | flush | write error | no new bytes", func(t *testing.T) {956 output := new(bytes.Buffer)957 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 8)958 ensureError(t, err)959 ensureWrite(t, lbf, "line 1\n")960 p := "line 2\nline 3"961 ensureWriteResponse(t, lbf, p, wantState{962 buf: " 1\n",963 n: 0,964 indexOfFinalNewline: 2,965 isShortWrite: true,966 })967 ensureBuffer(t, output, "line")968 })969 t.Run("buf not empty | buf single newline | at end | data multiple newlines | at end | flush | write error | zero new bytes", func(t *testing.T) {970 output := new(bytes.Buffer)971 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 7)), 16)972 ensureError(t, err)973 ensureWrite(t, lbf, "line 1\n")974 p := "line 2\nline 3\n"975 ensureWriteResponse(t, lbf, p, wantState{976 n: 0,977 indexOfFinalNewline: -1,978 isShortWrite: true,979 })980 ensureBuffer(t, output, "line 1\n")981 })982 t.Run("buf not empty | buf single newline | at end | data multiple newlines | at end | flush | write error | some new bytes", func(t *testing.T) {983 output := new(bytes.Buffer)984 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 12)), 16)985 ensureError(t, err)986 ensureWrite(t, lbf, "line 1\n")987 p := "line 2\nline 3\n"988 ensureWriteResponse(t, lbf, p, wantState{989 n: 5,990 indexOfFinalNewline: -1,991 isShortWrite: true,992 })993 ensureBuffer(t, output, "line 1\nline ")994 })995 t.Run("buf not empty | buf single newline | not at end | data multiple newlines | at end", func(t *testing.T) {996 const buf = "line 1\nline 2"997 const data = "\nline 3\nline 4"998 t.Run("no flush", func(t *testing.T) {999 output := new(bytes.Buffer)1000 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)1001 ensureError(t, err)1002 ensureWrite(t, lbf, buf)1003 ensureWriteResponse(t, lbf, data, wantState{1004 buf: "line 1\nline 2\nline 3\nline 4",1005 n: len(data),1006 indexOfFinalNewline: 20,1007 })1008 ensureBuffer(t, output, "")1009 })1010 t.Run("write", func(t *testing.T) {1011 output := new(bytes.Buffer)1012 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)1013 ensureError(t, err)1014 ensureWrite(t, lbf, buf)1015 ensureWriteResponse(t, lbf, data, wantState{1016 buf: "line 4",1017 n: len(data),1018 indexOfFinalNewline: -1,1019 })1020 ensureBuffer(t, output, "line 1\nline 2\nline 3\n")1021 })1022 t.Run("error", func(t *testing.T) {1023 output := new(bytes.Buffer)1024 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)1025 ensureError(t, err)1026 ensureWrite(t, lbf, buf)1027 ensureWriteResponse(t, lbf, data, wantState{1028 buf: " 1\nline 2",1029 n: 0,1030 indexOfFinalNewline: 2,1031 isShortWrite: true,1032 })1033 ensureBuffer(t, output, "line")1034 })1035 })1036 t.Run("buf not empty | buf single newline | not at end | data multiple newlines | not at end", func(t *testing.T) {1037 const buf = "\nline 1"1038 const data = "\nline 2\nline 3"1039 t.Run("no flush", func(t *testing.T) {1040 output := new(bytes.Buffer)1041 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)1042 ensureError(t, err)1043 ensureWrite(t, lbf, buf)1044 ensureWriteResponse(t, lbf, data, wantState{1045 buf: "\nline 1\nline 2\nline 3",1046 n: len(data),1047 indexOfFinalNewline: 14,1048 })1049 ensureBuffer(t, output, "")1050 })1051 t.Run("write", func(t *testing.T) {1052 output := new(bytes.Buffer)1053 lbf, err := NewWriteCloser(NopCloseWriter(output), 16)1054 ensureError(t, err)1055 ensureWrite(t, lbf, buf)1056 ensureWriteResponse(t, lbf, data, wantState{1057 buf: "line 3",1058 n: len(data),1059 indexOfFinalNewline: -1,1060 })1061 ensureBuffer(t, output, "\nline 1\nline 2\n")1062 })1063 t.Run("error", func(t *testing.T) {1064 output := new(bytes.Buffer)1065 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)1066 ensureError(t, err)1067 ensureWrite(t, lbf, buf)1068 ensureWriteResponse(t, lbf, data, wantState{1069 buf: "e 1",1070 n: 0,1071 indexOfFinalNewline: -1,1072 isShortWrite: true,1073 })1074 ensureBuffer(t, output, "\nlin")1075 })1076 })1077 t.Run("buf not empty | buf single newline | not at end | data no newline", func(t *testing.T) {1078 const buf = "line 1\nline 2"1079 const data = "line 3"1080 t.Run("no flush", func(t *testing.T) {1081 output := new(bytes.Buffer)1082 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)1083 ensureError(t, err)1084 ensureWrite(t, lbf, buf)1085 ensureWriteResponse(t, lbf, data, wantState{1086 buf: "line 1\nline 2line 3",1087 n: len(data),1088 indexOfFinalNewline: 6,1089 })1090 ensureBuffer(t, output, "")1091 })1092 t.Run("write", func(t *testing.T) {1093 output := new(bytes.Buffer)1094 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)1095 ensureError(t, err)1096 ensureWrite(t, lbf, buf)1097 ensureWriteResponse(t, lbf, data, wantState{1098 buf: "line 2line 3",1099 n: len(data),1100 indexOfFinalNewline: -1,1101 })1102 ensureBuffer(t, output, "line 1\n")1103 })1104 t.Run("error", func(t *testing.T) {1105 output := new(bytes.Buffer)1106 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)1107 ensureError(t, err)1108 ensureWrite(t, lbf, buf)1109 ensureWriteResponse(t, lbf, data, wantState{1110 buf: " 1\nline 2",1111 n: 0,1112 indexOfFinalNewline: 2,1113 isShortWrite: true,1114 })1115 ensureBuffer(t, output, "line")1116 })1117 })1118 t.Run("buf not empty | buf single newline | not at end | data single newline | at end", func(t *testing.T) {1119 const buf = "line 1\nline 2"1120 const data = "line 3\n"1121 t.Run("no flush", func(t *testing.T) {1122 output := new(bytes.Buffer)1123 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)1124 ensureError(t, err)1125 ensureWrite(t, lbf, buf)1126 ensureWriteResponse(t, lbf, data, wantState{1127 buf: "line 1\nline 2line 3\n",1128 n: len(data),1129 indexOfFinalNewline: 19,1130 })1131 ensureBuffer(t, output, "")1132 })1133 t.Run("write", func(t *testing.T) {1134 output := new(bytes.Buffer)1135 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)1136 ensureError(t, err)1137 ensureWrite(t, lbf, buf)1138 ensureWriteResponse(t, lbf, data, wantState{1139 n: len(data),1140 indexOfFinalNewline: -1,1141 })1142 ensureBuffer(t, output, "line 1\nline 2line 3\n")1143 })1144 t.Run("error", func(t *testing.T) {1145 output := new(bytes.Buffer)1146 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)1147 ensureError(t, err)1148 ensureWrite(t, lbf, buf)1149 ensureWriteResponse(t, lbf, data, wantState{1150 buf: " 1\nline 2",1151 n: 0,1152 indexOfFinalNewline: 2,1153 isShortWrite: true,1154 })1155 ensureBuffer(t, output, "line")1156 })1157 })1158 t.Run("buf not empty | buf single newline | not at end | data single newline | not at end", func(t *testing.T) {1159 const buf = "line 1\nline 2"1160 const data = "\nline 3"1161 t.Run("no flush", func(t *testing.T) {1162 output := new(bytes.Buffer)1163 lbf, err := NewWriteCloser(NopCloseWriter(output), 32)1164 ensureError(t, err)1165 ensureWrite(t, lbf, buf)1166 ensureWriteResponse(t, lbf, data, wantState{1167 buf: "line 1\nline 2\nline 3",1168 n: len(data),1169 indexOfFinalNewline: 13,1170 })1171 ensureBuffer(t, output, "")1172 })1173 t.Run("write", func(t *testing.T) {1174 output := new(bytes.Buffer)1175 lbf, err := NewWriteCloser(NopCloseWriter(output), 8)1176 ensureError(t, err)1177 ensureWrite(t, lbf, buf)1178 ensureWriteResponse(t, lbf, data, wantState{1179 buf: "line 3",1180 n: len(data),1181 indexOfFinalNewline: -1,1182 })1183 ensureBuffer(t, output, "line 1\nline 2\n")1184 })1185 t.Run("error", func(t *testing.T) {1186 output := new(bytes.Buffer)1187 lbf, err := NewWriteCloser(NopCloseWriter(ShortWriter(output, 4)), 16)1188 ensureError(t, err)1189 ensureWrite(t, lbf, buf)1190 ensureWriteResponse(t, lbf, data, wantState{1191 buf: " 1\nline 2",1192 n: 0,1193 indexOfFinalNewline: 2,1194 isShortWrite: true,1195 })1196 ensureBuffer(t, output, "line")1197 })1198 })1199 })1200}1201// flushCompleted writes all completed lines in buffer to underlying1202// io.WriteCloser. The final incomplete line will remain in the buffer.1203func (lbf *WriteCloser) flushCompleted(olen, dlen, index int) (int, error) {1204 if lbf.indexOfFinalNewline == -1 {1205 return 0, nil // buffer has no completed lines1206 }1207 return lbf.flush(olen, dlen, lbf.indexOfFinalNewline+1)1208}...

Full Screen

Full Screen

kotlin.go

Source:kotlin.go Github

copy

Full Screen

...54func generateReadScalar(field *il.SelectionField, tp string, output *Output) {55 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)56 storeKey := storeKey(field, output)57 requestKey := field.Alias58 output.WriteLine(scope + ".set(" + storeKey + ", " + scope + ".read" + tp + "(\"" + requestKey + "\"))")59}60func generateReadOptionalScalar(field *il.SelectionField, tp string, output *Output) {61 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)62 storeKey := storeKey(field, output)63 requestKey := field.Alias64 output.WriteLine(scope + ".set(" + storeKey + ", " + scope + ".read" + tp + "Optional(\"" + requestKey + "\"))")65}66func generateReadOptionalListScalar(tp string, output *Output) {67 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)68 output.WriteLine(scope + ".next(" + scope + ".read" + tp + "Optional(i))")69}70func generateReadListScalar(tp string, output *Output) {71 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)72 output.WriteLine(scope + ".next(" + scope + ".read" + tp + "(i))")73}74func newScope(field *il.SelectionField, output *Output) {75 storeKey := storeKey(field, output)76 requestKey := field.Alias77 output.NextScope()78 output.WriteLine("val scope" + strconv.FormatInt(output.GetScope(), 10) + " = scope" + strconv.FormatInt(output.ParentScope(), 10) +79 ".child(\"" + requestKey + "\", " + storeKey + ")")80}81func newScopeInList(output *Output) {82 output.NextScope()83 output.WriteLine("val scope" + strconv.FormatInt(output.GetScope(), 10) + " = scope" + strconv.FormatInt(output.ParentScope(), 10) +84 ".child(i)")85}86func newListScope(field *il.SelectionField, output *Output) {87 storeKey := storeKey(field, output)88 requestKey := field.Alias89 output.NextScope()90 output.WriteLine("val scope" + strconv.FormatInt(output.GetScope(), 10) + " = scope" + strconv.FormatInt(output.ParentScope(), 10) +91 ".childList(\"" + requestKey + "\", " + storeKey + ")")92}93func newListScopeInList(output *Output) {94 output.NextScope()95 output.WriteLine("val scope" + strconv.FormatInt(output.GetScope(), 10) + " = scope" + strconv.FormatInt(output.ParentScope(), 10) +96 ".childList(i)")97}98var nextLevel int6499func generateListNormalizer(level int64, fld *il.SelectionField, list il.List, output *Output) {100 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)101 output.WriteLine("for (i in 0 until " + scope + ".size) {")102 output.IndentAdd()103 if list.Inner.GetKind() == "NotNull" {104 inner := list.Inner.(il.NotNull).Inner105 if inner.GetKind() == "Scalar" {106 scalar := inner.(il.Scalar)107 if scalar.Name == "String" {108 generateReadListScalar("String", output)109 } else if scalar.Name == "Int" {110 generateReadListScalar("Int", output)111 } else if scalar.Name == "Float" {112 generateReadListScalar("Float", output)113 } else if scalar.Name == "ID" {114 generateReadListScalar("String", output)115 } else if scalar.Name == "Boolean" {116 generateReadListScalar("Boolean", output)117 } else if scalar.Name == "Date" {118 generateReadListScalar("String", output)119 } else {120 panic("Unsupported scalar: " + scalar.Name)121 }122 } else if inner.GetKind() == "Object" || inner.GetKind() == "Union" || inner.GetKind() == "Interface" {123 output.WriteLine(scope + ".assertObject(i)")124 newScopeInList(output)125 if inner.GetKind() == "Object" {126 obj := inner.(il.Object)127 generateNormalizer(obj.SelectionSet, output)128 } else if inner.GetKind() == "Interface" {129 obj := inner.(il.Interface)130 generateNormalizer(obj.SelectionSet, output)131 } else {132 obj := inner.(il.Union)133 generateNormalizer(obj.SelectionSet, output)134 }135 output.ScopePop()136 } else if inner.GetKind() == "Enum" {137 generateReadListScalar("String", output)138 } else {139 panic("Unsupported list inner type " + inner.GetKind())140 }141 } else {142 if list.Inner.GetKind() == "Scalar" {143 scalar := list.Inner.(il.Scalar)144 if scalar.Name == "String" {145 generateReadOptionalListScalar("String", output)146 } else if scalar.Name == "Int" {147 generateReadOptionalListScalar("Int", output)148 } else if scalar.Name == "Float" {149 generateReadOptionalListScalar("Float", output)150 } else if scalar.Name == "ID" {151 generateReadOptionalListScalar("String", output)152 } else if scalar.Name == "Boolean" {153 generateReadOptionalListScalar("Boolean", output)154 } else if scalar.Name == "Date" {155 generateReadOptionalListScalar("String", output)156 } else {157 panic("Unsupported scalar: " + scalar.Name)158 }159 } else if list.Inner.GetKind() == "Object" || list.Inner.GetKind() == "Union" || list.Inner.GetKind() == "Interface" {160 } else if list.Inner.GetKind() == "Enum" {161 generateReadOptionalListScalar("String", output)162 } else if list.Inner.GetKind() == "List" {163 output.WriteLine("if (" + scope + ".isNotNull(i)) {")164 output.IndentAdd()165 newListScopeInList(output)166 generateListNormalizer(nextLevel, fld, list.Inner.(il.List), output)167 output.ScopePop()168 output.IndentRemove()169 output.WriteLine("}")170 } else {171 panic("Unsupported list inner type " + list.Inner.GetKind())172 }173 }174 output.IndentRemove()175 output.WriteLine("}")176}177func generateNormalizer(set *il.SelectionSet, output *Output) {178 scope := "scope" + strconv.FormatInt(output.GetScope(), 10)179 for _, fld := range set.Fields {180 if fld.Type.GetKind() == "NotNull" {181 inner := fld.Type.(il.NotNull).Inner182 if inner.GetKind() == "Scalar" {183 scalar := inner.(il.Scalar)184 if scalar.Name == "String" {185 generateReadScalar(fld, "String", output)186 } else if scalar.Name == "Int" {187 generateReadScalar(fld, "Int", output)188 } else if scalar.Name == "Float" {189 generateReadScalar(fld, "Float", output)190 } else if scalar.Name == "ID" {191 generateReadScalar(fld, "String", output)192 } else if scalar.Name == "Boolean" {193 generateReadScalar(fld, "Boolean", output)194 } else if scalar.Name == "Date" {195 generateReadScalar(fld, "String", output)196 } else {197 panic("Unsupported scalar: " + scalar.Name)198 }199 } else if inner.GetKind() == "Object" || inner.GetKind() == "Union" || inner.GetKind() == "Interface" {200 output.WriteLine(scope + ".assertObject(\"" + fld.Alias + "\")")201 newScope(fld, output)202 if inner.GetKind() == "Object" {203 obj := inner.(il.Object)204 generateNormalizer(obj.SelectionSet, output)205 } else if inner.GetKind() == "Interface" {206 obj := inner.(il.Interface)207 generateNormalizer(obj.SelectionSet, output)208 } else {209 obj := inner.(il.Union)210 generateNormalizer(obj.SelectionSet, output)211 }212 output.ScopePop()213 } else if inner.GetKind() == "List" {214 output.WriteLine("if (" + scope + ".assertList(\"" + fld.Alias + "\")) {")215 output.IndentAdd()216 storeKey := storeKey(fld, output)217 newListScope(fld, output)218 nextLevel++219 generateListNormalizer(nextLevel, fld, inner.(il.List), output)220 listScope := "scope" + strconv.FormatInt(output.GetScope(), 10)221 output.WriteLine(scope + ".set(" + storeKey + ", " + listScope + ".completed())")222 output.ScopePop()223 output.IndentRemove()224 output.WriteLine("}")225 } else if inner.GetKind() == "Enum" {226 generateReadScalar(fld, "String", output)227 } else {228 panic("Unsupported type: " + inner.GetKind())229 }230 //231 } else {232 if fld.Type.GetKind() == "Scalar" {233 scalar := fld.Type.(il.Scalar)234 if scalar.Name == "String" {235 generateReadOptionalScalar(fld, "String", output)236 } else if scalar.Name == "Int" {237 generateReadOptionalScalar(fld, "Int", output)238 } else if scalar.Name == "Float" {239 generateReadOptionalScalar(fld, "Float", output)240 } else if scalar.Name == "ID" {241 generateReadOptionalScalar(fld, "String", output)242 } else if scalar.Name == "Boolean" {243 generateReadOptionalScalar(fld, "Boolean", output)244 } else if scalar.Name == "Date" {245 generateReadOptionalScalar(fld, "String", output)246 } else {247 panic("Unsupported scalar: " + scalar.Name)248 }249 } else if fld.Type.GetKind() == "Object" || fld.Type.GetKind() == "Union" || fld.Type.GetKind() == "Interface" {250 output.WriteLine("if (" + scope + ".hasKey(\"" + fld.Alias + "\")) {")251 output.IndentAdd()252 storeKey := storeKey(fld, output)253 newScope(fld, output)254 if fld.Type.GetKind() == "Object" {255 obj := fld.Type.(il.Object)256 generateNormalizer(obj.SelectionSet, output)257 } else if fld.Type.GetKind() == "Interface" {258 obj := fld.Type.(il.Interface)259 generateNormalizer(obj.SelectionSet, output)260 } else {261 obj := fld.Type.(il.Union)262 generateNormalizer(obj.SelectionSet, output)263 }264 output.IndentRemove()265 output.ScopePop()266 output.WriteLine("} else {")267 output.IndentAdd()268 output.WriteLine(scope + ".setNull(" + storeKey + ")")269 output.IndentRemove()270 output.WriteLine("}")271 } else if fld.Type.GetKind() == "List" {272 output.WriteLine("if (" + scope + ".hasKey(\"" + fld.Alias + "\")) {")273 output.IndentAdd()274 storeKey := storeKey(fld, output)275 newListScope(fld, output)276 nextLevel++277 generateListNormalizer(nextLevel, fld, fld.Type.(il.List), output)278 output.ScopePop()279 output.IndentRemove()280 output.WriteLine("} else {")281 output.IndentAdd()282 output.WriteLine(scope + ".setNull(" + storeKey + ")")283 output.IndentRemove()284 output.WriteLine("}")285 } else if fld.Type.GetKind() == "Enum" {286 generateReadOptionalScalar(fld, "String", output)287 } else {288 panic("Unsupported type: " + fld.Type.GetKind())289 }290 }291 }292 for _, inf := range set.InlineFragments {293 output.WriteLine("if (" + scope + ".isType(\"" + inf.TypeName + "\")) {")294 output.IndentAdd()295 generateNormalizer(inf.Selection, output)296 output.IndentRemove()297 output.WriteLine("}")298 }299 for _, fr := range set.Fragments {300 output.WriteLine("normalize" + fr.Name + "(scope" + strconv.FormatInt(output.GetScope(), 10) + ")")301 }302}303func inputValue(value il.Value) string {304 if value.GetKind() == "IntValue" {305 return "i(" + strconv.FormatInt(int64(value.(il.IntValue).Int), 10) + ")"306 } else if value.GetKind() == "StringValue" {307 return "s(\"" + value.(il.StringValue).String + "\")"308 } else if value.GetKind() == "VariableValue" {309 return "reference(\"" + value.(il.VariableValue).Name + "\")"310 }311 // TODO: Implement all types312 panic("Unexpected input type: " + value.GetKind())313}314func outputSelectors(set *il.SelectionSet, output *Output) {315 output.Append("obj(listOf(")316 output.IndentAdd()317 isFirst := true318 for _, s := range set.Fields {319 if isFirst {320 isFirst = false321 } else {322 output.Append(",")323 }324 if len(s.Arguments) > 0 {325 args := make([]string, 0)326 for _, a := range s.Arguments {327 args = append(args, "\""+a.Name+"\" to "+inputValue(a.Value)+"")328 }329 output.WriteLine("field(\"" + s.Name + "\",\"" + s.Alias + "\", mapOf(" + strings.Join(args, ", ") + "), ")330 } else {331 output.WriteLine("field(\"" + s.Name + "\",\"" + s.Alias + "\", ")332 }333 outputType(s.Type, output)334 output.Append(")")335 }336 for _, fr := range set.Fragments {337 if isFirst {338 isFirst = false339 } else {340 output.Append(",")341 }342 output.WriteLine("fragment(\"" + fr.TypeName + "\", " + fr.Name + "Selector)")343 }344 for _, fr := range set.InlineFragments {345 if isFirst {346 isFirst = false347 } else {348 output.Append(",")349 }350 output.WriteLine("inline(\"" + fr.TypeName + "\", ")351 outputSelectors(fr.Selection, output)352 output.Append(")")353 }354 output.IndentRemove()355 output.WriteLine("))")356}357func outputType(tp il.Type, output *Output) {358 if tp.GetKind() == "NotNull" {359 inner := tp.(il.NotNull)360 output.Append("notNull(")361 outputType(inner.Inner, output)362 output.Append(")")363 } else if tp.GetKind() == "Scalar" {364 scalar := tp.(il.Scalar)365 output.Append("scalar(\"" + scalar.Name + "\")")366 } else if tp.GetKind() == "Enum" {367 output.Append("scalar(\"String\")")368 } else if tp.GetKind() == "Object" || tp.GetKind() == "Union" || tp.GetKind() == "Interface" {369 var set *il.SelectionSet370 if tp.GetKind() == "Object" {371 set = tp.(il.Object).SelectionSet372 } else if tp.GetKind() == "Union" {373 set = tp.(il.Union).SelectionSet374 } else {375 set = tp.(il.Interface).SelectionSet376 }377 output.IndentAdd()378 outputSelectors(set, output)379 output.IndentRemove()380 } else if tp.GetKind() == "List" {381 output.Append("list(")382 outputType((tp.(il.List)).Inner, output)383 output.Append(")")384 } else {385 panic("Unsupported output type: " + tp.GetKind())386 }387}388func generateSelector(set *il.SelectionSet, output *Output) {389 output.IndentAdd()390 outputSelectors(set, output)391 output.IndentRemove()392}393func GenerateKotlin(model *il.Model) {394 output := NewOutput()395 output.WriteLine("package com.openland.soyuz.gen")396 output.WriteLine("")397 output.WriteLine("import com.openland.soyuz.store.RecordSet")398 output.WriteLine("import kotlinx.serialization.json.JsonObject")399 output.WriteLine("")400 //401 // Normalizers402 //403 for _, f := range model.Fragments {404 output.NextScope()405 output.WriteLine("fun normalize" + f.Name + "(scope" + strconv.FormatInt(output.GetScope(), 10) + ": Scope) {")406 output.IndentAdd()407 generateNormalizer(f.SelectionSet, output)408 output.IndentRemove()409 output.WriteLine("}")410 }411 for _, f := range model.Queries {412 output.NextScope()413 output.WriteLine("fun normalize" + f.Name + "(scope" + strconv.FormatInt(output.GetScope(), 10) + ": Scope) {")414 output.IndentAdd()415 generateNormalizer(f.SelectionSet, output)416 output.IndentRemove()417 output.WriteLine("}")418 }419 for _, f := range model.Mutations {420 output.NextScope()421 output.WriteLine("fun normalize" + f.Name + "(scope" + strconv.FormatInt(output.GetScope(), 10) + ": Scope) {")422 output.IndentAdd()423 generateNormalizer(f.SelectionSet, output)424 output.IndentRemove()425 output.WriteLine("}")426 }427 for _, f := range model.Subscriptions {428 output.NextScope()429 output.WriteLine("fun normalize" + f.Name + "(scope" + strconv.FormatInt(output.GetScope(), 10) + ": Scope) {")430 output.IndentAdd()431 generateNormalizer(f.SelectionSet, output)432 output.IndentRemove()433 output.WriteLine("}")434 }435 //436 // Selectors437 //438 for _, f := range model.Fragments {439 output.NextScope()440 output.WriteLine("val " + f.Name + "Selector = ")441 output.IndentAdd()442 generateSelector(f.SelectionSet, output)443 output.IndentRemove()444 output.WriteLine("")445 }446 for _, f := range model.Queries {447 output.NextScope()448 output.WriteLine("val " + f.Name + "Selector = ")449 output.IndentAdd()450 generateSelector(f.SelectionSet, output)451 output.IndentRemove()452 }453 //454 // Operations455 //456 output.WriteLine("")457 output.WriteLine("object Operations {")458 output.IndentAdd()459 for _, f := range model.Queries {460 output.WriteLine("val " + f.Name + " = object: OperationDefinition {")461 output.IndentAdd()462 output.WriteLine("override val name = \"" + f.Name + "\"")463 output.WriteLine("override val kind = OperationKind.QUERY")464 output.WriteLine("override val body = \"" + f.Body + "\"")465 output.WriteLine("override val selector = " + f.Name + "Selector")466 output.WriteLine("override fun normalizeResponse(response: JsonObject): RecordSet {")467 output.IndentAdd()468 output.WriteLine("val collection = NormalizedCollection()")469 output.WriteLine("normalize" + f.Name + "(Scope(\"ROOT_QUERY\", collection, response))")470 output.WriteLine("return collection.build()")471 output.IndentRemove()472 output.WriteLine("}")473 output.IndentRemove()474 output.WriteLine("}")475 }476 for _, f := range model.Mutations {477 output.WriteLine("val " + f.Name + " = object: OperationDefinition {")478 output.IndentAdd()479 output.WriteLine("override val name = \"" + f.Name + "\"")480 output.WriteLine("override val kind = OperationKind.MUTATION")481 output.WriteLine("override val body = \"" + f.Body + "\"")482 output.WriteLine("override val selector = null")483 output.WriteLine("override fun normalizeResponse(response: JsonObject): RecordSet {")484 output.IndentAdd()485 output.WriteLine("val collection = NormalizedCollection()")486 output.WriteLine("normalize" + f.Name + "(Scope(\"ROOT_QUERY\", collection, response))")487 output.WriteLine("return collection.build()")488 output.IndentRemove()489 output.WriteLine("}")490 output.IndentRemove()491 output.WriteLine("}")492 }493 for _, f := range model.Subscriptions {494 output.WriteLine("val " + f.Name + " = object: OperationDefinition {")495 output.IndentAdd()496 output.WriteLine("override val name = \"" + f.Name + "\"")497 output.WriteLine("override val kind = OperationKind.SUBSCRIPTION")498 output.WriteLine("override val body = \"" + f.Body + "\"")499 output.WriteLine("override val selector = null")500 output.WriteLine("override fun normalizeResponse(response: JsonObject): RecordSet {")501 output.IndentAdd()502 output.WriteLine("val collection = NormalizedCollection()")503 output.WriteLine("normalize" + f.Name + "(Scope(\"ROOT_QUERY\", collection, response))")504 output.WriteLine("return collection.build()")505 output.IndentRemove()506 output.WriteLine("}")507 output.IndentRemove()508 output.WriteLine("}")509 }510 output.IndentRemove()511 output.WriteLine("}")512 ioutil.WriteFile("/Users/steve/Develop/soyuz/src/commonMain/kotlin/com.openland.soyuz/gen/Generated.kt",513 []byte(output.String()), 0644)514}...

Full Screen

Full Screen

format_screen.go

Source:format_screen.go Github

copy

Full Screen

...3 "bytes"4 "github.com/projectdiscovery/nuclei/v2/pkg/types"5)6// formatScreen formats the output for showing on screen.7func (w *StandardWriter) formatScreen(output *ResultEvent) []byte {8 builder := &bytes.Buffer{}9 if !w.noMetadata {10 if !w.noTimestamp {11 builder.WriteRune('[')12 builder.WriteString(w.aurora.Cyan(output.Timestamp.Format("2006-01-02 15:04:05")).String())13 builder.WriteString("] ")14 }15 builder.WriteRune('[')16 builder.WriteString(w.aurora.BrightGreen(output.TemplateID).String())17 if output.MatcherName != "" {18 builder.WriteString(":")19 builder.WriteString(w.aurora.BrightGreen(output.MatcherName).Bold().String())20 } else if output.ExtractorName != "" {21 builder.WriteString(":")22 builder.WriteString(w.aurora.BrightGreen(output.ExtractorName).Bold().String())23 }24 if w.matcherStatus {25 builder.WriteString("] [")26 if !output.MatcherStatus {27 builder.WriteString(w.aurora.Red("failed").String())28 } else {29 builder.WriteString(w.aurora.Green("matched").String())30 }31 }32 builder.WriteString("] [")33 builder.WriteString(w.aurora.BrightBlue(output.Type).String())34 builder.WriteString("] ")35 builder.WriteString("[")36 builder.WriteString(w.severityColors(output.Info.SeverityHolder.Severity))37 builder.WriteString("] ")38 }39 if output.Matched != "" {40 builder.WriteString(output.Matched)41 } else {42 builder.WriteString(output.Host)43 }44 // If any extractors, write the results45 if len(output.ExtractedResults) > 0 {46 builder.WriteString(" [")47 for i, item := range output.ExtractedResults {48 builder.WriteString(w.aurora.BrightCyan(item).String())49 if i != len(output.ExtractedResults)-1 {50 builder.WriteRune(',')51 }52 }53 builder.WriteString("]")54 }55 // Write meta if any56 if len(output.Metadata) > 0 {57 builder.WriteString(" [")58 first := true59 for name, value := range output.Metadata {60 if !first {61 builder.WriteRune(',')62 }63 first = false64 builder.WriteString(w.aurora.BrightYellow(name).String())65 builder.WriteRune('=')66 builder.WriteString(w.aurora.BrightYellow(types.ToString(value)).String())67 }68 builder.WriteString("]")69 }70 return builder.Bytes()71}...

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2type output interface {3 Write([]byte) (int, error)4}5type file struct {6}7func (f *file) Write(data []byte) (int, error) {8 fmt.Printf("Write %s data '%s'9 return len(data), nil10}11func main() {12 var o output = &file{"file1"}13 o.Write([]byte("data1"))14}15import (16type output interface {17 Write([]byte) (int, error)18}19type file struct {20}21func (f *file) Write(data []byte) (int, error) {22 fmt.Printf("Write %s data '%s'23 return len(data), nil24}25func main() {26 writeData(&file{"file1"}, []byte("data1"))27}28func writeData(o output, data []byte) {29 fmt.Println("Write data")30 o.Write(data)31}

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 output, err = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)4 if err != nil {5 fmt.Println(err)6 }7 fmt.Fprintf(output, "This is a test8 output.Close()9}10import (11func main() {12 output, err = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)13 if err != nil {14 fmt.Println(err)15 }16 fmt.Fprintf(output, "This is a test17 output.Close()18}19import (20func main() {21 output, err = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)22 if err != nil {23 fmt.Println(err)24 }25 fmt.Fprintf(output, "This is a test26 output.Close()27}28import (29func main() {30 output, err = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)31 if err != nil {32 fmt.Println(err)33 }34 fmt.Fprintf(output, "This is a test35 output.Close()36}37import (38func main() {39 output, err = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE, 0666)40 if err != nil {41 fmt.Println(err)42 }43 fmt.Fprintf(output, "This is a test

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, world!")4 fmt.Println("Hello, world!")5 fmt.Println("Hello, world!")6 fmt.Println("Hello, world!")7 fmt.Println("Hello, world!")8}9import "fmt"10func main() {11 fmt.Println("Hello, world!")12 fmt.Println("Hello, world!")13 fmt.Println("Hello, world!")14 fmt.Println("Hello, world!")15 fmt.Println("Hello, world!")16}17import "fmt"18func main() {19 fmt.Println("Hello, world!")20 fmt.Println("Hello, world!")21 fmt.Println("Hello, world!")22 fmt.Println("Hello, world!")23 fmt.Println("Hello, world!")24}25import "fmt"26func main() {27 fmt.Println("Hello, world!")28 fmt.Println("Hello, world!")29 fmt.Println("Hello, world!")30 fmt.Println("Hello, world!")31 fmt.Println("Hello, world!")32}33import "fmt"34func main() {35 fmt.Println("Hello, world!")36 fmt.Println("Hello, world!")37 fmt.Println("Hello, world!")38 fmt.Println("Hello, world!")39 fmt.Println("Hello, world!")40}41import "fmt"42func main() {43 fmt.Println("Hello, world!")44 fmt.Println("Hello, world!")45 fmt.Println("Hello, world!")46 fmt.Println("Hello, world!")47 fmt.Println("Hello, world!")48}49import "fmt"50func main() {51 fmt.Println("Hello, world!")52 fmt.Println("Hello, world!")53 fmt.Println("Hello, world!")54 fmt.Println("Hello, world!")55 fmt.Println("Hello, world!")56}57import "fmt"58func main() {59 fmt.Println("

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Create("test.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer f.Close()8 l, err := f.WriteString("Hello world9 if err != nil {10 fmt.Println(err)11 f.Close()12 }13 fmt.Println(l, "bytes written successfully")14 l, err = f.WriteString("Writing to file in Go is easy15 if err != nil {16 fmt.Println(err)17 f.Close()18 }19 fmt.Println(l, "bytes written successfully")20}21The WriteString() method is a part of the Writer interface. The Writer interface is defined in the io package. The Writer interface has the following definition:22type Writer interface {23 Write(p []byte) (n int, err error)24}25The Write() method can be used to write a byte slice to a file. The following program demonstrates the use of the Write() method:26import (27func main() {28 f, err := os.Create("test.txt")29 if err != nil {30 fmt.Println(err)31 }32 defer f.Close()

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Print("Hello")4 fmt.Print("World")5}6import "fmt"7func main() {8 fmt.Println("Hello")9 fmt.Println("World")10}

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main(){3 fmt.Print("Enter your name: ")4 fmt.Scan(&name)5 fmt.Println("Hello", name)6}7func Scanln(a ...interface{}) (n int, err error)8import "fmt"9func main(){10 fmt.Print("Enter your name: ")11 fmt.Scanln(&name)12 fmt.Println("Hello", name)13}14func Scanf(format string, a ...interface{}) (n int, err error)15import "fmt"16func main(){17 fmt.Print("Enter your name: ")18 fmt.Scanf("%s", &name)19 fmt.Println("Hello", name)20}21func ReadString(delim byte) (line

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 w.Write([]byte("Hello, Writer5}6import (7func main() {8 fmt.Println("Hello, playground")9 fmt.Fprintf(w, "Hello, Writer10}11import (12func main() {13 fmt.Println("Hello, playground")14 fmt.Fprintln(w, "Hello, Writer")15}16import (17func main() {18 fmt.Println("Hello, playground")19 fmt.Fprint(w, "Hello, Writer20}21import (22func main() {23 fmt.Println("Hello, playground")24 fmt.Fprintln(w, "Hello, Writer")25}26import (27func main() {28 fmt.Println("Hello, playground")29 fmt.Fprint(w, "Hello, Writer30}31import (32func main() {33 fmt.Println("Hello, playground")34 fmt.Fprintln(w, "Hello, Writer")35}36import (37func main() {38 fmt.Println("Hello, playground")

Full Screen

Full Screen

Write

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello World!")4 fmt.Print("Hello World!")5 fmt.Printf("Hello World!")6}7import "fmt"8func main() {9 fmt.Println("Hello World!")10 fmt.Print("Hello World!")11 fmt.Printf("Hello World!")12}13The Println() method is used to print the string with a newline character at the end, Print() is used to print the string without a newline character at the end, and

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful