github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/internal/browser/stub.go (about)

     1  package browser
     2  
     3  type Stub struct {
     4  	urls []string
     5  }
     6  
     7  func (b *Stub) Browse(url string) error {
     8  	b.urls = append(b.urls, url)
     9  	return nil
    10  }
    11  
    12  func (b *Stub) BrowsedURL() string {
    13  	if len(b.urls) > 0 {
    14  		return b.urls[0]
    15  	}
    16  	return ""
    17  }
    18  
    19  type _testing interface {
    20  	Errorf(string, ...interface{})
    21  	Helper()
    22  }
    23  
    24  func (b *Stub) Verify(t _testing, url string) {
    25  	t.Helper()
    26  	if url != "" {
    27  		switch len(b.urls) {
    28  		case 0:
    29  			t.Errorf("expected browser to open URL %q, but it was never invoked", url)
    30  		case 1:
    31  			if url != b.urls[0] {
    32  				t.Errorf("expected browser to open URL %q, got %q", url, b.urls[0])
    33  			}
    34  		default:
    35  			t.Errorf("expected browser to open one URL, but was invoked %d times", len(b.urls))
    36  		}
    37  	} else if len(b.urls) > 0 {
    38  		t.Errorf("expected no browser to open, but was invoked %d times: %v", len(b.urls), b.urls)
    39  	}
    40  }