diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-06-14 10:28:09 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-06-14 10:50:14 -0700 |
commit | dc3b18eb6caffe0fdc5ba262a99aa9452e76d914 (patch) | |
tree | a037c54daab153ee59f802bebaa45a89f0435de7 | |
parent | 8c535dbb3a880199c2d7c7b43dccfbb287ac12bb (diff) | |
download | text.love-dc3b18eb6caffe0fdc5ba262a99aa9452e76d914.tar.gz |
start writing some tests for drawings
-rw-r--r-- | drawing.lua | 4 | ||||
-rw-r--r-- | drawing_tests.lua | 30 |
2 files changed, 33 insertions, 1 deletions
diff --git a/drawing.lua b/drawing.lua index 0dda2b5..204a93e 100644 --- a/drawing.lua +++ b/drawing.lua @@ -2,6 +2,8 @@ Drawing = {} geom = require 'geom' +require 'drawing_tests' + -- All drawings span 100% of some conceptual 'page width' and divide it up -- into 256 parts. function Drawing.draw(line) @@ -202,7 +204,7 @@ end function Drawing.in_drawing(drawing, x,y) if drawing.y == nil then return false end -- outside current page - return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= 16 and x < 16+Line_width + return y >= drawing.y and y < drawing.y + Drawing.pixels(drawing.h) and x >= Margin_left and x < Margin_left+Line_width end function Drawing.mouse_pressed(drawing, x,y, button) diff --git a/drawing_tests.lua b/drawing_tests.lua new file mode 100644 index 0000000..ceb11a3 --- /dev/null +++ b/drawing_tests.lua @@ -0,0 +1,30 @@ +-- major tests for drawings +-- We minimize assumptions about specific pixels, and try to test at the level +-- of specific shapes. In particular, no tests of freehand drawings. + +function test_draw_line() + io.write('\ntest_draw_line') + -- display a drawing followed by a line of text (you shouldn't ever have a drawing right at the end) + App.screen.init{width=Margin_left+300, height=300} + Lines = load_array{'```lines', '```', ''} + Line_width = 256 -- drawing coordinates 1:1 with pixels + Current_drawing_mode = 'line' + App.draw() + check_eq(#Lines, 2, 'F - test_draw_line/baseline/#lines') + check_eq(Lines[1].mode, 'drawing', 'F - test_draw_line/baseline/mode') + check_eq(Lines[1].y, Margin_top+Drawing_padding_top, 'F - test_draw_line/baseline/y') + check_eq(Lines[1].h, 128, 'F - test_draw_line/baseline/y') + check_eq(#Lines[1].shapes, 0, 'F - test_draw_line/baseline/#shapes') + -- draw a line + App.run_after_mouse_press(Margin_left+5, Margin_top+Drawing_padding_top+6, 1) + App.run_after_mouse_release(Margin_left+35, Margin_top+Drawing_padding_top+36, 1) + check_eq(#Lines[1].shapes, 1, 'F - test_draw_line/#shapes') + check_eq(#Lines[1].points, 2, 'F - test_draw_line/#points') + local drawing = Lines[1] + local p1 = drawing.points[drawing.shapes[1].p1] + local p2 = drawing.points[drawing.shapes[1].p2] + check_eq(p1.x, 5, 'F - test_draw_line/p1:x') + check_eq(p1.y, 6, 'F - test_draw_line/p1:y') + check_eq(p2.x, 35, 'F - test_draw_line/p2:x') + check_eq(p2.y, 36, 'F - test_draw_line/p2:y') +end |