/
morse_test.rb
159 lines (129 loc) · 4.64 KB
/
morse_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
require 'minitest/autorun'
require_relative 'morse'
# NOTE: The following tests are excercising only the `Morse::Encoder` because that's
# the intended interface that clients of this class will make use. As all the other
# classes are being used under the hood, they are being tested as well. But of course,
# to achieve a more granular unit testing, these classes could also be easily
# unit tested, should that be required.
class TestMorseEncoder < Minitest::Test
def test_that_the_definitions_are_correctly_encoded
definitions = YAML.load_file('definitions.yml')
definitions.each do |char, code|
assert_equal code, morse.encode_text(char)
end
end
def test_encoding_of_hello_world
assert_equal(
'....|.|.-..|.-..|---/.--|---|.-.|.-..|-..',
morse.encode_text('HELLO WORLD')
)
end
def test_encoding_of_text_containing_all_letters
assert_equal(
'-|....|./--.-|..-|..|-.-.|-.-/-...|.-.|---|.--|-./..-.|---|-..-/.---|..-|--|.--.|.../---|...-|.|.-./-|....|./.-..|.-|--..|-.--/-..|---|--.',
morse.encode_text('THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG')
)
end
def test_encoding_of_text_containing_all_numbers
assert_equal(
'----.|.----|---..|..---|--...|...--|-....|....-|.....',
morse.encode_text('918273645')
)
end
def test_encoding_of_text_containing_all_characters
assert_equal(
'.----/-|....|./..---/--.-|..-|..|-.-.|-.-/...--/-...|.-.|---|.--|-./....-/..-.|---|-..-/.....|--..--/.---|..-|--|.--.|.../-..../---|...-|.|.-./--.../-|....|./---../.-..|.-|--..|-.--/----./-..|---|--./.----|-----|.-.-.-',
morse.encode_text('1 THE 2 QUICK 3 BROWN 4 FOX 5, JUMPS 6 OVER 7 THE 8 LAZY 9 DOG 10.')
)
end
def test_encoding_of_text_with_trailing_spaces
assert_equal(
'../.-|--/..|-./-|.-.|---|..-|-...|.-..|.',
morse.encode_text(' I AM IN TROUBLE ')
)
end
def test_encoding_of_text_with_line_breaks
assert_equal(
"../.-|--/..|-.\n-|.-.|---|..-|-...|.-..|.",
morse.encode_text("I AM IN\nTROUBLE")
)
end
def test_encoding_of_empty_string
assert_equal '', morse.encode_text('')
end
def test_encoding_of_null_input
assert_equal '', morse.encode_text(nil)
end
def test_encoding_of_a_sequence_of_whitespaces
assert_equal '', morse.encode_text(' ')
end
def test_that_encoding_does_not_care_about_letter_case
assert_equal(
'../.-|--/..|-./-|.-.|---|..-|-...|.-..|.',
morse.encode_text('I am in Trouble')
)
end
def test_raise_an_error_when_input_contains_unmapped_characters
assert_raises Morse::InvalidChar do morse.encode_text('HELP!') end
end
private
def morse
@morse ||= Morse::Encoder.new
end
end
class TestObfuscatedMorseEncoder < Minitest::Test
def test_obfuscation
assert_equal(
'2/1A|B/2|A1/A|1A1|C|2A|A3|1A2|1',
morse.encode_text('I AM IN TROUBLE')
)
end
def test_encoding_of_text_containing_all_characters
assert_equal(
'1D/A|4|1/2C/B1A|2A|2|A1A1|A1A/3B/A3|1A1|C|1B|A1/4A/2A1|C|A2A/5|B2B/1C|2A|B|1B1|3/A4/C|3A|1|1A1/B3/A|4|1/C2/1A2|1A|B2|A1B/D1/A2|C|B1/1D|E|1A1A1A',
morse.encode_text('1 THE 2 QUICK 3 BROWN 4 FOX 5, JUMPS 6 OVER 7 THE 8 LAZY 9 DOG 10.')
)
end
def test_encoding_of_text_with_trailing_spaces
assert_equal(
'2/1A|B/2|A1/A|1A1|C|2A|A3|1A2|1',
morse.encode_text(' I AM IN TROUBLE ')
)
end
def test_encoding_of_empty_string
assert_equal '', morse.encode_text('')
end
def test_encoding_of_null_input
assert_equal '', morse.encode_text(nil)
end
def test_encoding_of_a_sequence_of_whitespaces
assert_equal '', morse.encode_text(' ')
end
def test_that_encoding_does_not_care_about_letter_case
assert_equal(
'2/1A|B/2|A1/A|1A1|C|2A|A3|1A2|1',
morse.encode_text('I am in Trouble')
)
end
def test_raise_an_error_when_input_contains_unmapped_characters
assert_raises Morse::InvalidChar do morse.encode_text('HELP!') end
end
def test_encoding_of_a_file_that_has_valid_content
test_file = 'test_file.valid'
encoded_file = morse.encode_file(test_file)
assert File.exist?(encoded_file)
assert_equal "#{test_file}.encoded", File.basename(encoded_file)
encoded_content = File.read(encoded_file).strip
assert_equal "4|1|1A2|1A2|C/1B|C|1A1|1A2|A2\n2/1A|B/2|A1/A|1A1|C|2A|A3|1A2|1", encoded_content
end
def test_encoding_of_a_file_that_has_invalid_content
test_file = 'test_file.invalid'
assert_raises Morse::InvalidChar do morse.encode_file(test_file) end
encoded_file = "#{test_file}.encoded"
assert !File.exist?(encoded_file)
end
private
def morse
@obfuscated_morse ||= Morse::EncoderObfuscated.new
end
end