Skip to content

Commit

Permalink
Add a test for expression underlining data
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Jan 7, 2024
1 parent 0c55280 commit e413643
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
98 changes: 98 additions & 0 deletions test/testExpressionUnderlining.krk
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import dis
import fileio

from collections import deque

def test_a():
def bar():
return "string" + 42
def foo():
return bar() * 4
foo()

def test_b():
let foo = {
'bar': {
'baz': None
}
}

print(foo['bar']['baz']['qux']['etc'])

def test_c():
import os
os.system(42)

def test_d():
let a = object()
a.name = '42'
let b = None
let c = object()
c.name = '42'
print(a.name, b.name, c.name)

def test_e():
let a = 1
let b = 2
class Confabulator:
def __add__(self,other):
return NotImplemented
let c = Confabulator()
let d = 4

let x = (a + b) @ (c + d)

def test_f():
class Thing:
def __init__(self, required):
pass
def __eq__(self, other):
raise ValueError("oh no")

Thing('a') == Thing()

def disrec(code, seen):
let next = deque()
next.append(code)
while next:
let co = next.popleft()
seen.add(co)
let offset = 0
for inst,size,operand in dis.examine(co):
let expr = dis.ip_to_expression(co, offset + size - 1)
if expr is not None:
let lineNo, start, midStart, midEnd, end = expr
if co.__file__:
let j = 1
with fileio.open(co.__file__,'r') as f:
let line = f.readlines()[lineNo-1].rstrip()
let i = 0
while i < len(line):
if line[i] not in ' \t': break
j++
i++
while i < len(line):
print(line[i],end='')
i++
print()
while j < start:
print(' ',end='')
j++
while j < midStart:
print('~',end='')
j++
while j < midEnd:
print('^',end='')
j++
while j < end:
print('~',end='')
j++
print()
if isinstance(operand,codeobject) and operand not in seen:
next.append(operand)
offset += size

for func_name in dir():
if func_name.startswith('test_'):
disrec(globals()[func_name].__code__,set())

54 changes: 54 additions & 0 deletions test/testExpressionUnderlining.krk.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
foo()
~~~^~
return "string" + 42
~~~~~~~~~^~~~
return bar() * 4
~~~^~
return bar() * 4
~~~~~~^~~
print(foo['bar']['baz']['qux']['etc'])
~~~^~~~~~~
print(foo['bar']['baz']['qux']['etc'])
~~~~~~~~~~^~~~~~~
print(foo['bar']['baz']['qux']['etc'])
~~~~~~~~~~~~~~~~~^~~~~~~
print(foo['bar']['baz']['qux']['etc'])
~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
print(foo['bar']['baz']['qux']['etc'])
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
os.system(42)
~~^~~~~~~
os.system(42)
~~~~~~~~~^~~~
let a = object()
~~~~~~^~
a.name = '42'
~~^^^^~~~~~~~
let c = object()
~~~~~~^~
c.name = '42'
~~^^^^~~~~~~~
print(a.name, b.name, c.name)
~^~~~~
print(a.name, b.name, c.name)
~^~~~~
print(a.name, b.name, c.name)
~^~~~~
print(a.name, b.name, c.name)
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
let c = Confabulator()
~~~~~~~~~~~~^~
let x = (a + b) @ (c + d)
~~^~~
let x = (a + b) @ (c + d)
~~^~~
let x = (a + b) @ (c + d)
~~~~~~~~^~~~~~~~~
Thing('a') == Thing()
~~~~~^~~~~
Thing('a') == Thing()
~~~~~^~
Thing('a') == Thing()
~~~~~~~~~~~^^~~~~~~~~
raise ValueError("oh no")
~~~~~~~~~~^~~~~~~~~

0 comments on commit e413643

Please sign in to comment.