• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang difflib.Diff函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: _assert

func _assert(t *testing.T, cases Cases) {
	for _, c := range cases {
		if !reflect.DeepEqual(c.got, c.want) {
			if s1, ok := c.got.(string); ok {
				if s2, ok := c.want.(string); ok {
					diff := difflib.Diff(strings.Split(s1, "\n"), strings.Split(s2, "\n"))
					diffs := []string{}

					for l, d := range diff {
						switch d.Delta {
						case difflib.LeftOnly:
							diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
						case difflib.RightOnly:
							diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
						}
					}

					t.Errorf("Unexpected result:\n%s", strings.Join(diffs, "\n"))
					return
				}
			}

			t.Errorf("%q\n%q\n", c.got, c.want)
		}
	}
}
开发者ID:jbuck,项目名称:rack,代码行数:26,代码来源:main_test.go


示例2: Diff

func Diff(t *testing.T, name, s1, s2 string) {
	diff1 := strings.Split(strings.TrimSpace(s1), "\n")
	diff2 := strings.Split(strings.TrimSpace(s2), "\n")

	diff := difflib.Diff(diff1, diff2)
	diffs := []string{}

	// bigger than max
	prev := 1000000

	for l, d := range diff {
		switch d.Delta {
		case difflib.LeftOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
			prev = l
		case difflib.RightOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
			prev = l
		}
	}

	if len(diffs) > 0 {
		t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
	}
}
开发者ID:gmelika,项目名称:rack,代码行数:31,代码来源:release_test.go


示例3: assertFixture

func assertFixture(t *testing.T, name string) {
	orig := manifest.ManifestRandomPorts
	manifest.ManifestRandomPorts = false
	defer func() { manifest.ManifestRandomPorts = orig }()

	app := models.App{
		Name: "httpd",
		Tags: map[string]string{
			"Name":   "httpd",
			"Type":   "app",
			"System": "convox",
			"Rack":   "convox-test",
		},
	}

	data, err := ioutil.ReadFile(fmt.Sprintf("fixtures/%s.yml", name))
	require.Nil(t, err)

	manifest, err := manifest.Load(data)
	require.Nil(t, err)

	formation, err := app.Formation(*manifest)
	require.Nil(t, err)

	pretty, err := models.PrettyJSON(formation)
	require.Nil(t, err)

	data, err = ioutil.ReadFile(fmt.Sprintf("fixtures/%s.json", name))
	require.Nil(t, err)

	diff1 := strings.Split(strings.TrimSpace(string(data)), "\n")
	diff2 := strings.Split(strings.TrimSpace(pretty), "\n")

	diff := difflib.Diff(diff1, diff2)
	diffs := []string{}

	// bigger than max
	prev := 1000000

	for l, d := range diff {
		switch d.Delta {
		case difflib.LeftOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d - %s", l, d.Payload))
			prev = l
		case difflib.RightOnly:
			if (l - prev) > 1 {
				diffs = append(diffs, "")
			}
			diffs = append(diffs, fmt.Sprintf("%04d + %s", l, d.Payload))
			prev = l
		}
	}

	if len(diffs) > 0 {
		t.Errorf("Unexpected results for %s:\n%s", name, strings.Join(diffs, "\n"))
	}
}
开发者ID:gmelika,项目名称:rack,代码行数:60,代码来源:fixtures_test.go


示例4: Cmp

