github.com/paketoio/libpak@v1.3.1/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/paketoio/libpak/bard"
    26  	"github.com/sclevine/spec"
    27  )
    28  
    29  func testWriter(t *testing.T, context spec.G, it spec.S) {
    30  	var (
    31  		Expect = NewWithT(t).Expect
    32  	)
    33  
    34  	context("Writer", func() {
    35  		var (
    36  			buffer *bytes.Buffer
    37  			writer bard.Writer
    38  		)
    39  
    40  		it.Before(func() {
    41  			buffer = bytes.NewBuffer(nil)
    42  			writer = bard.NewWriter(buffer)
    43  		})
    44  
    45  		context("Write", func() {
    46  			it("prints to the writer", func() {
    47  				_, err := writer.Write([]byte("some-text"))
    48  				Expect(err).NotTo(HaveOccurred())
    49  				Expect(buffer.String()).To(Equal("some-text"))
    50  			})
    51  
    52  			context("when the writer has a color", func() {
    53  				it.Before(func() {
    54  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgBlue))
    55  				})
    56  
    57  				it("prints to the writer with the correct color codes", func() {
    58  					_, err := writer.Write([]byte("some-text"))
    59  					Expect(err).NotTo(HaveOccurred())
    60  					Expect(buffer.String()).To(Equal("\x1b[34msome-text\x1b[0m"))
    61  				})
    62  			})
    63  
    64  			context("when the writer has an indent", func() {
    65  				it.Before(func() {
    66  					writer = bard.NewWriter(buffer, bard.WithIndent(2))
    67  				})
    68  
    69  				it("prints to the writer with the correct indentation", func() {
    70  					_, err := writer.Write([]byte("some-text\nother-text"))
    71  					Expect(err).NotTo(HaveOccurred())
    72  					Expect(buffer.String()).To(Equal("    some-text\n    other-text"))
    73  				})
    74  			})
    75  
    76  			context("when the writer has a return prefix", func() {
    77  				it.Before(func() {
    78  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgRed), bard.WithIndent(2))
    79  				})
    80  
    81  				it("prints to the writer with the correct indentation", func() {
    82  					_, err := writer.Write([]byte("\rsome-text"))
    83  					Expect(err).NotTo(HaveOccurred())
    84  					Expect(buffer.String()).To(Equal("\r\x1b[31m    some-text\x1b[0m"))
    85  				})
    86  			})
    87  
    88  			context("when the writer has a newline suffix", func() {
    89  				it.Before(func() {
    90  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgRed), bard.WithIndent(2))
    91  				})
    92  
    93  				it("prints to the writer with the correct indentation", func() {
    94  					_, err := writer.Write([]byte("some-text\n"))
    95  					Expect(err).NotTo(HaveOccurred())
    96  					Expect(buffer.String()).To(Equal("\x1b[31m    some-text\x1b[0m\n"))
    97  				})
    98  			})
    99  
   100  			context("when the input has a percent symbol", func() {
   101  				it.Before(func() {
   102  					writer = bard.NewWriter(buffer, bard.WithAttributes(color.FgMagenta))
   103  				})
   104  
   105  				it("prints to the writer with the correct indentation", func() {
   106  					_, err := writer.Write([]byte("some-%"))
   107  					Expect(err).NotTo(HaveOccurred())
   108  					Expect(buffer.String()).To(Equal("\x1b[35msome-%\x1b[0m"))
   109  				})
   110  			})
   111  		})
   112  	})
   113  }