gitee.com/mysnapcore/mysnapd@v0.1.0/progress/progress_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package progress_test
    21  
    22  import (
    23  	"bytes"
    24  	"fmt"
    25  	"os"
    26  	"testing"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"gitee.com/mysnapcore/mysnapd/progress"
    31  	"gitee.com/mysnapcore/mysnapd/progress/progresstest"
    32  )
    33  
    34  // Hook up check.v1 into the "go test" runner
    35  func Test(t *testing.T) { TestingT(t) }
    36  
    37  type ProgressTestSuite struct {
    38  }
    39  
    40  var _ = Suite(&ProgressTestSuite{})
    41  
    42  func (ts *ProgressTestSuite) TestMakeProgressBar(c *C) {
    43  	defer progress.MockIsTerminal(false)()
    44  
    45  	m := progress.MakeProgressBar(nil)
    46  	c.Check(m, FitsTypeOf, progress.QuietMeter{})
    47  	m = progress.MakeProgressBar(os.Stdout)
    48  	c.Check(m, FitsTypeOf, progress.QuietMeter{})
    49  
    50  	progress.MockIsTerminal(true)
    51  	m = progress.MakeProgressBar(nil)
    52  	c.Check(m, FitsTypeOf, &progress.ANSIMeter{})
    53  	m = progress.MakeProgressBar(os.Stdout)
    54  	c.Check(m, FitsTypeOf, &progress.ANSIMeter{})
    55  	var buf bytes.Buffer
    56  	m = progress.MakeProgressBar(&buf)
    57  	c.Check(m, FitsTypeOf, progress.QuietMeter{})
    58  }
    59  
    60  func (ts *ProgressTestSuite) testNotify(c *C, t progress.Meter, desc, expected string) {
    61  	var buf bytes.Buffer
    62  	defer progress.MockStdout(&buf)()
    63  
    64  	comment := Commentf(desc)
    65  
    66  	t.Notify("blah blah")
    67  
    68  	c.Check(buf.String(), Equals, expected, comment)
    69  }
    70  
    71  func (ts *ProgressTestSuite) TestQuietNotify(c *C) {
    72  	ts.testNotify(c, &progress.QuietMeter{}, "quiet", "blah blah\n")
    73  }
    74  
    75  func (ts *ProgressTestSuite) TestOtherWriterQuietNotify(c *C) {
    76  	var buf bytes.Buffer
    77  
    78  	m := progress.MakeProgressBar(&buf)
    79  	ts.testNotify(c, m, "quiet", "")
    80  
    81  	c.Check(buf.String(), Equals, "blah blah\n")
    82  }
    83  
    84  func (ts *ProgressTestSuite) TestANSINotify(c *C) {
    85  	expected := fmt.Sprint("\r", progress.ExitAttributeMode, progress.ClrEOL, "blah blah\n")
    86  	ts.testNotify(c, &progress.ANSIMeter{}, "ansi", expected)
    87  }
    88  
    89  func (ts *ProgressTestSuite) TestTestingNotify(c *C) {
    90  	p := &progresstest.Meter{}
    91  	ts.testNotify(c, p, "test", "")
    92  	c.Check(p.Notices, DeepEquals, []string{"blah blah"})
    93  }