func Cmp(feed1, feed2 MDQ) {
	ents1, _ := feed1.getEntityList()
	ents2, _ := feed2.getEntityList()
	//    len1 := len(ents1)
	len2 := len(ents2)
	seen := 0
	for id, _ := range ents1 {
		x1, _ := feed1.MDQ(id)
		x2, _ := feed2.MDQ(id)
		str1 := x1.Pp()
		str2 := x2.Pp()
		hash1 := hex.EncodeToString(gosaml.Hash(crypto.SHA1, str1))
		hash2 := hex.EncodeToString(gosaml.Hash(crypto.SHA1, str2))
		if hash1 == hash2 {
			fmt.Println("OK ", id)
		} else {
			fmt.Println("NOT", id)
			diffs := difflib.Diff(strings.Split(str1, "\n"), strings.Split(str2, "\n"))
			for _, diff := range diffs {
				if diff.Delta != difflib.Common {
					fmt.Println(diff)
				}
			}
			fmt.Println()
		}
		seen++
	}
	fmt.Println("Number of entityies: ", seen, len2)
}
开发者ID:wayf-dk,项目名称:lMDQ,代码行数:29,代码来源:lMDQ.go


示例5: compare

func compare(oldVersion []string, newVersion []string) string {
	diff := difflib.Diff(oldVersion, newVersion)
	output := ""
	for _, v := range diff {
		if v.Delta == difflib.Common {
			continue
		}
		output += "\n" + v.String()
	}
	return output
}
开发者ID:herzog31,项目名称:page-diff,代码行数:11,代码来源:main.go


示例6: applyPatch

func applyPatch(diff patch.TextDiff, prev string) (string, error) {
	curr, err := diff.Apply([]byte(prev))
	if err != nil {
		return "", err
	}

	chunks := difflib.Diff(strings.Split(prev, "\n"), strings.Split(string(curr), "\n"))
	ret := ""

	for _, ch := range chunks {
		ret += ch.String() + "\n"
	}

	return ret, nil
}
开发者ID:filiptc,项目名称:navpatch,代码行数:15,代码来源:patch.go


示例7: renderDiff

func renderDiff(to io.Writer, a, b string) {
	diffs := difflib.Diff(strings.Split(a, "\n"), strings.Split(b, "\n"))

	for _, diff := range diffs {
		text := diff.Payload

		switch diff.Delta {
		case difflib.RightOnly:
			fmt.Fprintf(to, "%s\n", ansi.Color(text, "green"))
		case difflib.LeftOnly:
			fmt.Fprintf(to, "%s\n", ansi.Color(text, "red"))
		case difflib.Common:
			fmt.Fprintf(to, "%s\n", text)
		}
	}
}
开发者ID:aemengo,项目名称:fly,代码行数:16,代码来源:diff.go


示例8: diff

//diff returns a slice of added users and a slice of deleted users; this is lame in that if a user's name, location, or description changes, it doesn't currently identify it as "changed" but just as both added and deleted
func diff() ([]string, []string) {
	prev, _ := ioutil.ReadFile(PREV)
	current, _ := ioutil.ReadFile(CURRENT)

	rawdiff := difflib.Diff(strings.Split(string(current), "\n"), strings.Split(string(prev), "\n"))

	var added []string
	var deleted []string
	for _, record := range rawdiff {
		if record.Delta == difflib.LeftOnly {
			added = append(added, record.Payload)
		}
		if record.Delta == difflib.RightOnly {
			deleted = append(deleted, record.Payload)
		}
	}
	return added, deleted
}
开发者ID:marcesher,项目名称:twitter_friend_changes,代码行数:19,代码来源:main.go


示例9: statsFromDiff

func statsFromDiff(diff patch.TextDiff) *DiffStats {
	stats := DiffStats{Chunks: diff}

	for _, chunk := range diff {
		atoms := difflib.Diff(
			strings.Split(string(chunk.Old), "\n"),
			strings.Split(string(chunk.New), "\n"),
		)
		for _, a := range atoms {
			if a.Delta == difflib.LeftOnly {
				stats.Deletions++
			} else if a.Delta == difflib.RightOnly {
				stats.Additions++
			}
		}
	}

	return &stats
}
开发者ID:filiptc,项目名称:navpatch,代码行数:19,代码来源:patch.go


示例10: TestCimDeclaration

