Skip to content

Commit a537152

Browse files
committed
Split tests into individual filess
1 parent 101977b commit a537152

12 files changed

Lines changed: 564 additions & 564 deletions

tests/test_bug_fixes.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require_relative 'test_helpers'
2+
3+
def test_assign_text_nil(_args, assert)
4+
input = build_text_input('1234567890', 10, 10)
5+
input.value = nil
6+
7+
assert.equal! input.value.to_s, ''
8+
assert.equal! input.selection_end, 0
9+
assert.equal! input.selection_start, 0
10+
end
11+
12+
def test_create_with_nil_value(_args, assert)
13+
input = build_text_input(nil)
14+
15+
assert.equal! input.value.to_s, ''
16+
assert.equal! input.selection_end, 0
17+
assert.equal! input.selection_start, 0
18+
end
19+
20+
def test_create_with_non_string_non_falsey_value(_args, assert)
21+
input = build_text_input(true)
22+
23+
assert.equal! input.value.to_s, 'true'
24+
assert.equal! input.selection_end, 0
25+
assert.equal! input.selection_start, 0
26+
end

tests/test_calcstringbox.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require_relative 'test_helpers'
2+
3+
def test_calcstringbox_works_in_tests(_args, assert)
4+
w, h = $gtk.calcstringbox('1234567890', 0, '')
5+
assert.true! w > 0
6+
assert.true! h > 0
7+
end
8+
9+
def test_calcstringbox_new_line_has_no_width(_args, assert)
10+
w, h = $gtk.calcstringbox("\n", 0, '')
11+
assert.equal! w, 0.0
12+
assert.equal! h, 22.0 # Yep, it has a height
13+
end
14+
15+
def test_calcstringbox_double_new_line_has_no_width(_args, assert)
16+
w, h = $gtk.calcstringbox("\n\n", 0, '')
17+
assert.equal! w, 0.0
18+
assert.equal! h, 22.0 # Yep, it has a height
19+
end
20+
21+
def test_calcstringbox_with_day_roman_new_line_has_width(_args, assert)
22+
w, h = $gtk.calcstringbox("\n\n", 0, 'fonts/day-roman/DAYROM__.ttf')
23+
assert.false! w == 0.0 # :/
24+
assert.false! h == 0.0 # :/
25+
end
26+
27+
def test_calcstringbox_tab_has_no_witdh(_args, assert)
28+
w, h = $gtk.calcstringbox("\t", 0, '')
29+
assert.equal! w, 0.0 # Yep, it has no width :/
30+
assert.equal! h, 22.0 # Yep, it has a height
31+
end

tests/test_current_word.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require_relative 'test_helpers'
2+
3+
def test_finds_current_word(_args, assert)
4+
assert_current_word(assert, '|word test', nil)
5+
assert_current_word(assert, 'w|ord test', 'word')
6+
assert_current_word(assert, 'wor|d test', 'word')
7+
assert_current_word(assert, 'word| test', 'word')
8+
assert_current_word(assert, 'word |test', nil)
9+
end

