summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVictor Uriarte <victor.m.uriarte@intel.com>2016-06-10 18:53:15 -0700
committerVictor Uriarte <victor.m.uriarte@intel.com>2016-06-11 04:34:14 -0700
commit0c468f5947741c0583330535e62c361f3a6af5ed (patch)
tree94666a30d05d45c1d2cd505874ef8215bad3f2d3
parentff0ee839987c43c420f1a5d167e3c3c0e873411c (diff)
downloadsqlparse-0c468f5947741c0583330535e62c361f3a6af5ed.tar.gz
Fix get_token_at_offset behavior at edge
At position 6 (with an index starting at 0) it should have been on 2nd word for the example sql='select * from dual'
-rw-r--r--sqlparse/sql.py2
-rw-r--r--tests/test_parse.py12
2 files changed, 13 insertions, 1 deletions
diff --git a/sqlparse/sql.py b/sqlparse/sql.py
index ced18af..a84dbf3 100644
--- a/sqlparse/sql.py
+++ b/sqlparse/sql.py
@@ -167,7 +167,7 @@ class TokenList(Token):
idx = 0
for token in self.flatten():
end = idx + len(token.value)
- if idx <= offset <= end:
+ if idx <= offset < end:
return token
idx = end
diff --git a/tests/test_parse.py b/tests/test_parse.py
index 2ea0f40..cc51a5d 100644
--- a/tests/test_parse.py
+++ b/tests/test_parse.py
@@ -303,3 +303,15 @@ def test_names_and_special_names(sql):
p = sqlparse.parse(sql)[0]
assert len(p.tokens) == 1
assert isinstance(p.tokens[0], sqlparse.sql.Identifier)
+
+
+def test_get_token_at_offset():
+ # 0123456789
+ p = sqlparse.parse('select * from dual')[0]
+ assert p.get_token_at_offset(0) == p.tokens[0]
+ assert p.get_token_at_offset(1) == p.tokens[0]
+ assert p.get_token_at_offset(6) == p.tokens[1]
+ assert p.get_token_at_offset(7) == p.tokens[2]
+ assert p.get_token_at_offset(8) == p.tokens[3]
+ assert p.get_token_at_offset(9) == p.tokens[4]
+ assert p.get_token_at_offset(10) == p.tokens[4]