当前位置: 首页>>代码示例>>Golang>>正文


Golang iotest.TruncateWriter函数代码示例

本文整理汇总了Golang中testing/iotest.TruncateWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang TruncateWriter函数的具体用法?Golang TruncateWriter怎么用?Golang TruncateWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了TruncateWriter函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestWriter

func TestWriter(t *testing.T) {
testLoop:
	for i, test := range writerTests {
		expected, err := io.ReadFile(test.file)
		if err != nil {
			t.Errorf("test %d: Unexpected error: %v", i, err)
			continue
		}

		buf := new(bytes.Buffer)
		tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
		for j, entry := range test.entries {
			if err := tw.WriteHeader(entry.header); err != nil {
				t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
				continue testLoop
			}
			if _, err := io.WriteString(tw, entry.contents); err != nil {
				t.Errorf("test %d, entry %d: Failed writing contents: %v", i, j, err)
				continue testLoop
			}
		}
		if err := tw.Close(); err != nil {
			t.Errorf("test %d: Failed closing archive: %v", err)
			continue testLoop
		}

		actual := buf.Bytes()
		if !bytes.Equal(expected, actual) {
			t.Errorf("test %d: Incorrect result: (-=expected, +=actual)\n%v",
				i, bytediff(expected, actual))
		}
	}
}
开发者ID:edisonwsk,项目名称:golang-on-cygwin,代码行数:33,代码来源:writer_test.go

示例2: TestStdCopyHeaderOnly

func TestStdCopyHeaderOnly(t *testing.T) {
	var input, stdout, stderr bytes.Buffer
	input.Write([]byte{2, 0, 0, 0, 0, 0, 0, 19})
	n, err := stdCopy(&stdout, iotest.TruncateWriter(&stderr, 7), &input)
	if err != io.ErrShortWrite {
		t.Errorf("stdCopy: wrong error. Want ShortWrite. Got %#v", err)
	}
	if n != 0 {
		t.Errorf("Wrong number of bytes. Want 0. Got %d.", n)
	}
	if got := stderr.String(); got != "" {
		t.Errorf("stdCopy: wrong stderr. Want %q. Got %q.", "", got)
	}
	if got := stdout.String(); got != "" {
		t.Errorf("stdCopy: wrong stdout. Want %q. Got %q.", "", got)
	}
}
开发者ID:pombredanne,项目名称:rudder,代码行数:17,代码来源:stdcopy_test.go

示例3: TestStdCopyTruncateWriter

func TestStdCopyTruncateWriter(t *testing.T) {
	var input, stdout, stderr bytes.Buffer
	input.Write([]byte{2, 0, 0, 0, 0, 0, 0, 19})
	input.Write([]byte("something happened!"))
	n, err := stdCopy(&stdout, iotest.TruncateWriter(&stderr, 7), &input)
	if err != nil {
		t.Fatal(err)
	}
	if expected := int64(19); n != expected {
		t.Errorf("Wrong number of bytes. Want %d. Got %d.", expected, n)
	}
	if got := stderr.String(); got != "somethi" {
		t.Errorf("stdCopy: wrong stderr. Want %q. Got %q.", "somethi", got)
	}
	if got := stdout.String(); got != "" {
		t.Errorf("stdCopy: wrong stdout. Want %q. Got %q.", "", got)
	}
}
开发者ID:pombredanne,项目名称:rudder,代码行数:18,代码来源:stdcopy_test.go

示例4: TestWriter

func TestWriter(t *testing.T) {
testLoop:
	for i, test := range writerTests {
		expected, err := ioutil.ReadFile(test.file)
		if err != nil {
			t.Errorf("test %d: Unexpected error: %v", i, err)
			continue
		}

		buf := new(bytes.Buffer)
		tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
		big := false
		for j, entry := range test.entries {
			big = big || entry.header.Size > 1<<10
			if err := tw.WriteHeader(entry.header); err != nil {
				t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
				continue testLoop
			}
			if _, err := io.WriteString(tw, entry.contents); err != nil {
				t.Errorf("test %d, entry %d: Failed writing contents: %v", i, j, err)
				continue testLoop
			}
		}
		// Only interested in Close failures for the small tests.
		if err := tw.Close(); err != nil && !big {
			t.Errorf("test %d: Failed closing archive: %v", i, err)
			continue testLoop
		}

		actual := buf.Bytes()
		if !bytes.Equal(expected, actual) {
			t.Errorf("test %d: Incorrect result: (-=expected, +=actual)\n%v",
				i, bytediff(expected, actual))
		}
		if testing.Short() { // The second test is expensive.
			break
		}
	}
}
开发者ID:varialus,项目名称:godfly,代码行数:39,代码来源:writer_test.go