tests/test_delete_back.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require_relative 'test_helpers'
2+
3+
def test_delete_back(_args, assert)
4+
input = build_text_input('1234567890', 10, 10)
5+
input.delete_back
6+
7+
assert.equal! input.value.to_s, '123456789'
8+
assert.equal! input.selection_end, 9
9+
assert.equal! input.selection_start, 9
10+
end
11+
12+
def test_delete_back_empty_value(_args, assert)
13+
input = build_text_input('', 0, 0)
14+
input.delete_back
15+
16+
assert.equal! input.value.to_s, ''
17+
assert.equal! input.selection_end, 0
18+
assert.equal! input.selection_start, 0
19+
end
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
require_relative 'test_helpers'
2+
3+
def test_default_height_is_calculated_from_padding_and_font_height(_args, assert)
4+
_, font_height = $gtk.calcstringbox('A', 0)
5+
text_input = Input::Text.new(padding: 10, size_enum: 0)
6+
7+
assert.equal! text_input.h, font_height + 20
8+
end
9+
10+
def test_multiline_scrolls_in_font_height_steps_by_default(args, assert)
11+
$args = args
12+
_, font_height = $gtk.calcstringbox('A', 0)
13+
input = Input::Multiline.new(x: 100, y: 100, w: 100, size_enum: 0)
14+
input.insert "line 1\n"
15+
input.insert "line 2\n"
16+
input.insert "line 3\n"
17+
18+
assert.equal! input.scroll_y, 0
19+
20+
mouse_is_inside(input)
21+
args.inputs.mouse.wheel = { y: 1 }
22+
input.tick
23+
24+
assert.equal! input.scroll_y, font_height
25+
end
26+
27+
def test_text_click_inside_sets_selection(args, assert)
28+
$args = args
29+
three_letters_wide, _ = $gtk.calcstringbox('ABC', 0)
30+
input = Input::Text.new(x: 100, y: 100, w: 100, size_enum: 0, value: 'ABCDEF', focussed: true)
31+
32+
mouse_is_inside(input, x: 100 + three_letters_wide)
33+
mouse_down
34+
input.tick
35+
36+
mouse_up
37+
input.tick
38+
39+
assert.equal! input.selection_start, 3
40+
assert.equal! input.selection_end, 3
41+
end
42+
43+
def test_text_drag_inside_sets_selection(args, assert)
44+
$args = args
45+
three_letters_wide, _ = $gtk.calcstringbox('ABC', 0)
46+
six_letters_wide, _ = $gtk.calcstringbox('ABCDEF', 0)
47+
input = Input::Text.new(x: 100, y: 100, w: 100, size_enum: 0, value: 'ABCDEFGH', focussed: true)
48+
49+
mouse_is_inside(input, x: 100 + three_letters_wide)
50+
mouse_down
51+
input.tick
52+
mouse_is_inside(input, x: 100 + six_letters_wide)
53+
mouse_up
54+
input.tick
55+
56+
assert.equal! input.selection_start, 3
57+
assert.equal! input.selection_end, 6
58+
end
59+
60+
def test_multiline_click_inside_sets_selection(args, assert)
61+
$args = args
62+
three_letters_wide, font_height = $gtk.calcstringbox('ABC', 0)
63+
input = Input::Multiline.new(x: 100, y: 100, w: 100, h: font_height * 2, size_enum: 0, value: "ABCDEF\nGHIJKL", focussed: true)
64+
inside_second_line_y = input.y + font_height.half
65+
66+
mouse_is_at(100 + three_letters_wide, inside_second_line_y)
67+
mouse_down
68+
input.tick
69+
mouse_up
70+
input.tick
71+
72+
73+
# 10 = ABCDEF\nGHI
74+
assert.equal! input.selection_start, 10
75+
assert.equal! input.selection_end, 10
76+
end
77+
78+
def test_text_drag_inside_sets_selection(args, assert)
79+
$args = args
80+
three_letters_wide, font_height = $gtk.calcstringbox('ABC', 0)
81+
input = Input::Multiline.new(x: 100, y: 100, w: 100, h: font_height * 2, size_enum: 0, value: "ABCDEF\nGHIJKL", focussed: true)
82+
inside_second_line_y = input.y + font_height.half
83+
inside_first_line_y = inside_second_line_y + font_height
84+
85+
mouse_is_at(100 + three_letters_wide, inside_first_line_y)
86+
mouse_down
87+
input.tick
88+
89+
mouse_is_at(100 + three_letters_wide, inside_second_line_y)
90+
mouse_up
91+
input.tick
92+
93+
assert.equal! input.selection_start, 3
94+
assert.equal! input.selection_end, 10
95+
end
96+
97+
# Two representative test cases using size_px instead of size_enum
98+
99+
def test_default_height_is_calculated_from_padding_and_font_height_size_px(_args, assert)
100+
_, font_height = $gtk.calcstringbox('A', size_px: 30)
101+
text_input = Input::Text.new(padding: 10, size_px: 30)
102+
103+
assert.equal! text_input.h, font_height + 20
104+
end
105+
106+
def test_text_drag_inside_sets_selection_size_px(args, assert)
107+
$args = args
108+
three_letters_wide, _ = $gtk.calcstringbox('ABC', size_px: 44)
109+
six_letters_wide, _ = $gtk.calcstringbox('ABCDEF', size_px: 44)
110+
input = Input::Text.new(x: 100, y: 100, w: 200, size_px: 44, value: 'ABCDEFGH', focussed: true)
111+
112+
mouse_is_inside(input, x: 100 + three_letters_wide)
113+
mouse_down
114+
input.tick
115+
mouse_is_inside(input, x: 100 + six_letters_wide)
116+
mouse_up
117+
input.tick
118+
119+
assert.equal! input.selection_start, 3
120+
assert.equal! input.selection_end, 6
121+
end