func TestCimDeclaration(t *testing.T) {
	bs, e := xml.MarshalIndent(declaration, "", "  ")
	if nil != e {
		t.Error(e)
		return
	} else {
		t.Log(string(bs))
	}

	var declaration2 CimDeclaration
	if e := xml.Unmarshal(bs, &declaration2); nil != e {
		t.Error(e)
		return
	}

	if !reflect.DeepEqual(declaration, declaration2) {

		// t.Errorf("excepted is %#v", declaration)
		// t.Errorf("actual is %#v", declaration2)

		bs2, e := xml.MarshalIndent(declaration2, "", "  ")
		if nil != e {

			t.Errorf("excepted is %#v", declaration)
			t.Errorf("actual is %#v", declaration2)
			t.Error(e)
			return
		} else {

			if string(bs) != string(bs2) {

				//t.Log(string(bs))

				results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
				if 0 != len(results) {
					for _, rec := range results {
						t.Error(rec.String())
					}
				}
			}
		}
	}
}
开发者ID:tasleson,项目名称:gowbem,代码行数:43,代码来源:types_test.go


示例11: TestCimClass

func TestCimClass(t *testing.T) {
	bs, e := xml.MarshalIndent(class, "", "  ")
	if nil != e {
		t.Error(e)
		return
	} else {
		t.Log(string(bs))
	}

	var cls2 CimClass
	if e := xml.Unmarshal(bs, &cls2); nil != e {
		t.Error(e)
		return
	}

	if !reflect.DeepEqual(class, cls2) {

		bs2, e := xml.MarshalIndent(cls2, "", "  ")
		if nil != e {

			t.Errorf("excepted is %#v", class)
			t.Errorf("actual is %#v", cls2)
			t.Error(e)
			return
		} else {

			if string(bs) != string(bs2) {

				t.Errorf("excepted is %#v", class)
				t.Errorf("actual is %#v", cls2)
				//t.Log(string(bs))

				results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
				if 0 != len(results) {
					for _, rec := range results {
						t.Error(rec.String())
					}
				}
			}
		}
	}
}
开发者ID:tasleson,项目名称:gowbem,代码行数:42,代码来源:types_test.go


示例12: TestMain

func TestMain(t *testing.T) {
	build, _ := filepath.Abs("build")
	filepath.Walk("test", func(path string, info os.FileInfo, err error) error {
		if strings.HasSuffix(path, ".sh") {
			cmd := exec.Command("bash", filepath.Base(path))
			cmd.Dir = filepath.Dir(path)
			cmd.Env = []string{"PATH=" + build}
			stderr := new(bytes.Buffer)
			cmd.Stderr = stderr
			output, err := cmd.Output()
			if err != nil {
				t.Errorf("FAIL: execution failed: " + path + ": " + err.Error() + " " + stderr.String())
			} else {
				outfile := strings.TrimSuffix(path, filepath.Ext(path)) + ".txt"
				expected, err := ioutil.ReadFile(outfile)
				if err != nil {
					t.Errorf("FAIL: error on reading output file: " + outfile)
				} else {
					diffs := difflib.Diff(strings.Split(stderr.String()+string(output), "\n"), strings.Split(string(expected), "\n"))
					help := strings.Contains(string(output), "NAME:")
					differs := false
					for _, diff := range diffs {
						differs = differs || (help && diff.Delta == difflib.RightOnly || !help && diff.Delta != difflib.Common)
					}
					if differs {
						buf := bytes.NewBufferString("")
						for _, diff := range diffs {
							if diff.Delta != difflib.Common {
								buf.WriteString(diff.String() + "\n")
							}
						}
						t.Errorf("FAIL: output differs: " + path + "\n" + buf.String())
					} else {
						t.Logf("PASS: " + path + "\n")
					}
				}
			}
		}
		return nil
	})
}
开发者ID:noscripter,项目名称:rexdep,代码行数:41,代码来源:main_test.go


示例13: assertGoCode

