Skip to content

Commit

Permalink
cqfn#96: stub find_github_user method
Browse files Browse the repository at this point in the history
  • Loading branch information
mbao01 committed Jul 12, 2021
1 parent 70ff725 commit e2362a0
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 68 deletions.
13 changes: 13 additions & 0 deletions test/test__helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,16 @@

require 'minitest/autorun'
require_relative '../lib/pdd'

def stub_source_find_github_user(file, path = '')
source = PDD::Source.new(file, path)
verbose_source = PDD::VerboseSource.new(file, source)
fake = proc do |info = {}|
email, author = info.values_at(:email, :author)
{ 'login' => 'yegor256' } if email == 'yegor256@gmail.com' ||
author == 'Yegor Bugayenko'
end
source.stub :find_github_user, fake do
yield verbose_source
end
end
140 changes: 79 additions & 61 deletions test/test_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ def test_parsing
~~ and it also has to work
"
)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal 2, list.size
puzzle = list.first
assert_equal '2-3', puzzle.props[:lines]
assert_equal 'привет, how are you doing?', puzzle.props[:body]
assert_equal '44', puzzle.props[:ticket]
assert puzzle.props[:author].nil?
assert puzzle.props[:email].nil?
assert puzzle.props[:time].nil?
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 2, list.size
puzzle = list.first
assert_equal '2-3', puzzle.props[:lines]
assert_equal 'привет, how are you doing?', puzzle.props[:body]
assert_equal '44', puzzle.props[:ticket]
assert puzzle.props[:author].nil?
assert puzzle.props[:email].nil?
assert puzzle.props[:time].nil?
end
end
end

Expand All @@ -65,12 +67,14 @@ def test_parsing_leading_spaces
* comment!
"
)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '2-4', puzzle.props[:lines]
assert_equal 'this is a multi-line comment!', puzzle.props[:body]
assert_equal '56', puzzle.props[:ticket]
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '2-4', puzzle.props[:lines]
assert_equal 'this is a multi-line comment!', puzzle.props[:body]
assert_equal '56', puzzle.props[:ticket]
end
end
end

Expand All @@ -85,7 +89,7 @@ def test_failing_on_invalid_puzzle
"
)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
stub_source_find_github_user(file, 'hey', &:puzzles)
end
assert !error.message.index('Space expected').nil?
end
Expand All @@ -106,13 +110,15 @@ def test_succeed_despite_bad_puzzles
"
)
PDD.opts = { 'skip-errors' => true }
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
PDD.opts = nil
assert_equal 1, list.size
puzzle = list.first
assert_equal '7-7', puzzle.props[:lines]
assert_equal 'This puzzle is correctly formatted', puzzle.props[:body]
assert_equal '123', puzzle.props[:ticket]
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
PDD.opts = nil
assert_equal 1, list.size
puzzle = list.first
assert_equal '7-7', puzzle.props[:lines]
assert_equal 'This puzzle is correctly formatted', puzzle.props[:body]
assert_equal '123', puzzle.props[:ticket]
end
end
end

Expand All @@ -126,7 +132,7 @@ def test_failing_on_incomplete_puzzle
"
)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'ff')).puzzles
stub_source_find_github_user(file, 'ff', &:puzzles)
end
assert !error.to_s.index("\x40todo is not followed by").nil?
end
Expand All @@ -137,7 +143,7 @@ def test_failing_on_broken_unicode
file = File.join(dir, 'xx.txt')
File.write(file, ' * \x40todo #44 this is a broken unicode: ' + 0x92.chr)
assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'xx')).puzzles
stub_source_find_github_user(file, 'xx', &:puzzles)
end
end
end
Expand All @@ -152,7 +158,7 @@ def test_failing_on_invalid_puzzle_without_hash_sign
"
)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
stub_source_find_github_user(file, 'hey', &:puzzles)
end
assert !error.message.index('is not followed by a puzzle marker').nil?
end
Expand All @@ -168,7 +174,7 @@ def test_failing_on_puzzle_without_leading_space
"
)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'x')).puzzles
stub_source_find_github_user(file, 'x', &:puzzles)
end
assert !error.message.index("\x40todo must have a leading space").nil?
end
Expand All @@ -184,7 +190,7 @@ def test_failing_on_puzzle_with_space_after_dash
"
)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'x')).puzzles
stub_source_find_github_user(file, 'x', &:puzzles)
end
assert !error.message.index('an unexpected space').nil?
end
Expand All @@ -203,15 +209,18 @@ def test_reads_git_author
git add a.txt
git commit --quiet -am 'first version'
")
list = PDD::Source.new(File.join(dir, 'a.txt'), '').puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test_unknown', puzzle.props[:author]
assert_equal 'test@teamed.io', puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, puzzle.props[:time])
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test_unknown', puzzle.props[:author]
assert_equal 'test@teamed.io', puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/,
puzzle.props[:time])
end
end
end

