diff --git a/utils/utils.go b/utils/utils.go index 4798633..0bfe58a 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -5,7 +5,7 @@ package utils func IsEscaped(input string, position int) bool { escapeCounter := 0 - for backtrackIndex := position - 1; backtrackIndex >= 0; backtrackIndex++ { + for backtrackIndex := position - 1; backtrackIndex >= 0; backtrackIndex-- { if input[backtrackIndex] != '\\' { break } diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..5c2e29f --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,39 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/suite" +) + +type utilsTestSuite struct { + suite.Suite +} + +func TestRunUtilsTestSuite(t *testing.T) { + suite.Run(t, new(utilsTestSuite)) +} + +func (s *utilsTestSuite) TestIsEscaped() { + s.True(IsEscaped(`abc\(de`, 4)) + s.True(IsEscaped(`\(abc`, 1)) + s.True(IsEscaped(`abc\(`, 4)) +} + +func (s *utilsTestSuite) TestIsEscaped_Backslashes() { + s.True(IsEscaped(`abc\\de`, 4)) + s.True(IsEscaped(`\\abc`, 1)) + s.True(IsEscaped(`abc\\`, 4)) +} + +func (s *utilsTestSuite) TestIsEscaped_Not() { + s.False(IsEscaped(`abc\\(de`, 5)) + s.False(IsEscaped(`\\(abc`, 2)) + s.False(IsEscaped(`abc\\(`, 5)) +} + +func (s *utilsTestSuite) TestIsEscaped_Not_Backslashes() { + s.False(IsEscaped(`abc\\\de`, 5)) + s.False(IsEscaped(`\\\abc`, 2)) + s.False(IsEscaped(`abc\\\`, 5)) +}