func assertGoCode(t *testing.T, s1, s2, msg string, com ...interface{}) {
	s1 = fmt.Sprintf("%s", s1)
	s2 = fmt.Sprintf("%s", s2)
	tmp1 := strings.Split(s1, "\n")
	tmp2 := strings.Split(s2, "\n")
	if s1 != s2 {
		b := bytes.NewBuffer(nil)
		for _, diff := range difflib.Diff(tmp1, tmp2) {
			p := strings.Replace(diff.Payload, "\t", "\\t", -1)
			switch diff.Delta {
			case difflib.Common:
				fmt.Fprintf(b, "%s %s\n", diff.Delta.String(), p)
			case difflib.LeftOnly:
				fmt.Fprintf(b, "\033[31m%s %s\033[39m\n", diff.Delta.String(), p)
			case difflib.RightOnly:
				fmt.Fprintf(b, "\033[32m%s %s\033[39m\n", diff.Delta.String(), p)
			}
		}
		t.Errorf(b.String())
	}
}
开发者ID:piotrkowalczuk,项目名称:pqt,代码行数:21,代码来源:generator_test.go


示例14: TestSimpleRsp

func TestSimpleRsp(t *testing.T) {
	for _, rsp := range []CimSimpleRsp{simple_rsp1, simple_error_rsp} {
		bs, e := xml.MarshalIndent(rsp, "", "  ")
		if nil != e {
			t.Error(e)
			return
		} else {
			t.Log(string(bs))
		}

		var unmarshal_rsp CimSimpleRsp
		if e := xml.Unmarshal(bs, &unmarshal_rsp); nil != e {
			t.Error(e)
			return
		}

		if !reflect.DeepEqual(rsp, unmarshal_rsp) {

			bs2, e := xml.MarshalIndent(unmarshal_rsp, "", "  ")
			if nil != e {
				t.Error(e)
				return
			} else {

				if string(bs) != string(bs2) {
					t.Errorf("excepted is %#v", rsp)
					t.Errorf("actual is %#v", unmarshal_rsp)
					//t.Log(string(bs))

					results := difflib.Diff(strings.Split(string(bs), "\n"), strings.Split(string(bs2), "\n"))
					if 0 != len(results) {
						for _, rec := range results {
							t.Error(rec.String())
						}
					}
				}
			}
		}
	}
}
开发者ID:tasleson,项目名称:gowbem,代码行数:40,代码来源:types_test.go


示例15: diff

func diff(a, b interface{}) []difflib.DiffRecord {
	return difflib.Diff(
		strings.SplitAfter(spew.Sdump(a), "\n"),
		strings.SplitAfter(spew.Sdump(b), "\n"),
	)
}
开发者ID:rafaeljusto,项目名称:gddoexp,代码行数:6,代码来源:gddoexp_test.go


示例16: testFmt