Expand All @@ -228,15 +237,18 @@ def test_skips_invalid_git_mail
git add a.txt
git commit --quiet -am 'first version'
")
list = PDD::Source.new(File.join(dir, 'a.txt'), '').puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test', puzzle.props[:author]
assert_nil puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/, puzzle.props[:time])
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-de87adc8', puzzle.props[:id]
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'this is the puzzle', puzzle.props[:body]
assert_equal 'test', puzzle.props[:author]
assert_nil puzzle.props[:email]
assert_match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z/,
puzzle.props[:time])
end
end
end

Expand All @@ -252,10 +264,12 @@ def test_uses_github_login
git add a.txt
git commit --quiet -am 'first version'
")
list = PDD::Source.new(File.join(dir, 'a.txt'), '').puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '@yegor256', puzzle.props[:author]
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '@yegor256', puzzle.props[:author]
end
end
end

Expand All @@ -272,11 +286,13 @@ def test_skips_uncommitted_changes
git commit --quiet -am 'first version'
echo '\x40todo #1 this is a puzzle uncommitted' > a.txt
")
list = PDD::Source.new(File.join(dir, 'a.txt'), '').puzzles
assert_equal 1, list.size
puzzle = list.first
assert_nil puzzle.props[:email]
assert_equal 'Not Committed Yet', puzzle.props[:author]
stub_source_find_github_user(File.join(dir, 'a.txt')) do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_nil puzzle.props[:email]
assert_equal 'Not Committed Yet', puzzle.props[:author]
end
end
end

Expand All @@ -287,12 +303,14 @@ def test_skips_thymeleaf_close_tag
file,
'<!--/* @todo #123 puzzle info */-->'
)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'puzzle info', puzzle.props[:body]
assert_equal '123', puzzle.props[:ticket]
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal 1, list.size
puzzle = list.first
assert_equal '1-1', puzzle.props[:lines]
assert_equal 'puzzle info', puzzle.props[:body]
assert_equal '123', puzzle.props[:ticket]
end
end
end
end
16 changes: 9 additions & 7 deletions test/test_source_todo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def check_valid_puzzle(text, lines, body, ticket, count = 1)
Dir.mktmpdir 'test' do |dir|
file = File.join(dir, 'a.txt')
File.write(file, text)
list = PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
assert_equal count, list.size
puzzle = list.first
assert_equal lines, puzzle.props[:lines]
assert_equal body, puzzle.props[:body]
assert_equal ticket, puzzle.props[:ticket]
stub_source_find_github_user(file, 'hey') do |source|
list = source.puzzles
assert_equal count, list.size
puzzle = list.first
assert_equal lines, puzzle.props[:lines]
assert_equal body, puzzle.props[:body]
assert_equal ticket, puzzle.props[:ticket]
end
end
end

Expand All @@ -42,7 +44,7 @@ def check_invalid_puzzle(text, error_msg)
file = File.join(dir, 'a.txt')
File.write(file, text)
error = assert_raises PDD::Error do
PDD::VerboseSource.new(file, PDD::Source.new(file, 'hey')).puzzles
stub_source_find_github_user(file, 'hey', &:puzzles)
end
assert !error.message.index(error_msg).nil?
end
Expand Down

0 comments on commit e2362a0

Please sign in to comment.