tests/test_helpers.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
def build_text_input(value, selection_start = 0, selection_end = selection_start, **attr)
2+
Input::Text.new(value: value, selection_start: selection_start, selection_end: selection_end, **attr)
3+
end
4+
5+
def make_word_break_error(input, actual, expected)
6+
<<-EOS
7+
Starting '#{input.value.to_s.dup.insert(input.selection_end, '|')}'
8+
Actual '#{input.value.to_s.dup.insert(actual, '|')}'
9+
Expected '#{input.value.to_s.dup.insert(expected, '|')}'
10+
EOS
11+
end
12+
13+
def assert_finds_word_break_left(assert, starting, expected)
14+
selection_end = starting.index('|')
15+
expected = expected.index('|')
16+
input = build_text_input(starting.delete('|'), selection_end)
17+
actual = input.find_word_break_left
18+
assert.equal! actual, expected, make_word_break_error(input, actual, expected)
19+
end
20+
21+
def assert_finds_word_break_right(assert, starting, expected)
22+
selection_end = starting.index('|')
23+
expected = expected.index('|')
24+
input = build_text_input(starting.delete('|'), selection_end)
25+
actual = input.find_word_break_right
26+
assert.equal! actual, expected, make_word_break_error(input, actual, expected)
27+
end
28+
29+
def make_current_word_error(input, actual, expected)
30+
<<-EOS
31+
Starting '#{input.value.to_s.dup.insert(input.selection_end, '|')}'
32+
Actual #{actual.nil? ? 'nil' : "'#{actual}'"}
33+
Expected #{expected.nil? ? 'nil' : "'#{expected}'"}
34+
EOS
35+
end
36+
37+
def assert_current_word(assert, starting, expected)
38+
selection_end = starting.index('|')
39+
input = build_text_input(starting.delete('|'), selection_end)
40+
actual = input.current_word
41+
assert.equal! actual, expected, make_current_word_error(input, actual, expected)
42+
end
43+
44+
def build_multiline_input(width_in_letters)
45+
# This works because the default DR font is monospaced
46+
width, _ = $gtk.calcstringbox('1' * width_in_letters, 0)
47+
Input::Multiline.new(w: width)
48+
end
49+
50+
def word_wrap_result(string, width_in_letters = 10)
51+
multiline = build_multiline_input(10)
52+
multiline.insert string
53+
multiline.lines.map(&:text)
54+
end
55+
56+
def mouse_is_at(x, y)
57+
$args.inputs.mouse.x = x
58+
$args.inputs.mouse.y = y
59+
end
60+
61+
def mouse_is_inside(rect, x: nil)
62+
mouse_is_at(
63+
x || rect.x + rect.w.half,
64+
rect.y + rect.h.half
65+
)
66+
end
67+
68+
def mouse_down
69+
$args.inputs.mouse.button_left = true
70+
$args.inputs.mouse.click = GTK::MousePoint.new($args.inputs.mouse.x, $args.inputs.mouse.y)
71+
$args.inputs.mouse.up = false
72+
end
73+
74+
def mouse_up
75+
$args.inputs.mouse.button_left = false
76+
$args.inputs.mouse.click = nil
77+
$args.inputs.mouse.up = true
78+
end

tests/test_line_wrap.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require_relative 'test_helpers'
2+
3+
def test_find_word_breaks_empty_value(_args, assert)
4+
assert.equal! word_wrap_result(''), ['']
5+
end
6+
7+
def test_find_word_breaks_single_space(_args, assert)
8+
assert.equal! word_wrap_result(' '), [' ']
9+
end
10+
11+
def test_find_word_breaks_single_char(_args, assert)
12+
assert.equal! word_wrap_result('a'), ['a']
13+
end
14+
15+
def test_multiline_word_breaks_two_words(_args, assert)
16+
assert.equal! word_wrap_result('Hello, world'), ['Hello, ', 'world']
17+
end
18+
19+
def test_find_word_breaks_leading_and_trailing_white_space(_args, assert)
20+
assert.equal! word_wrap_result(" \t hello \t "), [" \t hello \t "]
21+
end
22+
23+
def test_find_word_breaks_leading_and_trailing_white_space_multiple_words(_args, assert)
24+
assert.equal! word_wrap_result(" \t hello, \t world \t"), [" \t hello, \t ", "world \t"]
25+
end
26+
27+
def test_multiline_word_breaks_trailing_new_line(_args, assert)
28+
assert.equal! word_wrap_result("hello, \n"), ['hello, ', "\n"]
29+
end
30+
31+
def test_multiline_word_breaks_new_line(_args, assert)
32+
assert.equal! word_wrap_result("hello, \n world"), ['hello, ', "\n world"]
33+
end
34+
35+
def test_multiline_word_breaks_double_new_line(_args, assert)
36+
assert.equal! word_wrap_result("hello, \n\n world"), ['hello, ', "\n", "\n world"]
37+
end
38+
39+
def test_multiline_word_breaks_multiple_new_lines(_args, assert)
40+
assert.equal! word_wrap_result("hello, \n\n\n world"), ['hello, ', "\n", "\n", "\n world"]
41+
end
42+
43+
def test_perform_word_wrap_multiple_new_lines(_args, assert)
44+
assert.equal! word_wrap_result("1\n\n\n2"), ['1', "\n", "\n", "\n2"]
45+
end
46+
47+
def test_perform_word_wrap_trailing_new_line(_args, assert)
48+
assert.equal! word_wrap_result("1\n"), ['1', "\n"]
49+
end
50+
51+
def test_find_word_breaks_trailing_new_line_after_wrap(_args, assert)
52+
assert.equal! word_wrap_result("1234567890 1234567890 1234567890\n"), ['1234567890 ', '1234567890 ', '1234567890', "\n"]
53+
end
54+
55+
def test_multiline_word_breaks_a_very_long_word(_args, assert)
56+
assert.equal! word_wrap_result('Supercalifragilisticexpialidocious'), ['Supercalif', 'ragilistic', 'expialidoc', 'ious']
57+
end
58+
59+
def test_multiline_word_breaks_breaks_very_long_word_after_something_that_isnt(_args, assert)
60+
assert.equal! word_wrap_result('Super califragilisticexpialidocious'), ['Super ', 'califragil', 'isticexpia', 'lidocious']
61+
end

0 commit comments

Comments
 (0)