github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/parse_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"bufio"
     9  	"os"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func TestReadLine(t *testing.T) {
    15  	// /etc/services file does not exist on android, plan9, windows.
    16  	switch runtime.GOOS {
    17  	case "android", "plan9", "windows":
    18  		t.Skipf("skipping test on %q", runtime.GOOS)
    19  	}
    20  	filename := "/etc/services" // a nice big file
    21  
    22  	fd, err := os.Open(filename)
    23  	if err != nil {
    24  		t.Fatalf("open %s: %v", filename, err)
    25  	}
    26  	defer fd.Close()
    27  	br := bufio.NewReader(fd)
    28  
    29  	file, err := open(filename)
    30  	if file == nil {
    31  		t.Fatalf("net.open(%s) = nil", filename)
    32  	}
    33  	defer file.close()
    34  
    35  	lineno := 1
    36  	byteno := 0
    37  	for {
    38  		bline, berr := br.ReadString('\n')
    39  		if n := len(bline); n > 0 {
    40  			bline = bline[0 : n-1]
    41  		}
    42  		line, ok := file.readLine()
    43  		if (berr != nil) != !ok || bline != line {
    44  			t.Fatalf("%s:%d (#%d)\nbufio => %q, %v\nnet => %q, %v",
    45  				filename, lineno, byteno, bline, berr, line, ok)
    46  		}
    47  		if !ok {
    48  			break
    49  		}
    50  		lineno++
    51  		byteno += len(line) + 1
    52  	}
    53  }