示例5: TestWriter


//.........这里部分代码省略.........
			contents: strings.Repeat("\x00", 4<<10),
		}},
	}, {
		// TODO(dsnet): The Writer output should match the following file.
		// To fix an issue (see https://golang.org/issue/12594), we disabled
		// prefix support, which alters the generated output.
		/*
			// This file was produced using gnu tar 1.17
			// gnutar  -b 4 --format=ustar (longname/)*15 + file.txt
			file: "testdata/ustar.tar"
		*/
		file: "testdata/ustar.issue12594.tar", // This is a valid tar file, but not expected
		entries: []*entry{{
			header: &Header{
				Name:     strings.Repeat("longname/", 15) + "file.txt",
				Mode:     0644,
				Uid:      0765,
				Gid:      024,
				Size:     06,
				ModTime:  time.Unix(1360135598, 0),
				Typeflag: '0',
				Uname:    "shane",
				Gname:    "staff",
			},
			contents: "hello\n",
		}},
	}, {
		// This file was produced using gnu tar 1.26
		// echo "Slartibartfast" > file.txt
		// ln file.txt hard.txt
		// tar -b 1 --format=ustar -c -f hardlink.tar file.txt hard.txt
		file: "testdata/hardlink.tar",
		entries: []*entry{{
			header: &Header{
				Name:     "file.txt",
				Mode:     0644,
				Uid:      1000,
				Gid:      100,
				Size:     15,
				ModTime:  time.Unix(1425484303, 0),
				Typeflag: '0',
				Uname:    "vbatts",
				Gname:    "users",
			},
			contents: "Slartibartfast\n",
		}, {
			header: &Header{
				Name:     "hard.txt",
				Mode:     0644,
				Uid:      1000,
				Gid:      100,
				Size:     0,
				ModTime:  time.Unix(1425484303, 0),
				Typeflag: '1',
				Linkname: "file.txt",
				Uname:    "vbatts",
				Gname:    "users",
			},
			// no contents
		}},
	}}

testLoop:
	for i, v := range vectors {
		expected, err := ioutil.ReadFile(v.file)
		if err != nil {
			t.Errorf("test %d: Unexpected error: %v", i, err)
			continue
		}

		buf := new(bytes.Buffer)
		tw := NewWriter(iotest.TruncateWriter(buf, 4<<10)) // only catch the first 4 KB
		big := false
		for j, entry := range v.entries {
			big = big || entry.header.Size > 1<<10
			if err := tw.WriteHeader(entry.header); err != nil {
				t.Errorf("test %d, entry %d: Failed writing header: %v", i, j, err)
				continue testLoop
			}
			if _, err := io.WriteString(tw, entry.contents); err != nil {
				t.Errorf("test %d, entry %d: Failed writing contents: %v", i, j, err)
				continue testLoop
			}
		}
		// Only interested in Close failures for the small tests.
		if err := tw.Close(); err != nil && !big {
			t.Errorf("test %d: Failed closing archive: %v", i, err)
			continue testLoop
		}

		actual := buf.Bytes()
		if !bytes.Equal(expected, actual) {
			t.Errorf("test %d: Incorrect result: (-=expected, +=actual)\n%v",
				i, bytediff(expected, actual))
		}
		if testing.Short() { // The second test is expensive.
			break
		}
	}
}
开发者ID:Harvey-OS,项目名称:go,代码行数:101,代码来源:writer_test.go


注:本文中的testing/iotest.TruncateWriter函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。