// Make sure that generated code isn't altered by running gofmt.
func testFmt(t *testing.T, srcname string, src io.Reader) {
	var err error

	var fmtin io.WriteCloser
	var fmtout io.ReadCloser
	var validbuf *bytes.Buffer
	var fmtbuf *bytes.Buffer

	var validbytes []byte

	// process (i.e., "govalid" output) will be buffered here.
	validbuf = new(bytes.Buffer)
	// gofmt output will be buffered here.
	fmtbuf = new(bytes.Buffer)

	testProcess(t, "-", validbuf, srcname, src)
	validbytes = validbuf.Bytes()

	// XXX Isn't there a way to gofmt without having to launch a
	// subprocess?
	cmd := exec.Command("gofmt")

	fmtin, err = cmd.StdinPipe()
	if err != nil {
		t.Error(err)
	}
	fmtout, err = cmd.StdoutPipe()
	if err != nil {
		t.Error(err)
	}

	if err := cmd.Start(); err != nil {
		t.Error(err)
	}

	sync := make(chan struct{})
	go func() {
		io.Copy(fmtin, bytes.NewBuffer(validbytes))
		fmtin.Close()
		sync <- struct{}{}
	}()
	go func() {
		io.Copy(fmtbuf, fmtout)
		sync <- struct{}{}
	}()
	<-sync
	<-sync

	if err := cmd.Wait(); err != nil {
		t.Error(err)
	}

	if !bytes.Equal(validbytes, fmtbuf.Bytes()) {
		t.Log("gofmt output differs")
		validlines := strings.Split(string(validbytes), "\n")
		fmtlines := strings.Split(string(fmtbuf.Bytes()), "\n")
		diffrecords := difflib.Diff(validlines, fmtlines)
		t.Log("--- valid output")
		t.Log("+++ gofmt output")
		for _, dr := range diffrecords {
			if dr.Delta == difflib.Common {
				continue
			}
			t.Log(dr)
		}
		t.Fail()
	}
}
开发者ID:crunchyroll,项目名称:govalid,代码行数:69,代码来源:fmt_test.go


示例17: main

func main() {
	domain := flag.String("domain", "", "mailgun domain")
	privateKey := flag.String("private-key", "", "secret mailgun api key")
	publicKey := flag.String("public-key", "", "mailgun public api key")
	dry := flag.Bool("dry", false, "do not update cache & only print to stdout (no e-mail)")
	verbose := flag.Bool("verbose", false, "Print detailed output per monitored url.")
	flag.Parse()

	if *domain == "" || *privateKey == "" || *publicKey == "" {
		log.Fatalln("domain, private-key and public-key flags are required")
	}

	gun := m.NewMailgun(*domain, *privateKey, *publicKey)

	urls := loadUrls()
	for _, url := range urls {
		body, err := requestURL(url)
		if err != nil {
			log.Println("Skipped URL because of error requesting it:", url)
			continue
		}
		filename := getExecFolder() + "/cache/" + getMD5Hash(url) + ".html"

		cached, err := ioutil.ReadFile(filename)
		if err != nil {
			if *dry == false {
				updateCache(filename, body)
			}

			switch err := err.(type) {
			case *os.PathError:
				fmt.Printf("This URL will now be monitored: %s\n\n", url)
			default:
				log.Fatalf("Fatal errors type %T\n", err)
			}
		} else {
			if reflect.DeepEqual(cached, body) {
				if *verbose {
					fmt.Printf("This URL didn't change: %s\n\n", url)
				}
			} else {
				cachedDoc := getGoqueryDoc(cached)
				currentDoc := getGoqueryDoc(body)
				cachedContent, _ := cachedDoc.Find("#content").Html()
				currentContent, _ := currentDoc.Find("#content").Html()

				if cachedContent == currentContent {
					if *verbose {
						fmt.Println("The website changed, but the content stayed the same.")
					}
				} else {
					cachedText, _ := html2text.FromString(cachedContent)
					currentText, _ := html2text.FromString(currentContent)
					diff := difflib.Diff(strings.Split(cachedText, "\n"), strings.Split(currentText, "\n"))

					msg := createMessage(diff, url)
					htmlMsg := createHTMLMessage(diff, url)
					if *dry {
						fmt.Println(msg)
					} else {
						sendEmails(gun, msg, htmlMsg)
					}
				}
				if *dry == false {
					updateCache(filename, body)
				}
			}
		}
	}
}
开发者ID:davidpfahler,项目名称:unicrawl,代码行数:70,代码来源:crawl.go


示例18: getDifferentFiles

func getDifferentFiles(listA, listB []string) (diff []difflib.DiffRecord) {
	return difflib.Diff(listA, listB)
}
开发者ID:irstacks,项目名称:self-portrait,代码行数:3,代码来源:detect.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang gosh.NewShell函数代码示例发布时间:2022-05-24
下一篇:
Golang aws.MakeParams函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap