github.com/rrashidov/libpak@v0.0.0-20230911084305-75119185bb4d/bard/writer_test.go (about)

     1  /*
     2   * Copyright 2018-2020 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package bard_test
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/heroku/color"
    24  	. "github.com/onsi/gomega"
    25  	"github.com/sclevine/spec"
    26  
    27  	"github.com/paketo-buildpacks/libpak/bard"
    28  )
    29  
    30  func testWriter(t *testing.T, context spec.G, it spec.S) {
    31  	var (
    32  		Expect = NewWithT(t).Expect
    33  	)
    34  
    35  	context("Writer", func() {
    36  		var (
    37  			buffer *bytes.Buffer
    38  			writer *bard.Writer
    39  		)
    40  
    41  		it.Before(func() {
    42  			buffer = bytes.NewBuffer(nil)
    43  			writer = bard.NewWriter(buffer)
    44  		})
    45  
    46  		context("Write", func() {
    47  			it("prints to the writer", func() {
    48  				_, err := writer.Write([]byte("some-text"))
    49  				Expect(err).NotTo(HaveOccurred())
    50  				Expect(buffer.String()).To(Equal("some-text"))
    51  			})
    52  
    53  			context("when the writer has a color", func() {
    54  				it.Before(func() {
    55  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgBlue))
    56  				})
    57  
    58  				it("prints to the writer with the correct color codes", func() {
    59  					_, err := writer.Write([]byte("some-text"))
    60  					Expect(err).NotTo(HaveOccurred())
    61  					Expect(buffer.String()).To(Equal("\x1b[34msome-text\x1b[0m"))
    62  				})
    63  			})
    64  
    65  			context("when the writer has an indent", func() {
    66  				it.Before(func() {
    67  					writer = bard.NewWriter(buffer, bard.WithIndent(2))
    68  				})
    69  
    70  				it("prints to the writer with the correct indentation", func() {
    71  					_, err := writer.Write([]byte("some-text\nother-text"))
    72  					Expect(err).NotTo(HaveOccurred())
    73  					Expect(buffer.String()).To(Equal("    some-text\n    other-text"))
    74  				})
    75  			})
    76  
    77  			context("when the writer has a return prefix", func() {
    78  				it.Before(func() {
    79  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgRed), bard.WithIndent(2))
    80  				})
    81  
    82  				it("prints to the writer with the correct indentation", func() {
    83  					_, err := writer.Write([]byte("\rsome-text"))
    84  					Expect(err).NotTo(HaveOccurred())
    85  					Expect(buffer.String()).To(Equal("\r\x1b[31m    some-text\x1b[0m"))
    86  				})
    87  			})
    88  
    89  			context("when the writer has a newline suffix", func() {
    90  				it.Before(func() {
    91  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgRed), bard.WithIndent(2))
    92  				})
    93  
    94  				it("prints to the writer with the correct indentation", func() {
    95  					_, err := writer.Write([]byte("some-text\n"))
    96  					Expect(err).NotTo(HaveOccurred())
    97  					Expect(buffer.String()).To(Equal("\x1b[31m    some-text\x1b[0m\n"))
    98  				})
    99  			})
   100  
   101  			context("when there is multiple input", func() {
   102  				it.Before(func() {
   103  					writer = bard.NewWriter(buffer, bard.WithIndent(2))
   104  				})
   105  
   106  				it("skips indentation if there was not a line break", func() {
   107  					_, err := writer.Write([]byte("some-text"))
   108  					Expect(err).NotTo(HaveOccurred())
   109  
   110  					_, err = writer.Write([]byte("more-text"))
   111  					Expect(err).NotTo(HaveOccurred())
   112  
   113  					Expect(buffer.String()).To(Equal("    some-textmore-text"))
   114  				})
   115  
   116  				it("indents if there was a line break previously", func() {
   117  					_, err := writer.Write([]byte("some-text\na"))
   118  					Expect(err).NotTo(HaveOccurred())
   119  
   120  					_, err = writer.Write([]byte("more-text"))
   121  					Expect(err).NotTo(HaveOccurred())
   122  
   123  					Expect(buffer.String()).To(Equal("    some-text\n    amore-text"))
   124  				})
   125  			})
   126  
   127  			context("when the input has a percent symbol", func() {
   128  				it.Before(func() {
   129  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgMagenta))
   130  				})
   131  
   132  				it("prints to the writer with the correct indentation", func() {
   133  					_, err := writer.Write([]byte("some-%"))
   134  					Expect(err).NotTo(HaveOccurred())
   135  					Expect(buffer.String()).To(Equal("\x1b[35msome-%\x1b[0m"))
   136  				})
   137  			})
   138  		})
   139  	})
   140  }