func TestShortRead(t *testing.T) { var result []byte var buf [2]byte for _, test := range readerTests { result = result[:0] var ( r = NewReader(Rope{test.r}) n int err error ) for err == nil { n, err = r.Read(buf[:]) if err == nil { assert.NotEqual(t, n, 0, "Zero-length Read()") } result = append(result, buf[:n]...) } assert.Equal(t, test.want, string(result)) assert.Equal(t, err, io.EOF, "Non-EOF error: "+err.Error()) } }
func TestCrewRenderPlain(t *testing.T) { c := NewCrew("test") // Set some variables c.Vars = map[string]interface{}{ "path": "/usr/local/bin", "n": 4, "x": 3.45, } // No placeholders s, _ := c.Render("python test.py") assert.Equal(t, s, "python test.py") // Missing key s, err := c.Render("{{bindir}}/python test.py") assert.NotEqual(t, err, nil) // Typical s, err = c.Render("{{path}}/python -n {{n}} -x {{x}} test.py") assert.Equal(t, s, "/usr/local/bin/python -n 4 -x 3.45 test.py") // Single braces aren't affected s, err = c.Render("{path}") assert.Equal(t, s, "{path}") // Escaped braces aren't affected, but replaced s, err = c.Render("{\\{path\\}}") assert.Equal(t, s, "{{path}}") // Escaped braces, round 2 s, err = c.Render("\\{\\{path\\}\\}") assert.Equal(t, s, "{{path}}") }
func TestFullRead(t *testing.T) { var buf [9]byte // Big enough for all the test strings. Exactly right size for one of them. for _, test := range readerTests { r := NewReader(Rope{test.r}) n, err := r.Read(buf[:]) assert.Equal(t, n, len(test.want), "partial read") assert.Equal(t, err, io.EOF, "no immediate EOF") assert.Equal(t, test.want, string(buf[:n]), "wrong data") } }
func TestFailedRoutineRun(t *testing.T) { r := NewRoutine("git", "flub") c := NewCrew("c1") rr, err := r.Run(c) assert.NotEqual(t, err, nil) assert.Equal(t, rr.Done(), true) assert.Equal(t, rr.Failed, true) assert.Equal(t, rr.Stdin, "") assert.Equal(t, rr.Stdout, "") assert.Equal(t, rr.Stderr, "git: 'flub' is not a git command. See 'git --help'.\n\nDid you mean this?\n\tpull\n") }
func TestPlayMoveScene(t *testing.T) { p := NewPlay("P0", "") s0 := NewScene("S0", "") s1 := NewScene("S1", "") p.AddScene(s0) p.AddScene(s1) p.MoveScene(0, s1) assert.Equal(t, p.Scenes[0], s1) assert.Equal(t, p.Scenes[1], s0) }
func TestSuccessfulSceneRun(t *testing.T) { s := NewScene("S0", "") s.NewRoutine("echo", "Hello") s.NewRoutine("echo", "Gopher") s.NewRoutine("echo", "World") c := NewCrew("c1") sr, err := s.Run(c) assert.Equal(t, err, nil) assert.Equal(t, len(sr.Routines), 3) assert.Equal(t, sr.Failed, false) assert.Equal(t, sr.Done(), true) }
func TestFailedSceneRun(t *testing.T) { s := NewScene("S0", "") s.NewRoutine("git", "flub") s.NewRoutine("echo", "Gopher") s.NewRoutine("echo", "World") c := NewCrew("c1") sr, err := s.Run(c) assert.Equal(t, err, nil) assert.Equal(t, len(sr.Routines), 3) assert.Equal(t, sr.Failed, true) assert.Equal(t, sr.Done(), true) }
func TestAt(t *testing.T) { str := treeR.String() length := treeR.Len() for i := int64(0); i < length; i++ { assert.Equal(t, str[i], treeR.At(i)) } }
func TestPlayRun(t *testing.T) { p := NewPlay("P0", "") c := &Crew{} // Nothing to do.. pr, err := p.Run(c) assert.Equal(t, err, nil) assert.False(t, pr.Failed) // Scene and routine s0, _ := p.NewScene("S0", "") s0.NewRoutine("echo", "foo") pr, err = p.Run(c) assert.Equal(t, err, nil) assert.False(t, pr.Failed) }
func TestTrigger(t *testing.T) { // Null case c, _ := Trigger("testhook", nil) assert.Equal(t, c, 0) // Local file NewLocalHooks() }
func TestReader(t *testing.T) { buf := bytes.NewBuffer(nil) for _, test := range readerTests { buf.Reset() io.Copy(buf, NewReader(Rope{test.r})) assert.Equal(t, test.want, buf.String()) } }
func TestPlayAddScene(t *testing.T) { p := NewPlay("P0", "") s0 := NewScene("S0", "") s1 := NewScene("S1", "") // Yep.. p.AddScene(s0) assert.Equal(t, p.Scenes[0], s0) // Yep.. p.AddScene(s1) assert.Equal(t, p.Scenes[1], s1) // Nope.. err := p.AddScene(s1) assert.NotEqual(t, err, nil) }
func TestPlayInsertScene(t *testing.T) { p := NewPlay("P0", "") s0 := NewScene("S0", "") // Cannot insert at a position that does not exist err := p.InsertScene(0, s0) assert.NotEqual(t, err, nil) p.AddScene(s0) // Insert new scene at position 0 ns0 := NewScene("New S0", "") err = p.InsertScene(0, ns0) assert.Equal(t, err, nil) assert.Equal(t, p.Scenes[0], ns0) assert.Equal(t, p.Scenes[1], s0) }
func TestConcatSubstr(t *testing.T) { defer disableCoalesce()() for _, ss := range substrings { got := ss.orig.slice(ss.start, ss.end) msg := fmt.Sprintf("%q[%v:%v] != %q", Rope{ss.orig}, ss.start, ss.end, Rope{got}) assert.Equal(t, ss.want, got, msg) } }
func TestSlice(t *testing.T) { defer disableCoalesce()() // See concat_test.go for the table used. for _, ss := range substrings { orig := Rope{ss.orig} got := orig.Slice(ss.start, ss.end) msg := fmt.Sprintf("%q[%v:%v] != %q", orig, ss.start, ss.end, got) assert.Equal(t, ss.want, got.node, msg) } }
func TestEmptyRope(t *testing.T) { for _, r := range []Rope{Rope{}, New("")} { assert.Equal(t, int64(0), r.Len()) assert.Equal(t, nil, r.Bytes()) assert.Equal(t, "", r.String()) assert.Equal(t, "", r.DropPrefix(3).String()) assert.Equal(t, "", r.DropPrefix(-1).String()) assert.Equal(t, "", r.DropPostfix(3).String()) assert.Equal(t, "", r.DropPostfix(-1).String()) assert.Equal(t, "", r.Slice(-1, 200).String()) assert.Equal(t, "", r.Slice(0, 1).String()) buf := bytes.NewBuffer(nil) r.WriteTo(buf) assert.Equal(t, 0, buf.Len()) } }
func TestRepeat(t *testing.T) { r := New("a") assert.Equal(t, "", r.Repeat(0).String()) assert.Equal(t, "a", r.Repeat(1).String()) assert.Equal(t, "aa", r.Repeat(2).String()) assert.Equal(t, "aaa", r.Repeat(3).String()) assert.Equal(t, "aaaa", r.Repeat(4).String()) assert.Equal(t, "aaaaa", r.Repeat(5).String()) assert.Equal(t, "aaaaaa", r.Repeat(6).String()) }
func TestGoString(t *testing.T) { for i, format := range []string{"%v", "%#v"} { for _, str := range []string{"abc", "\""} { want := fmt.Sprintf(format, str) if MarkGoStringedRope && i == 1 { // GoStringer want = "/*Rope*/ " + want } assert.Equal(t, want, fmt.Sprintf(format, New(str))) } } }
func TestWalk(t *testing.T) { defer disableCoalesce()() for _, r := range []Rope{Rope{}, emptyRope} { r.Walk(func(_ string) error { t.Error("call to empty Rope's Walk parameter") return nil }) } for _, r := range []Rope{ New("abc").AppendString("def").AppendString("ghi"), } { str := r.String() err := r.Walk(func(part string) error { assert.Equal(t, str[:len(part)], part) str = str[len(part):] return nil }) assert.Nil(t, err) assert.Equal(t, "", str) } for _, r := range []Rope{ New("abc").AppendString("def").AppendString("ghi"), } { str := r.String() err := r.Walk(func(part string) error { assert.Equal(t, str[:len(part)], part) str = str[len(part):] if len(str) < 4 { return errors.New("stop now") } return nil }) assert.Equal(t, err, errors.New("stop now")) assert.True(t, 0 < len(str) && len(str) < 4) } }
func TestConcatDropPrefix(t *testing.T) { defer disableCoalesce()() for _, ss := range substrings { if ss.end < ss.orig.length() { // Ignore non-suffix substrings continue } got := ss.orig.dropPrefix(ss.start) msg := fmt.Sprintf("%q[%v:] != %q", Rope{ss.orig}, ss.start, Rope{got}) assert.Equal(t, ss.want, got, msg) } }
func TestConcatDropPostfix(t *testing.T) { defer disableCoalesce()() for _, ss := range substrings { if ss.start > 0 { // Ignore non-prefix substrings continue } got := ss.orig.dropPostfix(ss.end) msg := fmt.Sprintf("%q[:%v] != %q", Rope{ss.orig}, ss.end, Rope{got}) assert.Equal(t, ss.want, got, msg) } }
func TestReadAt(t *testing.T) { want := treeR.Bytes() buf := make([]byte, len(want)+1) for start := 0; start < len(buf); start++ { for end := start; end <= len(buf); end++ { length := end - start b := buf[0:length] n, err := treeR.ReadAt(b, int64(start)) // Basic io.ReaderAt contract assert.True(t, n <= length) if n < length { assert.Equal(t, io.EOF, err) } // Expected actual end and length eEnd := end if eEnd > len(want) { eEnd = len(want) } eLen := eEnd - start // Check for correctness assert.Equal(t, eLen, n) if eLen < length { assert.Equal(t, io.EOF, err) } else if eLen > length { assert.Nil(t, err) } else { assert.True(t, err == nil || err == io.EOF) } assert.Equal(t, want[start:eEnd], b[:n]) } } }
func TestConcatWalkLeaves(t *testing.T) { counter := 0 err := tree.walkLeaves(func(l string) error { switch counter { case 0: assert.Equal(t, string(lhs), l) case 1: assert.Equal(t, string(rhs), l) case 2: t.Errorf("leaf.walkLeaves: function called too many times") default: // Ignore more than two calls, error has already been produced. } counter++ return nil }) assert.Nil(t, err) counter = 0 err = tree.walkLeaves(func(l string) error { switch counter { case 0: assert.Equal(t, string(lhs), l) return errors.New("stop now") case 1: t.Errorf("leaf.walkLeaves: did not stop after returning true") default: // Ignore more than two calls, error has already been produced. } counter++ return nil }) assert.Equal(t, errors.New("stop now"), err) }
func TestDropPostfix(t *testing.T) { defer disableCoalesce()() // See concat_test.go for the table used. for _, ss := range substrings { if ss.start > 0 { // Ignore non-prefix substrings continue } orig := Rope{ss.orig} got := orig.DropPostfix(ss.end) msg := fmt.Sprintf("%q[:%v] != %q", orig, ss.end, got) assert.Equal(t, ss.want, got.node, msg) } }
func TestRebalance(t *testing.T) { defer disableCoalesce()() for _, orig := range rebalanceTestRopes { origStr := orig.String() rebalanced := orig.Rebalance() rebalancedStr := rebalanced.String() //~ pretty.Println(orig, "(", orig.isBalanced(), ") ==> (", rebalanced.isBalanced(), ")", rebalanced) assert.Equal(t, origStr, rebalancedStr) if rebalanced.node != nil && orig.node != nil { assert.True(t, rebalanced.node.depth() <= orig.node.depth()) } assert.True(t, rebalanced.isBalanced()) } }
func TestSuccessfulRoutineRun(t *testing.T) { r := NewRoutine("echo", "hello test") c := NewCrew("c1") rr, err := r.Run(c) assert.Equal(t, err, nil) assert.Equal(t, rr.Done(), true) assert.Equal(t, rr.Failed, false) assert.Equal(t, rr.Stdin, "") assert.Equal(t, rr.Stdout, "hello test\n") assert.Equal(t, rr.Stderr, "") }
func TestConcNoCoalesce(t *testing.T) { defer lowerCoalesceThreshold()() result := conc(big, big, -1, -1) cc := expectConcat(t, result) assert.Equal(t, big+big, flatten(cc)) assert.Equal(t, big, cc.Left) assert.Equal(t, big, cc.Right) assert.Equal(t, big.length(), cc.Split) assert.Equal(t, rLenT(big.length()), cc.RLen) assert.Equal(t, depthT(1), cc.depth()) }
func TestAppendString(t *testing.T) { defer disableCoalesce()() r := New("123") assert.Equal(t, "123", r.String()) assert.Equal(t, r, r.Append(Rope{})) assert.Equal(t, r, r.Append(New(""))) // Test for structural equality in presence of empty strings. rab := r.AppendString("a", "c", "b") raeb := r.AppendString("a", "c", "", "b") assert.Equal(t, rab, raeb, "should ignore empty arguments to AppendString") r2 := r.AppendString("456") assert.Equal(t, "123456", r2.String()) assert.Equal(t, "123", r.String()) r2 = r.AppendString("456", "abc", "def") assert.Equal(t, "123456abcdef", r2.String()) assert.Equal(t, "123", r.String()) }
func TestConcCoalesceRight(t *testing.T) { defer lowerCoalesceThreshold()() base := conc(big, small, -1, -1) _ = expectConcat(t, base) n := conc(base, small, -1, -1) cc := expectConcat(t, n) assert.Equal(t, big+small+small, flatten(cc)) assert.Equal(t, big, cc.Left) assert.Equal(t, big.length(), cc.Split) assert.Equal(t, small+small, cc.Right) assert.Equal(t, rLenT(2*small.length()), cc.RLen) assert.Equal(t, depthT(1), cc.depth()) }
func TestNew(t *testing.T) { for _, str := range []string{"", "abc"} { r := New(str) assert.Equal(t, r.node, leaf(str)) } }