-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from telebugs/send-code
Send code with errors
- Loading branch information
Showing
13 changed files
with
327 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Telebugs | ||
# Represents a small hunk of code consisting of a base line and a couple lines | ||
# around it | ||
class CodeHunk | ||
MAX_LINE_LEN = 200 | ||
|
||
# How many lines should be read around the base line. | ||
AROUND_LINES = 2 | ||
|
||
def self.get(file, line) | ||
start_line = [line - AROUND_LINES, 1].max | ||
|
||
lines = get_lines(file, start_line, line + AROUND_LINES) | ||
return {start_line: 0, lines: []} if lines.empty? | ||
|
||
{ | ||
start_line: start_line, | ||
lines: lines | ||
} | ||
end | ||
|
||
private_class_method def self.get_lines(file, start_line, end_line) | ||
lines = [] | ||
return lines unless (cached_file = get_from_cache(file)) | ||
|
||
cached_file.with_index(1) do |l, i| | ||
next if i < start_line | ||
break if i > end_line | ||
|
||
lines << l[0...MAX_LINE_LEN].rstrip | ||
end | ||
|
||
lines | ||
end | ||
|
||
private_class_method def self.get_from_cache(file) | ||
Telebugs::FileCache[file] ||= File.foreach(file) | ||
rescue | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module Telebugs | ||
module FileCache | ||
MAX_SIZE = 50 | ||
MUTEX = Mutex.new | ||
|
||
# Associates the value given by +value+ with the key given by +key+. Deletes | ||
# entries that exceed +MAX_SIZE+. | ||
def self.[]=(key, value) | ||
MUTEX.synchronize do | ||
data[key] = value | ||
data.delete(data.keys.first) if data.size > MAX_SIZE | ||
end | ||
end | ||
|
||
# Retrieve an object from the cache. | ||
def self.[](key) | ||
MUTEX.synchronize do | ||
data[key] | ||
end | ||
end | ||
|
||
# Checks whether the cache is empty. Needed only for the test suite. | ||
def self.empty? | ||
MUTEX.synchronize do | ||
data.empty? | ||
end | ||
end | ||
|
||
def self.reset | ||
MUTEX.synchronize do | ||
@data = {} | ||
end | ||
end | ||
|
||
private_class_method def self.data | ||
@data ||= {} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
class Botley | ||
RESPONSES = { | ||
/hello/i => "Oh, hello there! Are you ready to have some fun?", | ||
/how are you/i => "I'm just a bunch of code, but thanks for asking! How about you?", | ||
/what is your name/i => "I'm Botley, your friendly (and sometimes cheeky) virtual assistant!", | ||
/joke/i => "Why don't scientists trust atoms? Because they make up everything!", | ||
/bye|goodbye|exit/i => "Goodbye! Don't miss me too much!", | ||
/thank you|thanks/i => "You're welcome! I'm here all week.", | ||
/default/ => "I'm not sure what you mean, but it sounds intriguing!" | ||
} | ||
|
||
def initialize | ||
puts "Botley: Hello! I'm Botley, your virtual assistant. Type 'goodbye' to exit." | ||
end | ||
|
||
def start | ||
loop do | ||
print "You: " | ||
user_input = gets.chomp | ||
response = respond_to(user_input) | ||
puts "Botley: #{response}" | ||
break if user_input.match?(/bye|goodbye|exit/i) | ||
end | ||
end | ||
|
||
private | ||
|
||
def respond_to(input) | ||
RESPONSES.each do |pattern, response| | ||
return response if input.match?(pattern) | ||
end | ||
RESPONSES[:default] | ||
end | ||
end | ||
|
||
# Start the conversation with Botley | ||
Botley.new.start |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Boom.new.call |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestCodeHunk < Minitest::Test | ||
def test_get_when_file_is_empty | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/empty_file.rb", 1) | ||
|
||
assert_equal({start_line: 0, lines: []}, hunk) | ||
end | ||
|
||
def test_get_when_file_has_one_line | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/one_line.rb", 1) | ||
|
||
assert_equal( | ||
{ | ||
start_line: 1, | ||
lines: ["Boom.new.call"] | ||
}, | ||
hunk | ||
) | ||
end | ||
|
||
def test_get | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/code.rb", 18) | ||
|
||
assert_equal( | ||
{ | ||
start_line: 16, | ||
lines: [ | ||
" end", | ||
"", | ||
" def start", | ||
" loop do", | ||
" print \"You: \"" | ||
] | ||
}, | ||
hunk | ||
) | ||
end | ||
|
||
def test_get_with_edge_case_first_line | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/code.rb", 1) | ||
|
||
assert_equal( | ||
{ | ||
start_line: 1, | ||
lines: [ | ||
"# frozen_string_literal: true", | ||
"", | ||
"class Botley" | ||
] | ||
}, | ||
hunk | ||
) | ||
end | ||
|
||
def test_get_with_edge_case_last_line | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/code.rb", 39) | ||
|
||
assert_equal( | ||
{ | ||
start_line: 37, | ||
lines: [ | ||
"", | ||
"# Start the conversation with Botley", | ||
"Botley.new.start" | ||
] | ||
}, | ||
hunk | ||
) | ||
end | ||
|
||
def test_get_when_code_line_is_too_long | ||
hunk = Telebugs::CodeHunk.get("test/fixtures/project_root/long_line.rb", 1) | ||
|
||
assert_equal( | ||
{ | ||
start_line: 1, | ||
lines: [ | ||
"loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong" | ||
] | ||
}, | ||
hunk | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class TestFileCache < Minitest::Test | ||
def teardown | ||
Telebugs::FileCache.reset | ||
end | ||
|
||
def test_set_when_cache_limit_is_not_reached | ||
max_size = Telebugs::FileCache::MAX_SIZE | ||
max_size.times do |i| | ||
Telebugs::FileCache["key#{i}"] = "value#{i}" | ||
end | ||
|
||
assert_equal "value0", Telebugs::FileCache["key0"] | ||
assert_equal "value#{max_size - 1}", Telebugs::FileCache["key#{max_size - 1}"] | ||
end | ||
|
||
def test_set_when_cache_over_limit | ||
max_size = 2 * Telebugs::FileCache::MAX_SIZE | ||
max_size.times do |i| | ||
Telebugs::FileCache["key#{i}"] = "value#{i}" | ||
end | ||
|
||
assert_nil Telebugs::FileCache["key49"] | ||
assert_equal "value50", Telebugs::FileCache["key50"] | ||
assert_equal "value#{max_size - 1}", Telebugs::FileCache["key#{max_size - 1}"] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters