1
1
mirror of https://github.com/walles/moar.git synced 2024-11-27 01:05:23 +03:00
moar/m/matchRanges_test.go

90 lines
3.0 KiB
Go
Raw Normal View History

2019-06-30 10:43:58 +03:00
package m
import (
"regexp"
"testing"
2022-09-25 10:06:46 +03:00
"gotest.tools/v3/assert"
2019-06-30 10:43:58 +03:00
)
// This is really a constant, don't change it!
var _TestString = "mamma"
2019-06-30 10:43:58 +03:00
func TestGetMatchRanges(t *testing.T) {
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&_TestString, regexp.MustCompile("m+"))
2019-06-30 10:43:58 +03:00
assert.Equal(t, len(matchRanges.Matches), 2) // Two matches
assert.DeepEqual(t, matchRanges.Matches[0][0], 0) // First match starts at 0
assert.DeepEqual(t, matchRanges.Matches[0][1], 1) // And ends on 1 exclusive
assert.DeepEqual(t, matchRanges.Matches[1][0], 2) // Second match starts at 2
assert.DeepEqual(t, matchRanges.Matches[1][1], 4) // And ends on 4 exclusive
}
2019-06-30 10:53:24 +03:00
func TestGetMatchRangesNilPattern(t *testing.T) {
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&_TestString, nil)
2019-06-30 10:53:24 +03:00
assert.Assert(t, matchRanges == nil)
assert.Assert(t, !matchRanges.InRange(0))
}
2019-06-30 10:43:58 +03:00
func TestInRange(t *testing.T) {
// Should match the one in TestGetMatchRanges()
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&_TestString, regexp.MustCompile("m+"))
2019-06-30 10:43:58 +03:00
assert.Assert(t, !matchRanges.InRange(-1)) // Before start
assert.Assert(t, matchRanges.InRange(0)) // m
assert.Assert(t, !matchRanges.InRange(1)) // a
assert.Assert(t, matchRanges.InRange(2)) // m
assert.Assert(t, matchRanges.InRange(3)) // m
assert.Assert(t, !matchRanges.InRange(4)) // a
assert.Assert(t, !matchRanges.InRange(5)) // After end
}
func TestUtf8(t *testing.T) {
// This test verifies that the match ranges are by rune rather than by byte
unicodes := "-ä-ä-"
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&unicodes, regexp.MustCompile("ä"))
assert.Assert(t, !matchRanges.InRange(0)) // -
assert.Assert(t, matchRanges.InRange(1)) // ä
assert.Assert(t, !matchRanges.InRange(2)) // -
assert.Assert(t, matchRanges.InRange(3)) // ä
assert.Assert(t, !matchRanges.InRange(4)) // -
}
2019-11-06 23:38:39 +03:00
func TestNoMatch(t *testing.T) {
// This test verifies that the match ranges are by rune rather than by byte
unicodes := "gris"
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&unicodes, regexp.MustCompile("apa"))
2019-11-06 23:38:39 +03:00
assert.Assert(t, !matchRanges.InRange(0))
assert.Assert(t, !matchRanges.InRange(1))
assert.Assert(t, !matchRanges.InRange(2))
assert.Assert(t, !matchRanges.InRange(3))
assert.Assert(t, !matchRanges.InRange(4))
}
2019-11-06 23:40:14 +03:00
func TestEndMatch(t *testing.T) {
// This test verifies that the match ranges are by rune rather than by byte
unicodes := "-ä"
2020-12-30 00:57:44 +03:00
matchRanges := getMatchRanges(&unicodes, regexp.MustCompile("ä"))
2019-11-06 23:40:14 +03:00
assert.Assert(t, !matchRanges.InRange(0)) // -
assert.Assert(t, matchRanges.InRange(1)) // ä
assert.Assert(t, !matchRanges.InRange(2)) // Past the end
}
func TestRealWorldBug(t *testing.T) {
// Verify a real world bug found in v1.9.8
testString := "anna"
matchRanges := getMatchRanges(&testString, regexp.MustCompile("n"))
assert.Equal(t, len(matchRanges.Matches), 2) // Two matches
assert.DeepEqual(t, matchRanges.Matches[0][0], 1) // First match starts at 1
assert.DeepEqual(t, matchRanges.Matches[0][1], 2) // And ends on 2 exclusive
assert.DeepEqual(t, matchRanges.Matches[1][0], 2) // Second match starts at 2
assert.DeepEqual(t, matchRanges.Matches[1][1], 3) // And ends on 3 exclusive
}